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