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