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 | /* |
Davide Pesavento | 46cdf90 | 2020-06-08 16:38:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/encoding/block.hpp" |
| 23 | #include "ndn-cxx/encoding/block-helpers.hpp" |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
| 26 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 28 | #include <boost/test/data/test_case.hpp> |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 29 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 30 | #include <cstring> |
| 31 | #include <sstream> |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 32 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 33 | namespace ndn { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 34 | namespace tests { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 37 | BOOST_AUTO_TEST_SUITE(TestBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 38 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(Construction) |
| 40 | |
| 41 | static const uint8_t TEST_BUFFER[] = { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 42 | 0x42, 0x01, 0xfa, |
| 43 | 0x01, 0x01, 0xfb, |
| 44 | 0xfe, 0xff, 0xff, 0xff, 0xff, 0x00, // bug #4726 |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 45 | }; |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 46 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 47 | BOOST_AUTO_TEST_CASE(Default) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 48 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 49 | Block b; |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 50 | |
| 51 | BOOST_CHECK_EQUAL(b.isValid(), false); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(b.type(), tlv::Invalid); |
| 53 | BOOST_CHECK_EQUAL(b.hasValue(), false); |
| 54 | BOOST_CHECK_EQUAL(b.value_size(), 0); |
| 55 | BOOST_CHECK(b.value() == nullptr); |
| 56 | |
| 57 | BOOST_CHECK_THROW(b.size(), Block::Error); |
| 58 | BOOST_CHECK_THROW(b.begin(), Block::Error); |
| 59 | BOOST_CHECK_THROW(b.end(), Block::Error); |
| 60 | BOOST_CHECK_THROW(b.wire(), Block::Error); |
| 61 | BOOST_CHECK_THROW(b.blockFromValue(), Block::Error); |
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 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 64 | BOOST_AUTO_TEST_CASE(FromEncodingBuffer) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 65 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 66 | EncodingBuffer encoder; |
Davide Pesavento | 5c803b9 | 2019-02-02 19:23:48 -0500 | [diff] [blame] | 67 | BOOST_CHECK_THROW(Block{encoder}, tlv::Error); |
| 68 | |
| 69 | const uint8_t VALUE[] = {0x11, 0x12, 0x13, 0x14}; |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 70 | size_t length = encoder.prependByteArray(VALUE, sizeof(VALUE)); |
| 71 | encoder.prependVarNumber(length); |
| 72 | encoder.prependVarNumber(0xe0); |
| 73 | |
| 74 | Block b = encoder.block(); |
| 75 | BOOST_CHECK_EQUAL(b.type(), 0xe0); |
| 76 | BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE)); |
| 77 | BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(), |
| 78 | VALUE, VALUE + sizeof(VALUE)); |
| 79 | |
| 80 | b = Block(encoder); |
| 81 | BOOST_CHECK_EQUAL(b.type(), 0xe0); |
| 82 | BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE)); |
| 83 | BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(), |
| 84 | VALUE, VALUE + sizeof(VALUE)); |
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 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(FromBlock) |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 88 | { |
Davide Pesavento | 5c803b9 | 2019-02-02 19:23:48 -0500 | [diff] [blame] | 89 | const uint8_t BUFFER[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01}; |
| 90 | Block block(BUFFER, sizeof(BUFFER)); |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 91 | |
| 92 | Block derivedBlock(block, block.begin(), block.end()); |
| 93 | BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match |
| 94 | BOOST_CHECK(derivedBlock == block); // blocks should match |
| 95 | |
| 96 | derivedBlock = Block(block, block.begin() + 2, block.begin() + 5); |
| 97 | BOOST_CHECK(derivedBlock.begin() == block.begin() + 2); |
Davide Pesavento | 5c803b9 | 2019-02-02 19:23:48 -0500 | [diff] [blame] | 98 | BOOST_CHECK(derivedBlock == Block(BUFFER + 2, 3)); |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 99 | |
Davide Pesavento | 5c803b9 | 2019-02-02 19:23:48 -0500 | [diff] [blame] | 100 | Buffer otherBuffer(BUFFER, sizeof(BUFFER)); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 101 | BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), std::invalid_argument); |
| 102 | BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), std::invalid_argument); |
| 103 | BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), std::invalid_argument); |
Alexander Afanasyev | 4448d29 | 2015-08-09 20:11:37 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 106 | BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyOriginal) |
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 | const uint8_t BUFFER[] = { |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 109 | 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07, |
| 110 | }; |
| 111 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 112 | Block b1(BUFFER, sizeof(BUFFER)); |
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 | Block b2(b1, b1.begin(), b1.end()); |
| 115 | auto buf2 = b2.getBuffer(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 117 | b1.parse(); |
| 118 | b1.remove(tlv::Name); |
| 119 | b1.encode(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 120 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 121 | b2.parse(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 122 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL_COLLECTIONS(b2.begin(), b2.end(), BUFFER, BUFFER + sizeof(BUFFER)); |
| 124 | BOOST_CHECK_EQUAL(buf2, b2.getBuffer()); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 127 | BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyCopy) |
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 | const uint8_t BUFFER[] = { |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 130 | 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07, |
| 131 | }; |
| 132 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 133 | Block b1(BUFFER, sizeof(BUFFER)); |
| 134 | auto buf1 = b1.getBuffer(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 135 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 136 | Block b2(b1, b1.begin(), b1.end()); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 138 | b2.parse(); |
| 139 | b2.remove(tlv::Name); |
| 140 | b2.encode(); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 141 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 142 | b1.parse(); |
| 143 | BOOST_CHECK_EQUAL_COLLECTIONS(b1.begin(), b1.end(), BUFFER, BUFFER + sizeof(BUFFER)); |
| 144 | BOOST_CHECK_EQUAL(buf1, b1.getBuffer()); |
Eric Newberry | 6235aac | 2015-08-13 16:03:47 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 147 | BOOST_AUTO_TEST_CASE(FromType) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 148 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 149 | Block b1(4); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 150 | BOOST_CHECK_EQUAL(b1.isValid(), true); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 151 | BOOST_CHECK_EQUAL(b1.type(), 4); |
| 152 | BOOST_CHECK_EQUAL(b1.size(), 2); // 1-octet TLV-TYPE and 1-octet TLV-LENGTH |
Davide Pesavento | 5c803b9 | 2019-02-02 19:23:48 -0500 | [diff] [blame] | 153 | BOOST_CHECK_EQUAL(b1.hasValue(), false); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 154 | BOOST_CHECK_EQUAL(b1.value_size(), 0); |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 155 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 156 | Block b2(258); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL(b2.isValid(), true); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(b2.type(), 258); |
| 159 | BOOST_CHECK_EQUAL(b2.size(), 4); // 3-octet TLV-TYPE and 1-octet TLV-LENGTH |
Davide Pesavento | 5c803b9 | 2019-02-02 19:23:48 -0500 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(b2.hasValue(), false); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(b2.value_size(), 0); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 162 | |
| 163 | Block b3(tlv::Invalid); |
| 164 | BOOST_CHECK_EQUAL(b3.isValid(), false); |
| 165 | BOOST_CHECK_EQUAL(b3.type(), tlv::Invalid); |
| 166 | BOOST_CHECK_EQUAL(b3.hasValue(), false); |
| 167 | BOOST_CHECK_EQUAL(b3.value_size(), 0); |
| 168 | BOOST_CHECK(b3.value() == nullptr); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Davide Pesavento | 5c803b9 | 2019-02-02 19:23:48 -0500 | [diff] [blame] | 171 | BOOST_AUTO_TEST_CASE(FromTypeAndBuffer) |
| 172 | { |
| 173 | const uint8_t VALUE[] = {0x11, 0x12, 0x13, 0x14}; |
| 174 | auto bufferPtr = make_shared<Buffer>(VALUE, sizeof(VALUE)); |
| 175 | |
| 176 | Block b(42, bufferPtr); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(b.isValid(), true); |
Davide Pesavento | 5c803b9 | 2019-02-02 19:23:48 -0500 | [diff] [blame] | 178 | BOOST_CHECK_EQUAL(b.type(), 42); |
| 179 | BOOST_CHECK_EQUAL(b.size(), 6); |
| 180 | BOOST_CHECK_EQUAL(b.hasValue(), true); |
| 181 | BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE)); |
| 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE(FromTypeAndBlock) |
| 185 | { |
| 186 | const uint8_t BUFFER[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01}; |
| 187 | Block nested(BUFFER, sizeof(BUFFER)); |
| 188 | |
| 189 | Block b(84, nested); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(b.isValid(), true); |
Davide Pesavento | 5c803b9 | 2019-02-02 19:23:48 -0500 | [diff] [blame] | 191 | BOOST_CHECK_EQUAL(b.type(), 84); |
| 192 | BOOST_CHECK_EQUAL(b.size(), 10); |
| 193 | BOOST_CHECK_EQUAL(b.hasValue(), true); |
| 194 | BOOST_CHECK_EQUAL(b.value_size(), sizeof(BUFFER)); |
| 195 | } |
| 196 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 197 | BOOST_AUTO_TEST_CASE(FromStream) |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 198 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 199 | std::stringstream stream; |
| 200 | stream.write(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| 201 | stream.seekg(0); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 202 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 203 | Block b = Block::fromStream(stream); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 204 | BOOST_CHECK_EQUAL(b.type(), 66); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 205 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 206 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(*b.wire(), 0x42); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 208 | BOOST_CHECK_EQUAL(*b.value(), 0xfa); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 209 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 210 | b = Block::fromStream(stream); |
| 211 | BOOST_CHECK_EQUAL(b.type(), 1); |
| 212 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 213 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
| 214 | BOOST_CHECK_EQUAL(*b.wire(), 0x01); |
| 215 | BOOST_CHECK_EQUAL(*b.value(), 0xfb); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 216 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 217 | b = Block::fromStream(stream); |
| 218 | BOOST_CHECK_EQUAL(b.type(), 0xffffffff); |
| 219 | BOOST_CHECK_EQUAL(b.size(), 6); |
| 220 | BOOST_CHECK_EQUAL(b.value_size(), 0); |
| 221 | BOOST_CHECK_EQUAL(*b.wire(), 0xfe); |
| 222 | |
| 223 | BOOST_CHECK(stream.eof()); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 224 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 227 | BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728 |
| 228 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 229 | const uint8_t PACKET[] = { |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 230 | 0x06, 0x20, // Data |
| 231 | 0x07, 0x11, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 232 | 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello' |
| 233 | 0x08, 0x01, 0x31, // GenericNameComponent '1' |
| 234 | 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world' |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 235 | 0x14, 0x00, // MetaInfo empty |
| 236 | 0x15, 0x00, // Content empty |
| 237 | 0x16, 0x05, // SignatureInfo |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 238 | 0x1b, 0x01, 0x01, // SignatureType RSA |
| 239 | 0x1c, 0x00, // KeyLocator empty |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 240 | 0x17, 0x00 // SignatureValue empty |
| 241 | }; |
| 242 | // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace |
| 243 | |
| 244 | std::stringstream stream; |
| 245 | stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET)); |
| 246 | stream.seekg(0); |
| 247 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 248 | Block b = Block::fromStream(stream); |
| 249 | BOOST_CHECK_EQUAL(b.type(), 6); |
| 250 | BOOST_CHECK_EQUAL(b.value_size(), 32); |
| 251 | b.parse(); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 252 | BOOST_CHECK_EQUAL(b.elements_size(), 5); |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 255 | BOOST_AUTO_TEST_CASE(FromStreamZeroLength) |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 256 | { |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 257 | const uint8_t BUFFER[] = {0x70, 0x00, |
| 258 | 0x71, 0x03, 0x86, 0x11, 0x24, |
| 259 | 0x72, 0x00}; |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 260 | |
| 261 | std::stringstream stream; |
| 262 | stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER)); |
| 263 | stream.seekg(0); |
| 264 | |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 265 | Block b1 = Block::fromStream(stream); |
| 266 | BOOST_CHECK_EQUAL(b1.type(), 0x70); |
| 267 | BOOST_CHECK_EQUAL(b1.value_size(), 0); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 268 | |
Junxiao Shi | 760cc7b | 2017-07-22 19:17:49 +0000 | [diff] [blame] | 269 | Block b2 = Block::fromStream(stream); |
| 270 | BOOST_CHECK_EQUAL(b2.type(), 0x71); |
| 271 | BOOST_CHECK_EQUAL(b2.value_size(), 3); |
| 272 | const uint8_t EXPECTED_VALUE2[] = {0x86, 0x11, 0x24}; |
| 273 | BOOST_CHECK_EQUAL_COLLECTIONS(b2.value_begin(), b2.value_end(), |
| 274 | EXPECTED_VALUE2, EXPECTED_VALUE2 + sizeof(EXPECTED_VALUE2)); |
| 275 | |
| 276 | Block b3 = Block::fromStream(stream); |
| 277 | BOOST_CHECK_EQUAL(b3.type(), 0x72); |
| 278 | BOOST_CHECK_EQUAL(b3.value_size(), 0); |
| 279 | |
| 280 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
| 281 | } |
| 282 | |
| 283 | BOOST_AUTO_TEST_CASE(FromStreamPacketTooLarge) |
| 284 | { |
| 285 | const uint8_t BUFFER[] = {0x07, 0xfe, 0x00, 0x01, 0x00, 0x00}; |
| 286 | |
| 287 | std::stringstream stream; |
| 288 | stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER)); |
| 289 | for (int i = 0; i < 0x10000; ++i) { |
| 290 | stream.put('\0'); |
| 291 | } |
| 292 | stream.seekg(0); |
| 293 | |
| 294 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | BOOST_AUTO_TEST_CASE(FromWireBuffer) |
| 298 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 299 | auto buffer = make_shared<Buffer>(TEST_BUFFER, sizeof(TEST_BUFFER)); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 300 | |
| 301 | size_t offset = 0; |
| 302 | bool isOk = false; |
| 303 | Block b; |
| 304 | std::tie(isOk, b) = Block::fromBuffer(buffer, offset); |
| 305 | BOOST_CHECK(isOk); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 306 | BOOST_CHECK_EQUAL(b.type(), 66); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 307 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 308 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 309 | BOOST_CHECK_EQUAL(*b.wire(), 0x42); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 310 | BOOST_CHECK_EQUAL(*b.value(), 0xfa); |
| 311 | offset += b.size(); |
| 312 | |
| 313 | std::tie(isOk, b) = Block::fromBuffer(buffer, offset); |
| 314 | BOOST_CHECK(isOk); |
| 315 | BOOST_CHECK_EQUAL(b.type(), 1); |
| 316 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 317 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
| 318 | BOOST_CHECK_EQUAL(*b.wire(), 0x01); |
| 319 | BOOST_CHECK_EQUAL(*b.value(), 0xfb); |
| 320 | offset += b.size(); |
| 321 | |
| 322 | std::tie(isOk, b) = Block::fromBuffer(buffer, offset); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 323 | BOOST_CHECK(isOk); |
| 324 | BOOST_CHECK_EQUAL(b.type(), 0xffffffff); |
| 325 | BOOST_CHECK_EQUAL(b.size(), 6); |
| 326 | BOOST_CHECK_EQUAL(b.value_size(), 0); |
| 327 | BOOST_CHECK_EQUAL(*b.wire(), 0xfe); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | BOOST_AUTO_TEST_CASE(FromRawBuffer) |
| 331 | { |
| 332 | size_t offset = 0; |
| 333 | bool isOk = false; |
| 334 | Block b; |
| 335 | std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset); |
| 336 | BOOST_CHECK(isOk); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 337 | BOOST_CHECK_EQUAL(b.type(), 66); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 338 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 339 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 340 | BOOST_CHECK_EQUAL(*b.wire(), 0x42); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 341 | BOOST_CHECK_EQUAL(*b.value(), 0xfa); |
| 342 | offset += b.size(); |
| 343 | |
| 344 | std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset); |
| 345 | BOOST_CHECK(isOk); |
| 346 | BOOST_CHECK_EQUAL(b.type(), 1); |
| 347 | BOOST_CHECK_EQUAL(b.size(), 3); |
| 348 | BOOST_CHECK_EQUAL(b.value_size(), 1); |
| 349 | BOOST_CHECK_EQUAL(*b.wire(), 0x01); |
| 350 | BOOST_CHECK_EQUAL(*b.value(), 0xfb); |
| 351 | offset += b.size(); |
| 352 | |
| 353 | std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 354 | BOOST_CHECK(isOk); |
| 355 | BOOST_CHECK_EQUAL(b.type(), 0xffffffff); |
| 356 | BOOST_CHECK_EQUAL(b.size(), 6); |
| 357 | BOOST_CHECK_EQUAL(b.value_size(), 0); |
| 358 | BOOST_CHECK_EQUAL(*b.wire(), 0xfe); |
| 359 | } |
| 360 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 361 | static const Buffer MalformedInputs[] = { |
| 362 | {0x00, 0x00}, // invalid TLV type (zero) |
| 363 | {0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}, // TLV type too large |
| 364 | {0x01, 0xff, 0x42, 0x42}, // bad TLV length |
| 365 | {0x01, 0x02, 0x03}, // truncated TLV value |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 366 | }; |
| 367 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 368 | BOOST_DATA_TEST_CASE(Malformed, MalformedInputs) |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 369 | { |
| 370 | // constructor from raw buffer |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 371 | BOOST_CHECK_THROW(Block(sample.data(), sample.size()), tlv::Error); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 372 | |
| 373 | // fromStream() |
| 374 | std::stringstream stream; |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 375 | stream.write(sample.template get<char>(), sample.size()); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 376 | stream.seekg(0); |
| 377 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
| 378 | |
| 379 | // fromBuffer(), ConstBufferPtr overload |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 380 | auto buf = make_shared<Buffer>(sample.begin(), sample.end()); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 381 | bool isOk; |
| 382 | Block b; |
| 383 | std::tie(isOk, b) = Block::fromBuffer(buf, 0); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 384 | BOOST_CHECK(!isOk); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 385 | BOOST_CHECK(!b.isValid()); |
| 386 | |
| 387 | // fromBuffer(), raw buffer overload |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 388 | std::tie(isOk, b) = Block::fromBuffer(sample.data(), sample.size()); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 389 | BOOST_CHECK(!isOk); |
| 390 | BOOST_CHECK(!b.isValid()); |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 393 | BOOST_AUTO_TEST_SUITE_END() // Construction |
| 394 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 395 | BOOST_AUTO_TEST_SUITE(SubElements) |
| 396 | |
| 397 | BOOST_AUTO_TEST_CASE(Parse) |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 398 | { |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 399 | const uint8_t PACKET[] = { |
| 400 | 0x06, 0x20, // Data |
| 401 | 0x07, 0x11, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 402 | 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello' |
| 403 | 0x08, 0x01, 0x31, // GenericNameComponent '1' |
| 404 | 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world' |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 405 | 0x14, 0x00, // MetaInfo empty |
| 406 | 0x15, 0x00, // Content empty |
| 407 | 0x16, 0x05, // SignatureInfo |
| 408 | 0x1b, 0x01, 0x01, // SignatureType RSA |
| 409 | 0x1c, 0x00, // KeyLocator empty |
| 410 | 0x17, 0x00 // SignatureValue empty |
| 411 | }; |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 412 | Block data(PACKET, sizeof(PACKET)); |
| 413 | data.parse(); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 414 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 415 | BOOST_CHECK_EQUAL(data.elements_size(), 5); |
| 416 | BOOST_CHECK_EQUAL(data.elements().at(0).type(), 0x07); |
| 417 | 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] | 418 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 419 | BOOST_CHECK(data.get(0x15) == data.elements().at(2)); |
| 420 | BOOST_CHECK_THROW(data.get(0x01), Block::Error); |
| 421 | |
| 422 | BOOST_CHECK(data.find(0x15) == data.elements_begin() + 2); |
| 423 | BOOST_CHECK(data.find(0x01) == data.elements_end()); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 424 | |
| 425 | const uint8_t MALFORMED[] = { |
| 426 | // TLV-LENGTH of nested element is greater than TLV-LENGTH of enclosing element |
| 427 | 0x05, 0x05, 0x07, 0x07, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f |
| 428 | }; |
| 429 | Block bad(MALFORMED, sizeof(MALFORMED)); |
| 430 | BOOST_CHECK_THROW(bad.parse(), Block::Error); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 433 | BOOST_AUTO_TEST_CASE(InsertBeginning) |
| 434 | { |
| 435 | Block masterBlock(tlv::Name); |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 436 | Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName"); |
| 437 | Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName"); |
| 438 | Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName"); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 439 | |
| 440 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 441 | masterBlock.push_back(secondBlock); |
| 442 | masterBlock.push_back(thirdBlock); |
| 443 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 444 | Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 445 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 446 | |
| 447 | it = masterBlock.insert(it, firstBlock); |
| 448 | |
| 449 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 450 | BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true); |
| 451 | BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true); |
| 452 | } |
| 453 | |
| 454 | BOOST_AUTO_TEST_CASE(InsertEnd) |
| 455 | { |
| 456 | Block masterBlock(tlv::Name); |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 457 | Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName"); |
| 458 | Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName"); |
| 459 | Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName"); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 460 | |
| 461 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 462 | masterBlock.push_back(firstBlock); |
| 463 | masterBlock.push_back(secondBlock); |
| 464 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 465 | Block::element_const_iterator it = masterBlock.elements_end(); |
| 466 | BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| 467 | |
| 468 | it = masterBlock.insert(it, thirdBlock); |
| 469 | |
| 470 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 471 | BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| 472 | BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true); |
| 473 | } |
| 474 | |
| 475 | BOOST_AUTO_TEST_CASE(InsertMiddle) |
| 476 | { |
| 477 | Block masterBlock(tlv::Name); |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 478 | Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName"); |
| 479 | Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName"); |
| 480 | Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName"); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 481 | |
| 482 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 483 | masterBlock.push_back(firstBlock); |
| 484 | masterBlock.push_back(thirdBlock); |
| 485 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 486 | Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 487 | BOOST_CHECK_EQUAL(*it == firstBlock, true); |
| 488 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 489 | it = masterBlock.insert(it + 1, secondBlock); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 490 | |
| 491 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 492 | BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true); |
| 493 | BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| 494 | } |
| 495 | |
| 496 | BOOST_AUTO_TEST_CASE(EraseSingleElement) |
| 497 | { |
| 498 | Block masterBlock(tlv::Name); |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 499 | Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName"); |
| 500 | Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName"); |
| 501 | Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName"); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 502 | |
| 503 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 504 | masterBlock.push_back(firstBlock); |
| 505 | masterBlock.push_back(secondBlock); |
| 506 | masterBlock.push_back(thirdBlock); |
| 507 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 508 | Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 509 | it++; |
| 510 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 511 | |
| 512 | it = masterBlock.erase(it); |
| 513 | |
| 514 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 515 | BOOST_CHECK_EQUAL(*(it) == thirdBlock, true); |
| 516 | BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| 517 | } |
| 518 | |
| 519 | BOOST_AUTO_TEST_CASE(EraseRange) |
| 520 | { |
| 521 | Block masterBlock(tlv::Name); |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 522 | Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName"); |
| 523 | Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName"); |
| 524 | Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName"); |
| 525 | Block fourthBlock = makeStringBlock(tlv::GenericNameComponent, "fourthName"); |
| 526 | Block fifthBlock = makeStringBlock(tlv::GenericNameComponent, "fifthName"); |
| 527 | Block sixthBlock = makeStringBlock(tlv::GenericNameComponent, "sixthName"); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 528 | |
| 529 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 530 | masterBlock.push_back(firstBlock); |
| 531 | masterBlock.push_back(secondBlock); |
| 532 | masterBlock.push_back(thirdBlock); |
| 533 | masterBlock.push_back(fourthBlock); |
| 534 | masterBlock.push_back(fifthBlock); |
| 535 | masterBlock.push_back(sixthBlock); |
| 536 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6); |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 537 | Block::element_const_iterator itStart = masterBlock.find(tlv::GenericNameComponent); |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 538 | itStart++; |
| 539 | Block::element_const_iterator itEnd = itStart + 3; |
| 540 | BOOST_CHECK_EQUAL(*itStart == secondBlock, true); |
| 541 | BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true); |
| 542 | |
| 543 | Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd); |
| 544 | |
| 545 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 546 | BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true); |
| 547 | BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true); |
| 548 | } |
| 549 | |
Eric Newberry | a3d9fc0 | 2015-07-26 10:55:44 -0700 | [diff] [blame] | 550 | BOOST_AUTO_TEST_CASE(Remove) |
| 551 | { |
| 552 | Block block(tlv::Data); |
| 553 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0)); |
| 554 | block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123)); |
| 555 | block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix")); |
| 556 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2)); |
| 557 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1)); |
| 558 | |
| 559 | BOOST_CHECK_EQUAL(5, block.elements_size()); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 560 | BOOST_CHECK_NO_THROW(block.remove(tlv::ContentType)); |
| 561 | BOOST_REQUIRE_EQUAL(2, block.elements_size()); |
Eric Newberry | a3d9fc0 | 2015-07-26 10:55:44 -0700 | [diff] [blame] | 562 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 563 | auto elements = block.elements(); |
Eric Newberry | a3d9fc0 | 2015-07-26 10:55:44 -0700 | [diff] [blame] | 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()); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 567 | BOOST_CHECK_EQUAL(readString(elements[1]).compare("ndn:/test-prefix"), 0); |
Eric Newberry | a3d9fc0 | 2015-07-26 10:55:44 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 570 | BOOST_AUTO_TEST_SUITE_END() // SubElements |
| 571 | |
| 572 | BOOST_AUTO_TEST_CASE(Equality) |
| 573 | { |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 574 | const uint8_t one[] = {0x08, 0x00}; |
| 575 | Block a(one, sizeof(one)); |
| 576 | Block b(one, sizeof(one)); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 577 | BOOST_CHECK_EQUAL(a == b, true); |
| 578 | BOOST_CHECK_EQUAL(a != b, false); |
| 579 | |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 580 | const uint8_t two[] = {0x06, 0x00}; |
| 581 | Block c(two, sizeof(two)); |
| 582 | Block d(one, sizeof(one)); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 583 | BOOST_CHECK_EQUAL(c == d, false); |
| 584 | BOOST_CHECK_EQUAL(c != d, true); |
| 585 | |
Davide Pesavento | b10024c | 2017-09-22 01:36:44 -0400 | [diff] [blame] | 586 | const uint8_t three[] = {0x06, 0x01, 0xcc}; |
| 587 | Block e(two, sizeof(two)); |
| 588 | Block f(three, sizeof(three)); |
Junxiao Shi | dc4277a | 2017-07-17 11:34:02 +0000 | [diff] [blame] | 589 | BOOST_CHECK_EQUAL(e == f, false); |
| 590 | BOOST_CHECK_EQUAL(e != f, true); |
| 591 | } |
| 592 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 593 | BOOST_AUTO_TEST_CASE(Print) |
| 594 | { |
| 595 | // default constructed |
| 596 | Block b; |
| 597 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "[invalid]"); |
| 598 | |
| 599 | // zero length |
| 600 | b = "0700"_block; |
| 601 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "7[empty]"); |
| 602 | |
| 603 | // unparsed |
| 604 | b = "0E10FF7E4E6B3B21C902660F16ED589FCCCC"_block; |
| 605 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), |
| 606 | "14[16]=FF7E4E6B3B21C902660F16ED589FCCCC"); |
| 607 | // set and restore format flags |
| 608 | { |
| 609 | std::ostringstream oss; |
| 610 | oss << std::showbase << std::hex << 0xd23c4 << b << 0x4981e; |
| 611 | BOOST_CHECK_EQUAL(oss.str(), "0xd23c414[16]=FF7E4E6B3B21C902660F16ED589FCCCC0x4981e"); |
| 612 | } |
| 613 | |
| 614 | // parsed |
| 615 | b = "FD010808 0502CADD 59024E42"_block; |
| 616 | b.parse(); |
| 617 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), |
| 618 | "264[8]={5[2]=CADD,89[2]=4E42}"); |
| 619 | |
| 620 | // parsed then modified: print modified sub-elements |
| 621 | b = "FD010808 0502CADD 59024E42"_block; |
| 622 | b.parse(); |
| 623 | b.erase(b.elements_begin()); |
| 624 | b.push_back("10022386"_block); |
| 625 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), |
| 626 | "264[8]={89[2]=4E42,16[2]=2386}"); |
| 627 | } |
| 628 | |
Junxiao Shi | 2b322eb | 2018-08-24 10:43:27 -0600 | [diff] [blame] | 629 | BOOST_AUTO_TEST_SUITE(BlockLiteral) |
| 630 | |
| 631 | BOOST_AUTO_TEST_CASE(Simple) |
| 632 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 633 | Block b0 = "4200"_block; |
| 634 | BOOST_CHECK_EQUAL(b0.type(), 0x42); |
Junxiao Shi | 2b322eb | 2018-08-24 10:43:27 -0600 | [diff] [blame] | 635 | BOOST_CHECK_EQUAL(b0.value_size(), 0); |
| 636 | |
| 637 | Block b1 = "0101A0"_block; |
| 638 | BOOST_CHECK_EQUAL(b1.type(), 0x01); |
| 639 | BOOST_REQUIRE_EQUAL(b1.value_size(), 1); |
| 640 | BOOST_CHECK_EQUAL(b1.value()[0], 0xA0); |
| 641 | } |
| 642 | |
| 643 | BOOST_AUTO_TEST_CASE(Comment) |
| 644 | { |
| 645 | Block b0 = "a2b0c0d2eBf0G.B 1+"_block; |
| 646 | BOOST_CHECK_EQUAL(b0.type(), 0x20); |
| 647 | BOOST_REQUIRE_EQUAL(b0.value_size(), 2); |
| 648 | BOOST_CHECK_EQUAL(b0.value()[0], 0xB0); |
| 649 | BOOST_CHECK_EQUAL(b0.value()[1], 0xB1); |
| 650 | } |
| 651 | |
| 652 | BOOST_AUTO_TEST_CASE(BadInput) |
| 653 | { |
| 654 | BOOST_CHECK_THROW(""_block, std::invalid_argument); |
| 655 | BOOST_CHECK_THROW("1"_block, std::invalid_argument); |
| 656 | BOOST_CHECK_THROW("333"_block, std::invalid_argument); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 657 | BOOST_CHECK_THROW("xx yy zz"_block, std::invalid_argument); // only comments |
Junxiao Shi | 2b322eb | 2018-08-24 10:43:27 -0600 | [diff] [blame] | 658 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 659 | BOOST_CHECK_THROW("0000"_block, tlv::Error); // invalid type |
| 660 | BOOST_CHECK_THROW("0202C0"_block, tlv::Error); // truncated value |
| 661 | BOOST_CHECK_THROW("0201C0C1"_block, tlv::Error); // trailing garbage |
Junxiao Shi | 2b322eb | 2018-08-24 10:43:27 -0600 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | BOOST_AUTO_TEST_SUITE_END() // BlockLiteral |
| 665 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 666 | BOOST_AUTO_TEST_SUITE_END() // TestBlock |
| 667 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 668 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 669 | } // namespace tests |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 670 | } // namespace ndn |