blob: 1042d35c0bd5f7aa1c53931eba222155c0b6af93 [file] [log] [blame]
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc2e12b42017-07-09 20:35:09 -04002/*
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05003 * Copyright (c) 2013-2020 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/link.hpp"
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070023
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050024#include "tests/test-common.hpp"
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070025
26namespace ndn {
27namespace tests {
28
Davide Pesaventoeee3e822016-11-26 19:19:34 +010029BOOST_AUTO_TEST_SUITE(TestLink)
30
Junxiao Shid21abd32017-06-30 02:56:40 +000031const uint8_t GOOD_LINK[] = {
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700320x06, 0xda, // Data
33 0x07, 0x14, // Name
34 0x08, 0x05,
35 0x6c, 0x6f, 0x63, 0x61, 0x6c,
36 0x08, 0x03,
37 0x6e, 0x64, 0x6e,
38 0x08, 0x06,
39 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
40 0x14, 0x07, // MetaInfo
41 0x18, 0x01, // ContentType
42 0x01,
43 0x19, 0x02, // FreshnessPeriod
44 0x27, 0x10,
45 0x15, 0x1a, // Content
46 0x1f, 0x0c, // LinkDelegation
47 0x1e, 0x01, // LinkPreference
48 0x0a,
49 0x07, 0x07, // Name
50 0x08, 0x05,
51 0x6c, 0x6f, 0x63, 0x61, 0x6c,
52 0x1f, 0x0a, // LinkDelegation
53 0x1e, 0x01, // LinkPreference
54 0x14,
55 0x07, 0x05, // Name
56 0x08, 0x03,
57 0x6e, 0x64, 0x6e,
58 0x16, 0x1b, // SignatureInfo
59 0x1b, 0x01, // SignatureType
60 0x01,
61 0x1c, 0x16, // KeyLocator
62 0x07, 0x14, // Name
63 0x08, 0x04,
64 0x74, 0x65, 0x73, 0x74,
65 0x08, 0x03,
66 0x6b, 0x65, 0x79,
67 0x08, 0x07,
68 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
69 0x17, 0x80, // SignatureValue
70 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
71 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
72 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
73 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
74 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
75 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
76 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
77 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
78 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
79 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
80};
81
Junxiao Shid21abd32017-06-30 02:56:40 +000082BOOST_AUTO_TEST_SUITE(EncodeDecode)
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070083
84BOOST_AUTO_TEST_CASE(Decode)
85{
Junxiao Shid21abd32017-06-30 02:56:40 +000086 Link link(Block(GOOD_LINK, sizeof(GOOD_LINK)));
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070087 BOOST_CHECK_EQUAL(link.getName(), Name("/local/ndn/prefix"));
Junxiao Shid21abd32017-06-30 02:56:40 +000088 BOOST_CHECK_EQUAL(link.getDelegationList(),
89 DelegationList({{10, Name("/local")}, {20, Name("/ndn")}}));
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070090}
91
Junxiao Shid21abd32017-06-30 02:56:40 +000092BOOST_AUTO_TEST_CASE(DecodeBadContentType)
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070093{
Junxiao Shid21abd32017-06-30 02:56:40 +000094 Data linkData(Block(GOOD_LINK, sizeof(GOOD_LINK)));
95 linkData.setContentType(tlv::ContentType_Key);
96 Block badLink = linkData.wireEncode();
97
98 BOOST_CHECK_THROW((Link(badLink)), Link::Error);
99 Link link;
100 BOOST_CHECK_THROW(link.wireDecode(badLink), Link::Error);
101}
102
103BOOST_AUTO_TEST_CASE(Encode)
104{
105 Link link1("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
106 signData(link1);
107 Block wire = link1.wireEncode();
108
109 Link link2(wire);
110 BOOST_CHECK_EQUAL(link2.getName(), "/test");
111 BOOST_CHECK_EQUAL(link2.getDelegationList(),
112 DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}));
113}
114
115BOOST_AUTO_TEST_SUITE_END() // EncodeDecode
116
117BOOST_AUTO_TEST_SUITE(Modify)
118
119BOOST_AUTO_TEST_CASE(SetDelegationList)
120{
121 Link link("/test");
122 BOOST_CHECK_EQUAL(link.getDelegationList(), DelegationList());
123
124 link.setDelegationList(DelegationList({{10, "/test1"}, {20, "/test2"}}));
125 BOOST_CHECK_EQUAL(link.getDelegationList(), DelegationList({{10, "/test1"}, {20, "/test2"}}));
126}
127
128BOOST_AUTO_TEST_CASE(AddDelegation)
129{
130 Link link1("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
131 BOOST_CHECK_EQUAL(link1.getDelegationList(),
132 DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}));
133
134 link1.addDelegation(30, "/test4");
135 BOOST_CHECK_EQUAL(link1.getDelegationList(),
136 DelegationList({{10, "/test1"}, {20, "/test2"}, {30, "/test4"}, {100, "/test3"}}));
137
138 link1.addDelegation(40, "/test2");
139 BOOST_CHECK_EQUAL(link1.getDelegationList(),
140 DelegationList({{10, "/test1"}, {30, "/test4"}, {40, "/test2"}, {100, "/test3"}}));
141
142 signData(link1);
143 Link link2(link1.wireEncode());
144 BOOST_CHECK_EQUAL(link2.getDelegationList(),
145 DelegationList({{10, "/test1"}, {30, "/test4"}, {40, "/test2"}, {100, "/test3"}}));
146}
147
148BOOST_AUTO_TEST_CASE(RemoveDelegation)
149{
150 Link link1("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}});
151 BOOST_CHECK_EQUAL(link1.getDelegationList(),
152 DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}));
153
154 link1.removeDelegation("/test4"); // non-existent
155 BOOST_CHECK_EQUAL(link1.getDelegationList(),
156 DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}));
157
158 link1.removeDelegation("/test2");
159 BOOST_CHECK_EQUAL(link1.getDelegationList(),
160 DelegationList({{10, "/test1"}, {100, "/test3"}}));
161
162 signData(link1);
163 Link link2(link1.wireEncode());
164 BOOST_CHECK_EQUAL(link2.getDelegationList(),
165 DelegationList({{10, "/test1"}, {100, "/test3"}}));
166
167 link1.removeDelegation("/test1");
168 link1.removeDelegation("/test3");
169 BOOST_CHECK_EQUAL(link1.getDelegationList(), DelegationList());
170}
171
172BOOST_AUTO_TEST_SUITE_END() // Modify
173
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100174BOOST_AUTO_TEST_SUITE_END() // TestLink
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700175
176} // namespace tests
177} // namespace ndn