Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2013 Regents of the University of California. |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include <boost/test/unit_test.hpp> |
| 7 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 8 | #include "interest.hpp" |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 9 | |
| 10 | using namespace std; |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 11 | namespace ndn { |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 12 | |
| 13 | BOOST_AUTO_TEST_SUITE(TestInterest) |
| 14 | |
| 15 | const uint8_t Interest1[] = { |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 16 | 0x05, 0x41, // NDN Interest |
| 17 | 0x07, 0x14, // Name |
| 18 | 0x08, 0x5, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 19 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 20 | 0x08, 0x3, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 21 | 0x6e, 0x64, 0x6e, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 22 | 0x08, 0x6, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 23 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 24 | 0x09, 0x1f, // Selectors |
| 25 | 0x0d, 0x1, 0x1, // MinSuffix |
| 26 | 0x0e, 0x1, 0x1, // MaxSuffix |
| 27 | 0x10, 0x14, // Exclude |
| 28 | 0x08, 0x4, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 29 | 0x61, 0x6c, 0x65, 0x78, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 30 | 0x08, 0x4, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 31 | 0x78, 0x78, 0x78, 0x78, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 32 | 0x13, 0x0, // Any |
| 33 | 0x08, 0x4, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 34 | 0x79, 0x79, 0x79, 0x79, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 35 | 0x11, 0x1, // ChildSelector |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 36 | 0x1, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 37 | 0x0a, 0x1, // Nonce |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 38 | 0x1, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 39 | 0x0b, 0x1, // Scope |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 40 | 0x1, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 41 | 0x0c, // InterestLifetime |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 42 | 0x2, 0x3, 0xe8 |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame^] | 45 | const 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 | |
| 51 | const 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 Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 57 | BOOST_AUTO_TEST_CASE (Decode) |
| 58 | { |
| 59 | Block interestBlock(Interest1, sizeof(Interest1)); |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 61 | ndn::Interest i; |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 62 | BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock)); |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 64 | 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 Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 72 | BOOST_REQUIRE_EQUAL(i.getNonce(), 1); |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Alexander Afanasyev | 6835ad8 | 2014-02-12 10:07:20 -0800 | [diff] [blame] | 75 | BOOST_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 Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 95 | BOOST_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 Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 104 | Exclude exclude; |
| 105 | exclude |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 106 | .excludeOne(name::Component("alex")) |
| 107 | .excludeRange(name::Component("xxxx"), name::Component("yyyy")); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 108 | i.setExclude(exclude); |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 109 | i.setNonce(1); |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 110 | |
| 111 | const Block &wire = i.wireEncode(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame^] | 113 | BOOST_CHECK_EQUAL_COLLECTIONS(Interest1, Interest1+sizeof(Interest1), |
| 114 | wire.begin(), wire.end()); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | BOOST_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 | |
| 210 | BOOST_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 Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 238 | |
| 239 | } // namespace ndn |