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