Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 22 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 23 | #include "encoding/buffer-stream.hpp" |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 24 | #include "encoding/block-helpers.hpp" |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 28 | namespace ndn { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 29 | namespace tests { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 30 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(EncodingBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 33 | class BasicBlockFixture |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 34 | { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 35 | public: |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 36 | EncodingBuffer buffer; |
| 37 | EncodingEstimator estimator; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 38 | }; |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 40 | BOOST_FIXTURE_TEST_SUITE(Basic, BasicBlockFixture) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(VarNumberOneByte1) |
| 43 | { |
| 44 | size_t s1 = buffer.prependVarNumber(252); |
| 45 | size_t s2 = estimator.prependVarNumber(252); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 47 | BOOST_CHECK_EQUAL(s1, 1); |
| 48 | BOOST_CHECK_EQUAL(s2, 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 49 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 51 | BOOST_AUTO_TEST_CASE(VarNumberOneByte2) |
| 52 | { |
| 53 | size_t s1 = buffer.prependVarNumber(253); |
| 54 | size_t s2 = estimator.prependVarNumber(253); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 56 | BOOST_CHECK_EQUAL(s1, 3); |
| 57 | BOOST_CHECK_EQUAL(s2, 3); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 58 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 60 | BOOST_AUTO_TEST_CASE(VarNumberThreeBytes1) |
| 61 | { |
| 62 | size_t s1 = buffer.prependVarNumber(255); |
| 63 | size_t s2 = estimator.prependVarNumber(255); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 65 | BOOST_CHECK_EQUAL(s1, 3); |
| 66 | BOOST_CHECK_EQUAL(s2, 3); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 67 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 69 | BOOST_AUTO_TEST_CASE(VarNumberThreeBytes2) |
| 70 | { |
| 71 | size_t s1 = buffer.prependVarNumber(65535); |
| 72 | size_t s2 = estimator.prependVarNumber(65535); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 74 | BOOST_CHECK_EQUAL(s1, 3); |
| 75 | BOOST_CHECK_EQUAL(s2, 3); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 76 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 78 | BOOST_AUTO_TEST_CASE(VarNumberFiveBytes1) |
| 79 | { |
| 80 | size_t s1 = buffer.prependVarNumber(65536); |
| 81 | size_t s2 = estimator.prependVarNumber(65536); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 83 | BOOST_CHECK_EQUAL(s1, 5); |
| 84 | BOOST_CHECK_EQUAL(s2, 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 85 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(VarNumberFiveBytes2) |
| 88 | { |
| 89 | size_t s1 = buffer.prependVarNumber(4294967295LL); |
| 90 | size_t s2 = estimator.prependVarNumber(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 92 | BOOST_CHECK_EQUAL(s1, 5); |
| 93 | BOOST_CHECK_EQUAL(s2, 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 94 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 96 | BOOST_AUTO_TEST_CASE(VarNumberNineBytes) |
| 97 | { |
| 98 | size_t s1 = buffer.prependVarNumber(4294967296LL); |
| 99 | size_t s2 = estimator.prependVarNumber(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(buffer.size(), 9); |
| 101 | BOOST_CHECK_EQUAL(s1, 9); |
| 102 | BOOST_CHECK_EQUAL(s2, 9); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 103 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte1) |
| 106 | { |
| 107 | size_t s1 = buffer.prependNonNegativeInteger(252); |
| 108 | size_t s2 = estimator.prependNonNegativeInteger(252); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 109 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 110 | BOOST_CHECK_EQUAL(s1, 1); |
| 111 | BOOST_CHECK_EQUAL(s2, 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 112 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 113 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 114 | BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte2) |
| 115 | { |
| 116 | size_t s1 = buffer.prependNonNegativeInteger(255); |
| 117 | size_t s2 = estimator.prependNonNegativeInteger(255); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 119 | BOOST_CHECK_EQUAL(s1, 1); |
| 120 | BOOST_CHECK_EQUAL(s2, 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 121 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes1) |
| 124 | { |
| 125 | size_t s1 = buffer.prependNonNegativeInteger(256); |
| 126 | size_t s2 = estimator.prependNonNegativeInteger(256); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 127 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 128 | BOOST_CHECK_EQUAL(s1, 2); |
| 129 | BOOST_CHECK_EQUAL(s2, 2); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 130 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 132 | BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes2) |
| 133 | { |
| 134 | size_t s1 = buffer.prependNonNegativeInteger(65535); |
| 135 | size_t s2 = estimator.prependNonNegativeInteger(65535); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 137 | BOOST_CHECK_EQUAL(s1, 2); |
| 138 | BOOST_CHECK_EQUAL(s2, 2); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 139 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 141 | BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes1) |
| 142 | { |
| 143 | size_t s1 = buffer.prependNonNegativeInteger(65536); |
| 144 | size_t s2 = estimator.prependNonNegativeInteger(65536); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 145 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 146 | BOOST_CHECK_EQUAL(s1, 4); |
| 147 | BOOST_CHECK_EQUAL(s2, 4); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 148 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes2) |
| 151 | { |
| 152 | size_t s1 = buffer.prependNonNegativeInteger(4294967295LL); |
| 153 | size_t s2 = estimator.prependNonNegativeInteger(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 154 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 155 | BOOST_CHECK_EQUAL(s1, 4); |
| 156 | BOOST_CHECK_EQUAL(s2, 4); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 157 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 159 | BOOST_AUTO_TEST_CASE(NonNegativeNumberEightBytes) |
| 160 | { |
| 161 | size_t s1 = buffer.prependNonNegativeInteger(4294967296LL); |
| 162 | size_t s2 = estimator.prependNonNegativeInteger(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(buffer.size(), 8); |
| 164 | BOOST_CHECK_EQUAL(s1, 8); |
| 165 | BOOST_CHECK_EQUAL(s2, 8); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 168 | BOOST_AUTO_TEST_SUITE_END() // Basic |
| 169 | |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 170 | BOOST_AUTO_TEST_CASE(BlockFromBlock) |
| 171 | { |
| 172 | static uint8_t buffer[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01}; |
| 173 | Block block(buffer, sizeof(buffer)); |
| 174 | |
| 175 | Block derivedBlock(block, block.begin(), block.end()); |
| 176 | BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match |
| 177 | BOOST_CHECK(derivedBlock == block); // blocks should match |
| 178 | |
| 179 | derivedBlock = Block(block, block.begin() + 2, block.begin() + 5); |
| 180 | BOOST_CHECK(derivedBlock.begin() == block.begin() + 2); |
| 181 | BOOST_CHECK(derivedBlock == Block(buffer + 2, 3)); |
| 182 | |
| 183 | Buffer otherBuffer(buffer, sizeof(buffer)); |
| 184 | BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), Block::Error); |
| 185 | BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), Block::Error); |
| 186 | BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), Block::Error); |
| 187 | } |
| 188 | |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 189 | BOOST_AUTO_TEST_CASE(BlockFromBlockCopyOnWriteModifyOrig) |
| 190 | { |
| 191 | static uint8_t buffer[] = { |
| 192 | 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07, |
| 193 | }; |
| 194 | |
| 195 | Block block1(buffer, sizeof(buffer)); |
| 196 | |
| 197 | Block block2(block1, block1.begin(), block1.end()); |
| 198 | auto buf2 = block2.getBuffer(); |
| 199 | |
| 200 | block1.parse(); |
| 201 | block1.remove(tlv::Name); |
| 202 | block1.encode(); |
| 203 | |
| 204 | block2.parse(); |
| 205 | |
| 206 | BOOST_CHECK_EQUAL_COLLECTIONS(block2.begin(), block2.end(), buffer, buffer + sizeof(buffer)); |
| 207 | BOOST_CHECK_EQUAL(buf2, block2.getBuffer()); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_CASE(BlockFromBlockCopyOnWriteModifyCopy) |
| 211 | { |
| 212 | static uint8_t buffer[] = { |
| 213 | 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07, |
| 214 | }; |
| 215 | |
| 216 | Block block1(buffer, sizeof(buffer)); |
| 217 | auto buf1 = block1.getBuffer(); |
| 218 | |
| 219 | Block block2(block1, block1.begin(), block1.end()); |
| 220 | |
| 221 | block2.parse(); |
| 222 | block2.remove(tlv::Name); |
| 223 | block2.encode(); |
| 224 | |
| 225 | block1.parse(); |
| 226 | |
| 227 | BOOST_CHECK_EQUAL_COLLECTIONS(block1.begin(), block1.end(), buffer, buffer + sizeof(buffer)); |
| 228 | BOOST_CHECK_EQUAL(buf1, block1.getBuffer()); |
| 229 | } |
| 230 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 231 | BOOST_AUTO_TEST_CASE(EncodingBufferToBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 232 | { |
| 233 | uint8_t value[4]; |
| 234 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 235 | EncodingBuffer buffer; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 236 | size_t length = buffer.prependByteArray(value, sizeof(value)); |
| 237 | buffer.prependVarNumber(length); |
| 238 | buffer.prependVarNumber(0xe0); |
| 239 | |
| 240 | Block block; |
| 241 | BOOST_REQUIRE_NO_THROW(block = buffer.block()); |
| 242 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 243 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 244 | |
| 245 | BOOST_REQUIRE_NO_THROW(block = Block(buffer)); |
| 246 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 247 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
| 248 | } |
| 249 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 250 | BOOST_AUTO_TEST_CASE(BlockToBuffer) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 251 | { |
| 252 | shared_ptr<Buffer> buf = make_shared<Buffer>(10); |
| 253 | for (int i = 0; i < 10; i++) (*buf)[i] = i; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 254 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 255 | Block block(0xab, buf); |
| 256 | block.encode(); |
| 257 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 258 | { |
| 259 | BOOST_REQUIRE_NO_THROW(EncodingBuffer(block)); |
| 260 | EncodingBuffer buffer(block); |
| 261 | BOOST_CHECK_EQUAL(buffer.size(), 12); |
| 262 | BOOST_CHECK_EQUAL(buffer.capacity(), 12); |
| 263 | } |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 264 | |
| 265 | (*buf)[1] = 0xe0; |
| 266 | (*buf)[2] = 2; |
| 267 | BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5)); |
| 268 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 269 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 270 | { |
| 271 | BOOST_REQUIRE_NO_THROW(EncodingBuffer(block)); |
| 272 | EncodingBuffer buffer(block); |
| 273 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 274 | BOOST_CHECK_EQUAL(buffer.capacity(), 10); |
| 275 | } |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 278 | BOOST_AUTO_TEST_CASE(FromBuffer) |
| 279 | { |
| 280 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 281 | 0x01, 0x01, 0xfb, // ok |
| 282 | 0x03, 0x02, 0xff}; // not ok |
| 283 | BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER))); |
| 284 | |
| 285 | // using BufferPtr (avoids memory copy) |
| 286 | size_t offset = 0; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 287 | bool isOk = false; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 288 | Block testBlock; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 289 | std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| 290 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 291 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 292 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 293 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 294 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 295 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 296 | offset += testBlock.size(); |
| 297 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 298 | std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| 299 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 300 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 301 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 302 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 303 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 304 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 305 | offset += testBlock.size(); |
| 306 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 307 | std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| 308 | BOOST_CHECK(!isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 309 | |
| 310 | // just buffer, copies memory |
| 311 | offset = 0; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 312 | std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| 313 | sizeof(TEST_BUFFER) - offset); |
| 314 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 315 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 316 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 317 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 318 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 319 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 320 | offset += testBlock.size(); |
| 321 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 322 | std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| 323 | sizeof(TEST_BUFFER) - offset); |
| 324 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 325 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 326 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 327 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 328 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 329 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 330 | offset += testBlock.size(); |
| 331 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 332 | std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| 333 | sizeof(TEST_BUFFER) - offset); |
| 334 | BOOST_CHECK(!isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 337 | BOOST_AUTO_TEST_CASE(FromStream) |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 338 | { |
| 339 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 340 | 0x01, 0x01, 0xfb, // ok |
| 341 | 0x03, 0x02, 0xff}; // not ok |
| 342 | |
| 343 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 344 | ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| 345 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 346 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 347 | Block testBlock; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 348 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 349 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 350 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 351 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 352 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 353 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 354 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 355 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 356 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 357 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 358 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 359 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 360 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 361 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 362 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 365 | BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728 |
| 366 | { |
| 367 | uint8_t PACKET[] = { |
| 368 | 0x06, 0x20, // Data |
| 369 | 0x07, 0x11, // Name |
| 370 | 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello' |
| 371 | 0x08, 0x01, 0x31, // NameComponent '1' |
| 372 | 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world' |
| 373 | 0x14, 0x00, // MetaInfo empty |
| 374 | 0x15, 0x00, // Content empty |
| 375 | 0x16, 0x05, // SignatureInfo |
| 376 | 0x1b, 0x01, 0x01, // SignatureType RSA |
| 377 | 0x1c, 0x00, // KeyLocator empty |
| 378 | 0x17, 0x00 // SignatureValue empty |
| 379 | }; |
| 380 | // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace |
| 381 | |
| 382 | std::stringstream stream; |
| 383 | stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET)); |
| 384 | stream.seekg(0); |
| 385 | |
| 386 | Block block = Block::fromStream(stream); |
| 387 | BOOST_CHECK_NO_THROW(block.parse()); |
| 388 | } |
| 389 | |
| 390 | BOOST_AUTO_TEST_CASE(FromStreamZeroLength) // Bug 2729 |
| 391 | { |
| 392 | uint8_t BUFFER[] = { 0x07, 0x00 }; // TLV-LENGTH is zero |
| 393 | |
| 394 | std::stringstream stream; |
| 395 | stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER)); |
| 396 | stream.seekg(0); |
| 397 | |
| 398 | Block block; |
| 399 | BOOST_CHECK_NO_THROW(block = Block::fromStream(stream)); |
| 400 | BOOST_CHECK_EQUAL(block.type(), 0x07); |
| 401 | BOOST_CHECK_EQUAL(block.value_size(), 0); |
| 402 | } |
| 403 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 404 | BOOST_AUTO_TEST_CASE(Equality) |
| 405 | { |
| 406 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>)); |
| 407 | |
| 408 | Block a("\x08\x00", 2); |
| 409 | Block b("\x08\x00", 2);; |
| 410 | BOOST_CHECK_EQUAL(a == b, true); |
| 411 | BOOST_CHECK_EQUAL(a != b, false); |
| 412 | |
| 413 | Block c("\x06\x00", 2); |
| 414 | Block d("\x08\x00", 2);; |
| 415 | BOOST_CHECK_EQUAL(c == d, false); |
| 416 | BOOST_CHECK_EQUAL(c != d, true); |
| 417 | |
| 418 | Block e("\x06\x00", 2); |
| 419 | Block f("\x06\x01\xcc", 3);; |
| 420 | BOOST_CHECK_EQUAL(e == f, false); |
| 421 | BOOST_CHECK_EQUAL(e != f, true); |
| 422 | } |
| 423 | |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 424 | BOOST_AUTO_TEST_CASE(InsertBeginning) |
| 425 | { |
| 426 | Block masterBlock(tlv::Name); |
| 427 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 428 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 429 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 430 | |
| 431 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 432 | masterBlock.push_back(secondBlock); |
| 433 | masterBlock.push_back(thirdBlock); |
| 434 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 435 | Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| 436 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 437 | |
| 438 | it = masterBlock.insert(it, firstBlock); |
| 439 | |
| 440 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 441 | BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true); |
| 442 | BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true); |
| 443 | } |
| 444 | |
| 445 | BOOST_AUTO_TEST_CASE(InsertEnd) |
| 446 | { |
| 447 | Block masterBlock(tlv::Name); |
| 448 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 449 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 450 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 451 | |
| 452 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 453 | masterBlock.push_back(firstBlock); |
| 454 | masterBlock.push_back(secondBlock); |
| 455 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 456 | Block::element_const_iterator it = masterBlock.elements_end(); |
| 457 | BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| 458 | |
| 459 | it = masterBlock.insert(it, thirdBlock); |
| 460 | |
| 461 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 462 | BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| 463 | BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true); |
| 464 | } |
| 465 | |
| 466 | BOOST_AUTO_TEST_CASE(InsertMiddle) |
| 467 | { |
| 468 | Block masterBlock(tlv::Name); |
| 469 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 470 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 471 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 472 | |
| 473 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 474 | masterBlock.push_back(firstBlock); |
| 475 | masterBlock.push_back(thirdBlock); |
| 476 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 477 | Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| 478 | BOOST_CHECK_EQUAL(*it == firstBlock, true); |
| 479 | |
| 480 | it = masterBlock.insert(it+1, secondBlock); |
| 481 | |
| 482 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 483 | BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true); |
| 484 | BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| 485 | } |
| 486 | |
| 487 | BOOST_AUTO_TEST_CASE(EraseSingleElement) |
| 488 | { |
| 489 | Block masterBlock(tlv::Name); |
| 490 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 491 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 492 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 493 | |
| 494 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 495 | masterBlock.push_back(firstBlock); |
| 496 | masterBlock.push_back(secondBlock); |
| 497 | masterBlock.push_back(thirdBlock); |
| 498 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 499 | Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| 500 | it++; |
| 501 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 502 | |
| 503 | it = masterBlock.erase(it); |
| 504 | |
| 505 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 506 | BOOST_CHECK_EQUAL(*(it) == thirdBlock, true); |
| 507 | BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| 508 | } |
| 509 | |
| 510 | BOOST_AUTO_TEST_CASE(EraseRange) |
| 511 | { |
| 512 | Block masterBlock(tlv::Name); |
| 513 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 514 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 515 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 516 | Block fourthBlock = makeStringBlock(tlv::NameComponent, "fourthName"); |
| 517 | Block fifthBlock = makeStringBlock(tlv::NameComponent, "fifthName"); |
| 518 | Block sixthBlock = makeStringBlock(tlv::NameComponent, "sixthName"); |
| 519 | |
| 520 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 521 | masterBlock.push_back(firstBlock); |
| 522 | masterBlock.push_back(secondBlock); |
| 523 | masterBlock.push_back(thirdBlock); |
| 524 | masterBlock.push_back(fourthBlock); |
| 525 | masterBlock.push_back(fifthBlock); |
| 526 | masterBlock.push_back(sixthBlock); |
| 527 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6); |
| 528 | Block::element_const_iterator itStart = masterBlock.find(tlv::NameComponent); |
| 529 | itStart++; |
| 530 | Block::element_const_iterator itEnd = itStart + 3; |
| 531 | BOOST_CHECK_EQUAL(*itStart == secondBlock, true); |
| 532 | BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true); |
| 533 | |
| 534 | Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd); |
| 535 | |
| 536 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 537 | BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true); |
| 538 | BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true); |
| 539 | } |
| 540 | |
Eric Newberry | a3d9fc0 | 2015-07-26 10:55:44 -0700 | [diff] [blame] | 541 | BOOST_AUTO_TEST_CASE(Remove) |
| 542 | { |
| 543 | Block block(tlv::Data); |
| 544 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0)); |
| 545 | block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123)); |
| 546 | block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix")); |
| 547 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2)); |
| 548 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1)); |
| 549 | |
| 550 | BOOST_CHECK_EQUAL(5, block.elements_size()); |
| 551 | BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType)); |
| 552 | BOOST_CHECK_EQUAL(2, block.elements_size()); |
| 553 | |
| 554 | Block::element_container elements = block.elements(); |
| 555 | |
| 556 | BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type()); |
| 557 | BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0])); |
| 558 | BOOST_CHECK_EQUAL(tlv::Name, elements[1].type()); |
| 559 | BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0); |
| 560 | } |
| 561 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 562 | BOOST_AUTO_TEST_SUITE_END() |
| 563 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 564 | } // namespace tests |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 565 | } // namespace ndn |