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