blob: 932585176de51cd1cb2d1ae9601e23d270503496 [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/*
Junxiao Shie4603e12022-01-05 19:12:25 +00003 * Copyright (c) 2013-2022 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
27namespace ndn {
28namespace encoding {
29namespace tests {
30
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
Junxiao Shi5d75fd92017-08-08 18:09:20 +000034enum E8 : uint8_t
35{
36 E8_NONE
37};
38
39enum class EC8 : uint8_t
40{
41 NONE
42};
43
44enum E16 : uint16_t
45{
46 E16_NONE
47};
48
49enum class EC16 : uint16_t
50{
51 NONE
52};
53
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070054BOOST_AUTO_TEST_CASE(NonNegativeInteger)
55{
56 Block b = makeNonNegativeIntegerBlock(100, 1000);
57 BOOST_CHECK_EQUAL(b.type(), 100);
58 BOOST_CHECK_GT(b.value_size(), 0);
59 BOOST_CHECK_EQUAL(readNonNegativeInteger(b), 1000);
60
61 BOOST_CHECK_THROW(readNonNegativeInteger(Block()), tlv::Error);
Junxiao Shi5d75fd92017-08-08 18:09:20 +000062
63 BOOST_CHECK_THROW(readNonNegativeIntegerAs<uint8_t>(b), tlv::Error);
64 BOOST_CHECK_EQUAL(readNonNegativeIntegerAs<uint16_t>(b), 1000);
65 BOOST_CHECK_EQUAL(readNonNegativeIntegerAs<size_t>(b), 1000);
66 BOOST_CHECK_THROW(readNonNegativeIntegerAs<E8>(b), tlv::Error);
67 BOOST_CHECK_EQUAL(static_cast<uint16_t>(readNonNegativeIntegerAs<E16>(b)), 1000);
68 BOOST_CHECK_THROW(readNonNegativeIntegerAs<EC8>(b), tlv::Error);
69 BOOST_CHECK_EQUAL(static_cast<uint16_t>(readNonNegativeIntegerAs<EC16>(b)), 1000);
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070070}
71
72BOOST_AUTO_TEST_CASE(Empty)
73{
74 Block b = makeEmptyBlock(200);
75 BOOST_CHECK_EQUAL(b.type(), 200);
76 BOOST_CHECK_EQUAL(b.value_size(), 0);
77}
78
79BOOST_AUTO_TEST_CASE(String)
80{
81 Block b = makeStringBlock(100, "Hello, world!");
82 BOOST_CHECK_EQUAL(b.type(), 100);
83 BOOST_CHECK_GT(b.value_size(), 0);
84 BOOST_CHECK_EQUAL(readString(b), "Hello, world!");
85}
86
Tianxing Ma241df422018-10-09 22:21:47 -050087BOOST_AUTO_TEST_CASE(Double)
88{
89 const double f = 0.25;
90 Block b = makeDoubleBlock(100, f);
91 BOOST_CHECK_EQUAL(b, "64083FD0000000000000"_block);
92
93 EncodingEstimator estimator;
94 size_t totalLength = prependDoubleBlock(estimator, 100, f);
95 EncodingBuffer encoder(totalLength, 0);
96 prependDoubleBlock(encoder, 100, f);
97 BOOST_CHECK_EQUAL(encoder.block(), b);
98
99 BOOST_CHECK_EQUAL(readDouble(b), f);
100 BOOST_CHECK_THROW(readDouble("4200"_block), tlv::Error);
101 BOOST_CHECK_THROW(readDouble("64043E800000"_block), tlv::Error);
102}
103
Davide Pesavento765abc92021-12-27 00:44:04 -0500104BOOST_AUTO_TEST_CASE(Binary)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700105{
106 std::string buf1{1, 1, 1, 1};
107 const uint8_t buf2[]{1, 1, 1, 1};
108 std::list<uint8_t> buf3{1, 1, 1, 1};
109
Davide Pesavento765abc92021-12-27 00:44:04 -0500110 Block b1 = makeBinaryBlock(100, buf1.data(), buf1.size()); // char* overload
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500111 Block b2 = makeBinaryBlock(100, buf2); // span overload
112 Block b3 = makeBinaryBlock(100, buf1.begin(), buf1.end()); // fast encoding (random access iterator)
113 Block b4 = makeBinaryBlock(100, buf3.begin(), buf3.end()); // slow encoding (general iterator)
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700114
Junxiao Shi72c0c642018-04-20 15:41:09 +0000115 BOOST_CHECK_EQUAL(b1, b2);
116 BOOST_CHECK_EQUAL(b1, b3);
Davide Pesavento765abc92021-12-27 00:44:04 -0500117 BOOST_CHECK_EQUAL(b1, b4);
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700118 BOOST_CHECK_EQUAL(b1.type(), 100);
119 BOOST_CHECK_EQUAL(b1.value_size(), buf1.size());
Tianxing Ma241df422018-10-09 22:21:47 -0500120 BOOST_CHECK_EQUAL_COLLECTIONS(b1.value_begin(), b1.value_end(), buf2, buf2 + sizeof(buf2));
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500121
122 EncodingEstimator estimator;
123 size_t length = prependBinaryBlock(estimator, 100, buf2);
124 BOOST_CHECK_EQUAL(length, 6);
125
126 EncodingBuffer encoder(length, 0);
127 BOOST_CHECK_EQUAL(prependBinaryBlock(encoder, 100, buf2), 6);
128 BOOST_CHECK_EQUAL(encoder.block(), b1);
129}
130
131BOOST_AUTO_TEST_CASE(PrependBlock)
132{
133 EncodingEstimator estimator;
134 Block b1({0x01, 0x03, 0x00, 0x00, 0x00});
135 size_t length = prependBlock(estimator, b1);
136 BOOST_CHECK_EQUAL(length, 5);
137 Block b2(100, b1);
138 length += prependBlock(estimator, b2);
139 BOOST_CHECK_EQUAL(length, 12);
140
141 EncodingBuffer encoder(length, 0);
142 BOOST_CHECK_EQUAL(prependBlock(encoder, b1), 5);
143 BOOST_CHECK_EQUAL(prependBlock(encoder, b2), 7);
144 BOOST_CHECK_EQUAL(encoder.size(), 12);
145 encoder.prependVarNumber(encoder.size());
146 encoder.prependVarNumber(200);
147 BOOST_CHECK_EQUAL(encoder.block(), "C80C 64050103000000 0103000000"_block);
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700148}
149
150BOOST_AUTO_TEST_CASE(Nested)
151{
152 Name name("ndn:/Hello/World!");
153 Block b1 = makeNestedBlock(100, name);
154
155 BOOST_CHECK_EQUAL(b1.type(), 100);
156 b1.parse();
157 BOOST_CHECK_EQUAL(b1.elements().size(), 1);
158 BOOST_CHECK_EQUAL(b1.elements().begin()->type(), name.wireEncode().type());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000159 BOOST_CHECK_EQUAL(*b1.elements().begin(), name.wireEncode());
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700160}
161
Junxiao Shie4603e12022-01-05 19:12:25 +0000162BOOST_AUTO_TEST_CASE(NestedSequence)
163{
164 std::vector<Name> names;
165 names.emplace_back("/A");
166 names.emplace_back("/B");
167 Block b1 = makeNestedBlock(100, names.begin(), names.end());
168
169 BOOST_CHECK_EQUAL(b1.type(), 100);
170 b1.parse();
171 auto elements = b1.elements();
172 BOOST_REQUIRE_EQUAL(elements.size(), 2);
173 BOOST_CHECK_EQUAL(Name(elements[0]), names[0]);
174 BOOST_CHECK_EQUAL(Name(elements[1]), names[1]);
175}
176
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100177BOOST_AUTO_TEST_SUITE_END() // TestBlockHelpers
178BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700179
180} // namespace tests
181} // namespace encoding
182} // namespace ndn