blob: ce7d56d6fa3b287865c15c19e751bc9f7ea0539b [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[] = {
Alexander Afanasyev4b456282014-02-13 00:34:34 -080016 0x05, 0x41, // NDN Interest
17 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,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080024 0x09, 0x1f, // Selectors
25 0x0d, 0x1, 0x1, // MinSuffix
26 0x0e, 0x1, 0x1, // MaxSuffix
27 0x10, 0x14, // Exclude
28 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080029 0x61, 0x6c, 0x65, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080030 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080031 0x78, 0x78, 0x78, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080032 0x13, 0x0, // Any
33 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080034 0x79, 0x79, 0x79, 0x79,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080035 0x11, 0x1, // ChildSelector
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080036 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080037 0x0a, 0x1, // Nonce
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080038 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080039 0x0b, 0x1, // Scope
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080040 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080041 0x0c, // InterestLifetime
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080042 0x2, 0x3, 0xe8
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080043};
44
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080045const uint8_t InterestWithLocalControlHeader[] = {
46 0x50, 0x22, 0x51, 0x01, 0x0a, 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61,
47 0x6c, 0x08, 0x03, 0x6e, 0x64, 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09,
48 0x02, 0x12, 0x00, 0x0a, 0x01, 0x01
49};
50
51const uint8_t InterestWithoutLocalControlHeader[] = {
52 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x08, 0x03, 0x6e, 0x64,
53 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09, 0x02, 0x12, 0x00, 0x0a, 0x01,
54 0x01
55};
56
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080057BOOST_AUTO_TEST_CASE (Decode)
58{
59 Block interestBlock(Interest1, sizeof(Interest1));
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080060
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080061 ndn::Interest i;
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080062 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080063
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080064 BOOST_REQUIRE_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
65 BOOST_REQUIRE_EQUAL(i.getScope(), 1);
66 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), 1000);
67 BOOST_REQUIRE_EQUAL(i.getMinSuffixComponents(), 1);
68 BOOST_REQUIRE_EQUAL(i.getMaxSuffixComponents(), 1);
69 BOOST_REQUIRE_EQUAL(i.getChildSelector(), 1);
70 BOOST_REQUIRE_EQUAL(i.getMustBeFresh(), false);
71 BOOST_REQUIRE_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
Alexander Afanasyev840139f2013-12-28 15:02:50 -080072 BOOST_REQUIRE_EQUAL(i.getNonce(), 1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080073}
74
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080075BOOST_AUTO_TEST_CASE (DecodeFromStream)
76{
77 boost::iostreams::stream<boost::iostreams::array_source> is (reinterpret_cast<const char *>(Interest1), sizeof(Interest1));
78
79 Block interestBlock(is);
80
81 ndn::Interest i;
82 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
83
84 BOOST_REQUIRE_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
85 BOOST_REQUIRE_EQUAL(i.getScope(), 1);
86 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), 1000);
87 BOOST_REQUIRE_EQUAL(i.getMinSuffixComponents(), 1);
88 BOOST_REQUIRE_EQUAL(i.getMaxSuffixComponents(), 1);
89 BOOST_REQUIRE_EQUAL(i.getChildSelector(), 1);
90 BOOST_REQUIRE_EQUAL(i.getMustBeFresh(), false);
91 BOOST_REQUIRE_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
92 BOOST_REQUIRE_EQUAL(i.getNonce(), 1);
93}
94
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080095BOOST_AUTO_TEST_CASE (Encode)
96{
97 ndn::Interest i(ndn::Name("/local/ndn/prefix"));
98 i.setScope(1);
99 i.setInterestLifetime(1000);
100 i.setMinSuffixComponents(1);
101 i.setMaxSuffixComponents(1);
102 i.setChildSelector(1);
103 i.setMustBeFresh(false);
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800104 Exclude exclude;
105 exclude
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800106 .excludeOne(name::Component("alex"))
107 .excludeRange(name::Component("xxxx"), name::Component("yyyy"));
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800108 i.setExclude(exclude);
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800109 i.setNonce(1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800110
111 const Block &wire = i.wireEncode();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800112
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800113 BOOST_CHECK_EQUAL_COLLECTIONS(Interest1, Interest1+sizeof(Interest1),
114 wire.begin(), wire.end());
115}
116
117
118BOOST_AUTO_TEST_CASE(EncodeWithLocalHeader)
119{
120 {
121 ndn::Interest i1(ndn::Name("/local/ndn/prefix"));
122 i1.setMustBeFresh(true);
123 i1.setIncomingFaceId(10);
124 i1.setNonce(1);
125
126 BOOST_CHECK(!i1.hasWire());
127 const Block* block1 = &i1.getLocalControlHeader().wireEncode(i1);
128
129 BOOST_CHECK_EQUAL(block1, &i1.getLocalControlHeader().wireEncode(i1));
130 BOOST_CHECK(i1.hasWire());
131 BOOST_CHECK_NE(block1, &i1.wireEncode());
132 BOOST_CHECK_NE(block1->wire(), i1.wireEncode().wire());
133 BOOST_CHECK_NE(block1->size(), i1.wireEncode().size());
134
135 BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithLocalControlHeader,
136 InterestWithLocalControlHeader+sizeof(InterestWithLocalControlHeader),
137 block1->begin(), block1->end());
138
139 Block savedBlock1 = *block1;
140
141 i1.setNonce(1000);
142 BOOST_CHECK(!i1.hasWire());
143
144 const Block* block4 = &i1.getLocalControlHeader().wireEncode(i1);
145 BOOST_CHECK_EQUAL(block4, &i1.getLocalControlHeader().wireEncode(i1));
146 BOOST_CHECK_NE((void*)block4->wire(), (void*)savedBlock1.wire());
147 BOOST_CHECK_NE(block4->size(), savedBlock1.size());
148 }
149
150 {
151 ndn::Interest i1(ndn::Name("/local/ndn/prefix"));
152 i1.setMustBeFresh(true);
153 i1.setIncomingFaceId(10);
154 i1.setNonce(1);
155 i1.wireEncode(); // Encode with reserve for LocalControlHeader
156
157 const void* savedWire = i1.wireEncode().wire();
158 const Block* block1 = &i1.getLocalControlHeader().wireEncode(i1);
159
160 BOOST_CHECK_EQUAL((const void*)i1.wireEncode().wire(), savedWire);
161 BOOST_CHECK_EQUAL(i1.wireEncode().wire() - block1->wire(), 5);
162 }
163
164 {
165 ndn::Interest i1(ndn::Name("/local/ndn/prefix"));
166 i1.setMustBeFresh(true);
167 i1.setIncomingFaceId(10);
168 i1.setNonce(1);
169
170 EncodingBuffer buffer(31,0); // compared to previous version, there is not reserve for LocalControlHeader
171 i1.wireEncode(buffer);
172 i1.wireDecode(buffer.block());
173
174 const void* savedWire = i1.wireEncode().wire();
175 const Block* block1 = &i1.getLocalControlHeader().wireEncode(i1);
176
177 BOOST_CHECK_EQUAL((const void*)i1.wireEncode().wire(), savedWire);
178 BOOST_CHECK_NE(i1.wireEncode().wire() - block1->wire(), 5);
179 }
180
181 {
182 ndn::Interest i2(ndn::Name("/local/ndn/prefix"));
183 i2.setMustBeFresh(true);
184 i2.setNonce(1);
185
186 BOOST_CHECK(!i2.hasWire());
187 const Block* block2 = &i2.getLocalControlHeader().wireEncode(i2);
188 BOOST_CHECK_EQUAL(block2, &i2.getLocalControlHeader().wireEncode(i2));
189 BOOST_CHECK(i2.hasWire());
190 BOOST_CHECK_NE(block2, &i2.wireEncode());
191 BOOST_CHECK_EQUAL(block2->wire(), i2.wireEncode().wire());
192 BOOST_CHECK_EQUAL(block2->size(), i2.wireEncode().size());
193
194 Block savedBlock2 = *block2;
195
196 BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithoutLocalControlHeader,
197 InterestWithoutLocalControlHeader+sizeof(InterestWithoutLocalControlHeader),
198 block2->begin(), block2->end());
199
200 i2.setNonce(1);
201 BOOST_CHECK(!i2.hasWire());
202
203 const Block* block4 = &i2.getLocalControlHeader().wireEncode(i2);
204 BOOST_CHECK_EQUAL(block4, &i2.getLocalControlHeader().wireEncode(i2));
205 BOOST_CHECK_NE((void*)block4->wire(), (void*)savedBlock2.wire());
206 BOOST_CHECK_EQUAL(block4->size(), savedBlock2.size());
207 }
208}
209
210BOOST_AUTO_TEST_CASE (DecodeWithLocalHeader)
211{
212 Block b1(InterestWithLocalControlHeader, sizeof(InterestWithLocalControlHeader));
213 const Block& block1 = nfd::LocalControlHeader::getPayload(b1);
214 BOOST_REQUIRE_NE(&block1, &b1);
215
216 BOOST_CHECK_EQUAL(block1.type(), (uint32_t)Tlv::Interest);
217 BOOST_CHECK_EQUAL(b1.type(), (uint32_t)tlv::nfd::LocalControlHeader);
218
219 Interest i(block1);
220 BOOST_CHECK(!i.getLocalControlHeader().hasIncomingFaceId());
221 BOOST_CHECK(!i.getLocalControlHeader().hasNextHopFaceId());
222
223 BOOST_REQUIRE_NO_THROW(i.getLocalControlHeader().wireDecode(b1));
224
225 BOOST_CHECK_EQUAL(i.getIncomingFaceId(), 10);
226 BOOST_CHECK(!i.getLocalControlHeader().hasNextHopFaceId());
227
228 BOOST_CHECK_EQUAL((void*)i.getLocalControlHeader().wireEncode(i).wire(), (void*)b1.wire());
229
230 //
231
232 Block b2(InterestWithoutLocalControlHeader, sizeof(InterestWithoutLocalControlHeader));
233 const Block& block2 = nfd::LocalControlHeader::getPayload(b2);
234 BOOST_CHECK_EQUAL(&block2, &b2);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800235}
236
237BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800238
239} // namespace ndn