blob: bbd3fef542394879da94805598c669e39f683224 [file] [log] [blame]
Shock Jiangca7ea702014-10-02 11:23:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc2e12b42017-07-09 20:35:09 -04002/*
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Shock Jiangca7ea702014-10-02 11:23:25 -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
22#include "meta-info.hpp"
Shock Jiangca7ea702014-10-02 11:23:25 -070023#include "data.hpp"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080024
Shock Jiangca7ea702014-10-02 11:23:25 -070025#include "boost-test.hpp"
26
27namespace ndn {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(TestMetaInfo)
31
32const uint8_t MetaInfo1[] = {0x14, 0x04, 0x19, 0x02, 0x27, 0x10};
33const uint8_t MetaInfo2[] = {0x14, 0x14, 0x19, 0x02, 0x27, 0x10, 0x1a, 0x0e, 0x08, 0x0c,
34 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x77, 0x6f, 0x72, 0x6c,
35 0x64, 0x21};
36const uint8_t MetaInfo3[] = {0x14, 0x17, 0x18, 0x01, 0x01, 0x19, 0x02, 0x27, 0x10, 0x1a,
37 0x0e, 0x08, 0x0c, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x77,
38 0x6f, 0x72, 0x6c, 0x64, 0x21};
39
40BOOST_AUTO_TEST_CASE(Encode)
41{
42 MetaInfo meta;
Junxiao Shia464b922014-11-12 21:13:06 -070043 meta.setType(tlv::ContentType_Blob);
Shock Jiangca7ea702014-10-02 11:23:25 -070044 meta.setFreshnessPeriod(time::seconds(10));
45
46 BOOST_REQUIRE_NO_THROW(meta.wireEncode());
47 BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo1, MetaInfo1+sizeof(MetaInfo1),
48 meta.wireEncode().begin(), meta.wireEncode().end());
49
50 meta.setFinalBlockId(name::Component("hello,world!"));
51 BOOST_REQUIRE_NO_THROW(meta.wireEncode());
52 BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo2, MetaInfo2+sizeof(MetaInfo2),
53 meta.wireEncode().begin(), meta.wireEncode().end());
54
Junxiao Shia464b922014-11-12 21:13:06 -070055 meta.setType(tlv::ContentType_Link);
Shock Jiangca7ea702014-10-02 11:23:25 -070056 BOOST_REQUIRE_NO_THROW(meta.wireEncode());
57 BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo3, MetaInfo3+sizeof(MetaInfo3),
58 meta.wireEncode().begin(), meta.wireEncode().end());
59}
60
61BOOST_AUTO_TEST_CASE(Decode)
62{
63 MetaInfo meta(Block(MetaInfo1, sizeof(MetaInfo1)));
Junxiao Shia464b922014-11-12 21:13:06 -070064 BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob));
Shock Jiangca7ea702014-10-02 11:23:25 -070065 BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), time::seconds(10));
66 BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component());
67
68 meta.wireDecode(Block(MetaInfo2, sizeof(MetaInfo2)));
Junxiao Shia464b922014-11-12 21:13:06 -070069 BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob));
Shock Jiangca7ea702014-10-02 11:23:25 -070070 BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), time::seconds(10));
71 BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!"));
72
73 meta.wireDecode(Block(MetaInfo3, sizeof(MetaInfo3)));
Junxiao Shia464b922014-11-12 21:13:06 -070074 BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Link));
Shock Jiangca7ea702014-10-02 11:23:25 -070075 BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), time::seconds(10));
76 BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!"));
77}
78
79BOOST_AUTO_TEST_CASE(EqualityChecks)
80{
81 using namespace time;
82
83 MetaInfo a;
84 MetaInfo b;
85 BOOST_CHECK_EQUAL(a == b, true);
86 BOOST_CHECK_EQUAL(a != b, false);
87
88 a.setFreshnessPeriod(seconds(10));
89 BOOST_CHECK_EQUAL(a == b, false);
90 BOOST_CHECK_EQUAL(a != b, true);
91
92 b.setFreshnessPeriod(milliseconds(90000));
93 BOOST_CHECK_EQUAL(a == b, false);
94 BOOST_CHECK_EQUAL(a != b, true);
95
96 b.setFreshnessPeriod(milliseconds(10000));
97 BOOST_CHECK_EQUAL(a == b, true);
98 BOOST_CHECK_EQUAL(a != b, false);
99
100 a.setType(10);
101 BOOST_CHECK_EQUAL(a == b, false);
102 BOOST_CHECK_EQUAL(a != b, true);
103
104 b.setType(10);
105 BOOST_CHECK_EQUAL(a == b, true);
106 BOOST_CHECK_EQUAL(a != b, false);
107}
108
109BOOST_AUTO_TEST_CASE(AppMetaInfo)
110{
111 MetaInfo info1;
112 info1.setType(196);
113 info1.setFreshnessPeriod(time::milliseconds(3600));
114 info1.setFinalBlockId(name::Component("/att/final"));
115
116 uint32_t ints[5] = {128, 129, 130, 131, 132};
117 std::string ss[5] = {"h", "hello", "hello, world", "hello, world, alex",
118 "hello, world, alex, I am Xiaoke Jiang"};
119
120 for (int i = 0; i < 5; i++) {
121 uint32_t type = 128 + i * 10;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700122 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i]));
Shock Jiangca7ea702014-10-02 11:23:25 -0700123 type += 5;
Davide Pesaventoc2e12b42017-07-09 20:35:09 -0400124 info1.addAppMetaInfo(makeStringBlock(type, ss[i]));
Shock Jiangca7ea702014-10-02 11:23:25 -0700125 }
126
127 BOOST_CHECK(info1.findAppMetaInfo(252) == 0);
128
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700129 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Shock Jiangca7ea702014-10-02 11:23:25 -0700130 BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
131
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700132 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Shock Jiangca7ea702014-10-02 11:23:25 -0700133 BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
134
135 info1.removeAppMetaInfo(252);
136 BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
137
138 info1.removeAppMetaInfo(252);
139 BOOST_CHECK(info1.findAppMetaInfo(252) == 0);
140
141 // // These octets are obtained by the snippet below.
142 // // This check is intended to detect unexpected encoding change in the future.
143 // const Block& wire = info1.wireEncode();
144 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
145 // printf("0x%02x, ", *it);
146 // }
147
148 const uint8_t METAINFO[] = {0x14, 0x77, 0x18, 0x01, 0xc4, 0x19, 0x02, 0x0e, 0x10, 0x1a, 0x0c,
149 0x08, 0x0a, 0x2f, 0x61, 0x74, 0x74, 0x2f, 0x66, 0x69, 0x6e, 0x61,
150 0x6c, 0x80, 0x01, 0x80, 0x85, 0x01, 0x68, 0x8a, 0x01, 0x81, 0x8f,
151 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x94, 0x01, 0x82, 0x99, 0x0c,
152 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c,
153 0x64, 0x9e, 0x01, 0x83, 0xa3, 0x12, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
154 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, 0x6c,
155 0x65, 0x78, 0xa8, 0x01, 0x84, 0xad, 0x25, 0x68, 0x65, 0x6c, 0x6c,
156 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61,
157 0x6c, 0x65, 0x78, 0x2c, 0x20, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x58,
158 0x69, 0x61, 0x6f, 0x6b, 0x65, 0x20, 0x4a, 0x69, 0x61, 0x6e, 0x67};
159
160 BOOST_REQUIRE_EQUAL_COLLECTIONS(info1.wireEncode().begin(), info1.wireEncode().end(),
161 METAINFO, METAINFO + sizeof(METAINFO));
162
163 MetaInfo info2;
164 info2.wireDecode(Block(METAINFO, sizeof(METAINFO)));
165
166 for (int i = 0; i < 5; i++) {
167 uint32_t tlvType = 128 + i * 10;
168 const Block* block = info2.findAppMetaInfo(tlvType);
169 BOOST_REQUIRE(block != 0);
170 BOOST_CHECK_EQUAL(readNonNegativeInteger(*block), ints[i]);
171 tlvType += 5;
172
173 block = info2.findAppMetaInfo(tlvType);
174 BOOST_REQUIRE(block != 0);
175
176 std::string s3 = std::string(reinterpret_cast<const char*>(block->value()),
177 block->value_size());
178 BOOST_CHECK_EQUAL(s3, ss[i]);
179 }
180}
181
182BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange)
183{
184 MetaInfo info;
185
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700186 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000)));
187 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)));
Shock Jiangca7ea702014-10-02 11:23:25 -0700188
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700189 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error);
190 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error);
Shock Jiangca7ea702014-10-02 11:23:25 -0700191}
192
Eric Newberryb555b002017-05-17 00:30:44 -0700193BOOST_AUTO_TEST_CASE(EncodeDecodeFreshnessPeriod)
194{
195 const uint8_t expectedDefault[] = {
196 0x14, 0x00, // MetaInfo
197 };
198
199 const uint8_t expected1000ms[] = {
200 0x14, 0x04, // MetaInfo
201 0x19, 0x02, // FreshnessPeriod
202 0x03, 0xe8 // 1000ms
203 };
204
205 MetaInfo info;
206 BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(),
207 expectedDefault, expectedDefault + sizeof(expectedDefault));
208
209 info.setFreshnessPeriod(time::milliseconds::zero());
210 BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(),
211 expectedDefault, expectedDefault + sizeof(expectedDefault));
212
213 info.setFreshnessPeriod(time::milliseconds(1000));
214 BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(),
215 expected1000ms, expected1000ms + sizeof(expected1000ms));
216
217 const uint8_t inputDefault[] = {
218 0x14, 0x03, // MetaInfo
219 0x19, 0x01, // FreshnessPeriod
220 0x00 // 0ms
221 };
222
223 const uint8_t input2000ms[] = {
224 0x14, 0x04, // MetaInfo
225 0x19, 0x02, // FreshnessPeriod
226 0x07, 0xd0 // 2000ms
227 };
228
229 Block inputDefaultBlock(inputDefault, sizeof(inputDefault));
230 BOOST_CHECK_NO_THROW(info.wireDecode(inputDefaultBlock));
231 BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds::zero());
232
233 Block input2000msBlock(input2000ms, sizeof(input2000ms));
234 BOOST_CHECK_NO_THROW(info.wireDecode(input2000msBlock));
235 BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds(2000));
236
237 BOOST_CHECK_NO_THROW(info.setFreshnessPeriod(time::milliseconds(10000)));
238 BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds(10000));
239 BOOST_CHECK_THROW(info.setFreshnessPeriod(time::milliseconds(-1)), std::invalid_argument);
240 BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds(10000));
241}
242
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100243BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo
Shock Jiangca7ea702014-10-02 11:23:25 -0700244
245} // namespace tests
246} // namespace ndn