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 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 120 | ndn::Interest interest(ndn::Name("/local/ndn/prefix")); |
| 121 | interest.setMustBeFresh(true); |
| 122 | interest.setIncomingFaceId(10); |
| 123 | interest.setNonce(1); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 125 | BOOST_CHECK(!interest.hasWire()); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 127 | Block headerBlock = interest.getLocalControlHeader().wireEncode(interest, true, true); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 128 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 129 | BOOST_CHECK(interest.hasWire()); |
| 130 | BOOST_CHECK(headerBlock.hasWire()); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 132 | 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 Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 135 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithLocalControlHeader, |
| 137 | InterestWithLocalControlHeader+5, |
| 138 | headerBlock.begin(), headerBlock.end()); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 140 | interest.setNonce(1000); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 142 | Block updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true); |
| 143 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 145 | // only length should have changed |
| 146 | BOOST_CHECK_EQUAL_COLLECTIONS(updatedHeaderBlock.begin()+2, updatedHeaderBlock.end(), |
| 147 | headerBlock.begin()+2, headerBlock.end()); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 149 | // 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 Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 154 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 155 | // 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 Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 160 | // adding NextHopId |
| 161 | interest.setNextHopFaceId(1); |
| 162 | updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true); |
| 163 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 9); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 165 | // masking IncomingFaceId |
| 166 | updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, false, true); |
| 167 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 168 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 169 | // masking NextHopId |
| 170 | updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, false); |
| 171 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 172 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 173 | // masking everything |
| 174 | BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false), |
| 175 | nfd::LocalControlHeader::Error); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 179 | BOOST_AUTO_TEST_CASE (DecodeWithLocalHeader) |
| 180 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 181 | Block wireBlock(InterestWithLocalControlHeader, sizeof(InterestWithLocalControlHeader)); |
| 182 | const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock); |
| 183 | BOOST_REQUIRE_NE(&payload, &wireBlock); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 184 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(payload.type(), (uint32_t)Tlv::Interest); |
| 186 | BOOST_CHECK_EQUAL(wireBlock.type(), (uint32_t)tlv::nfd::LocalControlHeader); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 187 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 188 | 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 | |
| 214 | BOOST_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 Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 222 | |
| 223 | } // namespace ndn |