blob: eca80660bf4bbdd25b104e59dd5b9c6709a1826c [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);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080067 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);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070086 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), time::milliseconds(1000));
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080087 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);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800100 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{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800120 ndn::Interest interest(ndn::Name("/local/ndn/prefix"));
121 interest.setMustBeFresh(true);
122 interest.setIncomingFaceId(10);
123 interest.setNonce(1);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800124
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800125 BOOST_CHECK(!interest.hasWire());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800126
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800127 Block headerBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800128
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800129 BOOST_CHECK(interest.hasWire());
130 BOOST_CHECK(headerBlock.hasWire());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800131
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800132 BOOST_CHECK_NE(headerBlock.wire(), interest.wireEncode().wire());
133 BOOST_CHECK_NE(headerBlock.size(), interest.wireEncode().size());
134 BOOST_CHECK_EQUAL(headerBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800135
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800136 BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithLocalControlHeader,
137 InterestWithLocalControlHeader+5,
138 headerBlock.begin(), headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800139
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800140 interest.setNonce(1000);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800141
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800142 Block updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
143 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800144
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800145 // only length should have changed
146 BOOST_CHECK_EQUAL_COLLECTIONS(updatedHeaderBlock.begin()+2, updatedHeaderBlock.end(),
147 headerBlock.begin()+2, headerBlock.end());
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800148
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800149 // updating IncomingFaceId that keeps the length
150 interest.setIncomingFaceId(100);
151 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
152 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
153 BOOST_CHECK_NE(*(updatedHeaderBlock.begin()+4), *(headerBlock.begin()+4));
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800154
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800155 // updating IncomingFaceId that increases the length by 2
156 interest.setIncomingFaceId(1000);
157 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
158 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800159
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800160 // adding NextHopId
161 interest.setNextHopFaceId(1);
162 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true);
163 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 9);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800164
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800165 // masking IncomingFaceId
166 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, false, true);
167 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800168
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800169 // masking NextHopId
170 updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, false);
171 BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800172
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800173 // masking everything
174 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
175 nfd::LocalControlHeader::Error);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800176}
177
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800178
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800179BOOST_AUTO_TEST_CASE (DecodeWithLocalHeader)
180{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800181 Block wireBlock(InterestWithLocalControlHeader, sizeof(InterestWithLocalControlHeader));
182 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
183 BOOST_REQUIRE_NE(&payload, &wireBlock);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800184
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800185 BOOST_CHECK_EQUAL(payload.type(), (uint32_t)Tlv::Interest);
186 BOOST_CHECK_EQUAL(wireBlock.type(), (uint32_t)tlv::nfd::LocalControlHeader);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800187
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800188 Interest interest(payload);
189 BOOST_CHECK(!interest.getLocalControlHeader().hasIncomingFaceId());
190 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
191
192 BOOST_REQUIRE_NO_THROW(interest.getLocalControlHeader().wireDecode(wireBlock));
193
194 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
195
196 BOOST_CHECK_EQUAL(interest.getIncomingFaceId(), 10);
197 BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId());
198
199 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false),
200 nfd::LocalControlHeader::Error);
201
202 BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, true),
203 nfd::LocalControlHeader::Error);
204
205 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, false));
206 BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, true));
207
208 BOOST_CHECK_NE((void*)interest.getLocalControlHeader().wireEncode(interest, true, true).wire(),
209 (void*)wireBlock.wire());
210
211 BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5);
212}
213
214BOOST_AUTO_TEST_CASE (DecodeWithoutLocalHeader)
215{
216 Block wireBlock(InterestWithoutLocalControlHeader, sizeof(InterestWithoutLocalControlHeader));
217 const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock);
218 BOOST_CHECK_EQUAL(&payload, &wireBlock);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800219}
220
221BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800222
223} // namespace ndn