blob: 38580366bb208fa6b3f71fd7b3f2edd3ccc6e00b [file] [log] [blame]
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Spyridon Mastorakis3b54e852015-04-07 08:03: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 "link.hpp"
23#include "security/key-chain.hpp"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070024#include "security/v1/cryptopp.hpp"
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070025#include "encoding/buffer-stream.hpp"
26
27#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070028#include "identity-management-fixture.hpp"
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070029
30namespace ndn {
31namespace tests {
32
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(TestLink)
34
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070035const uint8_t LinkTest[] = {
360x06, 0xda, // Data
37 0x07, 0x14, // Name
38 0x08, 0x05,
39 0x6c, 0x6f, 0x63, 0x61, 0x6c,
40 0x08, 0x03,
41 0x6e, 0x64, 0x6e,
42 0x08, 0x06,
43 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
44 0x14, 0x07, // MetaInfo
45 0x18, 0x01, // ContentType
46 0x01,
47 0x19, 0x02, // FreshnessPeriod
48 0x27, 0x10,
49 0x15, 0x1a, // Content
50 0x1f, 0x0c, // LinkDelegation
51 0x1e, 0x01, // LinkPreference
52 0x0a,
53 0x07, 0x07, // Name
54 0x08, 0x05,
55 0x6c, 0x6f, 0x63, 0x61, 0x6c,
56 0x1f, 0x0a, // LinkDelegation
57 0x1e, 0x01, // LinkPreference
58 0x14,
59 0x07, 0x05, // Name
60 0x08, 0x03,
61 0x6e, 0x64, 0x6e,
62 0x16, 0x1b, // SignatureInfo
63 0x1b, 0x01, // SignatureType
64 0x01,
65 0x1c, 0x16, // KeyLocator
66 0x07, 0x14, // Name
67 0x08, 0x04,
68 0x74, 0x65, 0x73, 0x74,
69 0x08, 0x03,
70 0x6b, 0x65, 0x79,
71 0x08, 0x07,
72 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
73 0x17, 0x80, // SignatureValue
74 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
75 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
76 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
77 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
78 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
79 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
80 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
81 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
82 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
83 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
84};
85
86const uint8_t IncorrectContentTypeLink[] = {
870x06, 0xda, // Data
88 0x07, 0x14, // Name
89 0x08, 0x05,
90 0x6c, 0x6f, 0x63, 0x61, 0x6c,
91 0x08, 0x03,
92 0x6e, 0x64, 0x6e,
93 0x08, 0x06,
94 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
95 0x14, 0x07, // MetaInfo
96 0x18, 0x01, // ContentType
97 0x02,
98 0x19, 0x02, // FreshnessPeriod
99 0x27, 0x10,
100 0x15, 0x1a, // Content
101 0x1f, 0x0c, // LinkDelegation
102 0x1e, 0x01, // LinkPreference
103 0x0a,
104 0x07, 0x07, // Name
105 0x08, 0x05,
106 0x6c, 0x6f, 0x63, 0x61, 0x6c,
107 0x1f, 0x0a, // LinkDelegation
108 0x1e, 0x01, // LinkPreference
109 0x14,
110 0x07, 0x05, // Name
111 0x08, 0x03,
112 0x6e, 0x64, 0x6e,
113 0x16, 0x1b, // SignatureInfo
114 0x1b, 0x01, // SignatureType
115 0x01,
116 0x1c, 0x16, // KeyLocator
117 0x07, 0x14, // Name
118 0x08, 0x04,
119 0x74, 0x65, 0x73, 0x74,
120 0x08, 0x03,
121 0x6b, 0x65, 0x79,
122 0x08, 0x07,
123 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
124 0x17, 0x80, // SignatureValue
125 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
126 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
127 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
128 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
129 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
130 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
131 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
132 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
133 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
134 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
135};
136
137const uint8_t MissingPreferenceLink[] = {
1380x06, 0xd7, // Data
139 0x07, 0x14, // Name
140 0x08, 0x05,
141 0x6c, 0x6f, 0x63, 0x61, 0x6c,
142 0x08, 0x03,
143 0x6e, 0x64, 0x6e,
144 0x08, 0x06,
145 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
146 0x14, 0x07, // MetaInfo
147 0x18, 0x01, // ContentType
148 0x01,
149 0x19, 0x02, // FreshnessPeriod
150 0x27, 0x10,
151 0x15, 0x17, // Content
152 0x1f, 0x09, // LinkDelegation
153 0x07, 0x07, // Name
154 0x08, 0x05,
155 0x6c, 0x6f, 0x63, 0x61, 0x6c,
156 0x1f, 0x0a, // LinkDelegation
157 0x1e, 0x01, // LinkPreference
158 0x14,
159 0x07, 0x05, // Name
160 0x08, 0x03,
161 0x6e, 0x64, 0x6e,
162 0x16, 0x1b, // SignatureInfo
163 0x1b, 0x01, // SignatureType
164 0x01,
165 0x1c, 0x16, // KeyLocator
166 0x07, 0x14, // Name
167 0x08, 0x04,
168 0x74, 0x65, 0x73, 0x74,
169 0x08, 0x03,
170 0x6b, 0x65, 0x79,
171 0x08, 0x07,
172 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
173 0x17, 0x80, // SignatureValue
174 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
175 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
176 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
177 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
178 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
179 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
180 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
181 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
182 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
183 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
184};
185
186const uint8_t MissingNameLink[] = {
1870x06, 0xd1, // Data
188 0x07, 0x14, // Name
189 0x08, 0x05,
190 0x6c, 0x6f, 0x63, 0x61, 0x6c,
191 0x08, 0x03,
192 0x6e, 0x64, 0x6e,
193 0x08, 0x06,
194 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
195 0x14, 0x07, // MetaInfo
196 0x18, 0x01, // ContentType
197 0x01,
198 0x19, 0x02, // FreshnessPeriod
199 0x27, 0x10,
200 0x15, 0x11, // Content
201 0x1f, 0x03, // LinkDelegation
202 0x1e, 0x01, // LinkPreference
203 0x0a,
204 0x1f, 0x0a, // LinkDelegation
205 0x1e, 0x01, // LinkPreference
206 0x14,
207 0x07, 0x05, // Name
208 0x08, 0x03,
209 0x6e, 0x64, 0x6e,
210 0x16, 0x1b, // SignatureInfo
211 0x1b, 0x01, // SignatureType
212 0x01,
213 0x1c, 0x16, // KeyLocator
214 0x07, 0x14, // Name
215 0x08, 0x04,
216 0x74, 0x65, 0x73, 0x74,
217 0x08, 0x03,
218 0x6b, 0x65, 0x79,
219 0x08, 0x07,
220 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
221 0x17, 0x80, // SignatureValue
222 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
223 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
224 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
225 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
226 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
227 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
228 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
229 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
230 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
231 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
232};
233
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700234BOOST_AUTO_TEST_CASE(PairParsingCheck)
235{
236 Link link("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
237 BOOST_CHECK_EQUAL(link.getDelegations().size(), static_cast<uint32_t>(3));
238}
239
240BOOST_AUTO_TEST_CASE(CheckInsertDelegation)
241{
242 Link link("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
243
244 link.addDelegation(30, Name("test4"));
245 Link::DelegationSet delegations = link.getDelegations();
246 int counter = 0;
247 for (auto i = delegations.begin(); i != delegations.end(); i++) {
248 counter++;
249 if (counter == 1)
250 {
251 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
252 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
253 }
254 if (counter == 2)
255 {
256 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
257 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2"));
258 }
259 if (counter == 3)
260 {
261 BOOST_CHECK_EQUAL(std::get<0>(*i), 30);
262 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test4"));
263 }
264 if (counter == 4)
265 {
266 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
267 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
268 }
269 }
270}
271
272BOOST_AUTO_TEST_CASE(CheckRemoveDelegation)
273{
274 Link link("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
275
276 link.removeDelegation(Name("test2"));
277 Link::DelegationSet delegations = link.getDelegations();
278 int counter = 0;
279 for (auto i = delegations.begin(); i != delegations.end(); i++) {
280 counter++;
281 if (counter == 1)
282 {
283 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
284 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
285 }
286 if (counter == 2)
287 {
288 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
289 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
290 }
291 }
292}
293
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700294BOOST_FIXTURE_TEST_CASE(CheckEncodeDecode, IdentityManagementFixture)
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700295{
296 Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700297 m_keyChain.sign(link1);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700298 Block wire = link1.wireEncode();
299
300 Link link2;
301 link2.wireDecode(wire);
302 Name name = link2.getName();
303 BOOST_CHECK_EQUAL(Name("test"), name);
304
305 Link::DelegationSet delegations;
306 delegations = link2.getDelegations();
307 int counter = 0;
308 for (auto i = delegations.begin(); i != delegations.end(); i++) {
309 counter++;
310 if (counter == 1)
311 {
312 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
313 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
314 }
315 if (counter == 2)
316 {
317 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
318 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2"));
319 }
320 if (counter == 3)
321 {
322 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
323 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
324 }
325 }
326
327 Link link3(wire);
328 name = link3.getName();
329 BOOST_CHECK_EQUAL(Name("test"), name);
330
331 delegations = link3.getDelegations();
332 counter = 0;
333 for (auto i = delegations.begin(); i != delegations.end(); i++) {
334 counter++;
335 if (counter == 1)
336 {
337 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
338 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1"));
339 }
340 if (counter == 2)
341 {
342 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
343 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2"));
344 }
345 if (counter == 3)
346 {
347 BOOST_CHECK_EQUAL(std::get<0>(*i), 100);
348 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3"));
349 }
350 }
351}
352
353BOOST_AUTO_TEST_CASE(Decode)
354{
355 Block linkBlock(LinkTest, sizeof(LinkTest));
356 Link link(linkBlock);
357 BOOST_CHECK_EQUAL(link.getName(), Name("/local/ndn/prefix"));
358 Link::DelegationSet delegations = link.getDelegations();
359 int counter = 0;
360
361 for (auto i = delegations.begin(); i != delegations.end(); i++) {
362 counter++;
363 if (counter == 1)
364 {
365 BOOST_CHECK_EQUAL(std::get<0>(*i), 10);
366 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("local"));
367 }
368 if (counter == 2)
369 {
370 BOOST_CHECK_EQUAL(std::get<0>(*i), 20);
371 BOOST_CHECK_EQUAL(std::get<1>(*i), Name("ndn"));
372 }
373 }
374}
375
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700376BOOST_AUTO_TEST_CASE(CountDelegationFromWire)
377{
378 Block linkBlock(LinkTest, sizeof(LinkTest));
379 BOOST_CHECK_EQUAL(Link::countDelegationsFromWire(linkBlock), 2);
380}
381
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700382BOOST_AUTO_TEST_CASE(IncorrectContentType)
383{
384 Block linkBlock(IncorrectContentTypeLink, sizeof(IncorrectContentTypeLink));
385 Link link;
386 BOOST_REQUIRE_THROW(link.wireDecode(linkBlock), Link::Error);
387}
388
389BOOST_AUTO_TEST_CASE(MissingPreferenceEncoding)
390{
391 Block linkBlock(MissingPreferenceLink, sizeof(MissingPreferenceLink));
392 Link link;
393 BOOST_REQUIRE_THROW(link.wireDecode(linkBlock), Link::Error);
394}
395
396BOOST_AUTO_TEST_CASE(MissingNameEncoding)
397{
398 Block linkBlock(MissingNameLink, sizeof(MissingNameLink));
399 Link link;
400 BOOST_REQUIRE_THROW(link.wireDecode(linkBlock), Link::Error);
401}
402
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100403BOOST_AUTO_TEST_SUITE_END() // TestLink
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700404
405} // namespace tests
406} // namespace ndn