blob: 321685e2284043e3b0dcf40a90e0800463b80f26 [file] [log] [blame]
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -08001/**
2 * Copyright (C) 2013 Regents of the University of California.
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -08003 * See COPYING for copyright and distribution information.
4 */
5
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08006#include "interest.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -07007#include "data.hpp"
8#include "security/signature-sha256-with-rsa.hpp"
9#include "security/signature-sha256.hpp"
10
11#include <boost/test/unit_test.hpp>
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080012
13using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080014namespace ndn {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080015
16BOOST_AUTO_TEST_SUITE(TestInterest)
17
18const uint8_t Interest1[] = {
Junxiao Shib332e782014-03-31 14:23:46 -070019 0x05, 0x59, // NDN Interest
Alexander Afanasyev4b456282014-02-13 00:34:34 -080020 0x07, 0x14, // Name
21 0x08, 0x5, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080022 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080023 0x08, 0x3, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080024 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080025 0x08, 0x6, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080026 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
Junxiao Shib332e782014-03-31 14:23:46 -070027 0x09, 0x37, // Selectors
Alexander Afanasyev4b456282014-02-13 00:34:34 -080028 0x0d, 0x1, 0x1, // MinSuffix
29 0x0e, 0x1, 0x1, // MaxSuffix
Junxiao Shib332e782014-03-31 14:23:46 -070030 0x1c, 0x16, // KeyLocator
31 0x07, 0x14, // Name
32 0x08, 0x04,
33 0x74, 0x65, 0x73, 0x74,
34 0x08, 0x03,
35 0x6b, 0x65, 0x79,
36 0x08, 0x07,
37 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080038 0x10, 0x14, // Exclude
39 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080040 0x61, 0x6c, 0x65, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080041 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080042 0x78, 0x78, 0x78, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080043 0x13, 0x0, // Any
44 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080045 0x79, 0x79, 0x79, 0x79,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080046 0x11, 0x1, // ChildSelector
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080047 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080048 0x0a, 0x1, // Nonce
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080049 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080050 0x0b, 0x1, // Scope
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080051 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080052 0x0c, // InterestLifetime
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080053 0x2, 0x3, 0xe8
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080054};
55
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080056const uint8_t InterestWithLocalControlHeader[] = {
57 0x50, 0x22, 0x51, 0x01, 0x0a, 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61,
58 0x6c, 0x08, 0x03, 0x6e, 0x64, 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09,
59 0x02, 0x12, 0x00, 0x0a, 0x01, 0x01
60};
61
62const uint8_t InterestWithoutLocalControlHeader[] = {
63 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x08, 0x03, 0x6e, 0x64,
64 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09, 0x02, 0x12, 0x00, 0x0a, 0x01,
65 0x01
66};
67
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080068BOOST_AUTO_TEST_CASE (Decode)
69{
70 Block interestBlock(Interest1, sizeof(Interest1));
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080071
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080072 ndn::Interest i;
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080073 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080074
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080075 BOOST_REQUIRE_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
76 BOOST_REQUIRE_EQUAL(i.getScope(), 1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080078 BOOST_REQUIRE_EQUAL(i.getMinSuffixComponents(), 1);
79 BOOST_REQUIRE_EQUAL(i.getMaxSuffixComponents(), 1);
Junxiao Shib332e782014-03-31 14:23:46 -070080 BOOST_REQUIRE_EQUAL(i.getPublisherPublicKeyLocator().getType(),
81 static_cast<uint32_t>(KeyLocator::KeyLocator_Name));
82 BOOST_REQUIRE_EQUAL(i.getPublisherPublicKeyLocator().getName(), "ndn:/test/key/locator");
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080083 BOOST_REQUIRE_EQUAL(i.getChildSelector(), 1);
84 BOOST_REQUIRE_EQUAL(i.getMustBeFresh(), false);
85 BOOST_REQUIRE_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
Alexander Afanasyev840139f2013-12-28 15:02:50 -080086 BOOST_REQUIRE_EQUAL(i.getNonce(), 1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080087}
88
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080089BOOST_AUTO_TEST_CASE (DecodeFromStream)
90{
91 boost::iostreams::stream<boost::iostreams::array_source> is (reinterpret_cast<const char *>(Interest1), sizeof(Interest1));
Junxiao Shib332e782014-03-31 14:23:46 -070092
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080093 Block interestBlock(is);
94
95 ndn::Interest i;
96 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
97
98 BOOST_REQUIRE_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
99 BOOST_REQUIRE_EQUAL(i.getScope(), 1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
Alexander Afanasyev6835ad82014-02-12 10:07:20 -0800101 BOOST_REQUIRE_EQUAL(i.getMinSuffixComponents(), 1);
102 BOOST_REQUIRE_EQUAL(i.getMaxSuffixComponents(), 1);
103 BOOST_REQUIRE_EQUAL(i.getChildSelector(), 1);
104 BOOST_REQUIRE_EQUAL(i.getMustBeFresh(), false);
105 BOOST_REQUIRE_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
106 BOOST_REQUIRE_EQUAL(i.getNonce(), 1);
107}
108
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800109BOOST_AUTO_TEST_CASE (Encode)
110{
111 ndn::Interest i(ndn::Name("/local/ndn/prefix"));
112 i.setScope(1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700113 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800114 i.setMinSuffixComponents(1);
115 i.setMaxSuffixComponents(1);
Junxiao Shib332e782014-03-31 14:23:46 -0700116 i.setPublisherPublicKeyLocator(KeyLocator("ndn:/test/key/locator"));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800117 i.setChildSelector(1);
118 i.setMustBeFresh(false);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800119 Exclude exclude;
120 exclude
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800121 .excludeOne(name::Component("alex"))
122 .excludeRange(name::Component("xxxx"), name::Component("yyyy"));
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800123 i.setExclude(exclude);
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800124 i.setNonce(1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800125
126 const Block &wire = i.wireEncode();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800127
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800128 BOOST_CHECK_EQUAL_COLLECTIONS(Interest1, Interest1+sizeof(Interest1),
129 wire.begin(), wire.end());
130}
131
132
133BOOST_AUTO_TEST_CASE(EncodeWithLocalHeader)
134{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800135 ndn::Interest interest(ndn::Name("/local/ndn/prefix"));
136 interest.setMustBeFresh(true);
137 interest.setIncomingFaceId(10);
138 interest.setNonce(1);
Junxiao Shib332e782014-03-31 14:23:46 -0700139
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800140 BOOST_CHECK(!interest.hasWire());
Junxiao Shib332e782014-03-31 14:23:46 -0700141
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800142 Block headerBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800143
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800144 BOOST_CHECK(interest.hasWire());
145 BOOST_CHECK(headerBlock.hasWire());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800146
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800147 BOOST_CHECK_NE(headerBlock.wire(), interest.wireEncode().wire());
148 BOOST_CHECK_NE(headerBlock.size(), interest.wireEncode().size());
149 BOOST_CHECK_EQUAL(headerBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800150
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800151 BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithLocalControlHeader,
152 InterestWithLocalControlHeader+5,
153 headerBlock.begin(), headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800154
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800155 interest.setNonce(1000);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800156
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800157 Block updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
158 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800159
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800160 // only length should have changed
161 BOOST_CHECK_EQUAL_COLLECTIONS(updatedHeaderBlock.begin()+2, updatedHeaderBlock.end(),
162 headerBlock.begin()+2, headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800163
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800164 // updating IncomingFaceId that keeps the length
165 interest.setIncomingFaceId(100);
166 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
167 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
168 BOOST_CHECK_NE(*(updatedHeaderBlock.begin()+4), *(headerBlock.begin()+4));
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800169
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800170 // updating IncomingFaceId that increases the length by 2
171 interest.setIncomingFaceId(1000);
172 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
173 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800174
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800175 // adding NextHopId
176 interest.setNextHopFaceId(1);
177 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
178 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 9);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800179
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800180 // masking IncomingFaceId
181 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, false, true);
182 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800183
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800184 // masking NextHopId
185 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, false);
186 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800187
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800188 // masking everything
189 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
190 nfd::LocalControlHeader::Error);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800191}
192
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800193
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800194BOOST_AUTO_TEST_CASE (DecodeWithLocalHeader)
195{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800196 Block wireBlock(InterestWithLocalControlHeader, sizeof(InterestWithLocalControlHeader));
197 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
198 BOOST_REQUIRE_NE(&payload, &wireBlock);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800199
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800200 BOOST_CHECK_EQUAL(payload.type(), (uint32_t)Tlv::Interest);
201 BOOST_CHECK_EQUAL(wireBlock.type(), (uint32_t)tlv::nfd::LocalControlHeader);
Junxiao Shib332e782014-03-31 14:23:46 -0700202
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800203 Interest interest(payload);
204 BOOST_CHECK(!interest.getLocalControlHeader().hasIncomingFaceId());
205 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
206
207 BOOST_REQUIRE_NO_THROW(interest.getLocalControlHeader().wireDecode(wireBlock));
208
209 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
Junxiao Shib332e782014-03-31 14:23:46 -0700210
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800211 BOOST_CHECK_EQUAL(interest.getIncomingFaceId(), 10);
212 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
213
214 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
215 nfd::LocalControlHeader::Error);
216
217 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, true),
218 nfd::LocalControlHeader::Error);
219
220 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, false));
221 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, true));
Junxiao Shib332e782014-03-31 14:23:46 -0700222
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800223 BOOST_CHECK_NE((void*)interest.getLocalControlHeader().wireEncode(interest, true, true).wire(),
224 (void*)wireBlock.wire());
225
226 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
227}
228
229BOOST_AUTO_TEST_CASE (DecodeWithoutLocalHeader)
230{
231 Block wireBlock(InterestWithoutLocalControlHeader, sizeof(InterestWithoutLocalControlHeader));
232 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
233 BOOST_CHECK_EQUAL(&payload, &wireBlock);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800234}
235
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700236BOOST_AUTO_TEST_CASE(MatchesData)
237{
238 Interest interest;
239 interest.setName("ndn:/A")
240 .setMinSuffixComponents(2)
241 .setMaxSuffixComponents(2)
242 .setPublisherPublicKeyLocator(KeyLocator("ndn:/B"))
243 .setExclude(Exclude().excludeBefore(name::Component("C")));
244
245 Data data("ndn:/A/D");
246 SignatureSha256WithRsa signature;
247 signature.setKeyLocator(KeyLocator("ndn:/B"));
248 data.setSignature(signature);
249 BOOST_CHECK_EQUAL(interest.matchesData(data), true);
250
251 Data data1 = data;
252 data1.setName("ndn:/A");// violates MinSuffixComponents
253 BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
254
255 Data data2 = data;
256 data2.setName("ndn:/A/E/F");// violates MaxSuffixComponents
257 BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
258
259 Data data3 = data;
260 SignatureSha256WithRsa signature3;
261 signature3.setKeyLocator(KeyLocator("ndn:/G"));// violates PublisherPublicKeyLocator
262 data3.setSignature(signature3);
263 BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
264
265 Data data4 = data;
266 SignatureSha256 signature4;// violates PublisherPublicKeyLocator
267 data4.setSignature(signature4);
268 BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
269
270 Data data5 = data;
271 data5.setName("ndn:/A/C");// violates Exclude
272 BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
273
274 Data data6 = data;
275 data6.setName("ndn:/H/I");// violates Name
276 BOOST_CHECK_EQUAL(interest.matchesData(data6), false);
277}
278
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800279BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800280
281} // namespace ndn