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