diff options
Diffstat (limited to 'src/lib/util/tests/buffer_unittest.cc')
-rw-r--r-- | src/lib/util/tests/buffer_unittest.cc | 375 |
1 files changed, 187 insertions, 188 deletions
diff --git a/src/lib/util/tests/buffer_unittest.cc b/src/lib/util/tests/buffer_unittest.cc index 1c985713b1..66c43e8f21 100644 --- a/src/lib/util/tests/buffer_unittest.cc +++ b/src/lib/util/tests/buffer_unittest.cc @@ -42,110 +42,218 @@ protected: const uint8_t BufferTest::testdata[5] = {1, 2, 3, 4, 5}; +TEST_F(BufferTest, outputBufferClear) { + obuffer.writeData(testdata, sizeof(testdata)); + obuffer.clear(); + ASSERT_EQ(0, obuffer.getLength()); + ASSERT_FALSE(obuffer.getData()); +} + +TEST_F(BufferTest, outputBufferCopy) { + ASSERT_NO_THROW({ + obuffer.writeData(testdata, sizeof(testdata)); + + OutputBuffer copy(obuffer); + ASSERT_EQ(sizeof(testdata), copy.getLength()); + ASSERT_NE(obuffer.getData(), copy.getData()); + for (size_t i = 0; i < sizeof(testdata); ++i) { + ASSERT_EQ(testdata[i], copy[i]); + if (i + 1 < sizeof(testdata)) { + obuffer.writeUint16At(0, i); + } + ASSERT_EQ(testdata[i], copy[i]); + } + obuffer.clear(); + ASSERT_EQ(sizeof(testdata), copy.getLength()); + }); +} + +TEST_F(BufferTest, outputEmptyBufferCopy) { + ASSERT_NO_THROW({ + OutputBuffer copy(obuffer); + ASSERT_EQ(0, copy.getLength()); + ASSERT_FALSE(copy.getData()); + }); +} + +TEST_F(BufferTest, outputBufferAssign) { + ASSERT_NO_THROW({ + OutputBuffer another(0); + another.clear(); + obuffer.writeData(testdata, sizeof(testdata)); + + another = obuffer; + ASSERT_EQ(sizeof(testdata), another.getLength()); + ASSERT_NE(obuffer.getData(), another.getData()); + for (size_t i = 0; i < sizeof(testdata); ++i) { + ASSERT_EQ(testdata[i], another[i]); + if (i + 1 < sizeof(testdata)) { + obuffer.writeUint16At(0, i); + } + ASSERT_EQ(testdata[i], another[i]); + } + obuffer.clear(); + ASSERT_EQ(sizeof(testdata), another.getLength()); + }); +} + +TEST_F(BufferTest, outputEmptyBufferAssign) { + OutputBuffer copy(0); + ASSERT_NO_THROW(copy = obuffer;); + ASSERT_EQ(0, copy.getLength()); + ASSERT_EQ(0, copy.getData()); +} + +// Check assign to self doesn't break stuff +TEST_F(BufferTest, outputBufferAssignSelf) { + ASSERT_NO_THROW(obuffer = obuffer); +} + +TEST_F(BufferTest, inputBufferException) { + ASSERT_THROW(ibuffer.setPosition(6), isc::OutOfRange); + + ibuffer.setPosition(sizeof(testdata)); + ASSERT_THROW(ibuffer.peekUint8(), isc::OutOfRange); + ASSERT_THROW(ibuffer.readUint8(), isc::OutOfRange); + + ibuffer.setPosition(sizeof(testdata) - 1); + ASSERT_THROW(ibuffer.peekUint16(), isc::OutOfRange); + ASSERT_THROW(ibuffer.readUint16(), isc::OutOfRange); + + ibuffer.setPosition(sizeof(testdata) - 3); + ASSERT_THROW(ibuffer.peekUint32(), isc::OutOfRange); + ASSERT_THROW(ibuffer.readUint32(), isc::OutOfRange); + + ibuffer.setPosition(sizeof(testdata) - 4); + ASSERT_THROW(ibuffer.peekData(vdata, sizeof(vdata)), isc::OutOfRange); + ASSERT_THROW(ibuffer.readData(vdata, sizeof(vdata)), isc::OutOfRange); + ASSERT_THROW(ibuffer.peekVector(datav, sizeof(vdata)), isc::OutOfRange); + ASSERT_THROW(ibuffer.readVector(datav, sizeof(vdata)), isc::OutOfRange); +} + +TEST_F(BufferTest, outputBufferExtend) { + ASSERT_EQ(0, obuffer.getCapacity()); + ASSERT_EQ(0, obuffer.getLength()); + obuffer.writeUint8(10); + ASSERT_LT(0, obuffer.getCapacity()); + ASSERT_EQ(1, obuffer.getLength()); +} + +TEST_F(BufferTest, outputBufferSkip) { + obuffer.skip(4); + ASSERT_EQ(4, obuffer.getLength()); + + obuffer.skip(2); + ASSERT_EQ(6, obuffer.getLength()); +} + +TEST_F(BufferTest, outputBufferTrim) { + obuffer.writeData(testdata, sizeof(testdata)); + ASSERT_EQ(5, obuffer.getLength()); + + obuffer.trim(1); + ASSERT_EQ(4, obuffer.getLength()); + + obuffer.trim(2); + ASSERT_EQ(2, obuffer.getLength()); + + ASSERT_THROW(obuffer.trim(3), OutOfRange); +} + TEST_F(BufferTest, inputBufferRead) { - EXPECT_EQ(5, ibuffer.getLength()); - EXPECT_EQ(1, ibuffer.peekUint8()); - EXPECT_EQ(0, ibuffer.getPosition()); - EXPECT_EQ(1, ibuffer.readUint8()); - EXPECT_EQ(1, ibuffer.getPosition()); + ASSERT_EQ(5, ibuffer.getLength()); + ASSERT_EQ(1, ibuffer.peekUint8()); + ASSERT_EQ(0, ibuffer.getPosition()); + ASSERT_EQ(1, ibuffer.readUint8()); + ASSERT_EQ(1, ibuffer.getPosition()); data16 = ibuffer.peekUint16(); - EXPECT_EQ(1, ibuffer.getPosition()); - EXPECT_EQ(data16, ibuffer.readUint16()); - EXPECT_EQ((2 << 8) | 3, data16); - EXPECT_EQ(3, ibuffer.getPosition()); + ASSERT_EQ(1, ibuffer.getPosition()); + ASSERT_EQ(data16, ibuffer.readUint16()); + ASSERT_EQ((2 << 8) | 3, data16); + ASSERT_EQ(3, ibuffer.getPosition()); ibuffer.setPosition(1); - EXPECT_EQ(1, ibuffer.getPosition()); + ASSERT_EQ(1, ibuffer.getPosition()); data32 = ibuffer.peekUint32(); - EXPECT_EQ(1, ibuffer.getPosition()); - EXPECT_EQ(data32, ibuffer.readUint32()); - EXPECT_EQ((2 << 24) | (3 << 16) | (4 << 8) | 5, data32); + ASSERT_EQ(1, ibuffer.getPosition()); + ASSERT_EQ(data32, ibuffer.readUint32()); + ASSERT_EQ((2 << 24) | (3 << 16) | (4 << 8) | 5, data32); ibuffer.setPosition(0); memset(vdata, 0, sizeof(vdata)); ibuffer.peekData(vdata, sizeof(vdata)); - EXPECT_EQ(0, memcmp(vdata, testdata, sizeof(testdata))); - EXPECT_EQ(0, ibuffer.getPosition()); + ASSERT_EQ(0, memcmp(vdata, testdata, sizeof(testdata))); + ASSERT_EQ(0, ibuffer.getPosition()); memset(vdata, 0, sizeof(vdata)); ibuffer.readData(vdata, sizeof(vdata)); - EXPECT_EQ(0, memcmp(vdata, testdata, sizeof(testdata))); - EXPECT_EQ(sizeof(vdata), ibuffer.getPosition()); + ASSERT_EQ(0, memcmp(vdata, testdata, sizeof(testdata))); + ASSERT_EQ(sizeof(vdata), ibuffer.getPosition()); ibuffer.setPosition(0); datav.clear(); ibuffer.peekVector(datav, sizeof(vdata)); ASSERT_EQ(sizeof(vdata), datav.size()); - EXPECT_EQ(0, memcmp(&vdata[0], testdata, sizeof(testdata))); - EXPECT_EQ(0, ibuffer.getPosition()); + ASSERT_EQ(0, memcmp(&vdata[0], testdata, sizeof(testdata))); + ASSERT_EQ(0, ibuffer.getPosition()); datav.clear(); ibuffer.readVector(datav, sizeof(vdata)); ASSERT_EQ(sizeof(vdata), datav.size()); - EXPECT_EQ(0, memcmp(&vdata[0], testdata, sizeof(testdata))); - EXPECT_EQ(sizeof(vdata), ibuffer.getPosition()); + ASSERT_EQ(0, memcmp(&vdata[0], testdata, sizeof(testdata))); + ASSERT_EQ(sizeof(vdata), ibuffer.getPosition()); } -TEST_F(BufferTest, inputBufferException) { - EXPECT_THROW(ibuffer.setPosition(6), isc::OutOfRange); - - ibuffer.setPosition(sizeof(testdata)); - EXPECT_THROW(ibuffer.peekUint8(), isc::OutOfRange); - EXPECT_THROW(ibuffer.readUint8(), isc::OutOfRange); - - ibuffer.setPosition(sizeof(testdata) - 1); - EXPECT_THROW(ibuffer.peekUint16(), isc::OutOfRange); - EXPECT_THROW(ibuffer.readUint16(), isc::OutOfRange); - - ibuffer.setPosition(sizeof(testdata) - 3); - EXPECT_THROW(ibuffer.peekUint32(), isc::OutOfRange); - EXPECT_THROW(ibuffer.readUint32(), isc::OutOfRange); - - ibuffer.setPosition(sizeof(testdata) - 4); - EXPECT_THROW(ibuffer.peekData(vdata, sizeof(vdata)), isc::OutOfRange); - EXPECT_THROW(ibuffer.readData(vdata, sizeof(vdata)), isc::OutOfRange); - EXPECT_THROW(ibuffer.peekVector(datav, sizeof(vdata)), isc::OutOfRange); - EXPECT_THROW(ibuffer.readVector(datav, sizeof(vdata)), isc::OutOfRange); +TEST_F(BufferTest, outputBufferReadAt) { + obuffer.writeData(testdata, sizeof(testdata)); + for (size_t i = 0; i < sizeof(testdata); ++i) { + ASSERT_EQ(testdata[i], obuffer[i]); + } + ASSERT_THROW(obuffer[sizeof(testdata)], isc::OutOfRange); } -TEST_F(BufferTest, outputBufferExtend) { - EXPECT_EQ(0, obuffer.getCapacity()); - EXPECT_EQ(0, obuffer.getLength()); - obuffer.writeUint8(10); - EXPECT_LT(0, obuffer.getCapacity()); - EXPECT_EQ(1, obuffer.getLength()); +TEST_F(BufferTest, inputBufferReadVectorChunks) { + std::vector<uint8_t> vec; + + // check that vector can read the whole buffer + ibuffer.readVector(vec, 3); + ASSERT_EQ(3, vec.size()); + ASSERT_EQ(0, memcmp(&vec[0], testdata, 3)); + ASSERT_NO_THROW(ibuffer.readVector(vec, 2)); + ASSERT_EQ(2, vec.size()); + ASSERT_EQ(0, memcmp(&vec[0], &testdata[3], 2)); } TEST_F(BufferTest, outputBufferWrite) { obuffer.writeUint8(1); expected_size += sizeof(uint8_t); - EXPECT_EQ(expected_size, obuffer.getLength()); + ASSERT_EQ(expected_size, obuffer.getLength()); const uint8_t* cp = obuffer.getData(); - EXPECT_EQ(1, *cp); + ASSERT_EQ(1, *cp); obuffer.writeUint16(data16); expected_size += sizeof(data16); cp = obuffer.getData(); - EXPECT_EQ(expected_size, obuffer.getLength()); - EXPECT_EQ(2, *(cp + 1)); - EXPECT_EQ(3, *(cp + 2)); + ASSERT_EQ(expected_size, obuffer.getLength()); + ASSERT_EQ(2, *(cp + 1)); + ASSERT_EQ(3, *(cp + 2)); obuffer.writeUint32(data32); expected_size += sizeof(data32); cp = obuffer.getData(); - EXPECT_EQ(expected_size, obuffer.getLength()); - EXPECT_EQ(4, *(cp + 3)); - EXPECT_EQ(5, *(cp + 4)); - EXPECT_EQ(6, *(cp + 5)); - EXPECT_EQ(7, *(cp + 6)); + ASSERT_EQ(expected_size, obuffer.getLength()); + ASSERT_EQ(4, *(cp + 3)); + ASSERT_EQ(5, *(cp + 4)); + ASSERT_EQ(6, *(cp + 5)); + ASSERT_EQ(7, *(cp + 6)); obuffer.writeData(testdata, sizeof(testdata)); expected_size += sizeof(testdata); - EXPECT_EQ(expected_size, obuffer.getLength()); + ASSERT_EQ(expected_size, obuffer.getLength()); cp = obuffer.getData(); - EXPECT_EQ(0, memcmp(cp + 7, testdata, sizeof(testdata))); + ASSERT_EQ(0, memcmp(cp + 7, testdata, sizeof(testdata))); datav = obuffer.getVector(); ASSERT_EQ(expected_size, datav.size()); std::vector<uint8_t> expected = { 1, 2, 3, 4, 5, 6, 7 }; expected.insert(expected.end(), testdata, testdata + sizeof(testdata)); ASSERT_EQ(expected_size, expected.size()); - EXPECT_EQ(0, memcmp(&expected[0], &datav[0], expected_size)); + ASSERT_EQ(0, memcmp(&expected[0], &datav[0], expected_size)); } TEST_F(BufferTest, outputBufferWriteAt) { @@ -154,138 +262,29 @@ TEST_F(BufferTest, outputBufferWriteAt) { // overwrite 2nd byte obuffer.writeUint8At(4, 1); - EXPECT_EQ(expected_size, obuffer.getLength()); // length shouldn't change + ASSERT_EQ(expected_size, obuffer.getLength()); // length shouldn't change const uint8_t* cp = obuffer.getData(); - EXPECT_EQ(4, *(cp + 1)); + ASSERT_EQ(4, *(cp + 1)); // overwrite 2nd and 3rd bytes obuffer.writeUint16At(data16, 1); - EXPECT_EQ(expected_size, obuffer.getLength()); // length shouldn't change + ASSERT_EQ(expected_size, obuffer.getLength()); // length shouldn't change cp = obuffer.getData(); - EXPECT_EQ(2, *(cp + 1)); - EXPECT_EQ(3, *(cp + 2)); + ASSERT_EQ(2, *(cp + 1)); + ASSERT_EQ(3, *(cp + 2)); // overwrite 3rd and 4th bytes obuffer.writeUint16At(data16, 2); - EXPECT_EQ(expected_size, obuffer.getLength()); + ASSERT_EQ(expected_size, obuffer.getLength()); cp = obuffer.getData(); - EXPECT_EQ(2, *(cp + 2)); - EXPECT_EQ(3, *(cp + 3)); - - EXPECT_THROW(obuffer.writeUint8At(data16, 5), isc::OutOfRange); - EXPECT_THROW(obuffer.writeUint8At(data16, 4), isc::OutOfRange); - EXPECT_THROW(obuffer.writeUint16At(data16, 3), isc::OutOfRange); - EXPECT_THROW(obuffer.writeUint16At(data16, 4), isc::OutOfRange); - EXPECT_THROW(obuffer.writeUint16At(data16, 5), isc::OutOfRange); -} - -TEST_F(BufferTest, outputBufferSkip) { - obuffer.skip(4); - EXPECT_EQ(4, obuffer.getLength()); - - obuffer.skip(2); - EXPECT_EQ(6, obuffer.getLength()); -} - -TEST_F(BufferTest, outputBufferTrim) { - obuffer.writeData(testdata, sizeof(testdata)); - EXPECT_EQ(5, obuffer.getLength()); - - obuffer.trim(1); - EXPECT_EQ(4, obuffer.getLength()); - - obuffer.trim(2); - EXPECT_EQ(2, obuffer.getLength()); - - EXPECT_THROW(obuffer.trim(3), OutOfRange); -} - -TEST_F(BufferTest, outputBufferReadAt) { - obuffer.writeData(testdata, sizeof(testdata)); - for (size_t i = 0; i < sizeof(testdata); ++i) { - EXPECT_EQ(testdata[i], obuffer[i]); - } - EXPECT_THROW(obuffer[sizeof(testdata)], isc::OutOfRange); -} - -TEST_F(BufferTest, outputBufferClear) { - obuffer.writeData(testdata, sizeof(testdata)); - const uint8_t* cp = obuffer.getData(); - obuffer.clear(); - EXPECT_EQ(0, obuffer.getLength()); - EXPECT_FALSE(obuffer.getData()); -} - -TEST_F(BufferTest, outputBufferCopy) { - EXPECT_NO_THROW({ - obuffer.writeData(testdata, sizeof(testdata)); - - OutputBuffer copy(obuffer); - ASSERT_EQ(sizeof(testdata), copy.getLength()); - ASSERT_NE(obuffer.getData(), copy.getData()); - for (size_t i = 0; i < sizeof(testdata); ++i) { - EXPECT_EQ(testdata[i], copy[i]); - if (i + 1 < sizeof(testdata)) { - obuffer.writeUint16At(0, i); - } - EXPECT_EQ(testdata[i], copy[i]); - } - obuffer.clear(); - ASSERT_EQ(sizeof(testdata), copy.getLength()); - }); -} - -TEST_F(BufferTest, outputEmptyBufferCopy) { - EXPECT_NO_THROW({ - OutputBuffer copy(obuffer); - ASSERT_EQ(0, copy.getLength()); - EXPECT_FALSE(copy.getData()); - }); -} - -TEST_F(BufferTest, outputBufferAssign) { - EXPECT_NO_THROW({ - OutputBuffer another(0); - another.clear(); - obuffer.writeData(testdata, sizeof(testdata)); - - another = obuffer; - ASSERT_EQ(sizeof(testdata), another.getLength()); - ASSERT_NE(obuffer.getData(), another.getData()); - for (size_t i = 0; i < sizeof(testdata); ++i) { - EXPECT_EQ(testdata[i], another[i]); - if (i + 1 < sizeof(testdata)) { - obuffer.writeUint16At(0, i); - } - EXPECT_EQ(testdata[i], another[i]); - } - obuffer.clear(); - ASSERT_EQ(sizeof(testdata), another.getLength()); - }); -} - -TEST_F(BufferTest, outputEmptyBufferAssign) { - OutputBuffer copy(0); - EXPECT_NO_THROW(copy = obuffer;); - EXPECT_EQ(0, copy.getLength()); - EXPECT_EQ(0, copy.getData()); -} - -// Check assign to self doesn't break stuff -TEST_F(BufferTest, outputBufferAssignSelf) { - EXPECT_NO_THROW(obuffer = obuffer); -} - -TEST_F(BufferTest, inputBufferReadVectorChunks) { - std::vector<uint8_t> vec; - - // check that vector can read the whole buffer - ibuffer.readVector(vec, 3); - EXPECT_EQ(3, vec.size()); - EXPECT_EQ(0, memcmp(&vec[0], testdata, 3)); - EXPECT_NO_THROW(ibuffer.readVector(vec, 2)); - ASSERT_EQ(2, vec.size()); - EXPECT_EQ(0, memcmp(&vec[0], &testdata[3], 2)); + ASSERT_EQ(2, *(cp + 2)); + ASSERT_EQ(3, *(cp + 3)); + + ASSERT_THROW(obuffer.writeUint8At(data16, 5), isc::OutOfRange); + ASSERT_THROW(obuffer.writeUint8At(data16, 4), isc::OutOfRange); + ASSERT_THROW(obuffer.writeUint16At(data16, 3), isc::OutOfRange); + ASSERT_THROW(obuffer.writeUint16At(data16, 4), isc::OutOfRange); + ASSERT_THROW(obuffer.writeUint16At(data16, 5), isc::OutOfRange); } // Tests whether uint64 can be written properly. @@ -298,15 +297,15 @@ TEST_F(BufferTest, writeUint64) { obuffer.writeUint64(val1); ASSERT_EQ(sizeof(uint64_t), obuffer.getLength()); const uint8_t* cp = obuffer.getData(); - EXPECT_TRUE(cp); - EXPECT_FALSE(memcmp(exp_val1, obuffer.getData(), sizeof(uint64_t))); + ASSERT_TRUE(cp); + ASSERT_FALSE(memcmp(exp_val1, obuffer.getData(), sizeof(uint64_t))); - EXPECT_NO_THROW(obuffer.clear()); + ASSERT_NO_THROW(obuffer.clear()); obuffer.writeUint64(val2); ASSERT_EQ(sizeof(uint64_t), obuffer.getLength()); cp = obuffer.getData(); - EXPECT_TRUE(cp); - EXPECT_FALSE(memcmp(exp_val2, obuffer.getData(), sizeof(uint64_t))); + ASSERT_TRUE(cp); + ASSERT_FALSE(memcmp(exp_val2, obuffer.getData(), sizeof(uint64_t))); } } |