blob: a12793a20ff828cf401809022fe757549ec2c556 [file] [log] [blame]
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070023#include "security/v1/cryptopp.hpp"
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070024#include "encoding/buffer-stream.hpp"
25
26#include "boost-test.hpp"
Junxiao Shid21abd32017-06-30 02:56:40 +000027#include "make-interest-data.hpp"
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070028
29namespace ndn {
30namespace tests {
31
Davide Pesaventoeee3e822016-11-26 19:19:34 +010032BOOST_AUTO_TEST_SUITE(TestLink)
33
Junxiao Shid21abd32017-06-30 02:56:40 +000034const uint8_t GOOD_LINK[] = {
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700350x06, 0xda, // Data
36 0x07, 0x14, // Name
37 0x08, 0x05,
38 0x6c, 0x6f, 0x63, 0x61, 0x6c,
39 0x08, 0x03,
40 0x6e, 0x64, 0x6e,
41 0x08, 0x06,
42 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
43 0x14, 0x07, // MetaInfo
44 0x18, 0x01, // ContentType
45 0x01,
46 0x19, 0x02, // FreshnessPeriod
47 0x27, 0x10,
48 0x15, 0x1a, // Content
49 0x1f, 0x0c, // LinkDelegation
50 0x1e, 0x01, // LinkPreference
51 0x0a,
52 0x07, 0x07, // Name
53 0x08, 0x05,
54 0x6c, 0x6f, 0x63, 0x61, 0x6c,
55 0x1f, 0x0a, // LinkDelegation
56 0x1e, 0x01, // LinkPreference
57 0x14,
58 0x07, 0x05, // Name
59 0x08, 0x03,
60 0x6e, 0x64, 0x6e,
61 0x16, 0x1b, // SignatureInfo
62 0x1b, 0x01, // SignatureType
63 0x01,
64 0x1c, 0x16, // KeyLocator
65 0x07, 0x14, // Name
66 0x08, 0x04,
67 0x74, 0x65, 0x73, 0x74,
68 0x08, 0x03,
69 0x6b, 0x65, 0x79,
70 0x08, 0x07,
71 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
72 0x17, 0x80, // SignatureValue
73 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
74 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
75 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
76 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
77 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
78 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
79 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
80 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
81 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
82 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
83};
84
Junxiao Shid21abd32017-06-30 02:56:40 +000085BOOST_AUTO_TEST_SUITE(EncodeDecode)
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070086
87BOOST_AUTO_TEST_CASE(Decode)
88{
Junxiao Shid21abd32017-06-30 02:56:40 +000089 Link link(Block(GOOD_LINK, sizeof(GOOD_LINK)));
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070090 BOOST_CHECK_EQUAL(link.getName(), Name("/local/ndn/prefix"));
Junxiao Shid21abd32017-06-30 02:56:40 +000091 BOOST_CHECK_EQUAL(link.getDelegationList(),
92 DelegationList({{10, Name("/local")}, {20, Name("/ndn")}}));
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070093}
94
Junxiao Shid21abd32017-06-30 02:56:40 +000095BOOST_AUTO_TEST_CASE(DecodeBadContentType)
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070096{
Junxiao Shid21abd32017-06-30 02:56:40 +000097 Data linkData(Block(GOOD_LINK, sizeof(GOOD_LINK)));
98 linkData.setContentType(tlv::ContentType_Key);
99 Block badLink = linkData.wireEncode();
100
101 BOOST_CHECK_THROW((Link(badLink)), Link::Error);
102 Link link;
103 BOOST_CHECK_THROW(link.wireDecode(badLink), Link::Error);
104}
105
106BOOST_AUTO_TEST_CASE(Encode)
107{
108 Link link1("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
109 signData(link1);
110 Block wire = link1.wireEncode();
111
112 Link link2(wire);
113 BOOST_CHECK_EQUAL(link2.getName(), "/test");
114 BOOST_CHECK_EQUAL(link2.getDelegationList(),
115 DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}));
116}
117
118BOOST_AUTO_TEST_SUITE_END() // EncodeDecode
119
120BOOST_AUTO_TEST_SUITE(Modify)
121
122BOOST_AUTO_TEST_CASE(SetDelegationList)
123{
124 Link link("/test");
125 BOOST_CHECK_EQUAL(link.getDelegationList(), DelegationList());
126
127 link.setDelegationList(DelegationList({{10, "/test1"}, {20, "/test2"}}));
128 BOOST_CHECK_EQUAL(link.getDelegationList(), DelegationList({{10, "/test1"}, {20, "/test2"}}));
129}
130
131BOOST_AUTO_TEST_CASE(AddDelegation)
132{
133 Link link1("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
134 BOOST_CHECK_EQUAL(link1.getDelegationList(),
135 DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}));
136
137 link1.addDelegation(30, "/test4");
138 BOOST_CHECK_EQUAL(link1.getDelegationList(),
139 DelegationList({{10, "/test1"}, {20, "/test2"}, {30, "/test4"}, {100, "/test3"}}));
140
141 link1.addDelegation(40, "/test2");
142 BOOST_CHECK_EQUAL(link1.getDelegationList(),
143 DelegationList({{10, "/test1"}, {30, "/test4"}, {40, "/test2"}, {100, "/test3"}}));
144
145 signData(link1);
146 Link link2(link1.wireEncode());
147 BOOST_CHECK_EQUAL(link2.getDelegationList(),
148 DelegationList({{10, "/test1"}, {30, "/test4"}, {40, "/test2"}, {100, "/test3"}}));
149}
150
151BOOST_AUTO_TEST_CASE(RemoveDelegation)
152{
153 Link link1("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
154 BOOST_CHECK_EQUAL(link1.getDelegationList(),
155 DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}));
156
157 link1.removeDelegation("/test4"); // non-existent
158 BOOST_CHECK_EQUAL(link1.getDelegationList(),
159 DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}));
160
161 link1.removeDelegation("/test2");
162 BOOST_CHECK_EQUAL(link1.getDelegationList(),
163 DelegationList({{10, "/test1"}, {100, "/test3"}}));
164
165 signData(link1);
166 Link link2(link1.wireEncode());
167 BOOST_CHECK_EQUAL(link2.getDelegationList(),
168 DelegationList({{10, "/test1"}, {100, "/test3"}}));
169
170 link1.removeDelegation("/test1");
171 link1.removeDelegation("/test3");
172 BOOST_CHECK_EQUAL(link1.getDelegationList(), DelegationList());
173}
174
175BOOST_AUTO_TEST_SUITE_END() // Modify
176
177BOOST_AUTO_TEST_SUITE(Deprecated)
178
179BOOST_AUTO_TEST_CASE(PairInitializerList)
180{
181 Link link("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
182 Link::DelegationSet ds = link.getDelegations();
183 BOOST_REQUIRE_EQUAL(ds.size(), 3);
184 BOOST_CHECK_EQUAL(ds.begin()->first, 10);
185 BOOST_CHECK_EQUAL(ds.begin()->second, "/test1");
186}
187
188BOOST_AUTO_TEST_CASE(DelegationSet)
189{
190 Link link("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
191 Link::DelegationSet ds = link.getDelegations();
192 BOOST_REQUIRE_EQUAL(ds.size(), 3);
193 BOOST_CHECK_EQUAL(ds.begin()->first, 10);
194 BOOST_CHECK_EQUAL(ds.begin()->second, "/test1");
195}
196
197BOOST_AUTO_TEST_CASE(FromWire)
198{
199 Block linkBlock(GOOD_LINK, sizeof(GOOD_LINK));
200
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700201 BOOST_CHECK_EQUAL(Link::countDelegationsFromWire(linkBlock), 2);
Junxiao Shid21abd32017-06-30 02:56:40 +0000202
203 auto del0 = Link::getDelegationFromWire(linkBlock, 0);
204 BOOST_CHECK_EQUAL(std::get<0>(del0), 10);
205 BOOST_CHECK_EQUAL(std::get<1>(del0), "/local");
206 auto del1 = Link::getDelegationFromWire(linkBlock, 1);
207 BOOST_CHECK_EQUAL(std::get<0>(del1), 20);
208 BOOST_CHECK_EQUAL(std::get<1>(del1), "/ndn");
209 BOOST_CHECK_THROW(Link::getDelegationFromWire(linkBlock, 2), std::out_of_range);
210
211 BOOST_CHECK_EQUAL(Link::findDelegationFromWire(linkBlock, "/local"), 0);
212 BOOST_CHECK_EQUAL(Link::findDelegationFromWire(linkBlock, "/none"), -1);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700213}
214
Junxiao Shid21abd32017-06-30 02:56:40 +0000215BOOST_AUTO_TEST_SUITE_END() // Deprecated
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700216
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100217BOOST_AUTO_TEST_SUITE_END() // TestLink
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700218
219} // namespace tests
220} // namespace ndn