blob: 43935e3f822ffc4725ead7e1e460fc3c3026b7c9 [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/*
Davide Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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);
Davide Pesavento0f830802018-01-16 23:58:58 -050044 meta.setFreshnessPeriod(10_s);
Shock Jiangca7ea702014-10-02 11:23:25 -070045
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));
Davide Pesavento0f830802018-01-16 23:58:58 -050065 BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), 10_s);
Shock Jiangca7ea702014-10-02 11:23:25 -070066 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));
Davide Pesavento0f830802018-01-16 23:58:58 -050070 BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), 10_s);
Shock Jiangca7ea702014-10-02 11:23:25 -070071 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));
Davide Pesavento0f830802018-01-16 23:58:58 -050075 BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), 10_s);
Shock Jiangca7ea702014-10-02 11:23:25 -070076 BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!"));
77}
78
79BOOST_AUTO_TEST_CASE(EqualityChecks)
80{
Shock Jiangca7ea702014-10-02 11:23:25 -070081 MetaInfo a;
82 MetaInfo b;
83 BOOST_CHECK_EQUAL(a == b, true);
84 BOOST_CHECK_EQUAL(a != b, false);
85
Davide Pesavento0f830802018-01-16 23:58:58 -050086 a.setFreshnessPeriod(10_s);
Shock Jiangca7ea702014-10-02 11:23:25 -070087 BOOST_CHECK_EQUAL(a == b, false);
88 BOOST_CHECK_EQUAL(a != b, true);
89
Davide Pesavento0f830802018-01-16 23:58:58 -050090 b.setFreshnessPeriod(90_s);
Shock Jiangca7ea702014-10-02 11:23:25 -070091 BOOST_CHECK_EQUAL(a == b, false);
92 BOOST_CHECK_EQUAL(a != b, true);
93
Davide Pesavento0f830802018-01-16 23:58:58 -050094 b.setFreshnessPeriod(10_s);
Shock Jiangca7ea702014-10-02 11:23:25 -070095 BOOST_CHECK_EQUAL(a == b, true);
96 BOOST_CHECK_EQUAL(a != b, false);
97
98 a.setType(10);
99 BOOST_CHECK_EQUAL(a == b, false);
100 BOOST_CHECK_EQUAL(a != b, true);
101
102 b.setType(10);
103 BOOST_CHECK_EQUAL(a == b, true);
104 BOOST_CHECK_EQUAL(a != b, false);
105}
106
107BOOST_AUTO_TEST_CASE(AppMetaInfo)
108{
109 MetaInfo info1;
110 info1.setType(196);
Davide Pesavento0f830802018-01-16 23:58:58 -0500111 info1.setFreshnessPeriod(3600_ms);
Shock Jiangca7ea702014-10-02 11:23:25 -0700112 info1.setFinalBlockId(name::Component("/att/final"));
113
114 uint32_t ints[5] = {128, 129, 130, 131, 132};
115 std::string ss[5] = {"h", "hello", "hello, world", "hello, world, alex",
116 "hello, world, alex, I am Xiaoke Jiang"};
117
118 for (int i = 0; i < 5; i++) {
119 uint32_t type = 128 + i * 10;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700120 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i]));
Shock Jiangca7ea702014-10-02 11:23:25 -0700121 type += 5;
Davide Pesaventoc2e12b42017-07-09 20:35:09 -0400122 info1.addAppMetaInfo(makeStringBlock(type, ss[i]));
Shock Jiangca7ea702014-10-02 11:23:25 -0700123 }
124
125 BOOST_CHECK(info1.findAppMetaInfo(252) == 0);
126
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700127 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Shock Jiangca7ea702014-10-02 11:23:25 -0700128 BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
129
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700130 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Shock Jiangca7ea702014-10-02 11:23:25 -0700131 BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
132
133 info1.removeAppMetaInfo(252);
134 BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
135
136 info1.removeAppMetaInfo(252);
137 BOOST_CHECK(info1.findAppMetaInfo(252) == 0);
138
139 // // These octets are obtained by the snippet below.
140 // // This check is intended to detect unexpected encoding change in the future.
141 // const Block& wire = info1.wireEncode();
142 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
143 // printf("0x%02x, ", *it);
144 // }
145
146 const uint8_t METAINFO[] = {0x14, 0x77, 0x18, 0x01, 0xc4, 0x19, 0x02, 0x0e, 0x10, 0x1a, 0x0c,
147 0x08, 0x0a, 0x2f, 0x61, 0x74, 0x74, 0x2f, 0x66, 0x69, 0x6e, 0x61,
148 0x6c, 0x80, 0x01, 0x80, 0x85, 0x01, 0x68, 0x8a, 0x01, 0x81, 0x8f,
149 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x94, 0x01, 0x82, 0x99, 0x0c,
150 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c,
151 0x64, 0x9e, 0x01, 0x83, 0xa3, 0x12, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
152 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, 0x6c,
153 0x65, 0x78, 0xa8, 0x01, 0x84, 0xad, 0x25, 0x68, 0x65, 0x6c, 0x6c,
154 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61,
155 0x6c, 0x65, 0x78, 0x2c, 0x20, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x58,
156 0x69, 0x61, 0x6f, 0x6b, 0x65, 0x20, 0x4a, 0x69, 0x61, 0x6e, 0x67};
157
158 BOOST_REQUIRE_EQUAL_COLLECTIONS(info1.wireEncode().begin(), info1.wireEncode().end(),
159 METAINFO, METAINFO + sizeof(METAINFO));
160
161 MetaInfo info2;
162 info2.wireDecode(Block(METAINFO, sizeof(METAINFO)));
163
164 for (int i = 0; i < 5; i++) {
165 uint32_t tlvType = 128 + i * 10;
166 const Block* block = info2.findAppMetaInfo(tlvType);
167 BOOST_REQUIRE(block != 0);
168 BOOST_CHECK_EQUAL(readNonNegativeInteger(*block), ints[i]);
169 tlvType += 5;
170
171 block = info2.findAppMetaInfo(tlvType);
172 BOOST_REQUIRE(block != 0);
173
174 std::string s3 = std::string(reinterpret_cast<const char*>(block->value()),
175 block->value_size());
176 BOOST_CHECK_EQUAL(s3, ss[i]);
177 }
178}
179
180BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange)
181{
182 MetaInfo info;
183
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700184 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000)));
185 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)));
Shock Jiangca7ea702014-10-02 11:23:25 -0700186
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700187 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error);
188 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error);
Shock Jiangca7ea702014-10-02 11:23:25 -0700189}
190
Eric Newberryb555b002017-05-17 00:30:44 -0700191BOOST_AUTO_TEST_CASE(EncodeDecodeFreshnessPeriod)
192{
193 const uint8_t expectedDefault[] = {
194 0x14, 0x00, // MetaInfo
195 };
196
197 const uint8_t expected1000ms[] = {
198 0x14, 0x04, // MetaInfo
199 0x19, 0x02, // FreshnessPeriod
200 0x03, 0xe8 // 1000ms
201 };
202
203 MetaInfo info;
204 BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(),
205 expectedDefault, expectedDefault + sizeof(expectedDefault));
206
207 info.setFreshnessPeriod(time::milliseconds::zero());
208 BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(),
209 expectedDefault, expectedDefault + sizeof(expectedDefault));
210
Davide Pesavento0f830802018-01-16 23:58:58 -0500211 info.setFreshnessPeriod(1000_ms);
Eric Newberryb555b002017-05-17 00:30:44 -0700212 BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(),
213 expected1000ms, expected1000ms + sizeof(expected1000ms));
214
215 const uint8_t inputDefault[] = {
216 0x14, 0x03, // MetaInfo
217 0x19, 0x01, // FreshnessPeriod
218 0x00 // 0ms
219 };
220
221 const uint8_t input2000ms[] = {
222 0x14, 0x04, // MetaInfo
223 0x19, 0x02, // FreshnessPeriod
224 0x07, 0xd0 // 2000ms
225 };
226
227 Block inputDefaultBlock(inputDefault, sizeof(inputDefault));
228 BOOST_CHECK_NO_THROW(info.wireDecode(inputDefaultBlock));
229 BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds::zero());
230
231 Block input2000msBlock(input2000ms, sizeof(input2000ms));
232 BOOST_CHECK_NO_THROW(info.wireDecode(input2000msBlock));
Davide Pesavento0f830802018-01-16 23:58:58 -0500233 BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), 2000_ms);
Eric Newberryb555b002017-05-17 00:30:44 -0700234
Davide Pesavento0f830802018-01-16 23:58:58 -0500235 BOOST_CHECK_NO_THROW(info.setFreshnessPeriod(10000_ms));
236 BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), 10000_ms);
Eric Newberryb555b002017-05-17 00:30:44 -0700237 BOOST_CHECK_THROW(info.setFreshnessPeriod(time::milliseconds(-1)), std::invalid_argument);
Davide Pesavento0f830802018-01-16 23:58:58 -0500238 BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), 10000_ms);
Eric Newberryb555b002017-05-17 00:30:44 -0700239}
240
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100241BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo
Shock Jiangca7ea702014-10-02 11:23:25 -0700242
243} // namespace tests
244} // namespace ndn