Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/encoding/block-helpers.hpp" |
| 23 | #include "ndn-cxx/name.hpp" |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 27 | namespace ndn::tests { |
| 28 | |
| 29 | using namespace ndn::encoding; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 32 | BOOST_AUTO_TEST_SUITE(TestBlockHelpers) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 34 | BOOST_AUTO_TEST_CASE(Empty) |
| 35 | { |
| 36 | Block b = makeEmptyBlock(200); |
| 37 | BOOST_CHECK_EQUAL(b.type(), 200); |
| 38 | BOOST_CHECK_EQUAL(b.value_size(), 0); |
| 39 | } |
| 40 | |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 41 | enum E8 : uint8_t |
| 42 | { |
| 43 | E8_NONE |
| 44 | }; |
| 45 | |
| 46 | enum class EC8 : uint8_t |
| 47 | { |
| 48 | NONE |
| 49 | }; |
| 50 | |
| 51 | enum E16 : uint16_t |
| 52 | { |
| 53 | E16_NONE |
| 54 | }; |
| 55 | |
| 56 | enum class EC16 : uint16_t |
| 57 | { |
| 58 | NONE |
| 59 | }; |
| 60 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 61 | BOOST_AUTO_TEST_CASE(NonNegativeInteger) |
| 62 | { |
| 63 | Block b = makeNonNegativeIntegerBlock(100, 1000); |
| 64 | BOOST_CHECK_EQUAL(b.type(), 100); |
| 65 | BOOST_CHECK_GT(b.value_size(), 0); |
| 66 | BOOST_CHECK_EQUAL(readNonNegativeInteger(b), 1000); |
| 67 | |
| 68 | BOOST_CHECK_THROW(readNonNegativeInteger(Block()), tlv::Error); |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 69 | |
| 70 | BOOST_CHECK_THROW(readNonNegativeIntegerAs<uint8_t>(b), tlv::Error); |
| 71 | BOOST_CHECK_EQUAL(readNonNegativeIntegerAs<uint16_t>(b), 1000); |
| 72 | BOOST_CHECK_EQUAL(readNonNegativeIntegerAs<size_t>(b), 1000); |
| 73 | BOOST_CHECK_THROW(readNonNegativeIntegerAs<E8>(b), tlv::Error); |
| 74 | BOOST_CHECK_EQUAL(static_cast<uint16_t>(readNonNegativeIntegerAs<E16>(b)), 1000); |
| 75 | BOOST_CHECK_THROW(readNonNegativeIntegerAs<EC8>(b), tlv::Error); |
| 76 | BOOST_CHECK_EQUAL(static_cast<uint16_t>(readNonNegativeIntegerAs<EC16>(b)), 1000); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 79 | BOOST_AUTO_TEST_CASE(Double) |
| 80 | { |
| 81 | const double f = 0.25; |
| 82 | Block b = makeDoubleBlock(100, f); |
| 83 | BOOST_CHECK_EQUAL(b, "64083FD0000000000000"_block); |
| 84 | |
| 85 | EncodingEstimator estimator; |
| 86 | size_t totalLength = prependDoubleBlock(estimator, 100, f); |
| 87 | EncodingBuffer encoder(totalLength, 0); |
| 88 | prependDoubleBlock(encoder, 100, f); |
| 89 | BOOST_CHECK_EQUAL(encoder.block(), b); |
| 90 | |
| 91 | BOOST_CHECK_EQUAL(readDouble(b), f); |
| 92 | BOOST_CHECK_THROW(readDouble("4200"_block), tlv::Error); |
| 93 | BOOST_CHECK_THROW(readDouble("64043E800000"_block), tlv::Error); |
| 94 | } |
| 95 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 96 | BOOST_AUTO_TEST_CASE(Binary) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 97 | { |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 98 | const std::string buf1{1, 1, 1, 1}; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 99 | const uint8_t buf2[]{1, 1, 1, 1}; |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 100 | const std::list<uint8_t> buf3{1, 1, 1, 1}; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 101 | |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 102 | #pragma GCC diagnostic push |
| 103 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 104 | Block b1 = makeBinaryBlock(100, buf1.data(), buf1.size()); // char* overload (deprecated) |
| 105 | #pragma GCC diagnostic pop |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 106 | Block b2 = makeBinaryBlock(100, buf2); // span overload |
| 107 | Block b3 = makeBinaryBlock(100, buf1.begin(), buf1.end()); // fast encoding (random access iterator) |
| 108 | Block b4 = makeBinaryBlock(100, buf3.begin(), buf3.end()); // slow encoding (general iterator) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 109 | |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 110 | BOOST_TEST(b1 == b2); |
| 111 | BOOST_TEST(b1 == b3); |
| 112 | BOOST_TEST(b1 == b4); |
| 113 | BOOST_TEST(b1.type() == 100); |
| 114 | BOOST_TEST(b1.value_size() == sizeof(buf2)); |
| 115 | BOOST_TEST(b1.value_bytes() == buf2, boost::test_tools::per_element()); |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 116 | |
| 117 | EncodingEstimator estimator; |
| 118 | size_t length = prependBinaryBlock(estimator, 100, buf2); |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 119 | BOOST_TEST(length == 6); |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 120 | |
| 121 | EncodingBuffer encoder(length, 0); |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 122 | BOOST_TEST(prependBinaryBlock(encoder, 100, buf2) == 6); |
| 123 | BOOST_TEST(encoder.block() == b1); |
| 124 | } |
| 125 | |
| 126 | BOOST_AUTO_TEST_CASE(String) |
| 127 | { |
| 128 | constexpr std::string_view sv{"Hello, world!"sv}; |
| 129 | Block b = makeStringBlock(100, sv); |
| 130 | BOOST_TEST(b.type() == 100); |
| 131 | BOOST_TEST(b.value_size() == sv.size()); |
| 132 | BOOST_TEST(readString(b) == sv); |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | BOOST_AUTO_TEST_CASE(PrependBlock) |
| 136 | { |
| 137 | EncodingEstimator estimator; |
| 138 | Block b1({0x01, 0x03, 0x00, 0x00, 0x00}); |
| 139 | size_t length = prependBlock(estimator, b1); |
| 140 | BOOST_CHECK_EQUAL(length, 5); |
| 141 | Block b2(100, b1); |
| 142 | length += prependBlock(estimator, b2); |
| 143 | BOOST_CHECK_EQUAL(length, 12); |
| 144 | |
| 145 | EncodingBuffer encoder(length, 0); |
| 146 | BOOST_CHECK_EQUAL(prependBlock(encoder, b1), 5); |
| 147 | BOOST_CHECK_EQUAL(prependBlock(encoder, b2), 7); |
| 148 | BOOST_CHECK_EQUAL(encoder.size(), 12); |
| 149 | encoder.prependVarNumber(encoder.size()); |
| 150 | encoder.prependVarNumber(200); |
| 151 | BOOST_CHECK_EQUAL(encoder.block(), "C80C 64050103000000 0103000000"_block); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | BOOST_AUTO_TEST_CASE(Nested) |
| 155 | { |
| 156 | Name name("ndn:/Hello/World!"); |
| 157 | Block b1 = makeNestedBlock(100, name); |
| 158 | |
| 159 | BOOST_CHECK_EQUAL(b1.type(), 100); |
| 160 | b1.parse(); |
| 161 | BOOST_CHECK_EQUAL(b1.elements().size(), 1); |
| 162 | BOOST_CHECK_EQUAL(b1.elements().begin()->type(), name.wireEncode().type()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(*b1.elements().begin(), name.wireEncode()); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Junxiao Shi | e4603e1 | 2022-01-05 19:12:25 +0000 | [diff] [blame] | 166 | BOOST_AUTO_TEST_CASE(NestedSequence) |
| 167 | { |
| 168 | std::vector<Name> names; |
| 169 | names.emplace_back("/A"); |
| 170 | names.emplace_back("/B"); |
| 171 | Block b1 = makeNestedBlock(100, names.begin(), names.end()); |
| 172 | |
| 173 | BOOST_CHECK_EQUAL(b1.type(), 100); |
| 174 | b1.parse(); |
| 175 | auto elements = b1.elements(); |
| 176 | BOOST_REQUIRE_EQUAL(elements.size(), 2); |
| 177 | BOOST_CHECK_EQUAL(Name(elements[0]), names[0]); |
| 178 | BOOST_CHECK_EQUAL(Name(elements[1]), names[1]); |
| 179 | } |
| 180 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 181 | BOOST_AUTO_TEST_SUITE_END() // TestBlockHelpers |
| 182 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 183 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 184 | } // namespace ndn::tests |