Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 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" |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 26 | #include <cstring> |
| 27 | #include <sstream> |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 28 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 29 | namespace ndn { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 30 | namespace tests { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 32 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 33 | BOOST_AUTO_TEST_SUITE(TestBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 34 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 35 | BOOST_AUTO_TEST_SUITE(Construction) |
| 36 | |
| 37 | static const uint8_t TEST_BUFFER[] = { |
| 38 | 0x00, 0x01, 0xfa, // ok |
| 39 | 0x01, 0x01, 0xfb, // ok |
| 40 | 0x03, 0x02, 0xff // bad: TLV-LENGTH is 2 but there's only 1-octet TLV-VALUE |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 41 | }; |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 42 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 43 | BOOST_AUTO_TEST_CASE(Empty) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 44 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 45 | Block b; |
| 46 | BOOST_CHECK_EQUAL(b.empty(), true); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 47 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 48 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 49 | BOOST_AUTO_TEST_CASE(FromEncodingBuffer) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 50 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 51 | const uint8_t VALUE[4] = {0x11, 0x12, 0x13, 0x14}; |
| 52 | |
| 53 | EncodingBuffer encoder; |
| 54 | size_t length = encoder.prependByteArray(VALUE, sizeof(VALUE)); |
| 55 | encoder.prependVarNumber(length); |
| 56 | encoder.prependVarNumber(0xe0); |
| 57 | |
| 58 | Block b = encoder.block(); |
| 59 | BOOST_CHECK_EQUAL(b.type(), 0xe0); |
| 60 | BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE)); |
| 61 | BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(), |
| 62 | VALUE, VALUE + sizeof(VALUE)); |
| 63 | |
| 64 | b = Block(encoder); |
| 65 | BOOST_CHECK_EQUAL(b.type(), 0xe0); |
| 66 | BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE)); |
| 67 | BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(), |
| 68 | VALUE, VALUE + sizeof(VALUE)); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 69 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 71 | BOOST_AUTO_TEST_CASE(FromEmptyEncodingBuffer) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 72 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 73 | EncodingBuffer encoder; |
| 74 | Block b; |
| 75 | BOOST_CHECK_THROW(b = Block(encoder), tlv::Error); |
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 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 78 | BOOST_AUTO_TEST_CASE(FromBlock) |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 79 | { |
| 80 | static uint8_t buffer[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01}; |
| 81 | Block block(buffer, sizeof(buffer)); |
| 82 | |
| 83 | Block derivedBlock(block, block.begin(), block.end()); |
| 84 | BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match |
| 85 | BOOST_CHECK(derivedBlock == block); // blocks should match |
| 86 | |
| 87 | derivedBlock = Block(block, block.begin() + 2, block.begin() + 5); |
| 88 | BOOST_CHECK(derivedBlock.begin() == block.begin() + 2); |
| 89 | BOOST_CHECK(derivedBlock == Block(buffer + 2, 3)); |
| 90 | |
| 91 | Buffer otherBuffer(buffer, sizeof(buffer)); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 92 | BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), std::invalid_argument); |
| 93 | BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), std::invalid_argument); |
| 94 | BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), std::invalid_argument); |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyOriginal) |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 98 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 99 | const uint8_t BUFFER[] = { |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 100 | 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07, |
| 101 | }; |
| 102 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 103 | Block b1(BUFFER, sizeof(BUFFER)); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 104 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 105 | Block b2(b1, b1.begin(), b1.end()); |
| 106 | auto buf2 = b2.getBuffer(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 108 | b1.parse(); |
| 109 | b1.remove(tlv::Name); |
| 110 | b1.encode(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 112 | b2.parse(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 114 | BOOST_CHECK_EQUAL_COLLECTIONS(b2.begin(), b2.end(), BUFFER, BUFFER + sizeof(BUFFER)); |
| 115 | BOOST_CHECK_EQUAL(buf2, b2.getBuffer()); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 118 | BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyCopy) |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 119 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 120 | const uint8_t BUFFER[] = { |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 121 | 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07, |
| 122 | }; |
| 123 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 124 | Block b1(BUFFER, sizeof(BUFFER)); |
| 125 | auto buf1 = b1.getBuffer(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 126 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 127 | Block b2(b1, b1.begin(), b1.end()); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 128 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 129 | b2.parse(); |
| 130 | b2.remove(tlv::Name); |
| 131 | b2.encode(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 132 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 133 | b1.parse(); |
| 134 | BOOST_CHECK_EQUAL_COLLECTIONS(b1.begin(), b1.end(), BUFFER, BUFFER + sizeof(BUFFER)); |
| 135 | BOOST_CHECK_EQUAL(buf1, b1.getBuffer()); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 138 | BOOST_AUTO_TEST_CASE(FromType) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 139 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 140 | Block b1(4); |
| 141 | BOOST_CHECK_EQUAL(b1.empty(), false); |
| 142 | BOOST_CHECK_EQUAL(b1.type(), 4); |
| 143 | BOOST_CHECK_EQUAL(b1.size(), 2); // 1-octet TLV-TYPE and 1-octet TLV-LENGTH |
| 144 | BOOST_CHECK_EQUAL(b1.value_size(), 0); |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 145 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 146 | Block b2(258); |
| 147 | BOOST_CHECK_EQUAL(b2.type(), 258); |
| 148 | BOOST_CHECK_EQUAL(b2.size(), 4); // 3-octet TLV-TYPE and 1-octet TLV-LENGTH |
| 149 | BOOST_CHECK_EQUAL(b2.value_size(), 0); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 152 | BOOST_AUTO_TEST_CASE(FromStream) |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 153 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 154 | std::stringstream stream; |
| 155 | stream.write(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| 156 | stream.seekg(0); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 158 | Block b = Block::fromStream(stream); |
| 159 | BOOST_CHECK_EQUAL(b.type(), 0); |
| 160 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 161 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
| 162 | BOOST_CHECK_EQUAL(*b.wire(), 0x00); |
| 163 | BOOST_CHECK_EQUAL(*b.value(), 0xfa); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 164 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 165 | b = Block::fromStream(stream); |
| 166 | BOOST_CHECK_EQUAL(b.type(), 1); |
| 167 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 168 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
| 169 | BOOST_CHECK_EQUAL(*b.wire(), 0x01); |
| 170 | BOOST_CHECK_EQUAL(*b.value(), 0xfb); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 171 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 172 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 175 | BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728 |
| 176 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 177 | const uint8_t PACKET[] = { |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 178 | 0x06, 0x20, // Data |
| 179 | 0x07, 0x11, // Name |
| 180 | 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello' |
| 181 | 0x08, 0x01, 0x31, // NameComponent '1' |
| 182 | 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world' |
| 183 | 0x14, 0x00, // MetaInfo empty |
| 184 | 0x15, 0x00, // Content empty |
| 185 | 0x16, 0x05, // SignatureInfo |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 186 | 0x1b, 0x01, 0x01, // SignatureType RSA |
| 187 | 0x1c, 0x00, // KeyLocator empty |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 188 | 0x17, 0x00 // SignatureValue empty |
| 189 | }; |
| 190 | // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace |
| 191 | |
| 192 | std::stringstream stream; |
| 193 | stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET)); |
| 194 | stream.seekg(0); |
| 195 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 196 | Block b = Block::fromStream(stream); |
| 197 | BOOST_CHECK_EQUAL(b.type(), 6); |
| 198 | BOOST_CHECK_EQUAL(b.value_size(), 32); |
| 199 | b.parse(); |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 202 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(FromStreamZeroLength, 1) |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 203 | BOOST_AUTO_TEST_CASE(FromStreamZeroLength) // Bug 2729 |
| 204 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 205 | const uint8_t BUFFER[] = {0x07, 0x00, // TLV-LENGTH is zero |
| 206 | 0x09, 0x01, 0x01}; |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 207 | |
| 208 | std::stringstream stream; |
| 209 | stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER)); |
| 210 | stream.seekg(0); |
| 211 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 212 | Block b = Block::fromStream(stream); |
| 213 | BOOST_CHECK_EQUAL(b.type(), 0x07); |
| 214 | BOOST_CHECK_EQUAL(b.value_size(), 0); |
| 215 | |
| 216 | BOOST_CHECK_NO_THROW(b = Block::fromStream(stream)); // expected failure Bug 4180 |
| 217 | } |
| 218 | |
| 219 | BOOST_AUTO_TEST_CASE(FromWireBuffer) |
| 220 | { |
| 221 | ConstBufferPtr buffer = make_shared<Buffer>(TEST_BUFFER, sizeof(TEST_BUFFER)); |
| 222 | |
| 223 | size_t offset = 0; |
| 224 | bool isOk = false; |
| 225 | Block b; |
| 226 | std::tie(isOk, b) = Block::fromBuffer(buffer, offset); |
| 227 | BOOST_CHECK(isOk); |
| 228 | BOOST_CHECK_EQUAL(b.type(), 0); |
| 229 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 230 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
| 231 | BOOST_CHECK_EQUAL(*b.wire(), 0x00); |
| 232 | BOOST_CHECK_EQUAL(*b.value(), 0xfa); |
| 233 | offset += b.size(); |
| 234 | |
| 235 | std::tie(isOk, b) = Block::fromBuffer(buffer, offset); |
| 236 | BOOST_CHECK(isOk); |
| 237 | BOOST_CHECK_EQUAL(b.type(), 1); |
| 238 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 239 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
| 240 | BOOST_CHECK_EQUAL(*b.wire(), 0x01); |
| 241 | BOOST_CHECK_EQUAL(*b.value(), 0xfb); |
| 242 | offset += b.size(); |
| 243 | |
| 244 | std::tie(isOk, b) = Block::fromBuffer(buffer, offset); |
| 245 | BOOST_CHECK(!isOk); |
| 246 | } |
| 247 | |
| 248 | BOOST_AUTO_TEST_CASE(FromRawBuffer) |
| 249 | { |
| 250 | size_t offset = 0; |
| 251 | bool isOk = false; |
| 252 | Block b; |
| 253 | std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset); |
| 254 | BOOST_CHECK(isOk); |
| 255 | BOOST_CHECK_EQUAL(b.type(), 0); |
| 256 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 257 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
| 258 | BOOST_CHECK_EQUAL(*b.wire(), 0x00); |
| 259 | BOOST_CHECK_EQUAL(*b.value(), 0xfa); |
| 260 | offset += b.size(); |
| 261 | |
| 262 | std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset); |
| 263 | BOOST_CHECK(isOk); |
| 264 | BOOST_CHECK_EQUAL(b.type(), 1); |
| 265 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 266 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
| 267 | BOOST_CHECK_EQUAL(*b.wire(), 0x01); |
| 268 | BOOST_CHECK_EQUAL(*b.value(), 0xfb); |
| 269 | offset += b.size(); |
| 270 | |
| 271 | std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset); |
| 272 | BOOST_CHECK(!isOk); |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 275 | BOOST_AUTO_TEST_SUITE_END() // Construction |
| 276 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 277 | BOOST_AUTO_TEST_SUITE(SubElements) |
| 278 | |
| 279 | BOOST_AUTO_TEST_CASE(Parse) |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 280 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 281 | const uint8_t PACKET[] = { |
| 282 | 0x06, 0x20, // Data |
| 283 | 0x07, 0x11, // Name |
| 284 | 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello' |
| 285 | 0x08, 0x01, 0x31, // NameComponent '1' |
| 286 | 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world' |
| 287 | 0x14, 0x00, // MetaInfo empty |
| 288 | 0x15, 0x00, // Content empty |
| 289 | 0x16, 0x05, // SignatureInfo |
| 290 | 0x1b, 0x01, 0x01, // SignatureType RSA |
| 291 | 0x1c, 0x00, // KeyLocator empty |
| 292 | 0x17, 0x00 // SignatureValue empty |
| 293 | }; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 294 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 295 | Block data(PACKET, sizeof(PACKET)); |
| 296 | data.parse(); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 297 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 298 | BOOST_CHECK_EQUAL(data.elements_size(), 5); |
| 299 | BOOST_CHECK_EQUAL(data.elements().at(0).type(), 0x07); |
| 300 | BOOST_CHECK_EQUAL(data.elements().at(0).elements().size(), 0); // parse is not recursive |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 301 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 302 | BOOST_CHECK(data.get(0x15) == data.elements().at(2)); |
| 303 | BOOST_CHECK_THROW(data.get(0x01), Block::Error); |
| 304 | |
| 305 | BOOST_CHECK(data.find(0x15) == data.elements_begin() + 2); |
| 306 | BOOST_CHECK(data.find(0x01) == data.elements_end()); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 309 | BOOST_AUTO_TEST_CASE(InsertBeginning) |
| 310 | { |
| 311 | Block masterBlock(tlv::Name); |
| 312 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 313 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 314 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 315 | |
| 316 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 317 | masterBlock.push_back(secondBlock); |
| 318 | masterBlock.push_back(thirdBlock); |
| 319 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 320 | Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| 321 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 322 | |
| 323 | it = masterBlock.insert(it, firstBlock); |
| 324 | |
| 325 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 326 | BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true); |
| 327 | BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true); |
| 328 | } |
| 329 | |
| 330 | BOOST_AUTO_TEST_CASE(InsertEnd) |
| 331 | { |
| 332 | Block masterBlock(tlv::Name); |
| 333 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 334 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 335 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 336 | |
| 337 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 338 | masterBlock.push_back(firstBlock); |
| 339 | masterBlock.push_back(secondBlock); |
| 340 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 341 | Block::element_const_iterator it = masterBlock.elements_end(); |
| 342 | BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| 343 | |
| 344 | it = masterBlock.insert(it, thirdBlock); |
| 345 | |
| 346 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 347 | BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| 348 | BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true); |
| 349 | } |
| 350 | |
| 351 | BOOST_AUTO_TEST_CASE(InsertMiddle) |
| 352 | { |
| 353 | Block masterBlock(tlv::Name); |
| 354 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 355 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 356 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 357 | |
| 358 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 359 | masterBlock.push_back(firstBlock); |
| 360 | masterBlock.push_back(thirdBlock); |
| 361 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 362 | Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| 363 | BOOST_CHECK_EQUAL(*it == firstBlock, true); |
| 364 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 365 | it = masterBlock.insert(it + 1, secondBlock); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 366 | |
| 367 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 368 | BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true); |
| 369 | BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| 370 | } |
| 371 | |
| 372 | BOOST_AUTO_TEST_CASE(EraseSingleElement) |
| 373 | { |
| 374 | Block masterBlock(tlv::Name); |
| 375 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 376 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 377 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 378 | |
| 379 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 380 | masterBlock.push_back(firstBlock); |
| 381 | masterBlock.push_back(secondBlock); |
| 382 | masterBlock.push_back(thirdBlock); |
| 383 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 384 | Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| 385 | it++; |
| 386 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 387 | |
| 388 | it = masterBlock.erase(it); |
| 389 | |
| 390 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 391 | BOOST_CHECK_EQUAL(*(it) == thirdBlock, true); |
| 392 | BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| 393 | } |
| 394 | |
| 395 | BOOST_AUTO_TEST_CASE(EraseRange) |
| 396 | { |
| 397 | Block masterBlock(tlv::Name); |
| 398 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 399 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 400 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 401 | Block fourthBlock = makeStringBlock(tlv::NameComponent, "fourthName"); |
| 402 | Block fifthBlock = makeStringBlock(tlv::NameComponent, "fifthName"); |
| 403 | Block sixthBlock = makeStringBlock(tlv::NameComponent, "sixthName"); |
| 404 | |
| 405 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 406 | masterBlock.push_back(firstBlock); |
| 407 | masterBlock.push_back(secondBlock); |
| 408 | masterBlock.push_back(thirdBlock); |
| 409 | masterBlock.push_back(fourthBlock); |
| 410 | masterBlock.push_back(fifthBlock); |
| 411 | masterBlock.push_back(sixthBlock); |
| 412 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6); |
| 413 | Block::element_const_iterator itStart = masterBlock.find(tlv::NameComponent); |
| 414 | itStart++; |
| 415 | Block::element_const_iterator itEnd = itStart + 3; |
| 416 | BOOST_CHECK_EQUAL(*itStart == secondBlock, true); |
| 417 | BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true); |
| 418 | |
| 419 | Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd); |
| 420 | |
| 421 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 422 | BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true); |
| 423 | BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true); |
| 424 | } |
| 425 | |
Eric Newberry | a3d9fc0 | 2015-07-26 10:55:44 -0700 | [diff] [blame] | 426 | BOOST_AUTO_TEST_CASE(Remove) |
| 427 | { |
| 428 | Block block(tlv::Data); |
| 429 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0)); |
| 430 | block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123)); |
| 431 | block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix")); |
| 432 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2)); |
| 433 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1)); |
| 434 | |
| 435 | BOOST_CHECK_EQUAL(5, block.elements_size()); |
| 436 | BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType)); |
| 437 | BOOST_CHECK_EQUAL(2, block.elements_size()); |
| 438 | |
| 439 | Block::element_container elements = block.elements(); |
| 440 | |
| 441 | BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type()); |
| 442 | BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0])); |
| 443 | BOOST_CHECK_EQUAL(tlv::Name, elements[1].type()); |
| 444 | BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0); |
| 445 | } |
| 446 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame^] | 447 | BOOST_AUTO_TEST_SUITE_END() // SubElements |
| 448 | |
| 449 | BOOST_AUTO_TEST_CASE(Equality) |
| 450 | { |
| 451 | Block a("\x08\x00", 2); |
| 452 | Block b("\x08\x00", 2);; |
| 453 | BOOST_CHECK_EQUAL(a == b, true); |
| 454 | BOOST_CHECK_EQUAL(a != b, false); |
| 455 | |
| 456 | Block c("\x06\x00", 2); |
| 457 | Block d("\x08\x00", 2);; |
| 458 | BOOST_CHECK_EQUAL(c == d, false); |
| 459 | BOOST_CHECK_EQUAL(c != d, true); |
| 460 | |
| 461 | Block e("\x06\x00", 2); |
| 462 | Block f("\x06\x01\xcc", 3);; |
| 463 | BOOST_CHECK_EQUAL(e == f, false); |
| 464 | BOOST_CHECK_EQUAL(e != f, true); |
| 465 | } |
| 466 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 467 | BOOST_AUTO_TEST_SUITE_END() // TestBlock |
| 468 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 469 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 470 | } // namespace tests |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 471 | } // namespace ndn |