blob: 2583f98456b458a4a2e1deb307097215147c6421 [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
6#include <boost/test/unit_test.hpp>
7
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08008#include "interest.hpp"
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -08009
10using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080011namespace ndn {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080012
13BOOST_AUTO_TEST_SUITE(TestInterest)
14
15const uint8_t Interest1[] = {
Junxiao Shib332e782014-03-31 14:23:46 -070016 0x05, 0x59, // NDN Interest
Alexander Afanasyev4b456282014-02-13 00:34:34 -080017 0x07, 0x14, // Name
18 0x08, 0x5, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080019 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080020 0x08, 0x3, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080021 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080022 0x08, 0x6, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080023 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
Junxiao Shib332e782014-03-31 14:23:46 -070024 0x09, 0x37, // Selectors
Alexander Afanasyev4b456282014-02-13 00:34:34 -080025 0x0d, 0x1, 0x1, // MinSuffix
26 0x0e, 0x1, 0x1, // MaxSuffix
Junxiao Shib332e782014-03-31 14:23:46 -070027 0x1c, 0x16, // KeyLocator
28 0x07, 0x14, // Name
29 0x08, 0x04,
30 0x74, 0x65, 0x73, 0x74,
31 0x08, 0x03,
32 0x6b, 0x65, 0x79,
33 0x08, 0x07,
34 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080035 0x10, 0x14, // Exclude
36 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080037 0x61, 0x6c, 0x65, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080038 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080039 0x78, 0x78, 0x78, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080040 0x13, 0x0, // Any
41 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080042 0x79, 0x79, 0x79, 0x79,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080043 0x11, 0x1, // ChildSelector
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080044 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080045 0x0a, 0x1, // Nonce
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080046 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080047 0x0b, 0x1, // Scope
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080048 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080049 0x0c, // InterestLifetime
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080050 0x2, 0x3, 0xe8
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080051};
52
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080053const uint8_t InterestWithLocalControlHeader[] = {
54 0x50, 0x22, 0x51, 0x01, 0x0a, 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61,
55 0x6c, 0x08, 0x03, 0x6e, 0x64, 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09,
56 0x02, 0x12, 0x00, 0x0a, 0x01, 0x01
57};
58
59const uint8_t InterestWithoutLocalControlHeader[] = {
60 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x08, 0x03, 0x6e, 0x64,
61 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09, 0x02, 0x12, 0x00, 0x0a, 0x01,
62 0x01
63};
64
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080065BOOST_AUTO_TEST_CASE (Decode)
66{
67 Block interestBlock(Interest1, sizeof(Interest1));
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080068
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080069 ndn::Interest i;
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080070 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080071
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080072 BOOST_REQUIRE_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
73 BOOST_REQUIRE_EQUAL(i.getScope(), 1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080075 BOOST_REQUIRE_EQUAL(i.getMinSuffixComponents(), 1);
76 BOOST_REQUIRE_EQUAL(i.getMaxSuffixComponents(), 1);
Junxiao Shib332e782014-03-31 14:23:46 -070077 BOOST_REQUIRE_EQUAL(i.getPublisherPublicKeyLocator().getType(),
78 static_cast<uint32_t>(KeyLocator::KeyLocator_Name));
79 BOOST_REQUIRE_EQUAL(i.getPublisherPublicKeyLocator().getName(), "ndn:/test/key/locator");
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080080 BOOST_REQUIRE_EQUAL(i.getChildSelector(), 1);
81 BOOST_REQUIRE_EQUAL(i.getMustBeFresh(), false);
82 BOOST_REQUIRE_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
Alexander Afanasyev840139f2013-12-28 15:02:50 -080083 BOOST_REQUIRE_EQUAL(i.getNonce(), 1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080084}
85
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080086BOOST_AUTO_TEST_CASE (DecodeFromStream)
87{
88 boost::iostreams::stream<boost::iostreams::array_source> is (reinterpret_cast<const char *>(Interest1), sizeof(Interest1));
Junxiao Shib332e782014-03-31 14:23:46 -070089
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080090 Block interestBlock(is);
91
92 ndn::Interest i;
93 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
94
95 BOOST_REQUIRE_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
96 BOOST_REQUIRE_EQUAL(i.getScope(), 1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070097 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080098 BOOST_REQUIRE_EQUAL(i.getMinSuffixComponents(), 1);
99 BOOST_REQUIRE_EQUAL(i.getMaxSuffixComponents(), 1);
100 BOOST_REQUIRE_EQUAL(i.getChildSelector(), 1);
101 BOOST_REQUIRE_EQUAL(i.getMustBeFresh(), false);
102 BOOST_REQUIRE_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
103 BOOST_REQUIRE_EQUAL(i.getNonce(), 1);
104}
105
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800106BOOST_AUTO_TEST_CASE (Encode)
107{
108 ndn::Interest i(ndn::Name("/local/ndn/prefix"));
109 i.setScope(1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800111 i.setMinSuffixComponents(1);
112 i.setMaxSuffixComponents(1);
Junxiao Shib332e782014-03-31 14:23:46 -0700113 i.setPublisherPublicKeyLocator(KeyLocator("ndn:/test/key/locator"));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800114 i.setChildSelector(1);
115 i.setMustBeFresh(false);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800116 Exclude exclude;
117 exclude
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800118 .excludeOne(name::Component("alex"))
119 .excludeRange(name::Component("xxxx"), name::Component("yyyy"));
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800120 i.setExclude(exclude);
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800121 i.setNonce(1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800122
123 const Block &wire = i.wireEncode();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800124
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800125 BOOST_CHECK_EQUAL_COLLECTIONS(Interest1, Interest1+sizeof(Interest1),
126 wire.begin(), wire.end());
127}
128
129
130BOOST_AUTO_TEST_CASE(EncodeWithLocalHeader)
131{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800132 ndn::Interest interest(ndn::Name("/local/ndn/prefix"));
133 interest.setMustBeFresh(true);
134 interest.setIncomingFaceId(10);
135 interest.setNonce(1);
Junxiao Shib332e782014-03-31 14:23:46 -0700136
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800137 BOOST_CHECK(!interest.hasWire());
Junxiao Shib332e782014-03-31 14:23:46 -0700138
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800139 Block headerBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800140
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800141 BOOST_CHECK(interest.hasWire());
142 BOOST_CHECK(headerBlock.hasWire());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800143
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800144 BOOST_CHECK_NE(headerBlock.wire(), interest.wireEncode().wire());
145 BOOST_CHECK_NE(headerBlock.size(), interest.wireEncode().size());
146 BOOST_CHECK_EQUAL(headerBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800147
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800148 BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithLocalControlHeader,
149 InterestWithLocalControlHeader+5,
150 headerBlock.begin(), headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800151
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800152 interest.setNonce(1000);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800153
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800154 Block updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
155 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800156
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800157 // only length should have changed
158 BOOST_CHECK_EQUAL_COLLECTIONS(updatedHeaderBlock.begin()+2, updatedHeaderBlock.end(),
159 headerBlock.begin()+2, headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800160
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800161 // updating IncomingFaceId that keeps the length
162 interest.setIncomingFaceId(100);
163 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
164 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
165 BOOST_CHECK_NE(*(updatedHeaderBlock.begin()+4), *(headerBlock.begin()+4));
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800166
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800167 // updating IncomingFaceId that increases the length by 2
168 interest.setIncomingFaceId(1000);
169 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
170 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800171
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800172 // adding NextHopId
173 interest.setNextHopFaceId(1);
174 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
175 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 9);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800176
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800177 // masking IncomingFaceId
178 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, false, true);
179 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800180
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800181 // masking NextHopId
182 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, false);
183 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800184
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800185 // masking everything
186 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
187 nfd::LocalControlHeader::Error);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800188}
189
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800190
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800191BOOST_AUTO_TEST_CASE (DecodeWithLocalHeader)
192{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800193 Block wireBlock(InterestWithLocalControlHeader, sizeof(InterestWithLocalControlHeader));
194 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
195 BOOST_REQUIRE_NE(&payload, &wireBlock);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800196
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800197 BOOST_CHECK_EQUAL(payload.type(), (uint32_t)Tlv::Interest);
198 BOOST_CHECK_EQUAL(wireBlock.type(), (uint32_t)tlv::nfd::LocalControlHeader);
Junxiao Shib332e782014-03-31 14:23:46 -0700199
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800200 Interest interest(payload);
201 BOOST_CHECK(!interest.getLocalControlHeader().hasIncomingFaceId());
202 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
203
204 BOOST_REQUIRE_NO_THROW(interest.getLocalControlHeader().wireDecode(wireBlock));
205
206 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
Junxiao Shib332e782014-03-31 14:23:46 -0700207
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800208 BOOST_CHECK_EQUAL(interest.getIncomingFaceId(), 10);
209 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
210
211 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
212 nfd::LocalControlHeader::Error);
213
214 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, true),
215 nfd::LocalControlHeader::Error);
216
217 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, false));
218 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, true));
Junxiao Shib332e782014-03-31 14:23:46 -0700219
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800220 BOOST_CHECK_NE((void*)interest.getLocalControlHeader().wireEncode(interest, true, true).wire(),
221 (void*)wireBlock.wire());
222
223 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
224}
225
226BOOST_AUTO_TEST_CASE (DecodeWithoutLocalHeader)
227{
228 Block wireBlock(InterestWithoutLocalControlHeader, sizeof(InterestWithoutLocalControlHeader));
229 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
230 BOOST_CHECK_EQUAL(&payload, &wireBlock);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800231}
232
233BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800234
235} // namespace ndn