blob: 59a76523020b010a77ed22a441c57e4eba7bbf53 [file] [log] [blame]
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5d75fd92017-08-08 18:09:20 +00002/*
Davide Pesavento3fdb02f2023-04-12 02:32:38 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -07004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/encoding/block-helpers.hpp"
23#include "ndn-cxx/name.hpp"
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070026
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040027namespace ndn::tests {
28
29using namespace ndn::encoding;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070030
Davide Pesaventoeee3e822016-11-26 19:19:34 +010031BOOST_AUTO_TEST_SUITE(Encoding)
32BOOST_AUTO_TEST_SUITE(TestBlockHelpers)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070033
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040034BOOST_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 Shi5d75fd92017-08-08 18:09:20 +000041enum E8 : uint8_t
42{
43 E8_NONE
44};
45
46enum class EC8 : uint8_t
47{
48 NONE
49};
50
51enum E16 : uint16_t
52{
53 E16_NONE
54};
55
56enum class EC16 : uint16_t
57{
58 NONE
59};
60
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070061BOOST_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 Shi5d75fd92017-08-08 18:09:20 +000069
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 Afanasyevd5c48e02015-06-24 11:58:14 -070077}
78
Tianxing Ma241df422018-10-09 22:21:47 -050079BOOST_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 Pesavento765abc92021-12-27 00:44:04 -050096BOOST_AUTO_TEST_CASE(Binary)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070097{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040098 const std::string buf1{1, 1, 1, 1};
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070099 const uint8_t buf2[]{1, 1, 1, 1};
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400100 const std::list<uint8_t> buf3{1, 1, 1, 1};
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700101
Davide Pesavento19b55282023-12-04 15:05:22 -0500102 Block b1 = makeBinaryBlock(100, buf2); // span overload
103 Block b2 = makeBinaryBlock(100, buf1.begin(), buf1.end()); // fast encoding (random access iterator)
104 Block b3 = makeBinaryBlock(100, buf3.begin(), buf3.end()); // slow encoding (general iterator)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700105
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400106 BOOST_TEST(b1 == b2);
107 BOOST_TEST(b1 == b3);
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400108 BOOST_TEST(b1.type() == 100);
109 BOOST_TEST(b1.value_size() == sizeof(buf2));
110 BOOST_TEST(b1.value_bytes() == buf2, boost::test_tools::per_element());
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500111
112 EncodingEstimator estimator;
113 size_t length = prependBinaryBlock(estimator, 100, buf2);
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400114 BOOST_TEST(length == 6);
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500115
116 EncodingBuffer encoder(length, 0);
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400117 BOOST_TEST(prependBinaryBlock(encoder, 100, buf2) == 6);
118 BOOST_TEST(encoder.block() == b1);
119}
120
121BOOST_AUTO_TEST_CASE(String)
122{
123 constexpr std::string_view sv{"Hello, world!"sv};
124 Block b = makeStringBlock(100, sv);
125 BOOST_TEST(b.type() == 100);
126 BOOST_TEST(b.value_size() == sv.size());
127 BOOST_TEST(readString(b) == sv);
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500128}
129
130BOOST_AUTO_TEST_CASE(PrependBlock)
131{
132 EncodingEstimator estimator;
133 Block b1({0x01, 0x03, 0x00, 0x00, 0x00});
134 size_t length = prependBlock(estimator, b1);
135 BOOST_CHECK_EQUAL(length, 5);
136 Block b2(100, b1);
137 length += prependBlock(estimator, b2);
138 BOOST_CHECK_EQUAL(length, 12);
139
140 EncodingBuffer encoder(length, 0);
141 BOOST_CHECK_EQUAL(prependBlock(encoder, b1), 5);
142 BOOST_CHECK_EQUAL(prependBlock(encoder, b2), 7);
143 BOOST_CHECK_EQUAL(encoder.size(), 12);
144 encoder.prependVarNumber(encoder.size());
145 encoder.prependVarNumber(200);
146 BOOST_CHECK_EQUAL(encoder.block(), "C80C 64050103000000 0103000000"_block);
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700147}
148
149BOOST_AUTO_TEST_CASE(Nested)
150{
151 Name name("ndn:/Hello/World!");
152 Block b1 = makeNestedBlock(100, name);
153
154 BOOST_CHECK_EQUAL(b1.type(), 100);
155 b1.parse();
156 BOOST_CHECK_EQUAL(b1.elements().size(), 1);
157 BOOST_CHECK_EQUAL(b1.elements().begin()->type(), name.wireEncode().type());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000158 BOOST_CHECK_EQUAL(*b1.elements().begin(), name.wireEncode());
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700159}
160
Junxiao Shie4603e12022-01-05 19:12:25 +0000161BOOST_AUTO_TEST_CASE(NestedSequence)
162{
163 std::vector<Name> names;
164 names.emplace_back("/A");
165 names.emplace_back("/B");
166 Block b1 = makeNestedBlock(100, names.begin(), names.end());
167
168 BOOST_CHECK_EQUAL(b1.type(), 100);
169 b1.parse();
170 auto elements = b1.elements();
171 BOOST_REQUIRE_EQUAL(elements.size(), 2);
172 BOOST_CHECK_EQUAL(Name(elements[0]), names[0]);
173 BOOST_CHECK_EQUAL(Name(elements[1]), names[1]);
174}
175
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100176BOOST_AUTO_TEST_SUITE_END() // TestBlockHelpers
177BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700178
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400179} // namespace ndn::tests