encoding: fix strict-aliasing warning and extending TLV test cases
Change-Id: I9455b3a3ad6e8860a9e939108896a481a3b64164
diff --git a/src/encoding/tlv.hpp b/src/encoding/tlv.hpp
index 441bc99..803df9f 100644
--- a/src/encoding/tlv.hpp
+++ b/src/encoding/tlv.hpp
@@ -244,107 +244,70 @@
if (begin == end)
throw Error("Empty buffer during TLV processing");
- uint8_t firstOctet = *begin;
- ++begin;
- if (firstOctet < 253)
- {
- return firstOctet;
- }
- else if (firstOctet == 253)
- {
- if (end - begin < 2)
- throw Error("Insufficient data during TLV processing");
+ uint64_t value;
+ bool isOk = readVarNumber(begin, end, value);
+ if (!isOk)
+ throw Error("Insufficient data during TLV processing");
- uint16_t value = *reinterpret_cast<const uint16_t*>(&*begin);
- begin += 2;
- return be16toh(value);
- }
- else if (firstOctet == 254)
- {
- if (end - begin < 4)
- throw Error("Insufficient data during TLV processing");
-
- uint32_t value = *reinterpret_cast<const uint32_t*>(&*begin);
- begin += 4;
- return be32toh(value);
- }
- else // if (firstOctet == 255)
- {
- if (end - begin < 8)
- throw Error("Insufficient data during TLV processing");
-
- uint64_t value = *reinterpret_cast<const uint64_t*>(&*begin);
- begin += 8;
-
- return be64toh(value);
- }
+ return value;
}
template<>
-inline uint64_t
+inline bool
readVarNumber<std::istream_iterator<uint8_t> >(std::istream_iterator<uint8_t>& begin,
- const std::istream_iterator<uint8_t>& end)
+ const std::istream_iterator<uint8_t>& end,
+ uint64_t& value)
{
if (begin == end)
- throw Error("Empty buffer during TLV processing");
+ return false;
uint8_t firstOctet = *begin;
++begin;
if (firstOctet < 253)
{
- return firstOctet;
+ value = firstOctet;
}
else if (firstOctet == 253)
{
- uint8_t buffer[2];
- int count = 0;
+ value = 0;
+ size_t count = 0;
+ for (; begin != end && count < 2; ++count)
+ {
+ value = ((value << 8) | *begin);
+ begin++;
+ }
- while(begin != end && count < 2){
- buffer[count] = *begin;
- begin++;
- count++;
- }
-
- if (count < 2)
- throw Error("Insufficient data during TLV processing");
-
- uint16_t value = *reinterpret_cast<const uint16_t*>(buffer);
- return be16toh(value);
+ if (count != 2)
+ return false;
}
else if (firstOctet == 254)
{
- uint8_t buffer[4];
- int count = 0;
+ value = 0;
+ size_t count = 0;
+ for (; begin != end && count < 4; ++count)
+ {
+ value = ((value << 8) | *begin);
+ begin++;
+ }
- while(begin != end && count < 4){
- buffer[count] = *begin;
- begin++;
- count++;
- }
-
- if (count < 4)
- throw Error("Insufficient data during TLV processing");
-
- uint32_t value = *reinterpret_cast<const uint32_t*>(buffer);
- return be32toh(value);
+ if (count != 4)
+ return false;
}
else // if (firstOctet == 255)
{
- uint8_t buffer[8];
- int count = 0;
+ value = 0;
+ size_t count = 0;
+ for (; begin != end && count < 8; ++count)
+ {
+ value = ((value << 8) | *begin);
+ begin++;
+ }
- while(begin != end && count < 8){
- buffer[count] = *begin;
- begin++;
- count++;
- }
-
- if (count < 8)
- throw Error("Insufficient data during TLV processing");
-
- uint64_t value = *reinterpret_cast<const uint64_t*>(buffer);
- return be64toh(value);
+ if (count != 8)
+ return false;
}
+
+ return true;
}
template<class InputIterator>
@@ -461,60 +424,54 @@
if (begin == end)
throw Error("Insufficient data during TLV processing");
- uint8_t value = *begin;
+ uint64_t value = *begin;
begin++;
return value;
}
case 2:
{
- uint8_t buffer[2];
- int count = 0;
+ uint64_t value = 0;
+ size_t count = 0;
+ for (; begin != end && count < 2; ++count)
+ {
+ value = ((value << 8) | *begin);
+ begin++;
+ }
- while (begin != end && count < 2) {
- buffer[count] = *begin;
- begin++;
- count++;
- }
-
- if (count < 2)
+ if (count != 2)
throw Error("Insufficient data during TLV processing");
- uint16_t value = *reinterpret_cast<const uint16_t*>(buffer);
- return be16toh(value);
+ return value;
}
case 4:
{
- uint8_t buffer[4];
- int count = 0;
+ uint64_t value = 0;
+ size_t count = 0;
+ for (; begin != end && count < 4; ++count)
+ {
+ value = ((value << 8) | *begin);
+ begin++;
+ }
- while (begin != end && count < 4) {
- buffer[count] = *begin;
- begin++;
- count++;
- }
-
- if (count < 4)
+ if (count != 4)
throw Error("Insufficient data during TLV processing");
- uint32_t value = *reinterpret_cast<const uint32_t*>(buffer);
- return be32toh(value);
+ return value;
}
case 8:
{
- uint8_t buffer[8];
- int count = 0;
+ uint64_t value = 0;
+ size_t count = 0;
+ for (; begin != end && count < 8; ++count)
+ {
+ value = ((value << 8) | *begin);
+ begin++;
+ }
- while (begin != end && count < 8){
- buffer[count] = *begin;
- begin++;
- count++;
- }
-
- if (count < 8)
+ if (count != 8)
throw Error("Insufficient data during TLV processing");
- uint64_t value = *reinterpret_cast<const uint64_t*>(buffer);
- return be64toh(value);
+ return value;
}
}
throw Error("Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)");
diff --git a/tests/test-block.cpp b/tests/test-block.cpp
index be2d2a6..32cd8cd 100644
--- a/tests/test-block.cpp
+++ b/tests/test-block.cpp
@@ -12,7 +12,331 @@
BOOST_AUTO_TEST_SUITE(TestBlock)
-BOOST_AUTO_TEST_CASE(Basic)
+BOOST_AUTO_TEST_CASE(TlvFromBuffer)
+{
+ static const uint8_t BUFFER[] = {
+ 0x01, // == 1
+ 0xfc, // == 252
+ 0xfd, 0x00, 0xfd, // == 253
+ 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
+ 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
+ };
+
+ using namespace Tlv;
+
+ const uint8_t* begin;
+ uint64_t value;
+
+ begin = BUFFER;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
+ begin = BUFFER;
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
+ BOOST_CHECK_EQUAL(value, 1);
+
+ begin = BUFFER + 1;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
+ begin = BUFFER + 1;
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
+ BOOST_CHECK_EQUAL(value, 252);
+
+ begin = BUFFER + 2;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
+ begin = BUFFER + 2;
+ BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
+
+ begin = BUFFER + 2;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
+ begin = BUFFER + 2;
+ BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
+
+ begin = BUFFER + 2;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
+ begin = BUFFER + 2;
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
+ BOOST_CHECK_EQUAL(value, 253);
+
+
+ begin = BUFFER + 5;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
+ begin = BUFFER + 5;
+ BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
+
+ begin = BUFFER + 5;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
+ begin = BUFFER + 5;
+ BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
+
+ begin = BUFFER + 5;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
+ begin = BUFFER + 5;
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
+ BOOST_CHECK_EQUAL(value, 65536);
+
+ begin = BUFFER + 10;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
+ begin = BUFFER + 10;
+ BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
+
+ begin = BUFFER + 10;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
+ begin = BUFFER + 10;
+ BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
+
+ begin = BUFFER + 10;
+ BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
+ begin = BUFFER + 10;
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
+ BOOST_CHECK_EQUAL(value, 4294967296LL);
+}
+
+BOOST_AUTO_TEST_CASE(TlvFromStream)
+{
+ static const uint8_t BUFFER[] = {
+ 0x01, // == 1
+ 0xfc, // == 252
+ 0xfd, 0x00, 0xfd, // == 253
+ 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
+ 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
+ };
+
+ using namespace Tlv;
+
+ typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
+ typedef std::istream_iterator<uint8_t> Iterator;
+
+ Iterator end; // end of stream
+ uint64_t value;
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
+ Iterator begin(stream);
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
+ BOOST_CHECK_EQUAL(value, 1);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
+ Iterator begin(stream);
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
+ BOOST_CHECK_EQUAL(value, 252);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
+ Iterator begin(stream);
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
+ BOOST_CHECK_EQUAL(value, 253);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
+ Iterator begin(stream);
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
+ BOOST_CHECK_EQUAL(value, 65536);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
+ Iterator begin(stream);
+ BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
+ Iterator begin(stream);
+ BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
+ BOOST_CHECK_EQUAL(value, 4294967296LL);
+ }
+}
+
+BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromBuffer)
+{
+ static const uint8_t BUFFER[] = {
+ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
+ };
+
+ using namespace Tlv;
+
+ const uint8_t* begin;
+ uint64_t value;
+
+ begin = BUFFER;
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, begin + 0), Error);
+ BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, begin + 1));
+ BOOST_CHECK_EQUAL(value, 1);
+
+ begin = BUFFER;
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, begin + 1), Error);
+ BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, begin + 2));
+ BOOST_CHECK_EQUAL(value, 257);
+
+ begin = BUFFER;
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, begin + 3), Error);
+ BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, begin + 4));
+ BOOST_CHECK_EQUAL(value, 16843009LL);
+
+ begin = BUFFER;
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, begin + 7), Error);
+ BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, begin + 8));
+ BOOST_CHECK_EQUAL(value, 72340172838076673LL);
+
+ begin = BUFFER;
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, begin + 3), Error);
+}
+
+BOOST_AUTO_TEST_CASE(NonNegativeIntegerFromStream)
+{
+ static const uint8_t BUFFER[] = {
+ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
+ };
+
+ using namespace Tlv;
+
+ typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
+ typedef std::istream_iterator<uint8_t> Iterator;
+
+ Iterator end; // end of stream
+ uint64_t value;
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, end), Error);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
+ Iterator begin(stream);
+ BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, end));
+ BOOST_CHECK_EQUAL(value, 1);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, end), Error);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 2);
+ Iterator begin(stream);
+ BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, end));
+ BOOST_CHECK_EQUAL(value, 257);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, end), Error);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 4);
+ Iterator begin(stream);
+ BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, end));
+ BOOST_CHECK_EQUAL(value, 16843009LL);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, end), Error);
+ }
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 8);
+ Iterator begin(stream);
+ BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, end));
+ BOOST_CHECK_EQUAL(value, 72340172838076673LL);
+ }
+
+ {
+ ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
+ Iterator begin(stream);
+ BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, end), Error);
+ }
+}
+
+BOOST_AUTO_TEST_CASE(BlockBasic)
{
EncodingBuffer buffer;
EncodingEstimator estimator;
@@ -55,15 +379,15 @@
BOOST_CHECK_EQUAL(s2, 5);
buffer = EncodingBuffer();
- s1 = buffer.prependVarNumber(4294967295);
- s2 = estimator.prependVarNumber(4294967295);
+ s1 = buffer.prependVarNumber(4294967295LL);
+ s2 = estimator.prependVarNumber(4294967295LL);
BOOST_CHECK_EQUAL(buffer.size(), 5);
BOOST_CHECK_EQUAL(s1, 5);
BOOST_CHECK_EQUAL(s2, 5);
buffer = EncodingBuffer();
- s1 = buffer.prependVarNumber(4294967296);
- s2 = estimator.prependVarNumber(4294967296);
+ s1 = buffer.prependVarNumber(4294967296LL);
+ s2 = estimator.prependVarNumber(4294967296LL);
BOOST_CHECK_EQUAL(buffer.size(), 9);
BOOST_CHECK_EQUAL(s1, 9);
BOOST_CHECK_EQUAL(s2, 9);
@@ -106,15 +430,15 @@
BOOST_CHECK_EQUAL(s2, 4);
buffer = EncodingBuffer();
- s1 = buffer.prependNonNegativeInteger(4294967295);
- s2 = estimator.prependNonNegativeInteger(4294967295);
+ s1 = buffer.prependNonNegativeInteger(4294967295LL);
+ s2 = estimator.prependNonNegativeInteger(4294967295LL);
BOOST_CHECK_EQUAL(buffer.size(), 4);
BOOST_CHECK_EQUAL(s1, 4);
BOOST_CHECK_EQUAL(s2, 4);
buffer = EncodingBuffer();
- s1 = buffer.prependNonNegativeInteger(4294967296);
- s2 = estimator.prependNonNegativeInteger(4294967296);
+ s1 = buffer.prependNonNegativeInteger(4294967296LL);
+ s2 = estimator.prependNonNegativeInteger(4294967296LL);
BOOST_CHECK_EQUAL(buffer.size(), 8);
BOOST_CHECK_EQUAL(s1, 8);
BOOST_CHECK_EQUAL(s2, 8);
@@ -212,6 +536,33 @@
BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock));
}
+BOOST_AUTO_TEST_CASE(BlockFromStream)
+{
+ const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok
+ 0x01, 0x01, 0xfb, // ok
+ 0x03, 0x02, 0xff}; // not ok
+
+ typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
+ ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
+
+ Block testBlock;
+ BOOST_REQUIRE_NO_THROW(testBlock = Block(stream));
+ BOOST_CHECK_EQUAL(testBlock.type(), 0);
+ BOOST_CHECK_EQUAL(testBlock.size(), 3);
+ BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
+ BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00);
+ BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa);
+
+ BOOST_REQUIRE_NO_THROW(testBlock = Block(stream));
+ BOOST_CHECK_EQUAL(testBlock.type(), 1);
+ BOOST_CHECK_EQUAL(testBlock.size(), 3);
+ BOOST_CHECK_EQUAL(testBlock.value_size(), 1);
+ BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01);
+ BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb);
+
+ BOOST_CHECK_THROW(testBlock = Block(stream), Tlv::Error);
+}
+
BOOST_AUTO_TEST_SUITE_END()
} // namespace ndn