diff --git a/src/blobstore/implementations/onblocks/BlobOnBlocks.cpp b/src/blobstore/implementations/onblocks/BlobOnBlocks.cpp index 589c6adb..cb6faef5 100644 --- a/src/blobstore/implementations/onblocks/BlobOnBlocks.cpp +++ b/src/blobstore/implementations/onblocks/BlobOnBlocks.cpp @@ -115,7 +115,7 @@ void BlobOnBlocks::_read(void *target, uint64_t offset, uint64_t count) const { auto onExistingLeaf = [target, offset, count] (uint64_t indexOfFirstLeafByte, LeafHandle leaf, uint32_t leafDataOffset, uint32_t leafDataSize) { ASSERT(indexOfFirstLeafByte+leafDataOffset>=offset && indexOfFirstLeafByte-offset+leafDataOffset <= count && indexOfFirstLeafByte-offset+leafDataOffset+leafDataSize <= count, "Writing to target out of bounds"); //TODO Simplify formula, make it easier to understand - leaf.node()->read((uint8_t*)target + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize); + leaf.node()->read(static_cast(target) + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize); }; auto onCreateLeaf = [] (uint64_t /*beginByte*/, uint32_t /*count*/) -> Data { ASSERT(false, "Reading shouldn't create new leaves."); @@ -128,11 +128,11 @@ void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t count) { ASSERT(indexOfFirstLeafByte+leafDataOffset>=offset && indexOfFirstLeafByte-offset+leafDataOffset <= count && indexOfFirstLeafByte-offset+leafDataOffset+leafDataSize <= count, "Reading from source out of bounds"); if (leafDataOffset == 0 && leafDataSize == leaf.nodeStore()->layout().maxBytesPerLeaf()) { Data leafData(leafDataSize); - std::memcpy(leafData.data(), (uint8_t*)source + indexOfFirstLeafByte - offset, leafDataSize); + std::memcpy(leafData.data(), static_cast(source) + indexOfFirstLeafByte - offset, leafDataSize); leaf.nodeStore()->overwriteLeaf(leaf.blockId(), std::move(leafData)); } else { //TODO Simplify formula, make it easier to understand - leaf.node()->write((uint8_t *) source + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, + leaf.node()->write(static_cast(source) + indexOfFirstLeafByte - offset + leafDataOffset, leafDataOffset, leafDataSize); } }; @@ -140,7 +140,7 @@ void BlobOnBlocks::write(const void *source, uint64_t offset, uint64_t count) { ASSERT(beginByte >= offset && beginByte-offset <= count && beginByte-offset+numBytes <= count, "Reading from source out of bounds"); Data result(numBytes); //TODO Simplify formula, make it easier to understand - std::memcpy(result.data(), (uint8_t*)source + beginByte - offset, numBytes); + std::memcpy(result.data(), static_cast(source) + beginByte - offset, numBytes); return result; }; _traverseLeaves(offset, count, onExistingLeaf, onCreateLeaf); diff --git a/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode.cpp b/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode.cpp index 13f3f3b2..87c7f3fd 100644 --- a/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode.cpp +++ b/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode.cpp @@ -43,7 +43,8 @@ Data DataInnerNode::_serializeChildren(const vector &children) { Data data(sizeof(ChildEntry) * children.size()); uint32_t i = 0; for (const BlockId &child : children) { - reinterpret_cast(data.data())[i++].setBlockId(child); + child.ToBinary(data.dataOffset(i * BlockId::BINARY_LENGTH)); + ++i; } return data; } @@ -52,49 +53,34 @@ uint32_t DataInnerNode::numChildren() const { return node().Size(); } -DataInnerNode::ChildEntry *DataInnerNode::ChildrenBegin() { - return const_cast(const_cast(this)->ChildrenBegin()); -} - -const DataInnerNode::ChildEntry *DataInnerNode::ChildrenBegin() const { - return node().DataBegin(); -} - -DataInnerNode::ChildEntry *DataInnerNode::ChildrenEnd() { - return const_cast(const_cast(this)->ChildrenEnd()); -} - -const DataInnerNode::ChildEntry *DataInnerNode::ChildrenEnd() const { - return ChildrenBegin() + node().Size(); -} - -DataInnerNode::ChildEntry *DataInnerNode::LastChild() { - return const_cast(const_cast(this)->LastChild()); -} - -const DataInnerNode::ChildEntry *DataInnerNode::LastChild() const { - return getChild(numChildren()-1); -} - -DataInnerNode::ChildEntry *DataInnerNode::getChild(unsigned int index) { - return const_cast(const_cast(this)->getChild(index)); -} - -const DataInnerNode::ChildEntry *DataInnerNode::getChild(unsigned int index) const { +DataInnerNode::ChildEntry DataInnerNode::readChild(unsigned int index) const { ASSERT(index < numChildren(), "Accessing child out of range"); - return ChildrenBegin()+index; + return ChildEntry(BlockId::FromBinary(static_cast(node().data()) + index * sizeof(ChildEntry))); +} + +void DataInnerNode::_writeChild(unsigned int index, const ChildEntry& child) { + ASSERT(index < numChildren(), "Accessing child out of range"); + node().write(child.blockId().data().data(), index * sizeof(ChildEntry), sizeof(ChildEntry)); +} + +DataInnerNode::ChildEntry DataInnerNode::readLastChild() const { + return readChild(numChildren() - 1); +} + +void DataInnerNode::_writeLastChild(const ChildEntry& child) { + _writeChild(numChildren() - 1, child); } void DataInnerNode::addChild(const DataNode &child) { ASSERT(numChildren() < maxStoreableChildren(), "Adding more children than we can store"); ASSERT(child.depth() == depth()-1, "The child that should be added has wrong depth"); node().setSize(node().Size()+1); - LastChild()->setBlockId(child.blockId()); + _writeLastChild(ChildEntry(child.blockId())); } void DataInnerNode::removeLastChild() { ASSERT(node().Size() > 1, "There is no child to remove"); - LastChild()->setBlockId(BlockId::Null()); + _writeLastChild(ChildEntry(BlockId::Null())); node().setSize(node().Size()-1); } diff --git a/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode.h b/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode.h index 14bb2eef..357d5608 100644 --- a/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode.h +++ b/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode.h @@ -14,15 +14,15 @@ public: static cpputils::unique_ref InitializeNewNode(cpputils::unique_ref block, const DataNodeLayout &layout, uint8_t depth, const std::vector &children); static cpputils::unique_ref CreateNewNode(blockstore::BlockStore *blockStore, const DataNodeLayout &layout, uint8_t depth, const std::vector &children); + using ChildEntry = DataInnerNode_ChildEntry; + DataInnerNode(DataNodeView block); ~DataInnerNode(); - using ChildEntry = DataInnerNode_ChildEntry; - uint32_t maxStoreableChildren() const; - ChildEntry *getChild(unsigned int index); - const ChildEntry *getChild(unsigned int index) const; + ChildEntry readChild(unsigned int index) const; + ChildEntry readLastChild() const; uint32_t numChildren() const; @@ -30,16 +30,9 @@ public: void removeLastChild(); - ChildEntry *LastChild(); - const ChildEntry *LastChild() const; - private: - - ChildEntry *ChildrenBegin(); - ChildEntry *ChildrenEnd(); - const ChildEntry *ChildrenBegin() const; - const ChildEntry *ChildrenEnd() const; - + void _writeChild(unsigned int index, const ChildEntry& child); + void _writeLastChild(const ChildEntry& child); static cpputils::Data _serializeChildren(const std::vector &children); DISALLOW_COPY_AND_ASSIGN(DataInnerNode); diff --git a/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode_ChildEntry.h b/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode_ChildEntry.h index 725720cf..72d719dd 100644 --- a/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode_ChildEntry.h +++ b/src/blobstore/implementations/onblocks/datanodestore/DataInnerNode_ChildEntry.h @@ -10,17 +10,19 @@ namespace datanodestore{ struct DataInnerNode_ChildEntry final { public: - blockstore::BlockId blockId() const { - return blockstore::BlockId::FromBinary(_blockIdData); - } -private: - void setBlockId(const blockstore::BlockId &blockId) { - blockId.ToBinary(_blockIdData); - } - friend class DataInnerNode; - uint8_t _blockIdData[blockstore::BlockId::BINARY_LENGTH]; + DataInnerNode_ChildEntry(const blockstore::BlockId &blockId): _blockId(blockId) {} - DISALLOW_COPY_AND_ASSIGN(DataInnerNode_ChildEntry); + const blockstore::BlockId& blockId() const { + return _blockId; + } + + DataInnerNode_ChildEntry(const DataInnerNode_ChildEntry&) = delete; + DataInnerNode_ChildEntry& operator=(const DataInnerNode_ChildEntry&) = delete; + DataInnerNode_ChildEntry(DataInnerNode_ChildEntry&&) = default; + DataInnerNode_ChildEntry& operator=(DataInnerNode_ChildEntry&&) = default; + +private: + blockstore::BlockId _blockId; }; } diff --git a/src/blobstore/implementations/onblocks/datanodestore/DataLeafNode.cpp b/src/blobstore/implementations/onblocks/datanodestore/DataLeafNode.cpp index efeecd8d..f4f9962a 100644 --- a/src/blobstore/implementations/onblocks/datanodestore/DataLeafNode.cpp +++ b/src/blobstore/implementations/onblocks/datanodestore/DataLeafNode.cpp @@ -38,7 +38,7 @@ unique_ref DataLeafNode::OverwriteNode(BlockStore *blockStore, con void DataLeafNode::read(void *target, uint64_t offset, uint64_t size) const { ASSERT(offset <= node().Size() && offset + size <= node().Size(), "Read out of valid area"); // Also check offset, because the addition could lead to overflows - std::memcpy(target, (uint8_t*)node().data() + offset, size); + std::memcpy(target, static_cast(node().data()) + offset, size); } void DataLeafNode::write(const void *source, uint64_t offset, uint64_t size) { diff --git a/src/blobstore/implementations/onblocks/datanodestore/DataNodeStore.cpp b/src/blobstore/implementations/onblocks/datanodestore/DataNodeStore.cpp index 9ecfcdb8..8ccc180b 100644 --- a/src/blobstore/implementations/onblocks/datanodestore/DataNodeStore.cpp +++ b/src/blobstore/implementations/onblocks/datanodestore/DataNodeStore.cpp @@ -99,7 +99,7 @@ void DataNodeStore::removeSubtree(unique_ref node) { auto inner = dynamic_pointer_move(node); ASSERT(inner != none, "Is neither a leaf nor an inner node"); for (uint32_t i = 0; i < (*inner)->numChildren(); ++i) { - removeSubtree((*inner)->depth()-1, (*inner)->getChild(i)->blockId()); + removeSubtree((*inner)->depth()-1, (*inner)->readChild(i).blockId()); } remove(std::move(*inner)); } @@ -115,7 +115,7 @@ void DataNodeStore::removeSubtree(uint8_t depth, const BlockId &blockId) { ASSERT(inner != none, "Is not an inner node, but depth was not zero"); ASSERT((*inner)->depth() == depth, "Wrong depth given"); for (uint32_t i = 0; i < (*inner)->numChildren(); ++i) { - removeSubtree(depth-1, (*inner)->getChild(i)->blockId()); + removeSubtree(depth-1, (*inner)->readChild(i).blockId()); } remove(std::move(*inner)); } diff --git a/src/blobstore/implementations/onblocks/datanodestore/DataNodeView.h b/src/blobstore/implementations/onblocks/datanodestore/DataNodeView.h index 64e92c88..256c2849 100644 --- a/src/blobstore/implementations/onblocks/datanodestore/DataNodeView.h +++ b/src/blobstore/implementations/onblocks/datanodestore/DataNodeView.h @@ -91,7 +91,7 @@ public: DataNodeView(DataNodeView &&rhs) = default; uint16_t FormatVersion() const { - return *((uint8_t*)_block->data()+DataNodeLayout::FORMAT_VERSION_OFFSET_BYTES); + return cpputils::deserializeWithOffset(_block->data(), DataNodeLayout::FORMAT_VERSION_OFFSET_BYTES); } void setFormatVersion(uint16_t value) { @@ -99,7 +99,7 @@ public: } uint8_t Depth() const { - return *((uint8_t*)_block->data()+DataNodeLayout::DEPTH_OFFSET_BYTES); + return cpputils::deserializeWithOffset(_block->data(), DataNodeLayout::DEPTH_OFFSET_BYTES); } void setDepth(uint8_t value) { @@ -107,7 +107,7 @@ public: } uint32_t Size() const { - return *(uint32_t*)((uint8_t*)_block->data()+DataNodeLayout::SIZE_OFFSET_BYTES); + return cpputils::deserializeWithOffset(_block->data(), DataNodeLayout::SIZE_OFFSET_BYTES); } void setSize(uint32_t value) { @@ -115,24 +115,13 @@ public: } const void *data() const { - return (uint8_t*)_block->data() + DataNodeLayout::HEADERSIZE_BYTES; + return static_cast(_block->data()) + DataNodeLayout::HEADERSIZE_BYTES; } void write(const void *source, uint64_t offset, uint64_t size) { _block->write(source, offset + DataNodeLayout::HEADERSIZE_BYTES, size); } - template - const Entry *DataBegin() const { - return GetOffset(); - } - - template - const Entry *DataEnd() const { - const unsigned int NUM_ENTRIES = layout().datasizeBytes() / sizeof(Entry); - return DataBegin() + NUM_ENTRIES; - } - DataNodeLayout layout() const { return DataNodeLayout(_block->size()); } @@ -154,16 +143,11 @@ public: } private: - template - const Type *GetOffset() const { - return (Type*)(((const int8_t*)_block->data())+offset); - } - static cpputils::Data _serialize(const DataNodeLayout &layout, uint16_t formatVersion, uint8_t depth, uint32_t size, cpputils::Data data) { cpputils::Data result(layout.blocksizeBytes()); - *((uint16_t*)result.dataOffset(layout.FORMAT_VERSION_OFFSET_BYTES)) = formatVersion; - *((uint8_t*)result.dataOffset(layout.DEPTH_OFFSET_BYTES)) = depth; - *((uint32_t*)result.dataOffset(layout.SIZE_OFFSET_BYTES)) = size; + cpputils::serialize(result.dataOffset(layout.FORMAT_VERSION_OFFSET_BYTES), formatVersion); + cpputils::serialize(result.dataOffset(layout.DEPTH_OFFSET_BYTES), depth); + cpputils::serialize(result.dataOffset(layout.SIZE_OFFSET_BYTES), size); std::memcpy(result.dataOffset(layout.HEADERSIZE_BYTES), data.data(), data.size()); std::memset(result.dataOffset(layout.HEADERSIZE_BYTES+data.size()), 0, layout.datasizeBytes()-data.size()); return result; diff --git a/src/blobstore/implementations/onblocks/datatreestore/DataTree.cpp b/src/blobstore/implementations/onblocks/datatreestore/DataTree.cpp index 9a02b27f..4e5bd4cf 100644 --- a/src/blobstore/implementations/onblocks/datatreestore/DataTree.cpp +++ b/src/blobstore/implementations/onblocks/datatreestore/DataTree.cpp @@ -82,8 +82,8 @@ uint32_t DataTree::_computeNumLeaves(const DataNode &node) const { } const DataInnerNode &inner = dynamic_cast(node); - uint64_t numLeavesInLeftChildren = (uint64_t)(inner.numChildren()-1) * leavesPerFullChild(inner); - auto lastChild = _nodeStore->load(inner.LastChild()->blockId()); + uint64_t numLeavesInLeftChildren = static_cast(inner.numChildren()-1) * leavesPerFullChild(inner); + auto lastChild = _nodeStore->load(inner.readLastChild().blockId()); ASSERT(lastChild != none, "Couldn't load last child"); uint64_t numLeavesInRightChild = _computeNumLeaves(**lastChild); @@ -112,7 +112,7 @@ void DataTree::_traverseLeaves(uint32_t beginIndex, uint32_t endIndex, } uint32_t DataTree::leavesPerFullChild(const DataInnerNode &root) const { - return utils::intPow(_nodeStore->layout().maxChildrenPerInnerNode(), (uint64_t)root.depth()-1); + return utils::intPow(_nodeStore->layout().maxChildrenPerInnerNode(), static_cast(root.depth())-1); } uint64_t DataTree::numStoredBytes() const { @@ -132,7 +132,7 @@ uint64_t DataTree::_numStoredBytes(const DataNode &root) const { const DataInnerNode &inner = dynamic_cast(root); uint64_t numBytesInLeftChildren = (inner.numChildren()-1) * leavesPerFullChild(inner) * _nodeStore->layout().maxBytesPerLeaf(); - auto lastChild = _nodeStore->load(inner.LastChild()->blockId()); + auto lastChild = _nodeStore->load(inner.readLastChild().blockId()); ASSERT(lastChild != none, "Couldn't load last child"); uint64_t numBytesInRightChild = _numStoredBytes(**lastChild); @@ -159,14 +159,14 @@ void DataTree::resizeNumBytes(uint64_t newNumBytes) { auto onBacktrackFromSubtree = [this, newNumLeaves, maxChildrenPerInnerNode] (DataInnerNode* node) { // This is only called for the right border nodes of the new tree. // When growing size, the following is a no-op. When shrinking, we're deleting the children that aren't needed anymore. - uint32_t maxLeavesPerChild = utils::intPow((uint64_t)maxChildrenPerInnerNode, ((uint64_t)node->depth()-1)); + uint32_t maxLeavesPerChild = utils::intPow(static_cast(maxChildrenPerInnerNode), (static_cast(node->depth())-1)); uint32_t neededNodesOnChildLevel = utils::ceilDivision(newNumLeaves, maxLeavesPerChild); uint32_t neededSiblings = utils::ceilDivision(neededNodesOnChildLevel, maxChildrenPerInnerNode); uint32_t neededChildrenForRightBorderNode = neededNodesOnChildLevel - (neededSiblings-1) * maxChildrenPerInnerNode; ASSERT(neededChildrenForRightBorderNode <= node->numChildren(), "Node has too few children"); // All children to the right of the new right-border-node are removed including their subtree. while(node->numChildren() > neededChildrenForRightBorderNode) { - _nodeStore->removeSubtree(node->depth()-1, node->LastChild()->blockId()); + _nodeStore->removeSubtree(node->depth()-1, node->readLastChild().blockId()); node->removeLastChild(); } }; diff --git a/src/blobstore/implementations/onblocks/datatreestore/impl/LeafTraverser.cpp b/src/blobstore/implementations/onblocks/datatreestore/impl/LeafTraverser.cpp index a6ae9a61..8447a4e8 100644 --- a/src/blobstore/implementations/onblocks/datatreestore/impl/LeafTraverser.cpp +++ b/src/blobstore/implementations/onblocks/datatreestore/impl/LeafTraverser.cpp @@ -122,7 +122,7 @@ namespace blobstore { // we still have to descend to the last old child to fill it with leaves and grow the last old leaf. if (isLeftBorderOfTraversal && beginChild >= numChildren) { ASSERT(numChildren > 0, "Node doesn't have children."); - auto childBlockId = root->getChild(numChildren-1)->blockId(); + auto childBlockId = root->readLastChild().blockId(); uint32_t childOffset = (numChildren-1) * leavesPerChild; _traverseExistingSubtree(childBlockId, root->depth()-1, leavesPerChild, leavesPerChild, childOffset, true, false, true, [] (uint32_t /*index*/, bool /*isRightBorderNode*/, LeafHandle /*leaf*/) {ASSERT(false, "We don't actually traverse any leaves.");}, @@ -132,7 +132,7 @@ namespace blobstore { // Traverse existing children for (uint32_t childIndex = beginChild; childIndex < std::min(endChild, numChildren); ++childIndex) { - auto childBlockId = root->getChild(childIndex)->blockId(); + auto childBlockId = root->readChild(childIndex).blockId(); uint32_t childOffset = childIndex * leavesPerChild; uint32_t localBeginIndex = utils::maxZeroSubtraction(beginIndex, childOffset); uint32_t localEndIndex = std::min(leavesPerChild, endIndex - childOffset); @@ -169,7 +169,7 @@ namespace blobstore { return _nodeStore->createNewLeafNode(leafCreator(leafOffset)); } - uint8_t minNeededDepth = utils::ceilLog(_nodeStore->layout().maxChildrenPerInnerNode(), (uint64_t)endIndex); + uint8_t minNeededDepth = utils::ceilLog(_nodeStore->layout().maxChildrenPerInnerNode(), static_cast(endIndex)); ASSERT(depth >= minNeededDepth, "Given tree depth doesn't fit given number of leaves to create."); uint32_t leavesPerChild = _maxLeavesForTreeDepth(depth-1); uint32_t beginChild = beginIndex/leavesPerChild; @@ -208,7 +208,7 @@ namespace blobstore { } uint32_t LeafTraverser::_maxLeavesForTreeDepth(uint8_t depth) const { - return utils::intPow(_nodeStore->layout().maxChildrenPerInnerNode(), (uint64_t)depth); + return utils::intPow(_nodeStore->layout().maxChildrenPerInnerNode(), static_cast(depth)); } function LeafTraverser::_createMaxSizeLeaf() const { @@ -221,7 +221,7 @@ namespace blobstore { unique_ref LeafTraverser::_whileRootHasOnlyOneChildReplaceRootWithItsChild(unique_ref root) { DataInnerNode *inner = dynamic_cast(root.get()); if (inner != nullptr && inner->numChildren() == 1) { - auto newRoot = _whileRootHasOnlyOneChildRemoveRootReturnChild(inner->getChild(0)->blockId()); + auto newRoot = _whileRootHasOnlyOneChildRemoveRootReturnChild(inner->readChild(0).blockId()); auto result = _nodeStore->overwriteNodeWith(std::move(root), *newRoot); _nodeStore->remove(std::move(newRoot)); return result; @@ -237,7 +237,7 @@ namespace blobstore { if (inner == none) { return std::move(*current); } else if ((*inner)->numChildren() == 1) { - auto result = _whileRootHasOnlyOneChildRemoveRootReturnChild((*inner)->getChild(0)->blockId()); + auto result = _whileRootHasOnlyOneChildRemoveRootReturnChild((*inner)->readChild(0).blockId()); _nodeStore->remove(std::move(*inner)); return result; } else { diff --git a/src/blobstore/implementations/onblocks/datatreestore/impl/algorithms.cpp b/src/blobstore/implementations/onblocks/datatreestore/impl/algorithms.cpp index 138ceda3..9445a481 100644 --- a/src/blobstore/implementations/onblocks/datatreestore/impl/algorithms.cpp +++ b/src/blobstore/implementations/onblocks/datatreestore/impl/algorithms.cpp @@ -23,7 +23,7 @@ namespace datatreestore { namespace algorithms { optional> getLastChildAsInnerNode(DataNodeStore *nodeStore, const DataInnerNode &node) { - BlockId blockId = node.LastChild()->blockId(); + BlockId blockId = node.readLastChild().blockId(); auto lastChild = nodeStore->load(blockId); ASSERT(lastChild != none, "Couldn't load last child"); return dynamic_pointer_move(*lastChild); diff --git a/src/blobstore/implementations/onblocks/utils/Math.h b/src/blobstore/implementations/onblocks/utils/Math.h index 370c1e01..738abdfb 100644 --- a/src/blobstore/implementations/onblocks/utils/Math.h +++ b/src/blobstore/implementations/onblocks/utils/Math.h @@ -33,7 +33,7 @@ inline INT_TYPE maxZeroSubtraction(INT_TYPE minuend, INT_TYPE subtrahend) { template inline INT_TYPE ceilLog(INT_TYPE base, INT_TYPE value) { - return std::ceil((long double)std::log(value)/(long double)std::log(base)); + return std::ceil(static_cast(std::log(value))/static_cast(std::log(base))); } } diff --git a/src/blockstore/implementations/caching/cache/CacheEntry.h b/src/blockstore/implementations/caching/cache/CacheEntry.h index 0af597e2..6400f5c4 100644 --- a/src/blockstore/implementations/caching/cache/CacheEntry.h +++ b/src/blockstore/implementations/caching/cache/CacheEntry.h @@ -19,7 +19,7 @@ public: CacheEntry(CacheEntry&& rhs) noexcept: _lastAccess(std::move(rhs._lastAccess)), _value(std::move(rhs._value)) {} double ageSeconds() const { - return ((double)(currentTime() - _lastAccess).total_nanoseconds()) / ((double)1000000000); + return static_cast((currentTime() - _lastAccess).total_nanoseconds()) / static_cast(1000000000); } Value releaseValue() { diff --git a/src/blockstore/implementations/caching/cache/PeriodicTask.cpp b/src/blockstore/implementations/caching/cache/PeriodicTask.cpp index a73697a2..9145111f 100644 --- a/src/blockstore/implementations/caching/cache/PeriodicTask.cpp +++ b/src/blockstore/implementations/caching/cache/PeriodicTask.cpp @@ -9,7 +9,7 @@ namespace caching { PeriodicTask::PeriodicTask(function task, double intervalSec) : _task(task), - _interval((uint64_t)(UINT64_C(1000000000) * intervalSec)), + _interval(static_cast(UINT64_C(1000000000) * intervalSec)), _thread(std::bind(&PeriodicTask::_loopIteration, this)) { _thread.start(); } diff --git a/src/blockstore/implementations/compressing/CompressedBlock.h b/src/blockstore/implementations/compressing/CompressedBlock.h index 77637d66..32209dba 100644 --- a/src/blockstore/implementations/compressing/CompressedBlock.h +++ b/src/blockstore/implementations/compressing/CompressedBlock.h @@ -68,7 +68,7 @@ cpputils::unique_ref> CompressedBlock::O template cpputils::unique_ref> CompressedBlock::Decompress(cpputils::unique_ref baseBlock) { - cpputils::Data decompressed = Compressor::Decompress((CryptoPP::byte*)baseBlock->data(), baseBlock->size()); + cpputils::Data decompressed = Compressor::Decompress(baseBlock->data(), baseBlock->size()); return cpputils::make_unique_ref>(std::move(baseBlock), std::move(decompressed)); } @@ -93,7 +93,7 @@ const void *CompressedBlock::data() const { template void CompressedBlock::write(const void *source, uint64_t offset, uint64_t size) { - std::memcpy((uint8_t*)_decompressedData.dataOffset(offset), source, size); + std::memcpy(_decompressedData.dataOffset(offset), source, size); _dataChanged = true; } diff --git a/src/blockstore/implementations/compressing/compressors/Gzip.cpp b/src/blockstore/implementations/compressing/compressors/Gzip.cpp index f2ade682..64f13e97 100644 --- a/src/blockstore/implementations/compressing/compressors/Gzip.cpp +++ b/src/blockstore/implementations/compressing/compressors/Gzip.cpp @@ -9,20 +9,20 @@ namespace blockstore { Data Gzip::Compress(const Data &data) { CryptoPP::Gzip zipper; - zipper.Put((CryptoPP::byte *) data.data(), data.size()); + zipper.Put(static_cast(data.data()), data.size()); zipper.MessageEnd(); Data compressed(zipper.MaxRetrievable()); - zipper.Get((CryptoPP::byte *) compressed.data(), compressed.size()); + zipper.Get(static_cast(compressed.data()), compressed.size()); return compressed; } Data Gzip::Decompress(const void *data, size_t size) { //TODO Change interface to taking cpputils::Data objects (needs changing blockstore so we can read their "class Data", because this is called from CompressedBlock::Decompress()). CryptoPP::Gunzip zipper; - zipper.Put((CryptoPP::byte *) data, size); + zipper.Put(static_cast(data), size); zipper.MessageEnd(); Data decompressed(zipper.MaxRetrievable()); - zipper.Get((CryptoPP::byte *) decompressed.data(), decompressed.size()); + zipper.Get(static_cast(decompressed.data()), decompressed.size()); return decompressed; } diff --git a/src/blockstore/implementations/compressing/compressors/RunLengthEncoding.cpp b/src/blockstore/implementations/compressing/compressors/RunLengthEncoding.cpp index 3a20d759..08d6bfa0 100644 --- a/src/blockstore/implementations/compressing/compressors/RunLengthEncoding.cpp +++ b/src/blockstore/implementations/compressing/compressors/RunLengthEncoding.cpp @@ -18,8 +18,8 @@ namespace blockstore { Data RunLengthEncoding::Compress(const Data &data) { ostringstream compressed; - uint8_t *current = (uint8_t*)data.data(); - uint8_t *end = (uint8_t*)data.data()+data.size(); + const uint8_t *current = static_cast(data.data()); + const uint8_t *end = static_cast(data.data())+data.size(); while (current < end) { _encodeArbitraryWords(¤t, end, &compressed); ASSERT(current <= end, "Overflow"); @@ -32,25 +32,25 @@ namespace blockstore { return _extractData(&compressed); } - void RunLengthEncoding::_encodeArbitraryWords(uint8_t **current, uint8_t* end, ostringstream *output) { + void RunLengthEncoding::_encodeArbitraryWords(const uint8_t **current, const uint8_t* end, ostringstream *output) { uint16_t size = _arbitraryRunLength(*current, end); - output->write((const char*)&size, sizeof(uint16_t)); - output->write((const char*)*current, size); + output->write(reinterpret_cast(&size), sizeof(uint16_t)); + output->write(reinterpret_cast(*current), size); *current += size; } - uint16_t RunLengthEncoding::_arbitraryRunLength(uint8_t *start, uint8_t* end) { + uint16_t RunLengthEncoding::_arbitraryRunLength(const uint8_t *start, const uint8_t* end) { // Each stopping of an arbitrary bytes run costs us 5 byte, because we have to store the length // for the identical bytes run (2 byte), the identical byte itself (1 byte) and the length for the next arbitrary bytes run (2 byte). // So to get an advantage from stopping an arbitrary bytes run, at least 6 bytes have to be identical. // realEnd avoids an overflow of the 16bit counter - uint8_t *realEnd = std::min(end, start + std::numeric_limits::max()); + const uint8_t *realEnd = std::min(end, start + std::numeric_limits::max()); // Count the number of identical bytes and return if it finds a run of more than 6 identical bytes. uint8_t lastByte = *start + 1; // Something different from the first byte uint8_t numIdenticalBytes = 1; - for(uint8_t *current = start; current != realEnd; ++current) { + for(const uint8_t *current = start; current != realEnd; ++current) { if (*current == lastByte) { ++numIdenticalBytes; if (numIdenticalBytes == 6) { @@ -65,16 +65,16 @@ namespace blockstore { return realEnd-start; } - void RunLengthEncoding::_encodeIdenticalWords(uint8_t **current, uint8_t* end, ostringstream *output) { + void RunLengthEncoding::_encodeIdenticalWords(const uint8_t **current, const uint8_t* end, ostringstream *output) { uint16_t size = _countIdenticalBytes(*current, end); - output->write((const char*)&size, sizeof(uint16_t)); - output->write((const char*)*current, 1); + output->write(reinterpret_cast(&size), sizeof(uint16_t)); + output->write(reinterpret_cast(*current), 1); *current += size; } - uint16_t RunLengthEncoding::_countIdenticalBytes(uint8_t *start, uint8_t *end) { - uint8_t *realEnd = std::min(end, start + std::numeric_limits::max()); // This prevents overflow of the 16bit counter - for (uint8_t *current = start+1; current != realEnd; ++current) { + uint16_t RunLengthEncoding::_countIdenticalBytes(const uint8_t *start, const uint8_t *end) { + const uint8_t *realEnd = std::min(end, start + std::numeric_limits::max()); // This prevents overflow of the 16bit counter + for (const uint8_t *current = start+1; current != realEnd; ++current) { if (*current != *start) { return current-start; } @@ -92,7 +92,7 @@ namespace blockstore { Data RunLengthEncoding::Decompress(const void *data, size_t size) { istringstream stream; - _parseData((uint8_t*)data, size, &stream); + _parseData(static_cast(data), size, &stream); ostringstream decompressed; while(_hasData(&stream)) { _decodeArbitraryWords(&stream, &decompressed); @@ -110,29 +110,29 @@ namespace blockstore { } void RunLengthEncoding::_parseData(const uint8_t *data, size_t size, istringstream *result) { - result->str(string((const char*)data, size)); + result->str(string(reinterpret_cast(data), size)); } void RunLengthEncoding::_decodeArbitraryWords(istringstream *stream, ostringstream *decompressed) { uint16_t size; - stream->read((char*)&size, sizeof(uint16_t)); + stream->read(reinterpret_cast(&size), sizeof(uint16_t)); ASSERT(stream->good(), "Premature end of stream"); Data run(size); - stream->read((char*)run.data(), size); + stream->read(static_cast(run.data()), size); ASSERT(stream->good(), "Premature end of stream"); - decompressed->write((const char*)run.data(), run.size()); + decompressed->write(static_cast(run.data()), run.size()); } void RunLengthEncoding::_decodeIdenticalWords(istringstream *stream, ostringstream *decompressed) { uint16_t size; - stream->read((char*)&size, sizeof(uint16_t)); + stream->read(reinterpret_cast(&size), sizeof(uint16_t)); ASSERT(stream->good(), "Premature end of stream"); uint8_t value; - stream->read((char*)&value, 1); + stream->read(reinterpret_cast(&value), 1); ASSERT(stream->good(), "Premature end of stream"); Data run(size); std::memset(run.data(), value, run.size()); - decompressed->write((const char*)run.data(), run.size()); + decompressed->write(static_cast(run.data()), run.size()); } } diff --git a/src/blockstore/implementations/compressing/compressors/RunLengthEncoding.h b/src/blockstore/implementations/compressing/compressors/RunLengthEncoding.h index 2342f61e..d0d04252 100644 --- a/src/blockstore/implementations/compressing/compressors/RunLengthEncoding.h +++ b/src/blockstore/implementations/compressing/compressors/RunLengthEncoding.h @@ -13,10 +13,10 @@ namespace blockstore { static cpputils::Data Decompress(const void *data, size_t size); private: - static void _encodeArbitraryWords(uint8_t **current, uint8_t* end, std::ostringstream *output); - static uint16_t _arbitraryRunLength(uint8_t *start, uint8_t* end); - static void _encodeIdenticalWords(uint8_t **current, uint8_t* end, std::ostringstream *output); - static uint16_t _countIdenticalBytes(uint8_t *start, uint8_t *end); + static void _encodeArbitraryWords(const uint8_t **current, const uint8_t* end, std::ostringstream *output); + static uint16_t _arbitraryRunLength(const uint8_t *start, const uint8_t* end); + static void _encodeIdenticalWords(const uint8_t **current, const uint8_t* end, std::ostringstream *output); + static uint16_t _countIdenticalBytes(const uint8_t *start, const uint8_t *end); static bool _hasData(std::istringstream *stream); static cpputils::Data _extractData(std::ostringstream *stream); static void _parseData(const uint8_t *data, size_t size, std::istringstream *result); diff --git a/src/blockstore/implementations/encrypted/EncryptedBlockStore2.h b/src/blockstore/implementations/encrypted/EncryptedBlockStore2.h index 4238d390..26e25cad 100644 --- a/src/blockstore/implementations/encrypted/EncryptedBlockStore2.h +++ b/src/blockstore/implementations/encrypted/EncryptedBlockStore2.h @@ -6,6 +6,7 @@ #include "cpp-utils/crypto/cryptopp_byte.h" #include #include +#include namespace blockstore { namespace encrypted { @@ -122,14 +123,14 @@ inline void EncryptedBlockStore2::forEachBlock(std::function inline cpputils::Data EncryptedBlockStore2::_encrypt(const cpputils::Data &data) const { - cpputils::Data encrypted = Cipher::encrypt((CryptoPP::byte*)data.data(), data.size(), _encKey); + cpputils::Data encrypted = Cipher::encrypt(static_cast(data.data()), data.size(), _encKey); return _prependFormatHeaderToData(encrypted); } template inline boost::optional EncryptedBlockStore2::_tryDecrypt(const BlockId &blockId, const cpputils::Data &data) const { _checkFormatHeader(data); - boost::optional decrypted = Cipher::decrypt((CryptoPP::byte*)data.dataOffset(sizeof(FORMAT_VERSION_HEADER)), data.size() - sizeof(FORMAT_VERSION_HEADER), _encKey); + boost::optional decrypted = Cipher::decrypt(static_cast(data.dataOffset(sizeof(FORMAT_VERSION_HEADER))), data.size() - sizeof(FORMAT_VERSION_HEADER), _encKey); if (decrypted == boost::none) { // TODO Log warning return boost::none; @@ -164,7 +165,7 @@ inline bool EncryptedBlockStore2::_blockIdHeaderIsCorrect(const BlockId template inline cpputils::Data EncryptedBlockStore2::_prependFormatHeaderToData(const cpputils::Data &data) { cpputils::Data dataWithHeader(sizeof(FORMAT_VERSION_HEADER) + data.size()); - std::memcpy(dataWithHeader.dataOffset(0), &FORMAT_VERSION_HEADER, sizeof(FORMAT_VERSION_HEADER)); + cpputils::serialize(dataWithHeader.dataOffset(0), FORMAT_VERSION_HEADER); std::memcpy(dataWithHeader.dataOffset(sizeof(FORMAT_VERSION_HEADER)), data.data(), data.size()); return dataWithHeader; } @@ -184,7 +185,7 @@ inline void EncryptedBlockStore2::_checkFormatHeader(const cpputils::Dat template uint16_t EncryptedBlockStore2::_readFormatHeader(const cpputils::Data &data) { - return *reinterpret_cast(data.data()); + return cpputils::deserialize(data.data()); } template diff --git a/src/blockstore/implementations/integrity/IntegrityBlockStore2.cpp b/src/blockstore/implementations/integrity/IntegrityBlockStore2.cpp index 7b3402b1..40207c21 100644 --- a/src/blockstore/implementations/integrity/IntegrityBlockStore2.cpp +++ b/src/blockstore/implementations/integrity/IntegrityBlockStore2.cpp @@ -1,9 +1,12 @@ #include #include "IntegrityBlockStore2.h" #include "KnownBlockVersions.h" +#include using cpputils::Data; using cpputils::unique_ref; +using cpputils::serialize; +using cpputils::deserialize; using std::string; using boost::optional; using boost::none; @@ -25,11 +28,11 @@ constexpr unsigned int IntegrityBlockStore2::HEADER_LENGTH; Data IntegrityBlockStore2::_prependHeaderToData(const BlockId& blockId, uint32_t myClientId, uint64_t version, const Data &data) { static_assert(HEADER_LENGTH == sizeof(FORMAT_VERSION_HEADER) + BlockId::BINARY_LENGTH + sizeof(myClientId) + sizeof(version), "Wrong header length"); Data result(data.size() + HEADER_LENGTH); - std::memcpy(result.dataOffset(0), &FORMAT_VERSION_HEADER, sizeof(FORMAT_VERSION_HEADER)); - std::memcpy(result.dataOffset(ID_HEADER_OFFSET), blockId.data().data(), BlockId::BINARY_LENGTH); - std::memcpy(result.dataOffset(CLIENTID_HEADER_OFFSET), &myClientId, sizeof(myClientId)); - std::memcpy(result.dataOffset(VERSION_HEADER_OFFSET), &version, sizeof(version)); - std::memcpy((uint8_t*)result.dataOffset(HEADER_LENGTH), data.data(), data.size()); + serialize(result.dataOffset(0), FORMAT_VERSION_HEADER); + blockId.ToBinary(result.dataOffset(ID_HEADER_OFFSET)); + serialize(result.dataOffset(CLIENTID_HEADER_OFFSET), myClientId); + serialize(result.dataOffset(VERSION_HEADER_OFFSET), version); + std::memcpy(result.dataOffset(HEADER_LENGTH), data.data(), data.size()); return result; } @@ -62,13 +65,11 @@ void IntegrityBlockStore2::_checkIdHeader(const BlockId &expectedBlockId, const } uint16_t IntegrityBlockStore2::_readFormatHeader(const Data &data) { - return *reinterpret_cast(data.data()); + return deserialize(data.data()); } uint32_t IntegrityBlockStore2::_readClientId(const Data &data) { - uint32_t clientId; - std::memcpy(&clientId, data.dataOffset(CLIENTID_HEADER_OFFSET), sizeof(clientId)); - return clientId; + return deserialize(data.dataOffset(CLIENTID_HEADER_OFFSET)); } BlockId IntegrityBlockStore2::_readBlockId(const Data &data) { @@ -76,9 +77,7 @@ BlockId IntegrityBlockStore2::_readBlockId(const Data &data) { } uint64_t IntegrityBlockStore2::_readVersion(const Data &data) { - uint64_t version; - std::memcpy(&version, data.dataOffset(VERSION_HEADER_OFFSET), sizeof(version)); - return version; + return deserialize(data.dataOffset(VERSION_HEADER_OFFSET)); } Data IntegrityBlockStore2::_removeHeader(const Data &data) { @@ -146,8 +145,8 @@ optional IntegrityBlockStore2::load(const BlockId &blockId) const { #ifndef CRYFS_NO_COMPATIBILITY Data IntegrityBlockStore2::_migrateBlock(const BlockId &blockId, const Data &data) { Data migrated(data.size() + BlockId::BINARY_LENGTH); - std::memcpy(migrated.dataOffset(0), &FORMAT_VERSION_HEADER, sizeof(FORMAT_VERSION_HEADER)); - std::memcpy(migrated.dataOffset(ID_HEADER_OFFSET), blockId.data().data(), BlockId::BINARY_LENGTH); + serialize(migrated.dataOffset(0), FORMAT_VERSION_HEADER); + blockId.ToBinary(migrated.dataOffset(ID_HEADER_OFFSET)); std::memcpy(migrated.dataOffset(ID_HEADER_OFFSET + BlockId::BINARY_LENGTH), data.dataOffset(sizeof(FORMAT_VERSION_HEADER)), data.size() - sizeof(FORMAT_VERSION_HEADER)); ASSERT(migrated.size() == sizeof(FORMAT_VERSION_HEADER) + BlockId::BINARY_LENGTH + (data.size() - sizeof(FORMAT_VERSION_HEADER)), "Wrong offset computation"); return migrated; diff --git a/src/blockstore/implementations/low2highlevel/LowToHighLevelBlock.cpp b/src/blockstore/implementations/low2highlevel/LowToHighLevelBlock.cpp index b20cc502..7c053ce0 100644 --- a/src/blockstore/implementations/low2highlevel/LowToHighLevelBlock.cpp +++ b/src/blockstore/implementations/low2highlevel/LowToHighLevelBlock.cpp @@ -48,12 +48,12 @@ LowToHighLevelBlock::~LowToHighLevelBlock() { } const void *LowToHighLevelBlock::data() const { - return (uint8_t*)_data.data(); + return _data.data(); } void LowToHighLevelBlock::write(const void *source, uint64_t offset, uint64_t count) { ASSERT(offset <= size() && offset + count <= size(), "Write outside of valid area"); //Also check offset < size() because of possible overflow in the addition - std::memcpy((uint8_t*)_data.data()+offset, source, count); + std::memcpy(_data.dataOffset(offset), source, count); _dataChanged = true; } diff --git a/src/blockstore/implementations/testfake/FakeBlock.cpp b/src/blockstore/implementations/testfake/FakeBlock.cpp index 1517d5d2..471a8e76 100644 --- a/src/blockstore/implementations/testfake/FakeBlock.cpp +++ b/src/blockstore/implementations/testfake/FakeBlock.cpp @@ -30,7 +30,7 @@ const void *FakeBlock::data() const { void FakeBlock::write(const void *source, uint64_t offset, uint64_t size) { ASSERT(offset <= _data->size() && offset + size <= _data->size(), "Write outside of valid area"); //Also check offset < _data->size() because of possible overflow in the addition - std::memcpy((uint8_t*)_data->data()+offset, source, size); + std::memcpy(_data->dataOffset(offset), source, size); _dataChanged = true; } diff --git a/src/blockstore/utils/IdWrapper.h b/src/blockstore/utils/IdWrapper.h index 86eba3a2..54d3d2cb 100644 --- a/src/blockstore/utils/IdWrapper.h +++ b/src/blockstore/utils/IdWrapper.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace blockstore { @@ -101,7 +102,7 @@ inline bool operator!=(const IdWrapper& lhs, const IdWrapper& rhs) { template <> struct hash { \ size_t operator()(const IdWrapper &idWrapper) const { \ /*Ids are random, so it is enough to use the first few bytes as a hash */ \ - return *(size_t*)(idWrapper.id_.data()); \ + return cpputils::deserialize(idWrapper.id_.data()); \ } \ }; \ /*Allow using IdWrapper in std::map / std::set */ \ diff --git a/src/cpp-utils/CMakeLists.txt b/src/cpp-utils/CMakeLists.txt index 62239b3b..bb287015 100644 --- a/src/cpp-utils/CMakeLists.txt +++ b/src/cpp-utils/CMakeLists.txt @@ -31,6 +31,7 @@ set(SOURCES random/RandomDataBuffer.cpp random/RandomGenerator.cpp lock/LockPool.cpp + data/SerializationHelper.cpp data/Serializer.cpp data/Deserializer.cpp data/DataFixture.cpp diff --git a/src/cpp-utils/crypto/RandomPadding.cpp b/src/cpp-utils/crypto/RandomPadding.cpp index 42a1adbc..4dd2f5ba 100644 --- a/src/cpp-utils/crypto/RandomPadding.cpp +++ b/src/cpp-utils/crypto/RandomPadding.cpp @@ -1,3 +1,4 @@ +#include #include "RandomPadding.h" #include "../logging/logging.h" #include "../random/Random.h" @@ -14,21 +15,20 @@ namespace cpputils { Data randomData = Random::PseudoRandom().get(targetSize-sizeof(size)-size); ASSERT(sizeof(size) + size + randomData.size() == targetSize, "Calculated size of randomData incorrectly"); Data result(targetSize); - std::memcpy(reinterpret_cast(result.data()), &size, sizeof(size)); - std::memcpy(reinterpret_cast(result.dataOffset(sizeof(size))), reinterpret_cast(data.data()), size); - std::memcpy(reinterpret_cast(result.dataOffset(sizeof(size)+size)), reinterpret_cast(randomData.data()), randomData.size()); + serialize(result.data(), size); + std::memcpy(result.dataOffset(sizeof(size)), data.data(), size); + std::memcpy(result.dataOffset(sizeof(size)+size), randomData.data(), randomData.size()); return result; } optional RandomPadding::remove(const Data &data) { - uint32_t size; - std::memcpy(&size, reinterpret_cast(data.data()), sizeof(size)); + uint32_t size = deserialize(data.data()); if(sizeof(size) + size >= data.size()) { LOG(ERROR, "Config file is invalid: Invalid padding."); return boost::none; }; Data result(size); - std::memcpy(reinterpret_cast(result.data()), reinterpret_cast(data.dataOffset(sizeof(size))), size); + std::memcpy(result.data(), data.dataOffset(sizeof(size)), size); return std::move(result); } } diff --git a/src/cpp-utils/crypto/hash/Hash.cpp b/src/cpp-utils/crypto/hash/Hash.cpp index 1ac30013..45402122 100644 --- a/src/cpp-utils/crypto/hash/Hash.cpp +++ b/src/cpp-utils/crypto/hash/Hash.cpp @@ -10,11 +10,11 @@ namespace hash { Hash hash(const Data& data, Salt salt) { SHA512 hasher; // NOLINT (workaround for clang-warning in libcrypto++) - hasher.Update((CryptoPP::byte*)salt.data(), Salt::BINARY_LENGTH); - hasher.Update((CryptoPP::byte*)data.data(), data.size()); + hasher.Update(static_cast(salt.data()), Salt::BINARY_LENGTH); + hasher.Update(static_cast(data.data()), data.size()); Digest digest = Digest::Null(); - hasher.Final((CryptoPP::byte*)digest.data()); + hasher.Final(static_cast(digest.data())); return Hash{ .digest = digest, diff --git a/src/cpp-utils/crypto/kdf/Scrypt.cpp b/src/cpp-utils/crypto/kdf/Scrypt.cpp index 5d805578..3a76ad81 100644 --- a/src/cpp-utils/crypto/kdf/Scrypt.cpp +++ b/src/cpp-utils/crypto/kdf/Scrypt.cpp @@ -24,7 +24,7 @@ namespace cpputils { void SCrypt::derive(void *destination, size_t size, const string &password) { _checkCallOnlyOnce(); int errorcode = crypto_scrypt(reinterpret_cast(password.c_str()), password.size(), - reinterpret_cast(_config.salt().data()), _config.salt().size(), + static_cast(_config.salt().data()), _config.salt().size(), _config.N(), _config.r(), _config.p(), static_cast(destination), size); if (errorcode != 0) { diff --git a/src/cpp-utils/crypto/symmetric/CFB_Cipher.h b/src/cpp-utils/crypto/symmetric/CFB_Cipher.h index 9de10c3d..971f1d7b 100644 --- a/src/cpp-utils/crypto/symmetric/CFB_Cipher.h +++ b/src/cpp-utils/crypto/symmetric/CFB_Cipher.h @@ -36,10 +36,10 @@ private: template Data CFB_Cipher::encrypt(const CryptoPP::byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { FixedSizeData iv = Random::PseudoRandom().getFixedSize(); - auto encryption = typename CryptoPP::CFB_Mode::Encryption((CryptoPP::byte*)encKey.data(), encKey.BINARY_LENGTH, iv.data()); + auto encryption = typename CryptoPP::CFB_Mode::Encryption(static_cast(encKey.data()), encKey.BINARY_LENGTH, iv.data()); Data ciphertext(ciphertextSize(plaintextSize)); - std::memcpy(ciphertext.data(), iv.data(), IV_SIZE); - encryption.ProcessData((CryptoPP::byte*)ciphertext.data() + IV_SIZE, plaintext, plaintextSize); + iv.ToBinary(ciphertext.data()); + encryption.ProcessData(static_cast(ciphertext.data()) + IV_SIZE, plaintext, plaintextSize); return ciphertext; } @@ -51,9 +51,9 @@ boost::optional CFB_Cipher::decrypt(const CryptoPP:: const CryptoPP::byte *ciphertextIV = ciphertext; const CryptoPP::byte *ciphertextData = ciphertext + IV_SIZE; - auto decryption = typename CryptoPP::CFB_Mode::Decryption((CryptoPP::byte*)encKey.data(), encKey.BINARY_LENGTH, ciphertextIV); + auto decryption = typename CryptoPP::CFB_Mode::Decryption(static_cast(encKey.data()), encKey.BINARY_LENGTH, ciphertextIV); Data plaintext(plaintextSize(ciphertextSize)); - decryption.ProcessData((CryptoPP::byte*)plaintext.data(), ciphertextData, plaintext.size()); + decryption.ProcessData(static_cast(plaintext.data()), ciphertextData, plaintext.size()); return std::move(plaintext); } diff --git a/src/cpp-utils/crypto/symmetric/Cipher.h b/src/cpp-utils/crypto/symmetric/Cipher.h index e6327f48..c0766111 100644 --- a/src/cpp-utils/crypto/symmetric/Cipher.h +++ b/src/cpp-utils/crypto/symmetric/Cipher.h @@ -18,8 +18,8 @@ public: same_type(UINT32_C(0), X::ciphertextSize(UINT32_C(5))); same_type(UINT32_C(0), X::plaintextSize(UINT32_C(5))); typename X::EncryptionKey key = X::EncryptionKey::CreateKey(Random::OSRandom()); - same_type(Data(0), X::encrypt((uint8_t*)nullptr, UINT32_C(0), key)); - same_type(boost::optional(Data(0)), X::decrypt((uint8_t*)nullptr, UINT32_C(0), key)); + same_type(Data(0), X::encrypt(static_cast(nullptr), UINT32_C(0), key)); + same_type(boost::optional(Data(0)), X::decrypt(static_cast(nullptr), UINT32_C(0), key)); string name = X::NAME; } diff --git a/src/cpp-utils/crypto/symmetric/GCM_Cipher.h b/src/cpp-utils/crypto/symmetric/GCM_Cipher.h index 8f9952b2..2eb00ec2 100644 --- a/src/cpp-utils/crypto/symmetric/GCM_Cipher.h +++ b/src/cpp-utils/crypto/symmetric/GCM_Cipher.h @@ -37,13 +37,13 @@ template Data GCM_Cipher::encrypt(const CryptoPP::byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { FixedSizeData iv = Random::PseudoRandom().getFixedSize(); typename CryptoPP::GCM::Encryption encryption; - encryption.SetKeyWithIV((CryptoPP::byte*)encKey.data(), encKey.BINARY_LENGTH, iv.data(), IV_SIZE); + encryption.SetKeyWithIV(static_cast(encKey.data()), encKey.BINARY_LENGTH, iv.data(), IV_SIZE); Data ciphertext(ciphertextSize(plaintextSize)); - std::memcpy(ciphertext.data(), iv.data(), IV_SIZE); + iv.ToBinary(ciphertext.data()); CryptoPP::ArraySource(plaintext, plaintextSize, true, new CryptoPP::AuthenticatedEncryptionFilter(encryption, - new CryptoPP::ArraySink((CryptoPP::byte*)ciphertext.data() + IV_SIZE, ciphertext.size() - IV_SIZE), + new CryptoPP::ArraySink(static_cast(ciphertext.data()) + IV_SIZE, ciphertext.size() - IV_SIZE), false, TAG_SIZE ) ); @@ -59,13 +59,13 @@ boost::optional GCM_Cipher::decrypt(const CryptoPP:: const CryptoPP::byte *ciphertextIV = ciphertext; const CryptoPP::byte *ciphertextData = ciphertext + IV_SIZE; typename CryptoPP::GCM::Decryption decryption; - decryption.SetKeyWithIV((CryptoPP::byte*)encKey.data(), encKey.BINARY_LENGTH, ciphertextIV, IV_SIZE); + decryption.SetKeyWithIV(static_cast(encKey.data()), encKey.BINARY_LENGTH, ciphertextIV, IV_SIZE); Data plaintext(plaintextSize(ciphertextSize)); try { - CryptoPP::ArraySource((CryptoPP::byte*)ciphertextData, ciphertextSize - IV_SIZE, true, + CryptoPP::ArraySource(static_cast(ciphertextData), ciphertextSize - IV_SIZE, true, new CryptoPP::AuthenticatedDecryptionFilter(decryption, - new CryptoPP::ArraySink((CryptoPP::byte*)plaintext.data(), plaintext.size()), + new CryptoPP::ArraySink(static_cast(plaintext.data()), plaintext.size()), CryptoPP::AuthenticatedDecryptionFilter::DEFAULT_FLAGS, TAG_SIZE ) ); diff --git a/src/cpp-utils/crypto/symmetric/testutils/FakeAuthenticatedCipher.h b/src/cpp-utils/crypto/symmetric/testutils/FakeAuthenticatedCipher.h index 0c055992..075d04e9 100644 --- a/src/cpp-utils/crypto/symmetric/testutils/FakeAuthenticatedCipher.h +++ b/src/cpp-utils/crypto/symmetric/testutils/FakeAuthenticatedCipher.h @@ -8,19 +8,20 @@ #include "cpp-utils/data/Data.h" #include "cpp-utils/random/RandomGenerator.h" #include +#include namespace cpputils { struct FakeKey { static FakeKey FromBinary(const void *data) { - return FakeKey{*(uint64_t *) data}; + return FakeKey{deserialize(data)}; } static constexpr unsigned int BINARY_LENGTH = sizeof(uint64_t); static FakeKey CreateKey(RandomGenerator &randomGenerator) { auto data = randomGenerator.getFixedSize(); - return FakeKey{*((uint64_t *) data.data())}; + return FromBinary(data.data()); } uint64_t value; @@ -54,14 +55,14 @@ namespace cpputils { //Add a random IV uint64_t iv = std::uniform_int_distribution()(random_); - std::memcpy(result.data(), &iv, sizeof(uint64_t)); + serialize(result.data(), iv); //Use xor chiffre on plaintext - _xor((CryptoPP::byte *) result.data() + sizeof(uint64_t), plaintext, plaintextSize, encKey.value ^ iv); + _xor(static_cast(result.dataOffset(sizeof(uint64_t))), plaintext, plaintextSize, encKey.value ^ iv); //Add checksum information - uint64_t checksum = _checksum((CryptoPP::byte *) result.data(), encKey, plaintextSize + sizeof(uint64_t)); - std::memcpy((CryptoPP::byte *) result.data() + plaintextSize + sizeof(uint64_t), &checksum, sizeof(uint64_t)); + uint64_t checksum = _checksum(static_cast(result.data()), encKey, plaintextSize + sizeof(uint64_t)); + serialize(result.dataOffset(plaintextSize + sizeof(uint64_t)), checksum); return result; } @@ -75,45 +76,35 @@ namespace cpputils { //Check checksum uint64_t expectedParity = _checksum(ciphertext, encKey, plaintextSize(ciphertextSize) + sizeof(uint64_t)); - uint64_t actualParity = *(uint64_t * )(ciphertext + plaintextSize(ciphertextSize) + sizeof(uint64_t)); + uint64_t actualParity = deserialize(ciphertext + plaintextSize(ciphertextSize) + sizeof(uint64_t)); if (expectedParity != actualParity) { return boost::none; } //Decrypt xor chiffre from ciphertext - uint64_t iv = *(uint64_t *) ciphertext; + uint64_t iv = deserialize(ciphertext); Data result(plaintextSize(ciphertextSize)); - _xor((CryptoPP::byte *) result.data(), ciphertext + sizeof(uint64_t), plaintextSize(ciphertextSize), encKey.value ^ iv); + _xor(static_cast(result.data()), ciphertext + sizeof(uint64_t), plaintextSize(ciphertextSize), encKey.value ^ iv); + return std::move(result); } static constexpr const char *NAME = "FakeAuthenticatedCipher"; private: - static uint64_t _checksum(const CryptoPP::byte *data, FakeKey encKey, unsigned int size) { + static uint64_t _checksum(const CryptoPP::byte *data, FakeKey encKey, std::size_t size) { uint64_t checksum = 34343435 * encKey.value; // some init value - const uint64_t *intData = reinterpret_cast(data); - unsigned int intSize = size / sizeof(uint64_t); - for (unsigned int i = 0; i < intSize; ++i) { - checksum = ((uint64_t)checksum) + intData[i]; - } - unsigned int remainingBytes = size - sizeof(uint64_t) * intSize; - for (unsigned int i = 0; i < remainingBytes; ++i) { - checksum = ((uint64_t)checksum) + (data[8 * intSize + i] << (56 - 8 * i)); + + for (unsigned int i = 0; i < size; ++i) { + checksum ^= (static_cast(data[i]) << (56 - 8 * (i%8))); } + return checksum; } static void _xor(CryptoPP::byte *dst, const CryptoPP::byte *src, unsigned int size, uint64_t key) { - const uint64_t *srcIntData = reinterpret_cast(src); - uint64_t *dstIntData = reinterpret_cast(dst); - unsigned int intSize = size / sizeof(uint64_t); - for (unsigned int i = 0; i < intSize; ++i) { - dstIntData[i] = srcIntData[i] ^ key; - } - unsigned int remainingBytes = size - sizeof(uint64_t) * intSize; - for (unsigned int i = 0; i < remainingBytes; ++i) { - dst[8 * intSize + i] = src[8 * intSize + i] ^ ((key >> (56 - 8*i)) & 0xFF); + for (unsigned int i = 0; i < size; ++i) { + dst[i] = src[i] ^ ((key >> (56 - 8*(i%8))) & 0xFF); } } diff --git a/src/cpp-utils/data/Data.cpp b/src/cpp-utils/data/Data.cpp index 4db5f4da..7d466155 100644 --- a/src/cpp-utils/data/Data.cpp +++ b/src/cpp-utils/data/Data.cpp @@ -49,7 +49,7 @@ Data Data::FromString(const std::string &data) { Data result(data.size() / 2); CryptoPP::StringSource(data, true, new CryptoPP::HexDecoder( - new CryptoPP::ArraySink((CryptoPP::byte*)result._data, result.size()) + new CryptoPP::ArraySink(static_cast(result._data), result.size()) ) ); return result; @@ -57,7 +57,7 @@ Data Data::FromString(const std::string &data) { std::string Data::ToString() const { std::string result; - CryptoPP::ArraySource((CryptoPP::byte*)_data, _size, true, + CryptoPP::ArraySource(static_cast(_data), _size, true, new CryptoPP::HexEncoder( new CryptoPP::StringSink(result) ) diff --git a/src/cpp-utils/data/DataFixture.cpp b/src/cpp-utils/data/DataFixture.cpp index b9918846..4707bffe 100644 --- a/src/cpp-utils/data/DataFixture.cpp +++ b/src/cpp-utils/data/DataFixture.cpp @@ -1,4 +1,5 @@ #include "DataFixture.h" +#include "SerializationHelper.h" namespace cpputils { Data DataFixture::generate(size_t size, long long int seed) { @@ -8,7 +9,7 @@ namespace cpputils { //MMIX linear congruential generator val *= 6364136223846793005L; val += 1442695040888963407; - reinterpret_cast(result.data())[i] = val; + serialize(result.dataOffset(i*sizeof(long long int)), val); } uint64_t alreadyWritten = (size/sizeof(long long int))*sizeof(long long int); val *= 6364136223846793005L; @@ -16,7 +17,7 @@ namespace cpputils { char *remainingBytes = reinterpret_cast(&val); //Fill remaining bytes for(size_t i=0; i(result.data())[alreadyWritten + i] = remainingBytes[i]; + serialize(result.dataOffset(alreadyWritten + i), remainingBytes[i]); } return result; } diff --git a/src/cpp-utils/data/Deserializer.h b/src/cpp-utils/data/Deserializer.h index 3533b75c..0c75f172 100644 --- a/src/cpp-utils/data/Deserializer.h +++ b/src/cpp-utils/data/Deserializer.h @@ -6,6 +6,7 @@ #include "../macros.h" #include "../assert/assert.h" #include "FixedSizeData.h" +#include "SerializationHelper.h" namespace cpputils { class Deserializer final { @@ -79,7 +80,7 @@ namespace cpputils { if (_pos + sizeof(DataType) > _source->size()) { throw std::runtime_error("Deserialization failed - size overflow"); } - DataType result = *reinterpret_cast(_source->dataOffset(_pos)); + DataType result = deserialize(_source->dataOffset(_pos)); _pos += sizeof(DataType); return result; } @@ -122,7 +123,7 @@ namespace cpputils { throw std::runtime_error("Deserialization failed - missing nullbyte for string termination"); } uint64_t size = static_cast(nullbytepos) - static_cast(_source->dataOffset(_pos)); - std::string result(reinterpret_cast(_source->dataOffset(_pos)), size); + std::string result(static_cast(_source->dataOffset(_pos)), size); _pos += size + 1; return result; } diff --git a/src/cpp-utils/data/SerializationHelper.cpp b/src/cpp-utils/data/SerializationHelper.cpp new file mode 100644 index 00000000..0adb8fc2 --- /dev/null +++ b/src/cpp-utils/data/SerializationHelper.cpp @@ -0,0 +1 @@ +#include "SerializationHelper.h" diff --git a/src/cpp-utils/data/SerializationHelper.h b/src/cpp-utils/data/SerializationHelper.h new file mode 100644 index 00000000..8e6fbd19 --- /dev/null +++ b/src/cpp-utils/data/SerializationHelper.h @@ -0,0 +1,78 @@ +#pragma once +#ifndef MESSMER_CPPUTILS_DATA_SERIALIZATIONHELPER_H +#define MESSMER_CPPUTILS_DATA_SERIALIZATIONHELPER_H + +#include +#include +#include + +namespace cpputils { + +namespace details { + +constexpr bool greater_than(size_t lhs, size_t rhs) { + return lhs > rhs; +} + +template struct serialize; + +// Specialize for 1-byte types for faster performance with direct pointer access (no memcpy). +template +struct serialize::type> final { + static_assert(std::is_pod::value, "Can only serialize PODs"); + static void call(void *dst, const DataType &obj) { + *static_cast(dst) = obj; + } +}; + +// Specialize for larger types with memcpy because unaligned data accesses through pointers are undefined behavior. +template +struct serialize::type> final { + static_assert(std::is_pod::value, "Can only serialize PODs"); + static void call(void *dst, const DataType &obj) { + std::memcpy(dst, &obj, sizeof(DataType)); + } +}; + +template struct deserialize; + +// Specialize for 1-byte types for faster performance with direct pointer access (no memcpy). +template +struct deserialize::type> final { + static_assert(std::is_pod::value, "Can only serialize PODs"); + static DataType call(const void *src) { + return *static_cast(src); + } +}; + +// Specialize for larger types with memcpy because unaligned data accesses through pointers are undefined behavior. +template +struct deserialize::type> final { + static_assert(std::is_pod::value, "Can only deserialize PODs"); + static DataType call(const void *src) { + typename std::remove_const::type result{}; + std::memcpy(&result, src, sizeof(DataType)); + return result; + } +}; + +} + +template +inline void serialize(void *dst, const DataType& obj) { + return details::serialize::call(dst, obj); +} + +template +inline DataType deserialize(const void *src) { + return details::deserialize::call(src); +} + +template +inline DataType deserializeWithOffset(const void* src, size_t offset) { + return deserialize(static_cast(src) + offset); +} + +} + +#endif diff --git a/src/cpp-utils/data/Serializer.h b/src/cpp-utils/data/Serializer.h index dbfef896..3cafbdc3 100644 --- a/src/cpp-utils/data/Serializer.h +++ b/src/cpp-utils/data/Serializer.h @@ -7,6 +7,7 @@ #include "../macros.h" #include "../assert/assert.h" #include +#include "SerializationHelper.h" namespace cpputils { //TODO Test Serializer/Deserializer @@ -83,11 +84,10 @@ namespace cpputils { template inline void Serializer::_write(DataType obj) { - static_assert(std::is_pod::value, "Can only serialize PODs"); if (_pos + sizeof(DataType) > _result.size()) { throw std::runtime_error("Serialization failed - size overflow"); } - *reinterpret_cast(_result.dataOffset(_pos)) = obj; + serialize(_result.dataOffset(_pos), obj); _pos += sizeof(DataType); } diff --git a/src/cpp-utils/network/CurlHttpClient.cpp b/src/cpp-utils/network/CurlHttpClient.cpp index dcfd85f8..01e8c52d 100644 --- a/src/cpp-utils/network/CurlHttpClient.cpp +++ b/src/cpp-utils/network/CurlHttpClient.cpp @@ -13,7 +13,7 @@ using std::ostringstream; namespace cpputils { size_t CurlHttpClient::write_data(void *ptr, size_t size, size_t nmemb, ostringstream *stream) { - stream->write((const char *) ptr, size * nmemb); + stream->write(static_cast(ptr), size * nmemb); return size * nmemb; } diff --git a/src/cpp-utils/random/OSRandomGenerator.h b/src/cpp-utils/random/OSRandomGenerator.h index b3268fce..8c8dc6f5 100644 --- a/src/cpp-utils/random/OSRandomGenerator.h +++ b/src/cpp-utils/random/OSRandomGenerator.h @@ -21,7 +21,7 @@ namespace cpputils { inline OSRandomGenerator::OSRandomGenerator() {} inline void OSRandomGenerator::_get(void *target, size_t bytes) { - CryptoPP::OS_GenerateRandomBlock(true, (CryptoPP::byte*)target, bytes); + CryptoPP::OS_GenerateRandomBlock(true, static_cast(target), bytes); } } diff --git a/src/cryfs/filesystem/fsblobstore/DirBlob.cpp b/src/cryfs/filesystem/fsblobstore/DirBlob.cpp index 45876e6b..4747ba25 100644 --- a/src/cryfs/filesystem/fsblobstore/DirBlob.cpp +++ b/src/cryfs/filesystem/fsblobstore/DirBlob.cpp @@ -152,7 +152,7 @@ void DirBlob::statChildWithSizeAlreadySet(const BlockId &blockId, struct ::stat result->st_mtim = child.lastModificationTime(); result->st_ctim = child.lastMetadataChangeTime(); //TODO Move ceilDivision to general utils which can be used by cryfs as well - result->st_blocks = blobstore::onblocks::utils::ceilDivision(result->st_size, (off_t)512); + result->st_blocks = blobstore::onblocks::utils::ceilDivision(result->st_size, static_cast(512)); result->st_blksize = _fsBlobStore->virtualBlocksizeBytes(); } diff --git a/src/cryfs/filesystem/fsblobstore/utils/DirEntry.cpp b/src/cryfs/filesystem/fsblobstore/utils/DirEntry.cpp index 8a38dc59..84a2925f 100644 --- a/src/cryfs/filesystem/fsblobstore/utils/DirEntry.cpp +++ b/src/cryfs/filesystem/fsblobstore/utils/DirEntry.cpp @@ -1,4 +1,6 @@ +#include #include "DirEntry.h" +#include using std::vector; using std::string; @@ -6,6 +8,64 @@ using blockstore::BlockId; namespace cryfs { namespace fsblobstore { + + namespace { + template + size_t _serialize(void* dst, const DataType& obj) { + cpputils::serialize(dst, obj); + return sizeof(DataType); + } + + template + DataType _deserialize(const char** src) { + DataType result = cpputils::deserialize(*src); + *src += sizeof(DataType); + return result; + } + + constexpr size_t _serializedTimeValueSize() { + return sizeof(uint64_t) + sizeof(uint32_t); + } + + unsigned int _serializeTimeValue(uint8_t *dest, timespec value) { + unsigned int offset = 0; + offset += _serialize(dest + offset, value.tv_sec); + offset += _serialize(dest + offset, value.tv_nsec); + ASSERT(offset == _serializedTimeValueSize(), "serialized to wrong size"); + return offset; + } + + timespec _deserializeTimeValue(const char **pos) { + timespec value{}; + value.tv_sec = _deserialize(pos); + value.tv_nsec = _deserialize(pos); + return value; + } + + unsigned int _serializeString(uint8_t *dest, const string &value) { + std::memcpy(dest, value.c_str(), value.size()+1); + return value.size() + 1; + } + + string _deserializeString(const char **pos) { + size_t length = strlen(*pos); + string value(*pos, length); + *pos += length + 1; + return value; + } + + unsigned int _serializeBlockId(uint8_t *dest, const BlockId &blockId) { + blockId.ToBinary(dest); + return blockId.BINARY_LENGTH; + } + + BlockId _deserializeBlockId(const char **pos) { + BlockId blockId = BlockId::FromBinary(*pos); + *pos += BlockId::BINARY_LENGTH; + return blockId; + } + } + void DirEntry::serialize(uint8_t *dest) const { ASSERT( ((_type == fspp::Dir::EntryType::FILE) && S_ISREG(_mode) && !S_ISDIR(_mode) && !S_ISLNK(_mode)) || @@ -15,10 +75,10 @@ namespace cryfs { _mode & S_IFDIR) + ", " + std::to_string(_mode & S_IFLNK) + ", " + std::to_string(static_cast(_type)) ); unsigned int offset = 0; - offset += _serializeUint8(dest + offset, static_cast(_type)); - offset += _serializeUint32(dest + offset, _mode); - offset += _serializeUint32(dest + offset, _uid); - offset += _serializeUint32(dest + offset, _gid); + offset += _serialize(dest + offset, static_cast(_type)); + offset += _serialize(dest + offset, _mode); + offset += _serialize(dest + offset, _uid); + offset += _serialize(dest + offset, _gid); offset += _serializeTimeValue(dest + offset, _lastAccessTime); offset += _serializeTimeValue(dest + offset, _lastModificationTime); offset += _serializeTimeValue(dest + offset, _lastMetadataChangeTime); @@ -28,10 +88,10 @@ namespace cryfs { } const char *DirEntry::deserializeAndAddToVector(const char *pos, vector *result) { - fspp::Dir::EntryType type = static_cast(_deserializeUint8(&pos)); - mode_t mode = _deserializeUint32(&pos); - uid_t uid = _deserializeUint32(&pos); - gid_t gid = _deserializeUint32(&pos); + fspp::Dir::EntryType type = static_cast(_deserialize(&pos)); + mode_t mode = _deserialize(&pos); + uid_t uid = _deserialize(&pos); + gid_t gid = _deserialize(&pos); timespec lastAccessTime = _deserializeTimeValue(&pos); timespec lastModificationTime = _deserializeTimeValue(&pos); timespec lastMetadataChangeTime = _deserializeTimeValue(&pos); @@ -42,74 +102,6 @@ namespace cryfs { return pos; } - unsigned int DirEntry::_serializeTimeValue(uint8_t *dest, timespec value) { - unsigned int offset = 0; - *reinterpret_cast(dest+offset) = value.tv_sec; - offset += sizeof(uint64_t); - *reinterpret_cast(dest+offset) = value.tv_nsec; - offset += sizeof(uint32_t); - ASSERT(offset == _serializedTimeValueSize(), "serialized to wrong size"); - return offset; - } - - size_t DirEntry::_serializedTimeValueSize() { - return sizeof(uint64_t) + sizeof(uint32_t); - } - - timespec DirEntry::_deserializeTimeValue(const char **pos) { - timespec value{}; - value.tv_sec = *(uint64_t*)(*pos); - *pos += sizeof(uint64_t); - value.tv_nsec = *(uint32_t*)(*pos); - *pos += sizeof(uint32_t); - return value; - } - - unsigned int DirEntry::_serializeUint8(uint8_t *dest, uint8_t value) { - *reinterpret_cast(dest) = value; - return sizeof(uint8_t); - } - - uint8_t DirEntry::_deserializeUint8(const char **pos) { - uint8_t value = *(uint8_t*)(*pos); - *pos += sizeof(uint8_t); - return value; - } - - unsigned int DirEntry::_serializeUint32(uint8_t *dest, uint32_t value) { - *reinterpret_cast(dest) = value; - return sizeof(uint32_t); - } - - uint32_t DirEntry::_deserializeUint32(const char **pos) { - uint32_t value = *(uint32_t*)(*pos); - *pos += sizeof(uint32_t); - return value; - } - - unsigned int DirEntry::_serializeString(uint8_t *dest, const string &value) { - std::memcpy(dest, value.c_str(), value.size()+1); - return value.size() + 1; - } - - string DirEntry::_deserializeString(const char **pos) { - size_t length = strlen(*pos); - string value(*pos, length); - *pos += length + 1; - return value; - } - - unsigned int DirEntry::_serializeBlockId(uint8_t *dest, const BlockId &blockId) { - blockId.ToBinary(dest); - return blockId.BINARY_LENGTH; - } - - BlockId DirEntry::_deserializeBlockId(const char **pos) { - BlockId blockId = BlockId::FromBinary(*pos); - *pos += BlockId::BINARY_LENGTH; - return blockId; - } - size_t DirEntry::serializedSize() const { return 1 + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t) + 3*_serializedTimeValueSize() + ( _name.size() + 1) + _blockId.BINARY_LENGTH; diff --git a/src/cryfs/filesystem/fsblobstore/utils/DirEntry.h b/src/cryfs/filesystem/fsblobstore/utils/DirEntry.h index 41acb6a3..6a60277e 100644 --- a/src/cryfs/filesystem/fsblobstore/utils/DirEntry.h +++ b/src/cryfs/filesystem/fsblobstore/utils/DirEntry.h @@ -46,17 +46,6 @@ namespace cryfs { timespec lastMetadataChangeTime() const; private: - static size_t _serializedTimeValueSize(); - static unsigned int _serializeTimeValue(uint8_t *dest, timespec value); - static unsigned int _serializeUint8(uint8_t *dest, uint8_t value); - static unsigned int _serializeUint32(uint8_t *dest, uint32_t value); - static unsigned int _serializeString(uint8_t *dest, const std::string &value); - static unsigned int _serializeBlockId(uint8_t *dest, const blockstore::BlockId &value); - static timespec _deserializeTimeValue(const char **pos); - static uint8_t _deserializeUint8(const char **pos); - static uint32_t _deserializeUint32(const char **pos); - static std::string _deserializeString(const char **pos); - static blockstore::BlockId _deserializeBlockId(const char **pos); void _updateLastMetadataChangeTime(); diff --git a/src/cryfs/filesystem/fsblobstore/utils/DirEntryList.cpp b/src/cryfs/filesystem/fsblobstore/utils/DirEntryList.cpp index 583d6fc8..ac4bfad5 100644 --- a/src/cryfs/filesystem/fsblobstore/utils/DirEntryList.cpp +++ b/src/cryfs/filesystem/fsblobstore/utils/DirEntryList.cpp @@ -211,11 +211,11 @@ void DirEntryList::setMode(const BlockId &blockId, mode_t mode) { bool DirEntryList::setUidGid(const BlockId &blockId, uid_t uid, gid_t gid) { auto found = _findById(blockId); bool changed = false; - if (uid != (uid_t)-1) { + if (uid != static_cast(-1)) { found->setUid(uid); changed = true; } - if (gid != (gid_t)-1) { + if (gid != static_cast(-1)) { found->setGid(gid); changed = true; } diff --git a/src/cryfs/localstate/LocalStateMetadata.cpp b/src/cryfs/localstate/LocalStateMetadata.cpp index 8c33d998..ca221eac 100644 --- a/src/cryfs/localstate/LocalStateMetadata.cpp +++ b/src/cryfs/localstate/LocalStateMetadata.cpp @@ -58,7 +58,7 @@ namespace { uint32_t _generateClientId() { uint32_t result; do { - result = *reinterpret_cast(Random::PseudoRandom().getFixedSize().data()); + result = cpputils::deserialize(Random::PseudoRandom().getFixedSize().data()); } while(result == KnownBlockVersions::CLIENT_ID_FOR_DELETED_BLOCK); // Safety check - CLIENT_ID_FOR_DELETED_BLOCK shouldn't be used by any valid client. return result; } diff --git a/src/fspp/fstest/FsppFileTest.h b/src/fspp/fstest/FsppFileTest.h index 10b0daa8..f88697d9 100644 --- a/src/fspp/fstest/FsppFileTest.h +++ b/src/fspp/fstest/FsppFileTest.h @@ -74,7 +74,7 @@ public: void Test_Chmod(fspp::File *file, fspp::Node *node) { node->chmod(S_IFREG | S_IRUSR | S_IWOTH); this->IN_STAT(file, node, [] (struct stat st){ - EXPECT_EQ((mode_t)(S_IFREG | S_IRUSR | S_IWOTH), st.st_mode); + EXPECT_EQ(static_cast(S_IFREG | S_IRUSR | S_IWOTH), st.st_mode); }); } diff --git a/src/fspp/fstest/FsppOpenFileTest.h b/src/fspp/fstest/FsppOpenFileTest.h index 3e113677..3c2f7bbb 100644 --- a/src/fspp/fstest/FsppOpenFileTest.h +++ b/src/fspp/fstest/FsppOpenFileTest.h @@ -15,7 +15,7 @@ public: void EXPECT_SIZE(uint64_t expectedSize, fspp::OpenFile *openFile) { IN_STAT(openFile, [expectedSize] (struct stat st) { - EXPECT_EQ(expectedSize, (uint64_t)st.st_size); + EXPECT_EQ(expectedSize, static_cast(st.st_size)); }); EXPECT_NUMBYTES_READABLE(expectedSize, openFile); @@ -26,7 +26,7 @@ public: //Try to read one byte more than the expected size ssize_t readBytes = openFile->read(data.data(), expectedSize+1, 0); //and check that it only read the expected size (but also not less) - EXPECT_EQ(expectedSize, (uint64_t)readBytes); + EXPECT_EQ(expectedSize, static_cast(readBytes)); } }; diff --git a/src/fspp/fstest/testutils/FileTest.h b/src/fspp/fstest/testutils/FileTest.h index 773e53cb..95ae2d14 100644 --- a/src/fspp/fstest/testutils/FileTest.h +++ b/src/fspp/fstest/testutils/FileTest.h @@ -38,7 +38,7 @@ public: void EXPECT_SIZE(uint64_t expectedSize, fspp::File *file, fspp::Node *node) { IN_STAT(file, node, [expectedSize] (struct stat st) { - EXPECT_EQ(expectedSize, (uint64_t)st.st_size); + EXPECT_EQ(expectedSize, static_cast(st.st_size)); }); EXPECT_NUMBYTES_READABLE(expectedSize, file); @@ -50,7 +50,7 @@ public: //Try to read one byte more than the expected size ssize_t readBytes = openFile->read(data.data(), expectedSize+1, 0); //and check that it only read the expected size (but also not less) - EXPECT_EQ(expectedSize, (uint64_t)readBytes); + EXPECT_EQ(expectedSize, static_cast(readBytes)); } void EXPECT_ATIME_EQ(struct timespec expected, struct stat st) { diff --git a/src/fspp/fstest/testutils/FsppNodeTest.h b/src/fspp/fstest/testutils/FsppNodeTest.h index d656c66a..c227a592 100644 --- a/src/fspp/fstest/testutils/FsppNodeTest.h +++ b/src/fspp/fstest/testutils/FsppNodeTest.h @@ -17,7 +17,7 @@ public: void EXPECT_SIZE(uint64_t expectedSize, fspp::Node *node) { IN_STAT(node, [expectedSize] (struct stat st) { - EXPECT_EQ(expectedSize, (uint64_t)st.st_size); + EXPECT_EQ(expectedSize, static_cast(st.st_size)); }); } }; diff --git a/src/fspp/fuse/Fuse.cpp b/src/fspp/fuse/Fuse.cpp index e35ae3c6..0df6933c 100644 --- a/src/fspp/fuse/Fuse.cpp +++ b/src/fspp/fuse/Fuse.cpp @@ -16,7 +16,7 @@ namespace bf = boost::filesystem; using namespace cpputils::logging; using namespace fspp::fuse; -#define FUSE_OBJ ((Fuse *) fuse_get_context()->private_data) +#define FUSE_OBJ (static_cast(fuse_get_context()->private_data)) // Remove the following line, if you don't want to output each fuse operation on the console //#define FSPP_LOG 1 @@ -236,7 +236,7 @@ void Fuse::run(const bf::path &mountdir, const vector &fuseOptions) { _argv = _build_argv(mountdir, fuseOptions); - fuse_main(_argv.size(), _argv.data(), operations(), (void*)this); + fuse_main(_argv.size(), _argv.data(), operations(), this); } vector Fuse::_build_argv(const bf::path &mountdir, const vector &fuseOptions) { diff --git a/src/stats/main.cpp b/src/stats/main.cpp index 5cee0ccc..76d01484 100644 --- a/src/stats/main.cpp +++ b/src/stats/main.cpp @@ -68,7 +68,7 @@ set _getBlockstoreUnaccountedBlocks(const CryConfig &config) { auto innerNode = dynamic_pointer_move(*node); if (innerNode != none) { for (uint32_t childIndex = 0; childIndex < (*innerNode)->numChildren(); ++childIndex) { - auto child = (*innerNode)->getChild(childIndex)->blockId(); + auto child = (*innerNode)->readChild(childIndex).blockId(); unaccountedBlocks.erase(child); } } diff --git a/test/blobstore/implementations/onblocks/BlobReadWriteTest.cpp b/test/blobstore/implementations/onblocks/BlobReadWriteTest.cpp index 9a6d995b..4b74b1a0 100644 --- a/test/blobstore/implementations/onblocks/BlobReadWriteTest.cpp +++ b/test/blobstore/implementations/onblocks/BlobReadWriteTest.cpp @@ -84,7 +84,7 @@ public: Data end(GetParam().blobsize - count - start); std::memcpy(begin.data(), expected.data(), start); - std::memcpy(end.data(), (uint8_t*)expected.data()+start+count, end.size()); + std::memcpy(end.data(), expected.dataOffset(start+count), end.size()); EXPECT_DATA_READS_AS(begin, blob, 0, start); EXPECT_DATA_READS_AS(end, blob, start + count, end.size()); @@ -152,7 +152,7 @@ TEST_P(BlobReadWriteDataTest, WriteWholeAndReadPart) { blob->write(this->backgroundData.data(), 0, GetParam().blobsize); Data read(GetParam().count); blob->read(read.data(), GetParam().offset, GetParam().count); - EXPECT_EQ(0, std::memcmp(read.data(), (uint8_t*)this->backgroundData.data()+GetParam().offset, GetParam().count)); + EXPECT_EQ(0, std::memcmp(read.data(), this->backgroundData.dataOffset(GetParam().offset), GetParam().count)); } TEST_P(BlobReadWriteDataTest, WritePartAndReadWhole) { @@ -161,6 +161,6 @@ TEST_P(BlobReadWriteDataTest, WritePartAndReadWhole) { blob->write(this->foregroundData.data(), GetParam().offset, GetParam().count); Data read = readBlob(*blob); EXPECT_EQ(0, std::memcmp(read.data(), this->backgroundData.data(), GetParam().offset)); - EXPECT_EQ(0, std::memcmp((uint8_t*)read.data()+GetParam().offset, this->foregroundData.data(), GetParam().count)); - EXPECT_EQ(0, std::memcmp((uint8_t*)read.data()+GetParam().offset+GetParam().count, (uint8_t*)this->backgroundData.data()+GetParam().offset+GetParam().count, GetParam().blobsize-GetParam().count-GetParam().offset)); + EXPECT_EQ(0, std::memcmp(read.dataOffset(GetParam().offset), this->foregroundData.data(), GetParam().count)); + EXPECT_EQ(0, std::memcmp(read.dataOffset(GetParam().offset+GetParam().count), this->backgroundData.dataOffset(GetParam().offset+GetParam().count), GetParam().blobsize-GetParam().count-GetParam().offset)); } diff --git a/test/blobstore/implementations/onblocks/BlobSizeTest.cpp b/test/blobstore/implementations/onblocks/BlobSizeTest.cpp index 5b7320f1..dcd9a998 100644 --- a/test/blobstore/implementations/onblocks/BlobSizeTest.cpp +++ b/test/blobstore/implementations/onblocks/BlobSizeTest.cpp @@ -152,7 +152,7 @@ TEST_F(BlobSizeDataTest, DataStaysIntactWhenGrowing) { blob->write(randomData.data(), 0, MEDIUM_SIZE); blob->resize(LARGE_SIZE); EXPECT_EQ(0, std::memcmp(readBlob(*blob).data(), randomData.data(), MEDIUM_SIZE)); - EXPECT_EQ(0, std::memcmp((uint8_t*)readBlob(*blob).data() + MEDIUM_SIZE, ZEROES.data(), LARGE_SIZE-MEDIUM_SIZE)); + EXPECT_EQ(0, std::memcmp(readBlob(*blob).dataOffset(MEDIUM_SIZE), ZEROES.data(), LARGE_SIZE-MEDIUM_SIZE)); } TEST_F(BlobSizeDataTest, DataStaysIntactWhenShrinking) { @@ -168,5 +168,5 @@ TEST_F(BlobSizeDataTest, ChangedAreaIsZeroedOutWhenShrinkingAndRegrowing) { blob->resize(MEDIUM_SIZE); blob->resize(LARGE_SIZE); EXPECT_EQ(0, std::memcmp(readBlob(*blob).data(), randomData.data(), MEDIUM_SIZE)); - EXPECT_EQ(0, std::memcmp((uint8_t*)readBlob(*blob).data() + MEDIUM_SIZE, ZEROES.data(), LARGE_SIZE-MEDIUM_SIZE)); + EXPECT_EQ(0, std::memcmp(readBlob(*blob).dataOffset(MEDIUM_SIZE), ZEROES.data(), LARGE_SIZE-MEDIUM_SIZE)); } diff --git a/test/blobstore/implementations/onblocks/BlobStoreTest.cpp b/test/blobstore/implementations/onblocks/BlobStoreTest.cpp index e7c87a8a..aab75fc1 100644 --- a/test/blobstore/implementations/onblocks/BlobStoreTest.cpp +++ b/test/blobstore/implementations/onblocks/BlobStoreTest.cpp @@ -25,13 +25,13 @@ TEST_F(BlobStoreTest, BlobIsNotLoadableAfterDeletion_DeleteDirectly) { auto blob = blobStore->create(); BlockId blockId = blob->blockId(); blobStore->remove(std::move(blob)); - EXPECT_FALSE((bool)blobStore->load(blockId)); + EXPECT_FALSE(static_cast(blobStore->load(blockId))); } TEST_F(BlobStoreTest, BlobIsNotLoadableAfterDeletion_DeleteByKey) { auto blockId = blobStore->create()->blockId(); blobStore->remove(blockId); - EXPECT_FALSE((bool)blobStore->load(blockId)); + EXPECT_FALSE(static_cast(blobStore->load(blockId))); } TEST_F(BlobStoreTest, BlobIsNotLoadableAfterDeletion_DeleteAfterLoading) { @@ -39,5 +39,5 @@ TEST_F(BlobStoreTest, BlobIsNotLoadableAfterDeletion_DeleteAfterLoading) { BlockId blockId = blob->blockId(); reset(std::move(blob)); blobStore->remove(loadBlob(blockId)); - EXPECT_FALSE((bool)blobStore->load(blockId)); + EXPECT_FALSE(static_cast(blobStore->load(blockId))); } diff --git a/test/blobstore/implementations/onblocks/datanodestore/DataInnerNodeTest.cpp b/test/blobstore/implementations/onblocks/datanodestore/DataInnerNodeTest.cpp index 35d8343c..6cb7b86c 100644 --- a/test/blobstore/implementations/onblocks/datanodestore/DataInnerNodeTest.cpp +++ b/test/blobstore/implementations/onblocks/datanodestore/DataInnerNodeTest.cpp @@ -124,7 +124,7 @@ TEST_F(DataInnerNodeTest, InitializesCorrectly) { auto node = DataInnerNode::CreateNewNode(blockStore, nodeStore->layout(), 1, {leaf->blockId()}); EXPECT_EQ(1u, node->numChildren()); - EXPECT_EQ(leaf->blockId(), node->getChild(0)->blockId()); + EXPECT_EQ(leaf->blockId(), node->readChild(0).blockId()); } TEST_F(DataInnerNodeTest, ReinitializesCorrectly) { @@ -132,22 +132,22 @@ TEST_F(DataInnerNodeTest, ReinitializesCorrectly) { auto node = DataInnerNode::InitializeNewNode(blockStore->load(blockId).value(), nodeStore->layout(), 1, {leaf->blockId()}); EXPECT_EQ(1u, node->numChildren()); - EXPECT_EQ(leaf->blockId(), node->getChild(0)->blockId()); + EXPECT_EQ(leaf->blockId(), node->readChild(0).blockId()); } TEST_F(DataInnerNodeTest, IsCorrectlyInitializedAfterLoading) { auto loaded = CreateAndLoadNewInnerNode(*leaf); EXPECT_EQ(1u, loaded->numChildren()); - EXPECT_EQ(leaf->blockId(), loaded->getChild(0)->blockId()); + EXPECT_EQ(leaf->blockId(), loaded->readChild(0).blockId()); } TEST_F(DataInnerNodeTest, AddingASecondLeaf) { BlockId leaf2_blockId = AddALeafTo(node.get()); EXPECT_EQ(2u, node->numChildren()); - EXPECT_EQ(leaf->blockId(), node->getChild(0)->blockId()); - EXPECT_EQ(leaf2_blockId, node->getChild(1)->blockId()); + EXPECT_EQ(leaf->blockId(), node->readChild(0).blockId()); + EXPECT_EQ(leaf2_blockId, node->readChild(1).blockId()); } TEST_F(DataInnerNodeTest, AddingASecondLeafAndReload) { @@ -155,8 +155,8 @@ TEST_F(DataInnerNodeTest, AddingASecondLeafAndReload) { auto loaded = CreateAndLoadNewInnerNode(1, {leaf->blockId(), leaf2->blockId()}); EXPECT_EQ(2u, loaded->numChildren()); - EXPECT_EQ(leaf->blockId(), loaded->getChild(0)->blockId()); - EXPECT_EQ(leaf2->blockId(), loaded->getChild(1)->blockId()); + EXPECT_EQ(leaf->blockId(), loaded->readChild(0).blockId()); + EXPECT_EQ(leaf2->blockId(), loaded->readChild(1).blockId()); } TEST_F(DataInnerNodeTest, BuildingAThreeLevelTree) { @@ -164,8 +164,8 @@ TEST_F(DataInnerNodeTest, BuildingAThreeLevelTree) { auto parent = CreateNewInnerNode(node->depth()+1, {node->blockId(), node2->blockId()}); EXPECT_EQ(2u, parent->numChildren()); - EXPECT_EQ(node->blockId(), parent->getChild(0)->blockId()); - EXPECT_EQ(node2->blockId(), parent->getChild(1)->blockId()); + EXPECT_EQ(node->blockId(), parent->readChild(0).blockId()); + EXPECT_EQ(node2->blockId(), parent->readChild(1).blockId()); } TEST_F(DataInnerNodeTest, BuildingAThreeLevelTreeAndReload) { @@ -173,8 +173,8 @@ TEST_F(DataInnerNodeTest, BuildingAThreeLevelTreeAndReload) { auto parent = CreateAndLoadNewInnerNode(node->depth()+1, {node->blockId(), node2->blockId()}); EXPECT_EQ(2u, parent->numChildren()); - EXPECT_EQ(node->blockId(), parent->getChild(0)->blockId()); - EXPECT_EQ(node2->blockId(), parent->getChild(1)->blockId()); + EXPECT_EQ(node->blockId(), parent->readChild(0).blockId()); + EXPECT_EQ(node2->blockId(), parent->readChild(1).blockId()); } TEST_F(DataInnerNodeTest, ConvertToInternalNode) { @@ -183,7 +183,7 @@ TEST_F(DataInnerNodeTest, ConvertToInternalNode) { unique_ref converted = DataNode::convertToNewInnerNode(std::move(node), nodeStore->layout(), *child); EXPECT_EQ(1u, converted->numChildren()); - EXPECT_EQ(child->blockId(), converted->getChild(0)->blockId()); + EXPECT_EQ(child->blockId(), converted->readChild(0).blockId()); EXPECT_EQ(node_blockId, converted->blockId()); } @@ -191,7 +191,7 @@ TEST_F(DataInnerNodeTest, ConvertToInternalNodeZeroesOutChildrenRegion) { BlockId blockId = CreateNodeWithDataConvertItToInnerNodeAndReturnKey(); auto block = blockStore->load(blockId).value(); - EXPECT_EQ(0, std::memcmp(ZEROES.data(), (uint8_t*)block->data()+DataNodeLayout::HEADERSIZE_BYTES+sizeof(DataInnerNode::ChildEntry), nodeStore->layout().maxBytesPerLeaf()-sizeof(DataInnerNode::ChildEntry))); + EXPECT_EQ(0, std::memcmp(ZEROES.data(), static_cast(block->data())+DataNodeLayout::HEADERSIZE_BYTES+sizeof(DataInnerNode::ChildEntry), nodeStore->layout().maxBytesPerLeaf()-sizeof(DataInnerNode::ChildEntry))); } TEST_F(DataInnerNodeTest, CopyingCreatesNewNode) { @@ -203,7 +203,7 @@ TEST_F(DataInnerNodeTest, CopyInnerNodeWithOneChild) { auto copied = CopyInnerNode(*node); EXPECT_EQ(node->numChildren(), copied->numChildren()); - EXPECT_EQ(node->getChild(0)->blockId(), copied->getChild(0)->blockId()); + EXPECT_EQ(node->readChild(0).blockId(), copied->readChild(0).blockId()); } TEST_F(DataInnerNodeTest, CopyInnerNodeWithTwoChildren) { @@ -211,21 +211,21 @@ TEST_F(DataInnerNodeTest, CopyInnerNodeWithTwoChildren) { auto copied = CopyInnerNode(*node); EXPECT_EQ(node->numChildren(), copied->numChildren()); - EXPECT_EQ(node->getChild(0)->blockId(), copied->getChild(0)->blockId()); - EXPECT_EQ(node->getChild(1)->blockId(), copied->getChild(1)->blockId()); + EXPECT_EQ(node->readChild(0).blockId(), copied->readChild(0).blockId()); + EXPECT_EQ(node->readChild(1).blockId(), copied->readChild(1).blockId()); } TEST_F(DataInnerNodeTest, LastChildWhenOneChild) { - EXPECT_EQ(leaf->blockId(), node->LastChild()->blockId()); + EXPECT_EQ(leaf->blockId(), node->readLastChild().blockId()); } TEST_F(DataInnerNodeTest, LastChildWhenTwoChildren) { BlockId blockId = AddALeafTo(node.get()); - EXPECT_EQ(blockId, node->LastChild()->blockId()); + EXPECT_EQ(blockId, node->readLastChild().blockId()); } TEST_F(DataInnerNodeTest, LastChildWhenThreeChildren) { AddALeafTo(node.get()); BlockId blockId = AddALeafTo(node.get()); - EXPECT_EQ(blockId, node->LastChild()->blockId()); + EXPECT_EQ(blockId, node->readLastChild().blockId()); } diff --git a/test/blobstore/implementations/onblocks/datanodestore/DataLeafNodeTest.cpp b/test/blobstore/implementations/onblocks/datanodestore/DataLeafNodeTest.cpp index cff5b9c9..1647bb0b 100644 --- a/test/blobstore/implementations/onblocks/datanodestore/DataLeafNodeTest.cpp +++ b/test/blobstore/implementations/onblocks/datanodestore/DataLeafNodeTest.cpp @@ -17,6 +17,7 @@ using cpputils::unique_ref; using cpputils::make_unique_ref; using std::string; using cpputils::DataFixture; +using cpputils::deserialize; //TODO Split into multiple files @@ -186,7 +187,7 @@ TEST_F(DataLeafNodeTest, SpaceGetsZeroFilledWhenShrinkingAndRegrowing) { leaf->resize(randomData.size()); //Check that the space was filled with zeroes - EXPECT_EQ(0, std::memcmp(ZEROES.data(), ((uint8_t*)loadData(*leaf).data())+smaller_size, 100)); + EXPECT_EQ(0, std::memcmp(ZEROES.data(), static_cast(loadData(*leaf).data())+smaller_size, 100)); } TEST_F(DataLeafNodeTest, DataGetsZeroFilledWhenShrinking) { @@ -195,14 +196,14 @@ TEST_F(DataLeafNodeTest, DataGetsZeroFilledWhenShrinking) { { //At first, we expect there to be random data in the underlying data block auto block = blockStore->load(blockId).value(); - EXPECT_EQ(0, std::memcmp((char*)randomData.data()+smaller_size, (uint8_t*)block->data()+DataNodeLayout::HEADERSIZE_BYTES+smaller_size, 100)); + EXPECT_EQ(0, std::memcmp(randomData.dataOffset(smaller_size), static_cast(block->data())+DataNodeLayout::HEADERSIZE_BYTES+smaller_size, 100)); } //After shrinking, we expect there to be zeroes in the underlying data block ResizeLeaf(blockId, smaller_size); { auto block = blockStore->load(blockId).value(); - EXPECT_EQ(0, std::memcmp(ZEROES.data(), (uint8_t*)block->data()+DataNodeLayout::HEADERSIZE_BYTES+smaller_size, 100)); + EXPECT_EQ(0, std::memcmp(ZEROES.data(), static_cast(block->data())+DataNodeLayout::HEADERSIZE_BYTES+smaller_size, 100)); } } @@ -221,7 +222,7 @@ TEST_F(DataLeafNodeTest, ConvertToInternalNode) { unique_ref converted = DataNode::convertToNewInnerNode(std::move(leaf), LAYOUT, *child); EXPECT_EQ(1u, converted->numChildren()); - EXPECT_EQ(child->blockId(), converted->getChild(0)->blockId()); + EXPECT_EQ(child->blockId(), converted->readChild(0).blockId()); EXPECT_EQ(leaf_blockId, converted->blockId()); } @@ -229,7 +230,7 @@ TEST_F(DataLeafNodeTest, ConvertToInternalNodeZeroesOutChildrenRegion) { BlockId blockId = CreateLeafWithDataConvertItToInnerNodeAndReturnKey(); auto block = blockStore->load(blockId).value(); - EXPECT_EQ(0, std::memcmp(ZEROES.data(), (uint8_t*)block->data()+DataNodeLayout::HEADERSIZE_BYTES+sizeof(DataInnerNode::ChildEntry), nodeStore->layout().maxBytesPerLeaf()-sizeof(DataInnerNode::ChildEntry))); + EXPECT_EQ(0, std::memcmp(ZEROES.data(), static_cast(block->data())+DataNodeLayout::HEADERSIZE_BYTES+sizeof(DataInnerNode::ChildEntry), nodeStore->layout().maxBytesPerLeaf()-sizeof(DataInnerNode::ChildEntry))); } TEST_F(DataLeafNodeTest, CopyingCreatesANewLeaf) { @@ -250,10 +251,10 @@ TEST_F(DataLeafNodeTest, CopyDataLeaf) { EXPECT_EQ(0, std::memcmp(loadData(*leaf).data(), loadData(*copied).data(), leaf->numBytes())); //Test that they have different data regions (changing the original one doesn't change the copy) - char data = '\0'; + uint8_t data = 0; leaf->write(&data, 0, 1); - EXPECT_EQ(data, *(char*)loadData(*leaf).data()); - EXPECT_NE(data, *(char*)loadData(*copied).data()); + EXPECT_EQ(data, deserialize(loadData(*leaf).data())); + EXPECT_NE(data, deserialize(loadData(*copied).data())); } @@ -292,7 +293,7 @@ public: Data end(GetParam().leafsize - count - start); std::memcpy(begin.data(), expected.data(), start); - std::memcpy(end.data(), (uint8_t*)expected.data()+start+count, end.size()); + std::memcpy(end.data(), expected.dataOffset(start+count), end.size()); EXPECT_DATA_READS_AS(begin, leaf, 0, start); EXPECT_DATA_READS_AS(end, leaf, start + count, end.size()); diff --git a/test/blobstore/implementations/onblocks/datanodestore/DataNodeStoreTest.cpp b/test/blobstore/implementations/onblocks/datanodestore/DataNodeStoreTest.cpp index 19dc4981..98b3149d 100644 --- a/test/blobstore/implementations/onblocks/datanodestore/DataNodeStoreTest.cpp +++ b/test/blobstore/implementations/onblocks/datanodestore/DataNodeStoreTest.cpp @@ -92,7 +92,7 @@ TEST_F(DataNodeStoreTest, CreatedInnerNodeIsInitialized) { auto leaf = nodeStore->createNewLeafNode(Data(0)); auto node = nodeStore->createNewInnerNode(1, {leaf->blockId()}); EXPECT_EQ(1u, node->numChildren()); - EXPECT_EQ(leaf->blockId(), node->getChild(0)->blockId()); + EXPECT_EQ(leaf->blockId(), node->readChild(0).blockId()); } TEST_F(DataNodeStoreTest, CreatedLeafNodeIsInitialized) { diff --git a/test/blobstore/implementations/onblocks/datanodestore/DataNodeViewTest.cpp b/test/blobstore/implementations/onblocks/datanodestore/DataNodeViewTest.cpp index b24f07a2..447f2b57 100644 --- a/test/blobstore/implementations/onblocks/datanodestore/DataNodeViewTest.cpp +++ b/test/blobstore/implementations/onblocks/datanodestore/DataNodeViewTest.cpp @@ -87,64 +87,12 @@ TEST_F(DataNodeViewTest, HeaderAndBodyDontOverlap) { EXPECT_EQ(0, std::memcmp(view.data(), randomData.data(), DATASIZE_BYTES)); } -TEST_F(DataNodeViewTest, DataBeginWorksWithOneByteEntries) { +TEST_F(DataNodeViewTest, Data) { auto block = blockStore->create(Data(BLOCKSIZE_BYTES)); - uint8_t *blockBegin = (uint8_t*)block->data(); + const uint8_t *blockBegin = static_cast(block->data()); DataNodeView view(std::move(block)); - EXPECT_EQ(blockBegin+DataNodeLayout::HEADERSIZE_BYTES, view.DataBegin()); -} - -TEST_F(DataNodeViewTest, DataBeginWorksWithEightByteEntries) { - auto block = blockStore->create(Data(BLOCKSIZE_BYTES)); - uint8_t *blockBegin = (uint8_t*)block->data(); - DataNodeView view(std::move(block)); - - EXPECT_EQ(blockBegin+DataNodeLayout::HEADERSIZE_BYTES, (uint8_t*)view.DataBegin()); -} - -TEST_F(DataNodeViewTest, DataEndWorksWithOneByteEntries) { - auto block = blockStore->create(Data(BLOCKSIZE_BYTES)); - uint8_t *blockBegin = (uint8_t*)block->data(); - DataNodeView view(std::move(block)); - - EXPECT_EQ(blockBegin+BLOCKSIZE_BYTES, view.DataEnd()); -} - -TEST_F(DataNodeViewTest, DataEndWorksWithEightByteEntries) { - auto block = blockStore->create(Data(BLOCKSIZE_BYTES)); - uint8_t *blockBegin = (uint8_t*)block->data(); - DataNodeView view(std::move(block)); - - EXPECT_EQ(blockBegin+BLOCKSIZE_BYTES, (uint8_t*)view.DataEnd()); -} - -struct SizedDataEntry { - uint8_t data[6]; -}; -BOOST_STATIC_ASSERT_MSG(DataNodeViewTest::DATASIZE_BYTES % sizeof(SizedDataEntry) != 0, - "This test case only makes sense, if the data entries don't fill up the whole space. " - "There should be some space left at the end that is not used, because it isn't enough space for a full entry. " - "If this static assertion fails, please use a different size for SizedDataEntry."); - -TEST_F(DataNodeViewTest, DataBeginWorksWithStructEntries) { - auto block = blockStore->create(Data(BLOCKSIZE_BYTES)); - uint8_t *blockBegin = (uint8_t*)block->data(); - DataNodeView view(std::move(block)); - - EXPECT_EQ(blockBegin+DataNodeLayout::HEADERSIZE_BYTES, (uint8_t*)view.DataBegin()); -} - -TEST_F(DataNodeViewTest, DataEndWorksWithStructByteEntries) { - auto block = blockStore->create(Data(BLOCKSIZE_BYTES)); - uint8_t *blockBegin = (uint8_t*)block->data(); - DataNodeView view(std::move(block)); - - unsigned int numFittingEntries = DATASIZE_BYTES / sizeof(SizedDataEntry); - - uint8_t *dataEnd = (uint8_t*)view.DataEnd(); - EXPECT_EQ(blockBegin+DataNodeLayout::HEADERSIZE_BYTES + numFittingEntries * sizeof(SizedDataEntry), dataEnd); - EXPECT_LT(dataEnd, blockBegin + BLOCKSIZE_BYTES); + EXPECT_EQ(blockBegin+DataNodeLayout::HEADERSIZE_BYTES, static_cast(view.data())); } //TODO Test that header fields (and data) are also stored over reloads diff --git a/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_NumStoredBytes.cpp b/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_NumStoredBytes.cpp index 85f54be6..12859550 100644 --- a/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_NumStoredBytes.cpp +++ b/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_NumStoredBytes.cpp @@ -19,7 +19,7 @@ TEST_F(DataTreeTest_NumStoredBytes, CreatedTreeIsEmpty) { class DataTreeTest_NumStoredBytes_P: public DataTreeTest_NumStoredBytes, public WithParamInterface {}; INSTANTIATE_TEST_CASE_P(EmptyLastLeaf, DataTreeTest_NumStoredBytes_P, Values(0u)); INSTANTIATE_TEST_CASE_P(HalfFullLastLeaf, DataTreeTest_NumStoredBytes_P, Values(5u, 10u)); -INSTANTIATE_TEST_CASE_P(FullLastLeaf, DataTreeTest_NumStoredBytes_P, Values((uint32_t)DataNodeLayout(DataTreeTest_NumStoredBytes::BLOCKSIZE_BYTES).maxBytesPerLeaf())); +INSTANTIATE_TEST_CASE_P(FullLastLeaf, DataTreeTest_NumStoredBytes_P, Values(static_cast(DataNodeLayout(DataTreeTest_NumStoredBytes::BLOCKSIZE_BYTES).maxBytesPerLeaf()))); TEST_P(DataTreeTest_NumStoredBytes_P, SingleLeaf) { BlockId blockId = CreateLeafWithSize(GetParam())->blockId(); diff --git a/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_ResizeByTraversing.cpp b/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_ResizeByTraversing.cpp index 604f2162..70357975 100644 --- a/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_ResizeByTraversing.cpp +++ b/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_ResizeByTraversing.cpp @@ -70,9 +70,9 @@ public: DataInnerNode *inner = dynamic_cast(root.get()); if (inner != nullptr) { for (uint32_t i = 0; i < inner->numChildren()-1; ++i) { - EXPECT_IS_MAXDATA_TREE(inner->getChild(i)->blockId()); + EXPECT_IS_MAXDATA_TREE(inner->readChild(i).blockId()); } - EXPECT_IS_LEFTMAXDATA_TREE(inner->LastChild()->blockId()); + EXPECT_IS_LEFTMAXDATA_TREE(inner->readLastChild().blockId()); } } @@ -81,7 +81,7 @@ public: DataInnerNode *inner = dynamic_cast(root.get()); if (inner != nullptr) { for (uint32_t i = 0; i < inner->numChildren(); ++i) { - EXPECT_IS_MAXDATA_TREE(inner->getChild(i)->blockId()); + EXPECT_IS_MAXDATA_TREE(inner->readChild(i).blockId()); } } else { DataLeafNode *leaf = dynamic_cast(root.get()); @@ -122,7 +122,7 @@ public: return std::move(*leaf); } auto inner = dynamic_pointer_move(root).value(); - return LastLeaf(inner->LastChild()->blockId()); + return LastLeaf(inner->readLastChild().blockId()); } uint32_t oldLastLeafSize; @@ -208,7 +208,7 @@ TEST_P(DataTreeTest_ResizeByTraversing_P, KeyDoesntChange) { } TEST_P(DataTreeTest_ResizeByTraversing_P, DataStaysIntact) { - uint32_t oldNumberOfLeaves = std::max(UINT64_C(1), ceilDivision(tree->numStoredBytes(), (uint64_t)nodeStore->layout().maxBytesPerLeaf())); + uint32_t oldNumberOfLeaves = std::max(UINT64_C(1), ceilDivision(tree->numStoredBytes(), static_cast(nodeStore->layout().maxBytesPerLeaf()))); TwoLevelDataFixture data(nodeStore, TwoLevelDataFixture::SizePolicy::Unchanged); BlockId blockId = tree->blockId(); cpputils::destruct(std::move(tree)); diff --git a/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_ResizeNumBytes.cpp b/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_ResizeNumBytes.cpp index 9a06e121..bf076b77 100644 --- a/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_ResizeNumBytes.cpp +++ b/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_ResizeNumBytes.cpp @@ -69,9 +69,9 @@ public: DataInnerNode *inner = dynamic_cast(root.get()); if (inner != nullptr) { for (uint32_t i = 0; i < inner->numChildren()-1; ++i) { - EXPECT_IS_MAXDATA_TREE(inner->getChild(i)->blockId()); + EXPECT_IS_MAXDATA_TREE(inner->readChild(i).blockId()); } - EXPECT_IS_LEFTMAXDATA_TREE(inner->LastChild()->blockId()); + EXPECT_IS_LEFTMAXDATA_TREE(inner->readLastChild().blockId()); } } @@ -80,7 +80,7 @@ public: DataInnerNode *inner = dynamic_cast(root.get()); if (inner != nullptr) { for (uint32_t i = 0; i < inner->numChildren(); ++i) { - EXPECT_IS_MAXDATA_TREE(inner->getChild(i)->blockId()); + EXPECT_IS_MAXDATA_TREE(inner->readChild(i).blockId()); } } else { DataLeafNode *leaf = dynamic_cast(root.get()); @@ -114,7 +114,7 @@ public: return std::move(*leaf); } auto inner = dynamic_pointer_move(root).value(); - return LastLeaf(inner->LastChild()->blockId()); + return LastLeaf(inner->readLastChild().blockId()); } uint32_t oldLastLeafSize; @@ -208,7 +208,7 @@ TEST_P(DataTreeTest_ResizeNumBytes_P, KeyDoesntChange) { } TEST_P(DataTreeTest_ResizeNumBytes_P, DataStaysIntact) { - uint32_t oldNumberOfLeaves = std::max(UINT64_C(1), ceilDivision(tree->numStoredBytes(), (uint64_t)nodeStore->layout().maxBytesPerLeaf())); + uint32_t oldNumberOfLeaves = std::max(UINT64_C(1), ceilDivision(tree->numStoredBytes(), static_cast(nodeStore->layout().maxBytesPerLeaf()))); TwoLevelDataFixture data(nodeStore, TwoLevelDataFixture::SizePolicy::Unchanged); BlockId blockId = tree->blockId(); cpputils::destruct(std::move(tree)); diff --git a/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_TraverseLeaves.cpp b/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_TraverseLeaves.cpp index 217b926a..91119f95 100644 --- a/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_TraverseLeaves.cpp +++ b/test/blobstore/implementations/onblocks/datatreestore/DataTreeTest_TraverseLeaves.cpp @@ -61,7 +61,7 @@ public: void EXPECT_TRAVERSE_ALL_CHILDREN_OF(const DataInnerNode &node, bool isRightBorderNode, uint32_t firstLeafIndex) { for (unsigned int i = 0; i < node.numChildren(); ++i) { - EXPECT_TRAVERSE_LEAF(node.getChild(i)->blockId(), isRightBorderNode && i == node.numChildren()-1, firstLeafIndex+i); + EXPECT_TRAVERSE_LEAF(node.readChild(i).blockId(), isRightBorderNode && i == node.numChildren()-1, firstLeafIndex+i); } } @@ -106,21 +106,21 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseNothingInSingleLeafTree2) { TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstLeafOfFullTwolevelTree) { auto root = CreateFullTwoLevel(); - EXPECT_TRAVERSE_LEAF(root->getChild(0)->blockId(), false, 0); + EXPECT_TRAVERSE_LEAF(root->readChild(0).blockId(), false, 0); TraverseLeaves(root.get(), 0, 1); } TEST_F(DataTreeTest_TraverseLeaves, TraverseMiddleLeafOfFullTwolevelTree) { auto root = CreateFullTwoLevel(); - EXPECT_TRAVERSE_LEAF(root->getChild(5)->blockId(), false, 5); + EXPECT_TRAVERSE_LEAF(root->readChild(5).blockId(), false, 5); TraverseLeaves(root.get(), 5, 6); } TEST_F(DataTreeTest_TraverseLeaves, TraverseLastLeafOfFullTwolevelTree) { auto root = CreateFullTwoLevel(); - EXPECT_TRAVERSE_LEAF(root->getChild(nodeStore->layout().maxChildrenPerInnerNode()-1)->blockId(), true, nodeStore->layout().maxChildrenPerInnerNode()-1); + EXPECT_TRAVERSE_LEAF(root->readChild(nodeStore->layout().maxChildrenPerInnerNode()-1).blockId(), true, nodeStore->layout().maxChildrenPerInnerNode()-1); TraverseLeaves(root.get(), nodeStore->layout().maxChildrenPerInnerNode()-1, nodeStore->layout().maxChildrenPerInnerNode()); } @@ -141,21 +141,21 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseNothingInFullTwolevelTree2) { TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstLeafOfThreeLevelMinDataTree) { auto root = CreateThreeLevelMinData(); - EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->getChild(0)->blockId())->getChild(0)->blockId(), false, 0); + EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->readChild(0).blockId())->readChild(0).blockId(), false, 0); TraverseLeaves(root.get(), 0, 1); } TEST_F(DataTreeTest_TraverseLeaves, TraverseMiddleLeafOfThreeLevelMinDataTree) { auto root = CreateThreeLevelMinData(); - EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->getChild(0)->blockId())->getChild(5)->blockId(), false, 5); + EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->readChild(0).blockId())->readChild(5).blockId(), false, 5); TraverseLeaves(root.get(), 5, 6); } TEST_F(DataTreeTest_TraverseLeaves, TraverseLastLeafOfThreeLevelMinDataTree) { auto root = CreateThreeLevelMinData(); - EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->getChild(1)->blockId())->getChild(0)->blockId(), true, nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->readChild(1).blockId())->readChild(0).blockId(), true, nodeStore->layout().maxChildrenPerInnerNode()); TraverseLeaves(root.get(), nodeStore->layout().maxChildrenPerInnerNode(), nodeStore->layout().maxChildrenPerInnerNode()+1); } @@ -169,15 +169,15 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseAllLeavesOfFullTwolevelTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseAllLeavesOfThreelevelMinDataTree) { auto root = CreateThreeLevelMinData(); - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->getChild(0)->blockId()), false, 0); - EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->getChild(1)->blockId())->getChild(0)->blockId(), true, nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->readChild(0).blockId()), false, 0); + EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->readChild(1).blockId())->readChild(0).blockId(), true, nodeStore->layout().maxChildrenPerInnerNode()); TraverseLeaves(root.get(), 0, nodeStore->layout().maxChildrenPerInnerNode()+1); } TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstChildOfThreelevelMinDataTree) { auto root = CreateThreeLevelMinData(); - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->getChild(0)->blockId()), false, 0); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->readChild(0).blockId()), false, 0); TraverseLeaves(root.get(), 0, nodeStore->layout().maxChildrenPerInnerNode()); } @@ -185,7 +185,7 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstChildOfThreelevelMinDataTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstPartOfFullTwolevelTree) { auto root = CreateFullTwoLevel(); for (unsigned int i = 0; i < 5; ++i) { - EXPECT_TRAVERSE_LEAF(root->getChild(i)->blockId(), false, i); + EXPECT_TRAVERSE_LEAF(root->readChild(i).blockId(), false, i); } TraverseLeaves(root.get(), 0, 5); @@ -194,7 +194,7 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstPartOfFullTwolevelTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseInnerPartOfFullTwolevelTree) { auto root = CreateFullTwoLevel(); for (unsigned int i = 5; i < 10; ++i) { - EXPECT_TRAVERSE_LEAF(root->getChild(i)->blockId(), false, i); + EXPECT_TRAVERSE_LEAF(root->readChild(i).blockId(), false, i); } TraverseLeaves(root.get(), 5, 10); @@ -203,7 +203,7 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseInnerPartOfFullTwolevelTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseLastPartOfFullTwolevelTree) { auto root = CreateFullTwoLevel(); for (unsigned int i = 5; i < nodeStore->layout().maxChildrenPerInnerNode(); ++i) { - EXPECT_TRAVERSE_LEAF(root->getChild(i)->blockId(), i==nodeStore->layout().maxChildrenPerInnerNode()-1, i); + EXPECT_TRAVERSE_LEAF(root->readChild(i).blockId(), i==nodeStore->layout().maxChildrenPerInnerNode()-1, i); } TraverseLeaves(root.get(), 5, nodeStore->layout().maxChildrenPerInnerNode()); @@ -211,9 +211,9 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseLastPartOfFullTwolevelTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstPartOfThreelevelMinDataTree) { auto root = CreateThreeLevelMinData(); - auto node = LoadInnerNode(root->getChild(0)->blockId()); + auto node = LoadInnerNode(root->readChild(0).blockId()); for (unsigned int i = 0; i < 5; ++i) { - EXPECT_TRAVERSE_LEAF(node->getChild(i)->blockId(), false, i); + EXPECT_TRAVERSE_LEAF(node->readChild(i).blockId(), false, i); } TraverseLeaves(root.get(), 0, 5); @@ -221,9 +221,9 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstPartOfThreelevelMinDataTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseInnerPartOfThreelevelMinDataTree) { auto root = CreateThreeLevelMinData(); - auto node = LoadInnerNode(root->getChild(0)->blockId()); + auto node = LoadInnerNode(root->readChild(0).blockId()); for (unsigned int i = 5; i < 10; ++i) { - EXPECT_TRAVERSE_LEAF(node->getChild(i)->blockId(), false, i); + EXPECT_TRAVERSE_LEAF(node->readChild(i).blockId(), false, i); } TraverseLeaves(root.get(), 5, 10); @@ -231,18 +231,18 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseInnerPartOfThreelevelMinDataTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseLastPartOfThreelevelMinDataTree) { auto root = CreateThreeLevelMinData(); - auto node = LoadInnerNode(root->getChild(0)->blockId()); + auto node = LoadInnerNode(root->readChild(0).blockId()); for (unsigned int i = 5; i < nodeStore->layout().maxChildrenPerInnerNode(); ++i) { - EXPECT_TRAVERSE_LEAF(node->getChild(i)->blockId(), false, i); + EXPECT_TRAVERSE_LEAF(node->readChild(i).blockId(), false, i); } - EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->getChild(1)->blockId())->getChild(0)->blockId(), true, nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->readChild(1).blockId())->readChild(0).blockId(), true, nodeStore->layout().maxChildrenPerInnerNode()); TraverseLeaves(root.get(), 5, nodeStore->layout().maxChildrenPerInnerNode()+1); } TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstLeafOfThreelevelTree) { auto root = CreateThreeLevel(); - EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->getChild(0)->blockId())->getChild(0)->blockId(), false, 0); + EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->readChild(0).blockId())->readChild(0).blockId(), false, 0); TraverseLeaves(root.get(), 0, 1); } @@ -250,7 +250,7 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstLeafOfThreelevelTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseLastLeafOfThreelevelTree) { auto root = CreateThreeLevel(); uint32_t numLeaves = nodeStore->layout().maxChildrenPerInnerNode() * 5 + 3; - EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->LastChild()->blockId())->LastChild()->blockId(), true, numLeaves-1); + EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->readLastChild().blockId())->readLastChild().blockId(), true, numLeaves-1); TraverseLeaves(root.get(), numLeaves-1, numLeaves); } @@ -258,7 +258,7 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseLastLeafOfThreelevelTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseMiddleLeafOfThreelevelTree) { auto root = CreateThreeLevel(); uint32_t wantedLeafIndex = nodeStore->layout().maxChildrenPerInnerNode() * 2 + 5; - EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->getChild(2)->blockId())->getChild(5)->blockId(), false, wantedLeafIndex); + EXPECT_TRAVERSE_LEAF(LoadInnerNode(root->readChild(2).blockId())->readChild(5).blockId(), false, wantedLeafIndex); TraverseLeaves(root.get(), wantedLeafIndex, wantedLeafIndex+1); } @@ -267,12 +267,12 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstPartOfThreelevelTree) { auto root = CreateThreeLevel(); //Traverse all leaves in the first two children of the root for(unsigned int i = 0; i < 2; ++i) { - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->getChild(i)->blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->readChild(i).blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); } //Traverse some of the leaves in the third child of the root - auto child = LoadInnerNode(root->getChild(2)->blockId()); + auto child = LoadInnerNode(root->readChild(2).blockId()); for(unsigned int i = 0; i < 5; ++i) { - EXPECT_TRAVERSE_LEAF(child->getChild(i)->blockId(), false, 2 * nodeStore->layout().maxChildrenPerInnerNode() + i); + EXPECT_TRAVERSE_LEAF(child->readChild(i).blockId(), false, 2 * nodeStore->layout().maxChildrenPerInnerNode() + i); } TraverseLeaves(root.get(), 0, 2 * nodeStore->layout().maxChildrenPerInnerNode() + 5); @@ -281,18 +281,18 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseFirstPartOfThreelevelTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseMiddlePartOfThreelevelTree_OnlyFullChildren) { auto root = CreateThreeLevel(); //Traverse some of the leaves in the second child of the root - auto child = LoadInnerNode(root->getChild(1)->blockId()); + auto child = LoadInnerNode(root->readChild(1).blockId()); for(unsigned int i = 5; i < nodeStore->layout().maxChildrenPerInnerNode(); ++i) { - EXPECT_TRAVERSE_LEAF(child->getChild(i)->blockId(), false, nodeStore->layout().maxChildrenPerInnerNode() + i); + EXPECT_TRAVERSE_LEAF(child->readChild(i).blockId(), false, nodeStore->layout().maxChildrenPerInnerNode() + i); } //Traverse all leaves in the third and fourth child of the root for(unsigned int i = 2; i < 4; ++i) { - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->getChild(i)->blockId()),false, i * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->readChild(i).blockId()),false, i * nodeStore->layout().maxChildrenPerInnerNode()); } //Traverse some of the leaves in the fifth child of the root - child = LoadInnerNode(root->getChild(4)->blockId()); + child = LoadInnerNode(root->readChild(4).blockId()); for(unsigned int i = 0; i < 5; ++i) { - EXPECT_TRAVERSE_LEAF(child->getChild(i)->blockId(), false, 4 * nodeStore->layout().maxChildrenPerInnerNode() + i); + EXPECT_TRAVERSE_LEAF(child->readChild(i).blockId(), false, 4 * nodeStore->layout().maxChildrenPerInnerNode() + i); } TraverseLeaves(root.get(), nodeStore->layout().maxChildrenPerInnerNode() + 5, 4 * nodeStore->layout().maxChildrenPerInnerNode() + 5); @@ -301,18 +301,18 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseMiddlePartOfThreelevelTree_OnlyFullC TEST_F(DataTreeTest_TraverseLeaves, TraverseMiddlePartOfThreelevelTree_AlsoLastNonfullChild) { auto root = CreateThreeLevel(); //Traverse some of the leaves in the second child of the root - auto child = LoadInnerNode(root->getChild(1)->blockId()); + auto child = LoadInnerNode(root->readChild(1).blockId()); for(unsigned int i = 5; i < nodeStore->layout().maxChildrenPerInnerNode(); ++i) { - EXPECT_TRAVERSE_LEAF(child->getChild(i)->blockId(), false, nodeStore->layout().maxChildrenPerInnerNode() + i); + EXPECT_TRAVERSE_LEAF(child->readChild(i).blockId(), false, nodeStore->layout().maxChildrenPerInnerNode() + i); } //Traverse all leaves in the third, fourth and fifth child of the root for(unsigned int i = 2; i < 5; ++i) { - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->getChild(i)->blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->readChild(i).blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); } //Traverse some of the leaves in the sixth child of the root - child = LoadInnerNode(root->getChild(5)->blockId()); + child = LoadInnerNode(root->readChild(5).blockId()); for(unsigned int i = 0; i < 2; ++i) { - EXPECT_TRAVERSE_LEAF(child->getChild(i)->blockId(), false, 5 * nodeStore->layout().maxChildrenPerInnerNode() + i); + EXPECT_TRAVERSE_LEAF(child->readChild(i).blockId(), false, 5 * nodeStore->layout().maxChildrenPerInnerNode() + i); } TraverseLeaves(root.get(), nodeStore->layout().maxChildrenPerInnerNode() + 5, 5 * nodeStore->layout().maxChildrenPerInnerNode() + 2); @@ -321,18 +321,18 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseMiddlePartOfThreelevelTree_AlsoLastN TEST_F(DataTreeTest_TraverseLeaves, TraverseLastPartOfThreelevelTree) { auto root = CreateThreeLevel(); //Traverse some of the leaves in the second child of the root - auto child = LoadInnerNode(root->getChild(1)->blockId()); + auto child = LoadInnerNode(root->readChild(1).blockId()); for(unsigned int i = 5; i < nodeStore->layout().maxChildrenPerInnerNode(); ++i) { - EXPECT_TRAVERSE_LEAF(child->getChild(i)->blockId(), false, nodeStore->layout().maxChildrenPerInnerNode() + i); + EXPECT_TRAVERSE_LEAF(child->readChild(i).blockId(), false, nodeStore->layout().maxChildrenPerInnerNode() + i); } //Traverse all leaves in the third, fourth and fifth child of the root for(unsigned int i = 2; i < 5; ++i) { - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->getChild(i)->blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->readChild(i).blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); } //Traverse all of the leaves in the sixth child of the root - child = LoadInnerNode(root->getChild(5)->blockId()); + child = LoadInnerNode(root->readChild(5).blockId()); for(unsigned int i = 0; i < child->numChildren(); ++i) { - EXPECT_TRAVERSE_LEAF(child->getChild(i)->blockId(), i == child->numChildren()-1, 5 * nodeStore->layout().maxChildrenPerInnerNode() + i); + EXPECT_TRAVERSE_LEAF(child->readChild(i).blockId(), i == child->numChildren()-1, 5 * nodeStore->layout().maxChildrenPerInnerNode() + i); } TraverseLeaves(root.get(), nodeStore->layout().maxChildrenPerInnerNode() + 5, 5 * nodeStore->layout().maxChildrenPerInnerNode() + child->numChildren()); @@ -342,12 +342,12 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseAllLeavesOfThreelevelTree) { auto root = CreateThreeLevel(); //Traverse all leaves in the third, fourth and fifth child of the root for(unsigned int i = 0; i < 5; ++i) { - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->getChild(i)->blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(root->readChild(i).blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); } //Traverse all of the leaves in the sixth child of the root - auto child = LoadInnerNode(root->getChild(5)->blockId()); + auto child = LoadInnerNode(root->readChild(5).blockId()); for(unsigned int i = 0; i < child->numChildren(); ++i) { - EXPECT_TRAVERSE_LEAF(child->getChild(i)->blockId(), i==child->numChildren()-1, 5 * nodeStore->layout().maxChildrenPerInnerNode() + i); + EXPECT_TRAVERSE_LEAF(child->readChild(i).blockId(), i==child->numChildren()-1, 5 * nodeStore->layout().maxChildrenPerInnerNode() + i); } TraverseLeaves(root.get(), 0, 5 * nodeStore->layout().maxChildrenPerInnerNode() + child->numChildren()); @@ -356,19 +356,19 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseAllLeavesOfThreelevelTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseAllLeavesOfFourLevelTree) { auto root = CreateFourLevel(); //Traverse all leaves of the full threelevel tree in the first child - auto firstChild = LoadInnerNode(root->getChild(0)->blockId()); + auto firstChild = LoadInnerNode(root->readChild(0).blockId()); for(unsigned int i = 0; i < firstChild->numChildren(); ++i) { - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(firstChild->getChild(i)->blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(firstChild->readChild(i).blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); } //Traverse all leaves of the full threelevel tree in the second child - auto secondChild = LoadInnerNode(root->getChild(1)->blockId()); + auto secondChild = LoadInnerNode(root->readChild(1).blockId()); for(unsigned int i = 0; i < secondChild->numChildren(); ++i) { - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(secondChild->getChild(i)->blockId()), false, (nodeStore->layout().maxChildrenPerInnerNode() + i) * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(secondChild->readChild(i).blockId()), false, (nodeStore->layout().maxChildrenPerInnerNode() + i) * nodeStore->layout().maxChildrenPerInnerNode()); } //Traverse all leaves of the non-full threelevel tree in the third child - auto thirdChild = LoadInnerNode(root->getChild(2)->blockId()); - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(thirdChild->getChild(0)->blockId()), false, 2 * nodeStore->layout().maxChildrenPerInnerNode() * nodeStore->layout().maxChildrenPerInnerNode()); - EXPECT_TRAVERSE_LEAF(LoadInnerNode(thirdChild->getChild(1)->blockId())->getChild(0)->blockId(), true, 2 * nodeStore->layout().maxChildrenPerInnerNode() * nodeStore->layout().maxChildrenPerInnerNode() + nodeStore->layout().maxChildrenPerInnerNode()); + auto thirdChild = LoadInnerNode(root->readChild(2).blockId()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(thirdChild->readChild(0).blockId()), false, 2 * nodeStore->layout().maxChildrenPerInnerNode() * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_LEAF(LoadInnerNode(thirdChild->readChild(1).blockId())->readChild(0).blockId(), true, 2 * nodeStore->layout().maxChildrenPerInnerNode() * nodeStore->layout().maxChildrenPerInnerNode() + nodeStore->layout().maxChildrenPerInnerNode()); TraverseLeaves(root.get(), 0, 2*nodeStore->layout().maxChildrenPerInnerNode()*nodeStore->layout().maxChildrenPerInnerNode() + nodeStore->layout().maxChildrenPerInnerNode() + 1); } @@ -376,24 +376,24 @@ TEST_F(DataTreeTest_TraverseLeaves, TraverseAllLeavesOfFourLevelTree) { TEST_F(DataTreeTest_TraverseLeaves, TraverseMiddlePartOfFourLevelTree) { auto root = CreateFourLevel(); //Traverse some leaves of the full threelevel tree in the first child - auto firstChild = LoadInnerNode(root->getChild(0)->blockId()); - auto secondChildOfFirstChild = LoadInnerNode(firstChild->getChild(1)->blockId()); + auto firstChild = LoadInnerNode(root->readChild(0).blockId()); + auto secondChildOfFirstChild = LoadInnerNode(firstChild->readChild(1).blockId()); for(unsigned int i = 5; i < secondChildOfFirstChild->numChildren(); ++i) { - EXPECT_TRAVERSE_LEAF(secondChildOfFirstChild->getChild(i)->blockId(), false, nodeStore->layout().maxChildrenPerInnerNode()+i); + EXPECT_TRAVERSE_LEAF(secondChildOfFirstChild->readChild(i).blockId(), false, nodeStore->layout().maxChildrenPerInnerNode()+i); } for(unsigned int i = 2; i < firstChild->numChildren(); ++i) { - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(firstChild->getChild(i)->blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(firstChild->readChild(i).blockId()), false, i * nodeStore->layout().maxChildrenPerInnerNode()); } //Traverse all leaves of the full threelevel tree in the second child - auto secondChild = LoadInnerNode(root->getChild(1)->blockId()); + auto secondChild = LoadInnerNode(root->readChild(1).blockId()); for(unsigned int i = 0; i < secondChild->numChildren(); ++i) { - EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(secondChild->getChild(i)->blockId()), false, (nodeStore->layout().maxChildrenPerInnerNode() + i) * nodeStore->layout().maxChildrenPerInnerNode()); + EXPECT_TRAVERSE_ALL_CHILDREN_OF(*LoadInnerNode(secondChild->readChild(i).blockId()), false, (nodeStore->layout().maxChildrenPerInnerNode() + i) * nodeStore->layout().maxChildrenPerInnerNode()); } //Traverse some leaves of the non-full threelevel tree in the third child - auto thirdChild = LoadInnerNode(root->getChild(2)->blockId()); - auto firstChildOfThirdChild = LoadInnerNode(thirdChild->getChild(0)->blockId()); + auto thirdChild = LoadInnerNode(root->readChild(2).blockId()); + auto firstChildOfThirdChild = LoadInnerNode(thirdChild->readChild(0).blockId()); for(unsigned int i = 0; i < firstChildOfThirdChild->numChildren()-1; ++i) { - EXPECT_TRAVERSE_LEAF(firstChildOfThirdChild->getChild(i)->blockId(), false, 2 * nodeStore->layout().maxChildrenPerInnerNode()*nodeStore->layout().maxChildrenPerInnerNode()+i); + EXPECT_TRAVERSE_LEAF(firstChildOfThirdChild->readChild(i).blockId(), false, 2 * nodeStore->layout().maxChildrenPerInnerNode()*nodeStore->layout().maxChildrenPerInnerNode()+i); } TraverseLeaves(root.get(), nodeStore->layout().maxChildrenPerInnerNode()+5, 2*nodeStore->layout().maxChildrenPerInnerNode()*nodeStore->layout().maxChildrenPerInnerNode() + nodeStore->layout().maxChildrenPerInnerNode() -1); diff --git a/test/blobstore/implementations/onblocks/datatreestore/impl/GetLowestRightBorderNodeWithMoreThanOneChildOrNullTest.cpp b/test/blobstore/implementations/onblocks/datatreestore/impl/GetLowestRightBorderNodeWithMoreThanOneChildOrNullTest.cpp index e3000cac..fe6d8747 100644 --- a/test/blobstore/implementations/onblocks/datatreestore/impl/GetLowestRightBorderNodeWithMoreThanOneChildOrNullTest.cpp +++ b/test/blobstore/implementations/onblocks/datatreestore/impl/GetLowestRightBorderNodeWithMoreThanOneChildOrNullTest.cpp @@ -66,7 +66,7 @@ public: TestData CreateFullThreeLevelTree() { auto root = CreateFullThreeLevel(); - return TestData{root->blockId(), root->LastChild()->blockId()}; + return TestData{root->blockId(), root->readLastChild().blockId()}; } }; diff --git a/test/blobstore/implementations/onblocks/datatreestore/testutils/DataTreeTest.cpp b/test/blobstore/implementations/onblocks/datatreestore/testutils/DataTreeTest.cpp index 82d69965..4f54a6df 100644 --- a/test/blobstore/implementations/onblocks/datatreestore/testutils/DataTreeTest.cpp +++ b/test/blobstore/implementations/onblocks/datatreestore/testutils/DataTreeTest.cpp @@ -137,9 +137,9 @@ unique_ref DataTreeTest::CreateTwoLeafWithSecondLeafSize(uint32_t unique_ref DataTreeTest::CreateFullTwoLevelWithLastLeafSize(uint32_t size) { auto root = CreateFullTwoLevel(); for (uint32_t i = 0; i < root->numChildren()-1; ++i) { - LoadLeafNode(root->getChild(i)->blockId())->resize(nodeStore->layout().maxBytesPerLeaf()); + LoadLeafNode(root->readChild(i).blockId())->resize(nodeStore->layout().maxBytesPerLeaf()); } - LoadLeafNode(root->LastChild()->blockId())->resize(size); + LoadLeafNode(root->readLastChild().blockId())->resize(size); return root; } @@ -176,12 +176,12 @@ unique_ref DataTreeTest::CreateThreeLevelWithThreeChildrenAndLast unique_ref DataTreeTest::CreateFullThreeLevelWithLastLeafSize(uint32_t size) { auto root = CreateFullThreeLevel(); for (uint32_t i = 0; i < root->numChildren(); ++i) { - auto node = LoadInnerNode(root->getChild(i)->blockId()); + auto node = LoadInnerNode(root->readChild(i).blockId()); for (uint32_t j = 0; j < node->numChildren(); ++j) { - LoadLeafNode(node->getChild(j)->blockId())->resize(nodeStore->layout().maxBytesPerLeaf()); + LoadLeafNode(node->readChild(j).blockId())->resize(nodeStore->layout().maxBytesPerLeaf()); } } - LoadLeafNode(LoadInnerNode(root->LastChild()->blockId())->LastChild()->blockId())->resize(size); + LoadLeafNode(LoadInnerNode(root->readLastChild().blockId())->readLastChild().blockId())->resize(size); return root; } @@ -205,14 +205,14 @@ void DataTreeTest::EXPECT_IS_INNER_NODE(const BlockId &blockId) { void DataTreeTest::EXPECT_IS_TWONODE_CHAIN(const BlockId &blockId) { auto node = LoadInnerNode(blockId); EXPECT_EQ(1u, node->numChildren()); - EXPECT_IS_LEAF_NODE(node->getChild(0)->blockId()); + EXPECT_IS_LEAF_NODE(node->readChild(0).blockId()); } void DataTreeTest::EXPECT_IS_FULL_TWOLEVEL_TREE(const BlockId &blockId) { auto node = LoadInnerNode(blockId); EXPECT_EQ(nodeStore->layout().maxChildrenPerInnerNode(), node->numChildren()); for (unsigned int i = 0; i < node->numChildren(); ++i) { - EXPECT_IS_LEAF_NODE(node->getChild(i)->blockId()); + EXPECT_IS_LEAF_NODE(node->readChild(i).blockId()); } } @@ -220,10 +220,10 @@ void DataTreeTest::EXPECT_IS_FULL_THREELEVEL_TREE(const BlockId &blockId) { auto root = LoadInnerNode(blockId); EXPECT_EQ(nodeStore->layout().maxChildrenPerInnerNode(), root->numChildren()); for (unsigned int i = 0; i < root->numChildren(); ++i) { - auto node = LoadInnerNode(root->getChild(i)->blockId()); + auto node = LoadInnerNode(root->readChild(i).blockId()); EXPECT_EQ(nodeStore->layout().maxChildrenPerInnerNode(), node->numChildren()); for (unsigned int j = 0; j < node->numChildren(); ++j) { - EXPECT_IS_LEAF_NODE(node->getChild(j)->blockId()); + EXPECT_IS_LEAF_NODE(node->readChild(j).blockId()); } } } @@ -235,7 +235,7 @@ void DataTreeTest::CHECK_DEPTH(int depth, const BlockId &blockId) { auto node = LoadInnerNode(blockId); EXPECT_EQ(depth, node->depth()); for (uint32_t i = 0; i < node->numChildren(); ++i) { - CHECK_DEPTH(depth-1, node->getChild(i)->blockId()); + CHECK_DEPTH(depth-1, node->readChild(i).blockId()); } } } diff --git a/test/blobstore/implementations/onblocks/datatreestore/testutils/LeafDataFixture.h b/test/blobstore/implementations/onblocks/datatreestore/testutils/LeafDataFixture.h index 4b00e533..a9fdabd9 100644 --- a/test/blobstore/implementations/onblocks/datatreestore/testutils/LeafDataFixture.h +++ b/test/blobstore/implementations/onblocks/datatreestore/testutils/LeafDataFixture.h @@ -23,7 +23,7 @@ public: EXPECT_EQ(_data.size(), leaf.numBytes()); EXPECT_EQ(0, std::memcmp(_data.data(), loadData(leaf).data(), _data.size())); } else { - EXPECT_LE(onlyCheckNumBytes, (int)leaf.numBytes()); + EXPECT_LE(onlyCheckNumBytes, static_cast(leaf.numBytes())); EXPECT_EQ(0, std::memcmp(_data.data(), loadData(leaf).data(), onlyCheckNumBytes)); } } diff --git a/test/blobstore/implementations/onblocks/datatreestore/testutils/TwoLevelDataFixture.h b/test/blobstore/implementations/onblocks/datatreestore/testutils/TwoLevelDataFixture.h index a27497e5..b5cbbd53 100644 --- a/test/blobstore/implementations/onblocks/datatreestore/testutils/TwoLevelDataFixture.h +++ b/test/blobstore/implementations/onblocks/datatreestore/testutils/TwoLevelDataFixture.h @@ -51,7 +51,7 @@ private: auto inner = dynamic_cast(node); int leafIndex = firstLeafIndex; for (uint32_t i = 0; i < inner->numChildren(); ++i) { - auto child = _dataNodeStore->load(inner->getChild(i)->blockId()).value(); + auto child = _dataNodeStore->load(inner->readChild(i).blockId()).value(); leafIndex = ForEachLeaf(child.get(), leafIndex, endLeafIndex, action); } return leafIndex; diff --git a/test/blobstore/implementations/onblocks/testutils/BlobStoreTest.h b/test/blobstore/implementations/onblocks/testutils/BlobStoreTest.h index d521602d..38bc4d33 100644 --- a/test/blobstore/implementations/onblocks/testutils/BlobStoreTest.h +++ b/test/blobstore/implementations/onblocks/testutils/BlobStoreTest.h @@ -16,7 +16,7 @@ public: cpputils::unique_ref loadBlob(const blockstore::BlockId &blockId) { auto loaded = blobStore->load(blockId); - EXPECT_TRUE((bool)loaded); + EXPECT_TRUE(static_cast(loaded)); return std::move(*loaded); } diff --git a/test/blobstore/implementations/onblocks/utils/CeilDivisionTest.cpp b/test/blobstore/implementations/onblocks/utils/CeilDivisionTest.cpp index af863111..959c3b8e 100644 --- a/test/blobstore/implementations/onblocks/utils/CeilDivisionTest.cpp +++ b/test/blobstore/implementations/onblocks/utils/CeilDivisionTest.cpp @@ -84,5 +84,5 @@ TEST_F(CeilDivisionTest, DivideLargeByItself) { TEST_F(CeilDivisionTest, 64bit) { uint64_t base = UINT64_C(1024)*1024*1024*1024; EXPECT_GT(base, std::numeric_limits::max()); - EXPECT_EQ(base/1024, ceilDivision(base, (uint64_t)1024)); + EXPECT_EQ(base/1024, ceilDivision(base, UINT64_C(1024))); } diff --git a/test/blobstore/implementations/onblocks/utils/CeilLogTest.cpp b/test/blobstore/implementations/onblocks/utils/CeilLogTest.cpp index 064f1b7c..c5bb67d4 100644 --- a/test/blobstore/implementations/onblocks/utils/CeilLogTest.cpp +++ b/test/blobstore/implementations/onblocks/utils/CeilLogTest.cpp @@ -28,7 +28,7 @@ TEST_F(CeilLogTest, Log3_4) { TEST_F(CeilLogTest, 64bit) { uint64_t value = UINT64_C(1024)*1024*1024*1024; EXPECT_GT(value, std::numeric_limits::max()); - EXPECT_EQ(4u, ceilLog((uint64_t)1024, value)); + EXPECT_EQ(4u, ceilLog(UINT64_C(1024), value)); } diff --git a/test/blobstore/implementations/onblocks/utils/IntPowTest.cpp b/test/blobstore/implementations/onblocks/utils/IntPowTest.cpp index f7533b69..6e925388 100644 --- a/test/blobstore/implementations/onblocks/utils/IntPowTest.cpp +++ b/test/blobstore/implementations/onblocks/utils/IntPowTest.cpp @@ -73,5 +73,5 @@ TEST_F(IntPowTest, ArbitraryNumbers3) { TEST_F(IntPowTest, 64bit) { uint64_t value = UINT64_C(1024)*1024*1024*1024; EXPECT_GT(value, std::numeric_limits::max()); - EXPECT_EQ(value*value*value, intPow(value, (uint64_t)3)); + EXPECT_EQ(value*value*value, intPow(value, UINT64_C(3))); } \ No newline at end of file diff --git a/test/blockstore/implementations/caching/cache/CacheTest_PushAndPop.cpp b/test/blockstore/implementations/caching/cache/CacheTest_PushAndPop.cpp index 9337cf7a..86c48796 100644 --- a/test/blockstore/implementations/caching/cache/CacheTest_PushAndPop.cpp +++ b/test/blockstore/implementations/caching/cache/CacheTest_PushAndPop.cpp @@ -47,7 +47,7 @@ TEST_F(CacheTest_PushAndPop, FullCache) { push(i, 2*i); } for(unsigned int i = 0; i < MAX_ENTRIES; ++i) { - EXPECT_EQ((signed int)(2*i), pop(i).value()); + EXPECT_EQ(static_cast(2*i), pop(i).value()); } } @@ -59,7 +59,7 @@ TEST_F(CacheTest_PushAndPop, FullCache_PushNonOrdered_PopOrdered) { push(i, 2*i); } for(unsigned int i = 0; i < MAX_ENTRIES; ++i) { - EXPECT_EQ((signed int)(2*i), pop(i).value()); + EXPECT_EQ(static_cast(2*i), pop(i).value()); } } @@ -68,10 +68,10 @@ TEST_F(CacheTest_PushAndPop, FullCache_PushOrdered_PopNonOrdered) { push(i, 2*i); } for(unsigned int i = 1; i < MAX_ENTRIES; i += 2) { - EXPECT_EQ((signed int)(2*i), pop(i).value()); + EXPECT_EQ(static_cast(2*i), pop(i).value()); } for(unsigned int i = 0; i < MAX_ENTRIES; i += 2) { - EXPECT_EQ((signed int)(2*i), pop(i).value()); + EXPECT_EQ(static_cast(2*i), pop(i).value()); } } @@ -99,10 +99,10 @@ TEST_F(CacheTest_PushAndPop, FullCache_PushNonOrdered_PopNonOrdered) { push(i, 2*i); } for(int i = roundDownToOdd(MAX_ENTRIES-1); i >= 0; i -= 2) { - EXPECT_EQ((signed int)(2*i), pop(i).value()); + EXPECT_EQ(static_cast(2*i), pop(i).value()); } for(unsigned int i = 0; i < MAX_ENTRIES; i += 2) { - EXPECT_EQ((signed int)(2*i), pop(i).value()); + EXPECT_EQ(static_cast(2*i), pop(i).value()); } } @@ -115,7 +115,7 @@ TEST_F(CacheTest_PushAndPop, MoreThanFullCache) { EXPECT_EQ(boost::none, pop(1)); //Check the other elements are still there for(unsigned int i = 2; i < MAX_ENTRIES + 2; ++i) { - EXPECT_EQ((signed int)(2*i), pop(i).value()); + EXPECT_EQ(static_cast(2*i), pop(i).value()); } } diff --git a/test/blockstore/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp b/test/blockstore/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp index 5c410328..b38efb57 100644 --- a/test/blockstore/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp +++ b/test/blockstore/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp @@ -47,7 +47,7 @@ public: void ModifyBaseBlock(const blockstore::BlockId &blockId) { auto block = baseBlockStore->load(blockId).value(); - byte* middle_byte = ((CryptoPP::byte*)block.data()) + 10; + byte* middle_byte = static_cast(block.data()) + 10; *middle_byte = *middle_byte + 1; baseBlockStore->store(blockId, block); } diff --git a/test/blockstore/implementations/integrity/IntegrityBlockStoreTest_Specific.cpp b/test/blockstore/implementations/integrity/IntegrityBlockStoreTest_Specific.cpp index b7af3e4a..677cc4fa 100644 --- a/test/blockstore/implementations/integrity/IntegrityBlockStoreTest_Specific.cpp +++ b/test/blockstore/implementations/integrity/IntegrityBlockStoreTest_Specific.cpp @@ -13,6 +13,8 @@ using cpputils::Data; using cpputils::unique_ref; using cpputils::make_unique_ref; using cpputils::TempFile; +using cpputils::serialize; +using cpputils::deserialize; using boost::none; using std::make_unique; using std::unique_ptr; @@ -66,7 +68,7 @@ public: void modifyBlock(const blockstore::BlockId &blockId) { auto block = blockStore->load(blockId).value(); - byte* first_byte = (byte*)block.data(); + byte* first_byte = static_cast(block.data()); *first_byte = *first_byte + 1; blockStore->store(blockId, block); } @@ -77,23 +79,26 @@ public: void decreaseVersionNumber(const blockstore::BlockId &blockId) { auto baseBlock = baseBlockStore->load(blockId).value(); - uint64_t* version = (uint64_t*)((uint8_t*)baseBlock.data()+IntegrityBlockStore2::VERSION_HEADER_OFFSET); - ASSERT(*version > 1, "Can't decrease the lowest allowed version number"); - *version -= 1; + void* versionPtr = static_cast(baseBlock.data()) + IntegrityBlockStore2::VERSION_HEADER_OFFSET; + uint64_t version = deserialize(versionPtr); + ASSERT(version > 1, "Can't decrease the lowest allowed version number"); + serialize(versionPtr, version-1); baseBlockStore->store(blockId, baseBlock); } void increaseVersionNumber(const blockstore::BlockId &blockId) { auto baseBlock = baseBlockStore->load(blockId).value(); - uint64_t* version = (uint64_t*)((uint8_t*)baseBlock.data()+IntegrityBlockStore2::VERSION_HEADER_OFFSET); - *version += 1; + void* versionPtr = static_cast(baseBlock.data()) + IntegrityBlockStore2::VERSION_HEADER_OFFSET; + uint64_t version = deserialize(versionPtr); + serialize(versionPtr, version+1); baseBlockStore->store(blockId, baseBlock); } void changeClientId(const blockstore::BlockId &blockId) { auto baseBlock = baseBlockStore->load(blockId).value(); - uint32_t* clientId = (uint32_t*)((uint8_t*)baseBlock.data()+IntegrityBlockStore2::CLIENTID_HEADER_OFFSET); - *clientId += 1; + void* clientIdPtr = static_cast(baseBlock.data()) + IntegrityBlockStore2::CLIENTID_HEADER_OFFSET; + uint64_t clientId = deserialize(clientIdPtr); + serialize(clientIdPtr, clientId+1); baseBlockStore->store(blockId, baseBlock); } diff --git a/test/blockstore/testutils/BlockStoreTest_Data.h b/test/blockstore/testutils/BlockStoreTest_Data.h index 6be3c920..6a0eac74 100644 --- a/test/blockstore/testutils/BlockStoreTest_Data.h +++ b/test/blockstore/testutils/BlockStoreTest_Data.h @@ -118,7 +118,7 @@ private: void EXPECT_DATA_READS_AS(const cpputils::Data &expected, const blockstore::Block &block, off_t offset, size_t count) { cpputils::Data read(count); - std::memcpy(read.data(), (uint8_t*)block.data() + offset, count); + std::memcpy(read.data(), static_cast(block.data()) + offset, count); EXPECT_EQ(expected, read); } @@ -127,7 +127,7 @@ private: cpputils::Data end(testData.blocksize - count - start); std::memcpy(begin.data(), expected.data(), start); - std::memcpy(end.data(), (uint8_t*)expected.data()+start+count, end.size()); + std::memcpy(end.data(), expected.dataOffset(start+count), end.size()); EXPECT_DATA_READS_AS(begin, block, 0, start); EXPECT_DATA_READS_AS(end, block, start + count, end.size()); diff --git a/test/cpp-utils/CMakeLists.txt b/test/cpp-utils/CMakeLists.txt index 0acc127a..392a8f56 100644 --- a/test/cpp-utils/CMakeLists.txt +++ b/test/cpp-utils/CMakeLists.txt @@ -34,6 +34,7 @@ set(SOURCES data/DataFixtureTest.cpp data/DataTest.cpp data/FixedSizeDataIncludeTest.cpp + data/SerializationHelperTest.cpp data/DataIncludeTest.cpp logging/LoggingLevelTest.cpp logging/LoggerTest.cpp diff --git a/test/cpp-utils/crypto/symmetric/CipherTest.cpp b/test/cpp-utils/crypto/symmetric/CipherTest.cpp index dc606916..10c8bfbe 100644 --- a/test/cpp-utils/crypto/symmetric/CipherTest.cpp +++ b/test/cpp-utils/crypto/symmetric/CipherTest.cpp @@ -40,16 +40,16 @@ public: } void ExpectDoesntDecrypt(const Data &ciphertext) { - auto decrypted = Cipher::decrypt((CryptoPP::byte*)ciphertext.data(), ciphertext.size(), this->encKey); + auto decrypted = Cipher::decrypt(static_cast(ciphertext.data()), ciphertext.size(), this->encKey); EXPECT_FALSE(decrypted); } Data Encrypt(const Data &plaintext) { - return Cipher::encrypt((CryptoPP::byte*)plaintext.data(), plaintext.size(), this->encKey); + return Cipher::encrypt(static_cast(plaintext.data()), plaintext.size(), this->encKey); } Data Decrypt(const Data &ciphertext) { - return Cipher::decrypt((CryptoPP::byte*)ciphertext.data(), ciphertext.size(), this->encKey).value(); + return Cipher::decrypt(static_cast(ciphertext.data()), ciphertext.size(), this->encKey).value(); } static Data CreateZeroes(unsigned int size) { @@ -149,49 +149,57 @@ TYPED_TEST_CASE_P(AuthenticatedCipherTest); TYPED_TEST_P(AuthenticatedCipherTest, ModifyFirstByte_Zeroes_Size1) { Data ciphertext = this->Encrypt(this->zeroes1); - *(CryptoPP::byte*)ciphertext.data() = *(CryptoPP::byte*)ciphertext.data() + 1; + void* firstByte = ciphertext.data(); + serialize(firstByte, deserialize(firstByte) + 1); this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyFirstByte_Data_Size1) { Data ciphertext = this->Encrypt(this->plaintext1); - *(CryptoPP::byte*)ciphertext.data() = *(CryptoPP::byte*)ciphertext.data() + 1; + void* firstByte = ciphertext.data(); + serialize(firstByte, deserialize(firstByte) + 1); this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyFirstByte_Zeroes) { Data ciphertext = this->Encrypt(this->zeroes2); - *(CryptoPP::byte*)ciphertext.data() = *(CryptoPP::byte*)ciphertext.data() + 1; + void* firstByte = ciphertext.data(); + serialize(firstByte, deserialize(firstByte) + 1); this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyFirstByte_Data) { Data ciphertext = this->Encrypt(this->plaintext2); - *(CryptoPP::byte*)ciphertext.data() = *(CryptoPP::byte*)ciphertext.data() + 1; + void* firstByte = ciphertext.data(); + serialize(firstByte, deserialize(firstByte) + 1); this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyLastByte_Zeroes) { Data ciphertext = this->Encrypt(this->zeroes2); - ((CryptoPP::byte*)ciphertext.data())[ciphertext.size() - 1] = ((CryptoPP::byte*)ciphertext.data())[ciphertext.size() - 1] + 1; + void* lastByte = ciphertext.dataOffset(ciphertext.size() - 1); + serialize(lastByte, deserialize(lastByte) + 1); this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyLastByte_Data) { Data ciphertext = this->Encrypt(this->plaintext2); - ((CryptoPP::byte*)ciphertext.data())[ciphertext.size() - 1] = ((CryptoPP::byte*)ciphertext.data())[ciphertext.size() - 1] + 1; + void* lastByte = ciphertext.dataOffset(ciphertext.size() - 1); + serialize(lastByte, deserialize(lastByte) + 1); this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyMiddleByte_Zeroes) { Data ciphertext = this->Encrypt(this->zeroes2); - ((CryptoPP::byte*)ciphertext.data())[ciphertext.size()/2] = ((CryptoPP::byte*)ciphertext.data())[ciphertext.size()/2] + 1; + void* middleByte = ciphertext.dataOffset(ciphertext.size()/2); + serialize(middleByte, deserialize(middleByte) + 1); this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyMiddleByte_Data) { Data ciphertext = this->Encrypt(this->plaintext2); - ((CryptoPP::byte*)ciphertext.data())[ciphertext.size()/2] = ((CryptoPP::byte*)ciphertext.data())[ciphertext.size()/2] + 1; + void* middleByte = ciphertext.dataOffset(ciphertext.size()/2); + serialize(middleByte, deserialize(middleByte) + 1); this->ExpectDoesntDecrypt(ciphertext); } diff --git a/test/cpp-utils/data/DataTest.cpp b/test/cpp-utils/data/DataTest.cpp index 7c0e7141..8076fca8 100644 --- a/test/cpp-utils/data/DataTest.cpp +++ b/test/cpp-utils/data/DataTest.cpp @@ -1,7 +1,7 @@ #include "cpp-utils/data/DataFixture.h" #include "cpp-utils/data/Data.h" +#include "cpp-utils/data/SerializationHelper.h" #include - #include "cpp-utils/tempfile/TempFile.h" #include @@ -24,7 +24,7 @@ class DataTest: public Test { public: bool DataIsZeroes(const Data &data) { for (size_t i = 0; i != data.size(); ++ i) { - if (((char*)data.data())[i] != 0) { + if (deserialize(data.dataOffset(i)) != 0) { return false; } } @@ -40,7 +40,7 @@ public: static void StoreData(const Data &data, const bf::path &filepath) { ofstream file(filepath.c_str(), std::ios::binary | std::ios::trunc); - file.write((char*)data.data(), data.size()); + file.write(static_cast(data.data()), data.size()); } static void EXPECT_STORED_FILE_DATA_CORRECT(const Data &data, const bf::path &filepath) { @@ -118,7 +118,7 @@ TEST_P(DataTestWithSizeParam, Copy) { TEST_F(DataTest, ChangingCopyDoesntChangeOriginal) { Data original = DataFixture::generate(1024); Data copy = original.copy(); - ((uint8_t*)copy.data())[0] = ((uint8_t*)copy.data())[0] + 1; + serialize(copy.data(), deserialize(copy.data()) + 1); EXPECT_EQ(DataFixture::generate(1024), original); EXPECT_NE(copy, original); } @@ -171,7 +171,7 @@ TEST_F(DataTest, Inequality_DifferentSize) { TEST_F(DataTest, Inequality_DifferentFirstByte) { Data data1 = DataFixture::generate(1024); Data data2 = DataFixture::generate(1024); - ((uint8_t*)data2.data())[0] = ((uint8_t*)data2.data())[0] + 1; + serialize(data2.data(), deserialize(data2.data()) + 1); EXPECT_FALSE(data1 == data2); EXPECT_TRUE(data1 != data2); } @@ -179,7 +179,7 @@ TEST_F(DataTest, Inequality_DifferentFirstByte) { TEST_F(DataTest, Inequality_DifferentMiddleByte) { Data data1 = DataFixture::generate(1024); Data data2 = DataFixture::generate(1024); - ((uint8_t*)data2.data())[500] = ((uint8_t*)data2.data())[500] + 1; + serialize(data2.dataOffset(500), deserialize(data2.dataOffset(500)) + 1); EXPECT_FALSE(data1 == data2); EXPECT_TRUE(data1 != data2); } @@ -187,7 +187,7 @@ TEST_F(DataTest, Inequality_DifferentMiddleByte) { TEST_F(DataTest, Inequality_DifferentLastByte) { Data data1 = DataFixture::generate(1024); Data data2 = DataFixture::generate(1024); - ((uint8_t*)data2.data())[1023] = ((uint8_t*)data2.data())[1023] + 1; + serialize(data2.dataOffset(1023), deserialize(data2.dataOffset(1023)) + 1); EXPECT_FALSE(data1 == data2); EXPECT_TRUE(data1 != data2); } diff --git a/test/cpp-utils/data/FixedSizeDataTest.cpp b/test/cpp-utils/data/FixedSizeDataTest.cpp index 024cb4de..4c7ea27d 100644 --- a/test/cpp-utils/data/FixedSizeDataTest.cpp +++ b/test/cpp-utils/data/FixedSizeDataTest.cpp @@ -89,19 +89,19 @@ const Data FixedSizeDataTestWithBinaryParam::VALUE2(DataFixture::generate(SIZE, INSTANTIATE_TEST_CASE_P(FixedSizeDataTestWithBinaryParam, FixedSizeDataTestWithBinaryParam, Values(&FixedSizeDataTestWithBinaryParam::VALUE1, &FixedSizeDataTestWithBinaryParam::VALUE2)); TEST_P(FixedSizeDataTestWithBinaryParam, FromBinary) { - FixedSizeData data = FixedSizeData::FromBinary((uint8_t*)GetParam()->data()); + FixedSizeData data = FixedSizeData::FromBinary(GetParam()->data()); EXPECT_DATA_EQ(*GetParam(), data); } TEST_P(FixedSizeDataTestWithBinaryParam, FromAndToBinary) { - FixedSizeData data = FixedSizeData::FromBinary((uint8_t*)GetParam()->data()); + FixedSizeData data = FixedSizeData::FromBinary(GetParam()->data()); Data output(FixedSizeData::BINARY_LENGTH); data.ToBinary(output.data()); EXPECT_EQ(*GetParam(), output); } TEST_P(FixedSizeDataTestWithBinaryParam, ToAndFromBinary) { - FixedSizeData data = FixedSizeData::FromBinary((uint8_t*)GetParam()->data()); + FixedSizeData data = FixedSizeData::FromBinary(GetParam()->data()); Data stored(FixedSizeData::BINARY_LENGTH); data.ToBinary(stored.data()); FixedSizeData loaded = FixedSizeData::FromBinary(stored.data()); diff --git a/test/cpp-utils/data/SerializationHelperTest.cpp b/test/cpp-utils/data/SerializationHelperTest.cpp new file mode 100644 index 00000000..e252ee5f --- /dev/null +++ b/test/cpp-utils/data/SerializationHelperTest.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + +using cpputils::serialize; +using cpputils::deserialize; +using cpputils::deserializeWithOffset; +using cpputils::Data; + +TEST(SerializationHelperTest, uint8) { + Data data(1); + serialize(data.data(), 5u); + EXPECT_EQ(5u, deserialize(data.data())); +} + +TEST(SerializationHelperTest, int8_positive) { + Data data(1); + serialize(data.data(), 5); + EXPECT_EQ(5, deserialize(data.data())); +} + +TEST(SerializationHelperTest, int8_negative) { + Data data(1); + serialize(data.data(), -5); + EXPECT_EQ(-5, deserialize(data.data())); +} + +TEST(SerializationHelperTest, uint16_aligned) { + Data data(2); + serialize(data.data(), 1000u); + EXPECT_EQ(1000u, deserialize(data.data())); +} + +TEST(SerializationHelperTest, uint16_unaligned) { + Data data(3); + serialize(data.dataOffset(1), 1000u); + EXPECT_EQ(1000u, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, int16_postive_aligned) { + Data data(2); + serialize(data.data(), 1000); + EXPECT_EQ(1000, deserialize(data.data())); +} + +TEST(SerializationHelperTest, int16_positive_unaligned) { + Data data(3); + serialize(data.dataOffset(1), 1000); + EXPECT_EQ(1000, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, int16_negative_aligned) { + Data data(2); + serialize(data.data(), -1000); + EXPECT_EQ(-1000, deserialize(data.data())); +} + +TEST(SerializationHelperTest, int16_negative_unaligned) { + Data data(3); + serialize(data.dataOffset(1), -1000); + EXPECT_EQ(-1000, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, uint32_aligned) { + Data data(4); + serialize(data.data(), 100000u); + EXPECT_EQ(100000u, deserialize(data.data())); +} + +TEST(SerializationHelperTest, uint32_unaligned) { + Data data(5); + serialize(data.dataOffset(1), 100000u); + EXPECT_EQ(100000u, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, int32_positive_aligned) { + Data data(4); + serialize(data.data(), 100000); + EXPECT_EQ(100000, deserialize(data.data())); +} + +TEST(SerializationHelperTest, int32_positive_unaligned) { + Data data(5); + serialize(data.dataOffset(1), 100000); + EXPECT_EQ(100000, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, int32_negative_aligned) { + Data data(4); + serialize(data.data(), -100000); + EXPECT_EQ(-100000, deserialize(data.data())); +} + +TEST(SerializationHelperTest, int32_negative_unaligned) { + Data data(5); + serialize(data.dataOffset(1), -100000); + EXPECT_EQ(-100000, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, uint64_aligned) { + Data data(8); + serialize(data.data(), 10000000000u); + EXPECT_EQ(10000000000u, deserialize(data.data())); +} + +TEST(SerializationHelperTest, uint64_unaligned) { + Data data(9); + serialize(data.dataOffset(1), 10000000000u); + EXPECT_EQ(10000000000u, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, int64_positive_aligned) { + Data data(8); + serialize(data.data(), 10000000000); + EXPECT_EQ(10000000000, deserialize(data.data())); +} + +TEST(SerializationHelperTest, int64_positive_unaligned) { + Data data(9); + serialize(data.dataOffset(1), 10000000000); + EXPECT_EQ(10000000000, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, int64_negative_aligned) { + Data data(8); + serialize(data.data(), -10000000000); + EXPECT_EQ(-10000000000, deserialize(data.data())); +} + +TEST(SerializationHelperTest, int64_negative_unaligned) { + Data data(9); + serialize(data.dataOffset(1), -10000000000); + EXPECT_EQ(-10000000000, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, float_aligned) { + Data data(sizeof(float)); + serialize(data.data(), 3.1415f); + EXPECT_EQ(3.1415f, deserialize(data.data())); +} + +TEST(SerializationHelperTest, float_unaligned) { + Data data(sizeof(float) + 1); + serialize(data.dataOffset(1), 3.1415f); + EXPECT_EQ(3.1415f, deserialize(data.dataOffset(1))); +} + +TEST(SerializationHelperTest, double_aligned) { + Data data(sizeof(double)); + serialize(data.data(), 3.1415); + EXPECT_EQ(3.1415, deserialize(data.data())); +} + +TEST(SerializationHelperTest, double_unaligned) { + Data data(sizeof(double) + 1); + serialize(data.dataOffset(1), 3.1415); + EXPECT_EQ(3.1415, deserialize(data.dataOffset(1))); +} + +namespace { +struct DataStructure final { + uint64_t v1; + uint32_t v2; + uint16_t v3; + uint8_t v4; +}; + +bool operator==(const DataStructure &lhs, const DataStructure &rhs) { + return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 && lhs.v4 == rhs.v4; +} +} + +TEST(SerializationHelperTest, struct_aligned) { + Data data(sizeof(DataStructure)); + const DataStructure fixture {10000000000u, 100000u, 1000u, 5u}; + serialize(data.data(), fixture); + EXPECT_EQ(fixture, deserialize(data.data())); +} + +TEST(SerializationHelperTest, struct_unaligned) { + Data data(sizeof(DataStructure) + 1); + const DataStructure fixture {10000000000u, 100000u, 1000u, 5u}; + serialize(data.dataOffset(1), fixture); + EXPECT_EQ(fixture, deserialize(data.dataOffset(1))); +} + +namespace { +struct OneByteStruct final { + uint8_t v; +}; +static_assert(sizeof(OneByteStruct) == 1, ""); +} + +TEST(SerializationHelperTest, onebytestruct) { + Data data(1); + OneByteStruct fixture {5}; + serialize(data.data(), fixture); + EXPECT_EQ(fixture.v, deserialize(data.data()).v); +} + +TEST(SerializationHelperTest, deserializeWithOffset) { + Data data(5); + serialize(data.dataOffset(1), 1000); + EXPECT_EQ(1000, deserializeWithOffset(data.data(), 1)); +} diff --git a/test/cpp-utils/pointer/optional_ownership_ptr_test.cpp b/test/cpp-utils/pointer/optional_ownership_ptr_test.cpp index a44691ee..69fcba55 100644 --- a/test/cpp-utils/pointer/optional_ownership_ptr_test.cpp +++ b/test/cpp-utils/pointer/optional_ownership_ptr_test.cpp @@ -115,7 +115,7 @@ TEST_F(OptionalOwnershipPointerTest, DoesntCrashWhenDestructingNullptr1) { } TEST_F(OptionalOwnershipPointerTest, DoesntCrashWhenDestructingNullptrWithoutOwnership) { - optional_ownership_ptr ptr = WithoutOwnership((TestObject*)nullptr); + optional_ownership_ptr ptr = WithoutOwnership(static_cast(nullptr)); UNUSED(ptr); } diff --git a/test/cpp-utils/pointer/unique_ref_test.cpp b/test/cpp-utils/pointer/unique_ref_test.cpp index f8d8e941..27459bc8 100644 --- a/test/cpp-utils/pointer/unique_ref_test.cpp +++ b/test/cpp-utils/pointer/unique_ref_test.cpp @@ -91,36 +91,36 @@ TEST(MakeUniqueRefTest, CanAssignToBaseClassSharedPtr) { TEST(NullcheckTest, givenUniquePtrToInt_withNullptr_whenNullcheckCalled_thenReturnsNone) { boost::optional> var = nullcheck(std::unique_ptr(nullptr)); - EXPECT_FALSE((bool)var); + EXPECT_FALSE(static_cast(var)); } TEST(NullcheckTest, givenUniquePtrToObject_withNullptr_whenNullcheckCalled_thenReturnsNone) { boost::optional> var = nullcheck(std::unique_ptr(nullptr)); - EXPECT_FALSE((bool)var); + EXPECT_FALSE(static_cast(var)); } TEST(NullcheckTest, givenUniquePtrToInt_withNonNullptr_whenNullcheckCalled_thenReturnsUniqueRef) { boost::optional> var = nullcheck(std::make_unique(3)); - EXPECT_TRUE((bool)var); + EXPECT_TRUE(static_cast(var)); EXPECT_EQ(3, **var); } TEST(NullcheckTest, givenUniquePtrToObject_withNonNullptr_whenNullcheckCalled_thenReturnsUniqueRef) { boost::optional> var = nullcheck(std::make_unique()); - EXPECT_TRUE((bool)var); + EXPECT_TRUE(static_cast(var)); //Check that the type is correct EXPECT_EQ(var->get(), dynamic_cast(var->get())); } TEST(NullcheckTest, givenUniquePtrToObjectWith1Parameter_withNonNullptr_whenNullcheckCalled_thenReturnsUniqueRef) { boost::optional> var = nullcheck(std::make_unique(5)); - EXPECT_TRUE((bool)var); + EXPECT_TRUE(static_cast(var)); EXPECT_EQ(5, (*var)->param); } TEST(NullcheckTest, givenUniquePtrToObjectWith2Parameters_withNonNullptr_whenNullcheckCalled_thenReturnsUniqueRef) { boost::optional> var = nullcheck(std::make_unique(7,2)); - EXPECT_TRUE((bool)var); + EXPECT_TRUE(static_cast(var)); EXPECT_EQ(7, (*var)->param1); EXPECT_EQ(2, (*var)->param2); } diff --git a/test/cryfs/config/CompatibilityTest.cpp b/test/cryfs/config/CompatibilityTest.cpp index eb24b3f6..4768bd45 100644 --- a/test/cryfs/config/CompatibilityTest.cpp +++ b/test/cryfs/config/CompatibilityTest.cpp @@ -37,7 +37,7 @@ private: Data result(hex.size()/2); CryptoPP::StringSource(hex, true, new CryptoPP::HexDecoder( - new CryptoPP::ArraySink((CryptoPP::byte*)result.data(), result.size()) + new CryptoPP::ArraySink(static_cast(result.data()), result.size()) ) ); return result; diff --git a/test/cryfs/config/crypto/CryConfigEncryptorTest.cpp b/test/cryfs/config/crypto/CryConfigEncryptorTest.cpp index df046928..36242100 100644 --- a/test/cryfs/config/crypto/CryConfigEncryptorTest.cpp +++ b/test/cryfs/config/crypto/CryConfigEncryptorTest.cpp @@ -13,6 +13,8 @@ using cpputils::AES128_CFB; using cpputils::AES256_GCM; using cpputils::Twofish256_GCM; using cpputils::Twofish128_CFB; +using cpputils::serialize; +using cpputils::deserialize; using boost::none; using namespace cryfs; @@ -102,7 +104,7 @@ TEST_F(CryConfigEncryptorTest, EncryptAndDecrypt_EmptyData) { TEST_F(CryConfigEncryptorTest, InvalidCiphertext) { auto encryptor = makeEncryptor(); Data encrypted = encryptor->encrypt(DataFixture::generate(400), AES256_GCM::NAME); - *(char*)encrypted.data() = *(char*)encrypted.data()+1; //Modify ciphertext + serialize(encrypted.data(), deserialize(encrypted.data()) + 1); //Modify ciphertext auto decrypted = encryptor->decrypt(encrypted); EXPECT_EQ(none, decrypted); } diff --git a/test/cryfs/config/crypto/inner/ConcreteInnerEncryptorTest.cpp b/test/cryfs/config/crypto/inner/ConcreteInnerEncryptorTest.cpp index c463a027..35a52870 100644 --- a/test/cryfs/config/crypto/inner/ConcreteInnerEncryptorTest.cpp +++ b/test/cryfs/config/crypto/inner/ConcreteInnerEncryptorTest.cpp @@ -12,6 +12,8 @@ using cpputils::make_unique_ref; using cpputils::AES256_GCM; using cpputils::AES256_CFB; using cpputils::Twofish128_CFB; +using cpputils::serialize; +using cpputils::deserialize; using namespace cryfs; // This is needed for google test @@ -63,7 +65,7 @@ TEST_F(ConcreteInnerEncryptorTest, DoesntDecryptWithWrongCipherName) { TEST_F(ConcreteInnerEncryptorTest, InvalidCiphertext) { auto encryptor = makeInnerEncryptor(); InnerConfig encrypted = encryptor->encrypt(DataFixture::generate(200)); - *(char*)encrypted.encryptedConfig.data() = *(char*)encrypted.encryptedConfig.data()+1; //Modify ciphertext + serialize(encrypted.encryptedConfig.data(), deserialize(encrypted.encryptedConfig.data()) + 1); //Modify ciphertext auto decrypted = encryptor->decrypt(encrypted); EXPECT_EQ(none, decrypted); } diff --git a/test/cryfs/config/crypto/outer/OuterEncryptorTest.cpp b/test/cryfs/config/crypto/outer/OuterEncryptorTest.cpp index 7fa5fe65..9b50b7f1 100644 --- a/test/cryfs/config/crypto/outer/OuterEncryptorTest.cpp +++ b/test/cryfs/config/crypto/outer/OuterEncryptorTest.cpp @@ -9,6 +9,8 @@ using cpputils::Data; using cpputils::DataFixture; using cpputils::unique_ref; using cpputils::make_unique_ref; +using cpputils::serialize; +using cpputils::deserialize; using namespace cryfs; // This is needed for google test @@ -48,7 +50,7 @@ TEST_F(OuterEncryptorTest, EncryptAndDecrypt_EmptyData) { TEST_F(OuterEncryptorTest, InvalidCiphertext) { auto encryptor = makeOuterEncryptor(); OuterConfig encrypted = encryptor->encrypt(DataFixture::generate(200)); - *(char*)encrypted.encryptedInnerConfig.data() = *(char*)encrypted.encryptedInnerConfig.data()+1; //Modify ciphertext + serialize(encrypted.encryptedInnerConfig.data(), deserialize(encrypted.encryptedInnerConfig.data()) + 1); //Modify ciphertext auto decrypted = encryptor->decrypt(encrypted); EXPECT_EQ(none, decrypted); } diff --git a/test/fspp/fuse/read/testutils/FuseReadTest.h b/test/fspp/fuse/read/testutils/FuseReadTest.h index 0afc2a9b..ab8bc3dc 100644 --- a/test/fspp/fuse/read/testutils/FuseReadTest.h +++ b/test/fspp/fuse/read/testutils/FuseReadTest.h @@ -24,7 +24,7 @@ public: ::testing::Action ReturnSuccessfulReadRegardingSize(size_t filesize) { return ::testing::Invoke([filesize](int, void *, size_t count, off_t offset) { - size_t ableToReadCount = std::min(count, (size_t)(filesize - offset)); + size_t ableToReadCount = std::min(count, static_cast(filesize - offset)); return ableToReadCount; }); } diff --git a/test/fspp/testutils/FuseTest.h b/test/fspp/testutils/FuseTest.h index 1cf3d345..a6a4edee 100644 --- a/test/fspp/testutils/FuseTest.h +++ b/test/fspp/testutils/FuseTest.h @@ -19,25 +19,25 @@ RETURNTYPE NAME(const boost::filesystem::path &path) override { \ return NAME(path.c_str()); \ } \ - MOCK_METHOD1(NAME, RETURNTYPE(const char*)); \ + MOCK_METHOD1(NAME, RETURNTYPE(const char*)) \ #define MOCK_PATH_METHOD2(NAME, RETURNTYPE, PARAM1) \ RETURNTYPE NAME(const boost::filesystem::path &path, PARAM1 param1) override { \ return NAME(path.c_str(), param1); \ } \ - MOCK_METHOD2(NAME, RETURNTYPE(const char*, PARAM1)); \ + MOCK_METHOD2(NAME, RETURNTYPE(const char*, PARAM1)) \ #define MOCK_PATH_METHOD3(NAME, RETURNTYPE, PARAM1, PARAM2) \ RETURNTYPE NAME(const boost::filesystem::path &path, PARAM1 p1, PARAM2 p2) override { \ return NAME(path.c_str(), p1, p2); \ } \ - MOCK_METHOD3(NAME, RETURNTYPE(const char*, PARAM1, PARAM2)); \ + MOCK_METHOD3(NAME, RETURNTYPE(const char*, PARAM1, PARAM2)) \ #define MOCK_PATH_METHOD4(NAME, RETURNTYPE, PARAM1, PARAM2, PARAM3) \ RETURNTYPE NAME(const boost::filesystem::path &path, PARAM1 p1, PARAM2 p2, PARAM3 p3) override { \ return NAME(path.c_str(), p1, p2, p3); \ } \ - MOCK_METHOD4(NAME, RETURNTYPE(const char*, PARAM1, PARAM2, PARAM3)); \ + MOCK_METHOD4(NAME, RETURNTYPE(const char*, PARAM1, PARAM2, PARAM3)) \ class MockFilesystem: public fspp::fuse::Filesystem { public: diff --git a/test/fspp/testutils/InMemoryFile.cpp b/test/fspp/testutils/InMemoryFile.cpp index 78df5f7d..684e4974 100644 --- a/test/fspp/testutils/InMemoryFile.cpp +++ b/test/fspp/testutils/InMemoryFile.cpp @@ -9,8 +9,8 @@ InMemoryFile::~InMemoryFile() { } int InMemoryFile::read(void *buf, size_t count, off_t offset) const { - size_t realCount = std::min(count, (size_t)(_data.size() - offset)); - std::memcpy(buf, (uint8_t*)_data.data() + offset, realCount); + size_t realCount = std::min(count, static_cast(_data.size() - offset)); + std::memcpy(buf, _data.dataOffset(offset), realCount); return realCount; } @@ -19,7 +19,7 @@ const void *InMemoryFile::data() const { } bool InMemoryFile::fileContentEquals(const Data &expected, off_t offset) const { - return 0 == std::memcmp((uint8_t*)expected.data(), (uint8_t*)_data.data() + offset, expected.size()); + return 0 == std::memcmp(expected.data(), _data.dataOffset(offset), expected.size()); } size_t InMemoryFile::size() const { @@ -32,7 +32,7 @@ WriteableInMemoryFile::WriteableInMemoryFile(Data data): InMemoryFile(std::move( void WriteableInMemoryFile::write(const void *buf, size_t count, off_t offset) { _extendFileSizeIfNecessary(count + offset); - std::memcpy((uint8_t*)_data.data() + offset, buf, count); + std::memcpy(_data.dataOffset(offset), buf, count); } void WriteableInMemoryFile::_extendFileSizeIfNecessary(size_t size) { @@ -52,5 +52,5 @@ bool WriteableInMemoryFile::sizeUnchanged() const { } bool WriteableInMemoryFile::regionUnchanged(off_t offset, size_t count) const { - return 0 == std::memcmp((uint8_t*)_data.data() + offset, (uint8_t*)_originalData.data() + offset, count); + return 0 == std::memcmp(_data.dataOffset(offset), _originalData.dataOffset(offset), count); } diff --git a/utils.cmake b/utils.cmake index 7c239269..823fde23 100644 --- a/utils.cmake +++ b/utils.cmake @@ -51,7 +51,7 @@ endif() ################################################# function(target_enable_style_warnings TARGET) # Enable compiler options - target_compile_options(${TARGET} PRIVATE -Wall -Wextra) + target_compile_options(${TARGET} PRIVATE -Wall -Wextra -Wold-style-cast -Wcast-align) # TODO consider -Wpedantic -Wchkp -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wundef -Wno-unused -Wno-variadic-macros -Wno-parentheses -fdiagnostics-show-option -Wconversion and others? # Enable clang-tidy #set_target_properties(