Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/interest.hpp" |
| 23 | #include "ndn-cxx/data.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 26 | #include "tests/make-interest-data.hpp" |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 28 | namespace ndn { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 29 | namespace tests { |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 30 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(TestInterest) |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 32 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 33 | class DisableAutoCheckParametersDigest |
| 34 | { |
| 35 | public: |
| 36 | DisableAutoCheckParametersDigest() |
| 37 | : m_saved(Interest::getAutoCheckParametersDigest()) |
| 38 | { |
| 39 | Interest::setAutoCheckParametersDigest(false); |
| 40 | } |
| 41 | |
| 42 | ~DisableAutoCheckParametersDigest() |
| 43 | { |
| 44 | Interest::setAutoCheckParametersDigest(m_saved); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | bool m_saved; |
| 49 | }; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 50 | |
| 51 | BOOST_AUTO_TEST_CASE(DefaultConstructor) |
| 52 | { |
| 53 | Interest i; |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(i.hasWire(), false); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(i.getName(), "/"); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 57 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true); |
| 59 | BOOST_CHECK_EQUAL(i.hasNonce(), false); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 61 | BOOST_CHECK(i.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 63 | BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false); |
| 64 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
Eric Newberry | 6e262f0 | 2020-05-29 23:11:25 -0700 | [diff] [blame] | 65 | BOOST_CHECK(i.getSignatureInfo() == nullopt); |
| 66 | BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false); |
| 67 | BOOST_CHECK_EQUAL(i.isSigned(), false); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 70 | BOOST_AUTO_TEST_SUITE(Encode) |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 71 | |
| 72 | BOOST_AUTO_TEST_CASE(Basic) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 73 | { |
| 74 | const uint8_t WIRE[] = { |
| 75 | 0x05, 0x1c, // Interest |
| 76 | 0x07, 0x14, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 77 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 78 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 79 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 80 | 0x0a, 0x04, // Nonce |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 81 | 0x01, 0x02, 0x03, 0x04, |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 84 | Interest i1; |
| 85 | i1.setName("/local/ndn/prefix"); |
| 86 | i1.setCanBePrefix(false); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 87 | i1.setNonce(0x01020304); |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true); |
| 89 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 90 | Block wire1 = i1.wireEncode(); |
| 91 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 92 | |
| 93 | Interest i2(wire1); |
| 94 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false); |
| 96 | BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true); |
Davide Pesavento | 835f027 | 2019-09-21 13:18:24 -0400 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(i2.hasNonce(), true); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(i2.getNonce(), 0x01020304); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 101 | BOOST_CHECK(i2.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false); |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false); |
Eric Newberry | 6e262f0 | 2020-05-29 23:11:25 -0700 | [diff] [blame] | 104 | BOOST_CHECK(i2.getSignatureInfo() == nullopt); |
| 105 | BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false); |
| 106 | BOOST_CHECK_EQUAL(i2.isSigned(), false); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(WithParameters) |
| 110 | { |
| 111 | const uint8_t WIRE[] = { |
| 112 | 0x05, 0x44, // Interest |
| 113 | 0x07, 0x36, // Name |
| 114 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 115 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 116 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
| 117 | 0x02, 0x20, // ParametersSha256DigestComponent |
| 118 | 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80, |
| 119 | 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc, |
| 120 | 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a, |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 121 | 0x0a, 0x04, // Nonce |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 122 | 0x00, 0x00, 0x00, 0x01, |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 123 | 0x24, 0x04, // ApplicationParameters |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 124 | 0xc0, 0xc1, 0xc2, 0xc3 |
| 125 | }; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 126 | |
| 127 | Interest i1; |
| 128 | i1.setName("/local/ndn/prefix"); |
| 129 | i1.setCanBePrefix(false); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 130 | i1.setNonce(0x1); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 131 | i1.setApplicationParameters("2404C0C1C2C3"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true); |
| 133 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 134 | Block wire1 = i1.wireEncode(); |
| 135 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 136 | |
| 137 | Interest i2(wire1); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL(i2.getName(), |
| 139 | "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false); |
| 141 | BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true); |
Davide Pesavento | 835f027 | 2019-09-21 13:18:24 -0400 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(i2.hasNonce(), true); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(i2.getNonce(), 0x1); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 145 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 146 | BOOST_CHECK(i2.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 147 | BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 148 | BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block); |
Eric Newberry | 6e262f0 | 2020-05-29 23:11:25 -0700 | [diff] [blame] | 149 | BOOST_CHECK(i2.getSignatureInfo() == nullopt); |
| 150 | BOOST_CHECK_EQUAL(i2.getSignatureValue().isValid(), false); |
| 151 | BOOST_CHECK_EQUAL(i2.isSigned(), false); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 154 | BOOST_AUTO_TEST_CASE(Full) |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 155 | { |
| 156 | const uint8_t WIRE[] = { |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 157 | 0x05, 0x5c, // Interest |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 158 | 0x07, 0x36, // Name |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 159 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 160 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 161 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 162 | 0x02, 0x20, // ParametersSha256DigestComponent |
| 163 | 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80, |
| 164 | 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc, |
| 165 | 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a, |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 166 | 0x21, 0x00, // CanBePrefix |
| 167 | 0x12, 0x00, // MustBeFresh |
| 168 | 0x1e, 0x0b, // ForwardingHint |
| 169 | 0x1f, 0x09, // Delegation List |
| 170 | 0x1e, 0x02, |
| 171 | 0x3e, 0x15, |
| 172 | 0x07, 0x03, |
| 173 | 0x08, 0x01, 0x48, |
| 174 | 0x0a, 0x04, // Nonce |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 175 | 0x4c, 0x1e, 0xcb, 0x4a, |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 176 | 0x0c, 0x02, // InterestLifetime |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 177 | 0x76, 0xa1, |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 178 | 0x22, 0x01, // HopLimit |
| 179 | 0xdc, |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 180 | 0x24, 0x04, // ApplicationParameters |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 181 | 0xc0, 0xc1, 0xc2, 0xc3 |
| 182 | }; |
| 183 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 184 | Interest i1; |
| 185 | i1.setName("/local/ndn/prefix"); |
| 186 | i1.setMustBeFresh(true); |
| 187 | i1.setCanBePrefix(true); |
| 188 | i1.setForwardingHint(DelegationList({{15893, "/H"}})); |
| 189 | i1.setNonce(0x4c1ecb4a); |
| 190 | i1.setInterestLifetime(30369_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 191 | i1.setHopLimit(220); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 192 | i1.setApplicationParameters("2404C0C1C2C3"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 193 | BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true); |
| 194 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 195 | Block wire1 = i1.wireEncode(); |
| 196 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 197 | |
| 198 | Interest i2(wire1); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 199 | BOOST_CHECK_EQUAL(i2.getName(), |
| 200 | "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true); |
| 202 | BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true); |
| 203 | BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}})); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 204 | BOOST_CHECK_EQUAL(i2.hasNonce(), true); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 205 | BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a); |
| 206 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(*i2.getHopLimit(), 220); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 208 | BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Eric Newberry | 6e262f0 | 2020-05-29 23:11:25 -0700 | [diff] [blame] | 211 | BOOST_AUTO_TEST_CASE(Signed) |
| 212 | { |
| 213 | const uint8_t WIRE[] = { |
| 214 | 0x05, 0x77, // Interest |
| 215 | 0x07, 0x36, // Name |
| 216 | 0x08, 0x05, // GenericNameComponent |
| 217 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 218 | 0x08, 0x03, // GenericNameComponent |
| 219 | 0x6e, 0x64, 0x6e, |
| 220 | 0x08, 0x06, // GenericNameComponent |
| 221 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 222 | 0x02, 0x20, // ParametersSha256DigestComponent |
| 223 | 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc, |
| 224 | 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c, |
| 225 | 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a, |
| 226 | 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20, |
| 227 | 0x12, 0x00, // MustBeFresh |
| 228 | 0x0a, 0x04, // Nonce |
| 229 | 0x4c, 0x1e, 0xcb, 0x4a, |
| 230 | 0x24, 0x04, // ApplicationParameters |
| 231 | 0xc0, 0xc1, 0xc2, 0xc3, |
| 232 | 0x2c, 0x0d, // InterestSignatureInfo |
| 233 | 0x1b, 0x01, // SignatureType |
| 234 | 0x00, |
| 235 | 0x26, 0x08, // SignatureNonce |
| 236 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 237 | 0x2e, 0x20, // InterestSignatureValue |
| 238 | 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1, |
| 239 | 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe, |
| 240 | 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e, |
| 241 | 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1, |
| 242 | }; |
| 243 | |
| 244 | SignatureInfo si(tlv::DigestSha256); |
| 245 | std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
| 246 | si.setNonce(nonce); |
| 247 | Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block); |
| 248 | |
| 249 | Interest i1(Block(WIRE, sizeof(WIRE))); |
| 250 | BOOST_CHECK_EQUAL(i1.getName(), |
| 251 | "/local/ndn/prefix/params-sha256=6f29586053ee9fccd8a422122925287c0a18435f4074c40abb0d5b30e4aa6220"); |
| 252 | BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false); |
| 253 | BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true); |
| 254 | BOOST_CHECK_EQUAL(i1.hasNonce(), true); |
| 255 | BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a); |
| 256 | BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256); |
| 257 | BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce); |
| 258 | BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(), |
| 259 | sv.begin(), sv.end()); |
| 260 | BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block); |
| 261 | BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true); |
| 262 | |
| 263 | // Reset wire |
| 264 | BOOST_CHECK_EQUAL(i1.hasWire(), true); |
| 265 | i1.setCanBePrefix(true); |
| 266 | i1.setCanBePrefix(false); |
| 267 | BOOST_CHECK_EQUAL(i1.hasWire(), false); |
| 268 | |
| 269 | Block wire1 = i1.wireEncode(); |
| 270 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 271 | |
| 272 | Interest i2("/local/ndn/prefix"); |
| 273 | i2.setCanBePrefix(false); |
| 274 | i2.setMustBeFresh(true); |
| 275 | i2.setNonce(0x4c1ecb4a); |
| 276 | i2.setApplicationParameters("2404C0C1C2C3"_block); |
| 277 | i2.setSignatureInfo(si); |
| 278 | i2.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size())); |
| 279 | BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true); |
| 280 | |
| 281 | Block wire2 = i2.wireEncode(); |
| 282 | BOOST_CHECK_EQUAL_COLLECTIONS(wire2.begin(), wire2.end(), WIRE, WIRE + sizeof(WIRE)); |
| 283 | } |
| 284 | |
| 285 | BOOST_AUTO_TEST_CASE(SignedApplicationElements) |
| 286 | { |
| 287 | const uint8_t WIRE[] = { |
| 288 | 0x05, 0x8f, // Interest |
| 289 | 0x07, 0x36, // Name |
| 290 | 0x08, 0x05, // GenericNameComponent |
| 291 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 292 | 0x08, 0x03, // GenericNameComponent |
| 293 | 0x6e, 0x64, 0x6e, |
| 294 | 0x08, 0x06, // GenericNameComponent |
| 295 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 296 | 0x02, 0x20, // ParametersSha256DigestComponent |
| 297 | 0xbc, 0x36, 0x30, 0xa4, 0xd6, 0x5e, 0x0d, 0xb5, |
| 298 | 0x48, 0x3d, 0xfa, 0x0d, 0x28, 0xb3, 0x31, 0x2f, |
| 299 | 0xca, 0xc1, 0xd4, 0x41, 0xec, 0x89, 0x61, 0xd4, |
| 300 | 0x17, 0x5e, 0x61, 0x75, 0x17, 0x78, 0x10, 0x8e, |
| 301 | 0x12, 0x00, // MustBeFresh |
| 302 | 0x0a, 0x04, // Nonce |
| 303 | 0x4c, 0x1e, 0xcb, 0x4a, |
| 304 | 0x24, 0x04, // ApplicationParameters |
| 305 | 0xc0, 0xc1, 0xc2, 0xc3, |
| 306 | 0xfd, 0x01, 0xfe, 0x08, // Application-specific element (Type 510) |
| 307 | 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, |
| 308 | 0x2c, 0x0d, // InterestSignatureInfo |
| 309 | 0x1b, 0x01, // SignatureType |
| 310 | 0x00, |
| 311 | 0x26, 0x08, // SignatureNonce |
| 312 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 313 | 0x2e, 0x20, // InterestSignatureValue |
| 314 | 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1, |
| 315 | 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe, |
| 316 | 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e, |
| 317 | 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1, |
| 318 | 0xfd, 0x02, 0x00, 0x08, // Application-specific element (Type 512) |
| 319 | 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 |
| 320 | }; |
| 321 | |
| 322 | SignatureInfo si(tlv::DigestSha256); |
| 323 | std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
| 324 | si.setNonce(nonce); |
| 325 | Block sv("2E20 12471AE0F8723AC1156C370A38711EBEBF2817DE9B2DD94E9B7E62F117B876C1"_block); |
| 326 | |
| 327 | Interest i1(Block(WIRE, sizeof(WIRE))); |
| 328 | BOOST_CHECK_EQUAL(i1.getName(), |
| 329 | "/local/ndn/prefix/params-sha256=bc3630a4d65e0db5483dfa0d28b3312fcac1d441ec8961d4175e61751778108e"); |
| 330 | BOOST_CHECK_EQUAL(i1.getCanBePrefix(), false); |
| 331 | BOOST_CHECK_EQUAL(i1.getMustBeFresh(), true); |
| 332 | BOOST_CHECK_EQUAL(i1.hasNonce(), true); |
| 333 | BOOST_CHECK_EQUAL(i1.getNonce(), 0x4c1ecb4a); |
| 334 | BOOST_CHECK_EQUAL(i1.getSignatureInfo()->getSignatureType(), tlv::DigestSha256); |
| 335 | BOOST_CHECK(i1.getSignatureInfo()->getNonce() == nonce); |
| 336 | BOOST_CHECK_EQUAL_COLLECTIONS(i1.getSignatureValue().begin(), i1.getSignatureValue().end(), |
| 337 | sv.begin(), sv.end()); |
| 338 | BOOST_CHECK_EQUAL(i1.getApplicationParameters(), "2404C0C1C2C3"_block); |
| 339 | BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true); |
| 340 | |
| 341 | // Reset wire |
| 342 | BOOST_CHECK_EQUAL(i1.hasWire(), true); |
| 343 | i1.setCanBePrefix(true); |
| 344 | i1.setCanBePrefix(false); |
| 345 | BOOST_CHECK_EQUAL(i1.hasWire(), false); |
| 346 | |
| 347 | Block wire1 = i1.wireEncode(); |
| 348 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 349 | } |
| 350 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 351 | BOOST_AUTO_TEST_CASE(MissingApplicationParameters) |
| 352 | { |
| 353 | Interest i; |
| 354 | i.setName(Name("/A").appendParametersSha256DigestPlaceholder()); |
| 355 | i.setCanBePrefix(false); |
| 356 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 357 | BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) { |
| 358 | return e.what() == "Interest without parameters must not have a ParametersSha256DigestComponent"s; |
| 359 | }); |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 360 | } |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 361 | |
| 362 | BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent) |
| 363 | { |
| 364 | // there's no way to create an Interest that fails this check via programmatic construction, |
| 365 | // so we have to decode an invalid Interest and force reencoding |
| 366 | |
| 367 | DisableAutoCheckParametersDigest disabler; |
| 368 | Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block); |
| 369 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 370 | BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding |
| 371 | |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 372 | // trigger reencoding |
| 373 | i.setNonce(42); |
| 374 | // now the check fails while attempting to reencode |
| 375 | BOOST_CHECK_EXCEPTION(i.wireEncode(), tlv::Error, [] (const auto& e) { |
| 376 | return e.what() == "Interest with parameters must have a ParametersSha256DigestComponent"s; |
| 377 | }); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 378 | } |
| 379 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 380 | BOOST_AUTO_TEST_SUITE_END() // Encode |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 381 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 382 | class DecodeFixture |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 383 | { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 384 | protected: |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 385 | DecodeFixture() |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 386 | { |
| 387 | // initialize all elements to non-empty, to verify wireDecode clears them |
| 388 | i.setName("/A"); |
| 389 | i.setForwardingHint({{10309, "/F"}}); |
| 390 | i.setNonce(0x03d645a8); |
| 391 | i.setInterestLifetime(18554_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 392 | i.setHopLimit(64); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 393 | i.setApplicationParameters("2404A0A1A2A3"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 394 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 395 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 396 | protected: |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 397 | Interest i; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 398 | }; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 399 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 400 | BOOST_FIXTURE_TEST_SUITE(Decode, DecodeFixture) |
| 401 | |
| 402 | BOOST_AUTO_TEST_CASE(NotAnInterest) |
| 403 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 404 | BOOST_CHECK_EXCEPTION(i.wireDecode("4202CAFE"_block), tlv::Error, [] (const auto& e) { |
| 405 | return e.what() == "Expecting Interest element, but TLV has type 66"s; |
| 406 | }); |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 407 | } |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 408 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 409 | BOOST_AUTO_TEST_CASE(NameOnly) |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 410 | { |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 411 | i.wireDecode("0505 0703(080149)"_block); |
Davide Pesavento | 835f027 | 2019-09-21 13:18:24 -0400 | [diff] [blame] | 412 | BOOST_CHECK_EQUAL(i.hasWire(), true); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 413 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 414 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
| 415 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 416 | BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true); |
Davide Pesavento | 835f027 | 2019-09-21 13:18:24 -0400 | [diff] [blame] | 417 | BOOST_CHECK_EQUAL(i.hasNonce(), false); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 418 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 419 | BOOST_CHECK(i.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 420 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 421 | BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 422 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 423 | // modify then re-encode |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 424 | i.setNonce(0x957c6554); |
Davide Pesavento | 835f027 | 2019-09-21 13:18:24 -0400 | [diff] [blame] | 425 | BOOST_CHECK_EQUAL(i.hasWire(), false); |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 426 | BOOST_CHECK_EQUAL(i.wireEncode(), "050B 0703(080149) 0A04957C6554"_block); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 429 | BOOST_AUTO_TEST_CASE(NameCanBePrefix) |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 430 | { |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 431 | i.wireDecode("0507 0703(080149) 2100"_block); |
Davide Pesavento | 835f027 | 2019-09-21 13:18:24 -0400 | [diff] [blame] | 432 | BOOST_CHECK_EQUAL(i.hasWire(), true); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 433 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 434 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 435 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 436 | BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true); |
Davide Pesavento | 835f027 | 2019-09-21 13:18:24 -0400 | [diff] [blame] | 437 | BOOST_CHECK_EQUAL(i.hasNonce(), false); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 438 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 439 | BOOST_CHECK(i.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 440 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 441 | BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false); |
| 442 | } |
| 443 | |
| 444 | BOOST_AUTO_TEST_CASE(FullWithoutParameters) |
| 445 | { |
| 446 | i.wireDecode("0531 0703(080149) " |
| 447 | "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) " |
| 448 | "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 449 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 450 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 451 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 452 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}})); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 453 | BOOST_CHECK_EQUAL(i.hasNonce(), true); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 454 | BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 455 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 456 | BOOST_CHECK_EQUAL(*i.getHopLimit(), 214); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 457 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 458 | BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 459 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 460 | // encode without modification: retain original wire encoding |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 461 | BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 462 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 463 | // modify then re-encode: unrecognized elements are discarded |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 464 | i.setName("/J"); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 465 | BOOST_CHECK_EQUAL(i.wireEncode(), |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 466 | "0523 0703(08014A) " |
| 467 | "2100 1200 1E0B(1F09 1E023E15 0703080148) " |
| 468 | "0A044ACB1E4C 0C0276A1 2201D6"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | BOOST_AUTO_TEST_CASE(FullWithParameters) |
| 472 | { |
| 473 | i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) " |
| 474 | "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) " |
| 475 | "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block); |
| 476 | BOOST_CHECK_EQUAL(i.getName(), |
| 477 | "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05"); |
| 478 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 479 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 480 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}})); |
| 481 | BOOST_CHECK_EQUAL(i.hasNonce(), true); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 482 | BOOST_CHECK_EQUAL(i.getNonce(), 0x4acb1e4c); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 483 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 484 | BOOST_CHECK_EQUAL(*i.getHopLimit(), 214); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 485 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
| 486 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block); |
| 487 | |
| 488 | // encode without modification: retain original wire encoding |
| 489 | BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91); |
| 490 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 491 | // modify then re-encode: unrecognized elements after ApplicationParameters |
| 492 | // are preserved, the rest are discarded |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 493 | i.setName("/J"); |
| 494 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 495 | BOOST_CHECK_EQUAL(i.wireEncode(), |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 496 | "054D 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) " |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 497 | "2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 498 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 499 | |
| 500 | // modify ApplicationParameters: unrecognized elements are preserved |
| 501 | i.setApplicationParameters("2402CAFE"_block); |
| 502 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 503 | BOOST_CHECK_EQUAL(i.wireEncode(), |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 504 | "054B 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) " |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 505 | "2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 506 | "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 509 | BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder) |
| 510 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 511 | BOOST_CHECK_EXCEPTION(i.wireDecode( |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 512 | "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 513 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 514 | tlv::Error, |
| 515 | [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; }); |
| 516 | BOOST_CHECK_EXCEPTION(i.wireDecode( |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 517 | "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 518 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 519 | tlv::Error, |
| 520 | [] (const auto& e) { return e.what() == "CanBePrefix element is out of order"s; }); |
| 521 | BOOST_CHECK_EXCEPTION(i.wireDecode( |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 522 | "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 523 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 524 | tlv::Error, |
| 525 | [] (const auto& e) { return e.what() == "MustBeFresh element is out of order"s; }); |
| 526 | BOOST_CHECK_EXCEPTION(i.wireDecode( |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 527 | "0529 0703080149 2100 1200 0A044ACB1E4C " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 528 | "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 529 | tlv::Error, |
| 530 | [] (const auto& e) { return e.what() == "ForwardingHint element is out of order"s; }); |
| 531 | BOOST_CHECK_EXCEPTION(i.wireDecode( |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 532 | "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 533 | "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block), |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 534 | tlv::Error, |
| 535 | [] (const auto& e) { return e.what() == "Nonce element is out of order"s; }); |
| 536 | BOOST_CHECK_EXCEPTION(i.wireDecode( |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 537 | "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 538 | "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block), |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 539 | tlv::Error, |
| 540 | [] (const auto& e) { return e.what() == "InterestLifetime element is out of order"s; }); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 543 | BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder) |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 544 | { |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 545 | // duplicate HopLimit |
| 546 | i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)" |
| 547 | "2201D6 2200 2404C0C1C2C3 22020101"_block); |
| 548 | BOOST_CHECK_EQUAL(i.getName(), |
| 549 | "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 550 | BOOST_CHECK_EQUAL(*i.getHopLimit(), 214); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 551 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 552 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 553 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 554 | // duplicate ApplicationParameters |
| 555 | i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)" |
| 556 | "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block); |
| 557 | BOOST_CHECK_EQUAL(i.getName(), |
| 558 | "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 559 | BOOST_CHECK_EQUAL(*i.getHopLimit(), 214); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 560 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
| 561 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 564 | BOOST_AUTO_TEST_CASE(MissingName) |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 565 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 566 | BOOST_CHECK_EXCEPTION(i.wireDecode("0500"_block), tlv::Error, |
| 567 | [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; }); |
| 568 | BOOST_CHECK_EXCEPTION(i.wireDecode("0502 1200"_block), tlv::Error, |
| 569 | [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; }); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 572 | BOOST_AUTO_TEST_CASE(BadName) |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 573 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 574 | BOOST_CHECK_EXCEPTION(i.wireDecode("0502 0700"_block), tlv::Error, |
| 575 | [] (const auto& e) { return e.what() == "Name has zero name components"s; }); |
| 576 | BOOST_CHECK_EXCEPTION(i.wireDecode("054C 074A(080149" |
| 577 | "02200000000000000000000000000000000000000000000000000000000000000000" |
| 578 | "080132" |
| 579 | "02200000000000000000000000000000000000000000000000000000000000000000)"_block), |
| 580 | tlv::Error, |
| 581 | [] (const auto& e) { return e.what() == "Name has more than one ParametersSha256DigestComponent"s; }); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | BOOST_AUTO_TEST_CASE(BadCanBePrefix) |
| 585 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 586 | BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 210102"_block), tlv::Error, |
| 587 | [] (const auto& e) { return e.what() == "CanBePrefix element has non-zero TLV-LENGTH"s; }); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | BOOST_AUTO_TEST_CASE(BadMustBeFresh) |
| 591 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 592 | BOOST_CHECK_EXCEPTION(i.wireDecode("0508 0703080149 120102"_block), tlv::Error, |
| 593 | [] (const auto& e) { return e.what() == "MustBeFresh element has non-zero TLV-LENGTH"s; }); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | BOOST_AUTO_TEST_CASE(BadNonce) |
| 597 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 598 | BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error, |
| 599 | [] (const auto& e) { return e.what() == "Nonce element is malformed"s; }); |
| 600 | BOOST_CHECK_EXCEPTION(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error, |
| 601 | [] (const auto& e) { return e.what() == "Nonce element is malformed"s; }); |
| 602 | BOOST_CHECK_EXCEPTION(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error, |
| 603 | [] (const auto& e) { return e.what() == "Nonce element is malformed"s; }); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 606 | BOOST_AUTO_TEST_CASE(BadHopLimit) |
| 607 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 608 | BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 2200"_block), tlv::Error, |
| 609 | [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; }); |
| 610 | BOOST_CHECK_EXCEPTION(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error, |
| 611 | [] (const auto& e) { return e.what() == "HopLimit element is malformed"s; }); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 612 | } |
| 613 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 614 | BOOST_AUTO_TEST_CASE(BadParametersDigest) |
| 615 | { |
| 616 | // ApplicationParameters without ParametersSha256DigestComponent |
| 617 | Block b1("0509 0703(080149) 2402CAFE"_block); |
| 618 | // ParametersSha256DigestComponent without ApplicationParameters |
| 619 | Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block); |
| 620 | // digest mismatch |
| 621 | Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) " |
| 622 | "2402CAFE"_block); |
| 623 | |
| 624 | BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error); |
| 625 | BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error); |
| 626 | BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error); |
| 627 | |
| 628 | DisableAutoCheckParametersDigest disabler; |
| 629 | BOOST_CHECK_NO_THROW(i.wireDecode(b1)); |
| 630 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 631 | BOOST_CHECK_NO_THROW(i.wireDecode(b2)); |
| 632 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 633 | BOOST_CHECK_NO_THROW(i.wireDecode(b3)); |
| 634 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 635 | } |
| 636 | |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 637 | BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName) |
| 638 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 639 | BOOST_CHECK_EXCEPTION(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error, |
| 640 | [] (const auto& e) { return e.what() == "Name element is missing or out of order"s; }); |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 643 | BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement) |
| 644 | { |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 645 | BOOST_CHECK_EXCEPTION(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error, |
| 646 | [] (const auto& e) { return e.what() == "Unrecognized element of critical type 251"s; }); |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 647 | // v0.2 packet with Selectors |
Davide Pesavento | 905d40f | 2020-06-09 21:33:25 -0400 | [diff] [blame] | 648 | BOOST_CHECK_EXCEPTION(i.wireDecode("0510 0703080149 09030D0101 0A0401000000"_block), tlv::Error, |
| 649 | [] (const auto& e) { return e.what() == "Unrecognized element of critical type 9"s; }); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 652 | BOOST_AUTO_TEST_SUITE_END() // Decode |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 653 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 654 | BOOST_AUTO_TEST_CASE(MatchesData) |
| 655 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 656 | auto interest = makeInterest("/A"); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 657 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 658 | auto data = makeData("/A"); |
| 659 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 660 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 661 | data->setName("/A/D"); |
| 662 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 663 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 664 | interest->setCanBePrefix(true); |
| 665 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 666 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 667 | interest->setMustBeFresh(true); |
| 668 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 669 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 670 | data->setFreshnessPeriod(1_s); |
| 671 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 672 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 673 | data->setName("/H/I"); |
| 674 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 675 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 676 | data->wireEncode(); |
| 677 | interest = makeInterest(data->getFullName()); |
| 678 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 679 | |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 680 | setNameComponent(*interest, -1, |
| 681 | name::Component::fromEscapedString("sha256digest=00000000000000000000000000" |
| 682 | "00000000000000000000000000000000000000")); |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 683 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1) |
| 687 | BOOST_AUTO_TEST_CASE(MatchesInterest) |
| 688 | { |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 689 | Interest interest; |
| 690 | interest.setName("/A") |
| 691 | .setCanBePrefix(true) |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 692 | .setMustBeFresh(true) |
| 693 | .setForwardingHint({{1, "/H"}}) |
| 694 | .setNonce(2228) |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 695 | .setInterestLifetime(5_s) |
| 696 | .setHopLimit(90); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 697 | |
| 698 | Interest other; |
| 699 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 700 | |
| 701 | other.setName(interest.getName()); |
| 702 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 703 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 704 | other.setCanBePrefix(interest.getCanBePrefix()); |
| 705 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 706 | |
| 707 | other.setMustBeFresh(interest.getMustBeFresh()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 708 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented |
| 709 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 710 | other.setForwardingHint(interest.getForwardingHint()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 711 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 712 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 713 | other.setNonce(9336); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 714 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 715 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 716 | other.setInterestLifetime(3_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 717 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 718 | |
| 719 | other.setHopLimit(31); |
| 720 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 723 | BOOST_AUTO_TEST_CASE(SetName) |
| 724 | { |
| 725 | Interest i; |
| 726 | BOOST_CHECK_EQUAL(i.getName(), "/"); |
| 727 | i.setName("/A/B"); |
| 728 | BOOST_CHECK_EQUAL(i.getName(), "/A/B"); |
| 729 | i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 730 | BOOST_CHECK_EQUAL(i.getName(), |
| 731 | "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 732 | BOOST_CHECK_THROW(i.setName("/I" |
| 733 | "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 734 | "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"), |
| 735 | std::invalid_argument); |
| 736 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 737 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 738 | BOOST_AUTO_TEST_CASE(SetCanBePrefix) |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 739 | { |
| 740 | Interest i; |
| 741 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 742 | i.setCanBePrefix(false); |
| 743 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 744 | i.setCanBePrefix(true); |
| 745 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 748 | BOOST_AUTO_TEST_CASE(SetMustBeFresh) |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 749 | { |
| 750 | Interest i; |
| 751 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 752 | i.setMustBeFresh(true); |
| 753 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 754 | i.setMustBeFresh(false); |
| 755 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | BOOST_AUTO_TEST_CASE(ModifyForwardingHint) |
| 759 | { |
Davide Pesavento | 0e0b389 | 2019-07-30 21:05:05 -0400 | [diff] [blame] | 760 | Interest i("/I"); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 761 | i.setCanBePrefix(false); |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 762 | i.setForwardingHint({{1, "/A"}}); |
| 763 | i.wireEncode(); |
| 764 | BOOST_CHECK(i.hasWire()); |
| 765 | |
| 766 | i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); }); |
| 767 | BOOST_CHECK(!i.hasWire()); |
| 768 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}})); |
| 769 | } |
| 770 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 771 | BOOST_AUTO_TEST_CASE(GetNonce) |
| 772 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 773 | unique_ptr<Interest> i1, i2; |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 774 | Interest::Nonce nonce1(0), nonce2(0); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 775 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 776 | // getNonce automatically assigns a random Nonce. |
| 777 | // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 778 | // identical Nonces in a row. |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 779 | int nIterations = 0; |
| 780 | do { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 781 | i1 = make_unique<Interest>(); |
| 782 | nonce1 = i1->getNonce(); |
| 783 | i2 = make_unique<Interest>(); |
| 784 | nonce2 = i2->getNonce(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 785 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 786 | while (nonce1 == nonce2 && ++nIterations < 100); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 787 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 788 | BOOST_CHECK_NE(nonce1, nonce2); |
| 789 | BOOST_CHECK(i1->hasNonce()); |
| 790 | BOOST_CHECK(i2->hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 791 | |
| 792 | // Once a Nonce is assigned, it should not change. |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 793 | BOOST_CHECK_EQUAL(i1->getNonce(), nonce1); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 794 | BOOST_CHECK_EQUAL(i2->getNonce(), nonce2); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | BOOST_AUTO_TEST_CASE(SetNonce) |
| 798 | { |
| 799 | Interest i1("/A"); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 800 | i1.setCanBePrefix(false); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 801 | BOOST_CHECK(!i1.hasNonce()); |
| 802 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 803 | i1.setNonce(1); |
| 804 | i1.wireEncode(); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 805 | BOOST_CHECK(i1.hasNonce()); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 806 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); |
| 807 | |
| 808 | Interest i2(i1); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 809 | BOOST_CHECK(i2.hasNonce()); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 810 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 811 | |
| 812 | i2.setNonce(2); |
| 813 | BOOST_CHECK_EQUAL(i2.getNonce(), 2); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 814 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1's Nonce (Bug #4168) |
| 815 | |
| 816 | i2.setNonce(nullopt); |
| 817 | BOOST_CHECK(!i2.hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | BOOST_AUTO_TEST_CASE(RefreshNonce) |
| 821 | { |
| 822 | Interest i; |
| 823 | BOOST_CHECK(!i.hasNonce()); |
| 824 | i.refreshNonce(); |
| 825 | BOOST_CHECK(!i.hasNonce()); |
| 826 | |
| 827 | i.setNonce(1); |
| 828 | BOOST_CHECK(i.hasNonce()); |
| 829 | i.refreshNonce(); |
| 830 | BOOST_CHECK(i.hasNonce()); |
| 831 | BOOST_CHECK_NE(i.getNonce(), 1); |
| 832 | } |
| 833 | |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 834 | BOOST_AUTO_TEST_CASE(NonceConversions) |
| 835 | { |
| 836 | Interest i; |
| 837 | i.setCanBePrefix(false); |
| 838 | |
| 839 | // 4-arg constructor |
| 840 | Interest::Nonce n1(1, 2, 3, 4); |
| 841 | i.setNonce(n1); |
| 842 | BOOST_CHECK_EQUAL(i.getNonce(), 0x01020304); |
| 843 | |
| 844 | // 4-arg constructor + assignment |
| 845 | n1 = {0xf, 0xe, 0xd, 0xc}; |
| 846 | i.setNonce(n1); |
| 847 | BOOST_CHECK_EQUAL(i.getNonce(), 0x0f0e0d0c); |
| 848 | |
| 849 | // 1-arg constructor + assignment (implicit conversion) |
| 850 | Interest::Nonce n2; |
| 851 | n2 = 42; |
| 852 | BOOST_CHECK_NE(n1, n2); |
| 853 | i.setNonce(n2); |
| 854 | n2 = 21; // should not affect i's Nonce |
| 855 | BOOST_CHECK_EQUAL(i.getNonce(), 42); |
| 856 | BOOST_CHECK_EQUAL(i.toUri(), "/?Nonce=0000002a"); // stored in big-endian |
| 857 | } |
| 858 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 859 | BOOST_AUTO_TEST_CASE(SetInterestLifetime) |
| 860 | { |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 861 | BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 862 | BOOST_CHECK_NO_THROW(Interest("/A", 0_ms)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 863 | |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 864 | Interest i; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 865 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 866 | BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 867 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 868 | i.setInterestLifetime(0_ms); |
| 869 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms); |
| 870 | i.setInterestLifetime(1_ms); |
| 871 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 872 | |
| 873 | i = Interest("/B", 15_s); |
| 874 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s); |
| 875 | } |
| 876 | |
| 877 | BOOST_AUTO_TEST_CASE(SetHopLimit) |
| 878 | { |
| 879 | Interest i; |
| 880 | BOOST_CHECK(i.getHopLimit() == nullopt); |
| 881 | i.setHopLimit(42); |
| 882 | BOOST_CHECK(i.getHopLimit() == 42); |
| 883 | i.setHopLimit(nullopt); |
| 884 | BOOST_CHECK(i.getHopLimit() == nullopt); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 887 | BOOST_AUTO_TEST_CASE(SetApplicationParameters) |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 888 | { |
| 889 | const uint8_t PARAMETERS1[] = {0xc1}; |
| 890 | const uint8_t PARAMETERS2[] = {0xc2}; |
| 891 | |
| 892 | Interest i; |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 893 | BOOST_CHECK(!i.hasApplicationParameters()); |
| 894 | i.setApplicationParameters("2400"_block); |
| 895 | BOOST_CHECK(i.hasApplicationParameters()); |
| 896 | i.unsetApplicationParameters(); |
| 897 | BOOST_CHECK(!i.hasApplicationParameters()); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 898 | |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 899 | // Block overload |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 900 | i.setApplicationParameters("2401C0"_block); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 901 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block); |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 902 | i.setApplicationParameters("8001C1"_block); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 903 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame^] | 904 | BOOST_CHECK_THROW(i.setApplicationParameters(Block{}), std::invalid_argument); |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 905 | |
| 906 | // raw buffer+size overload |
| 907 | i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1)); |
| 908 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block); |
| 909 | i.setApplicationParameters(nullptr, 0); |
| 910 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 911 | BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument); |
| 912 | |
| 913 | // ConstBufferPtr overload |
| 914 | i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2))); |
| 915 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block); |
| 916 | i.setApplicationParameters(make_shared<Buffer>()); |
| 917 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 918 | BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Eric Newberry | 6e262f0 | 2020-05-29 23:11:25 -0700 | [diff] [blame] | 921 | BOOST_AUTO_TEST_CASE(SetSignature) |
| 922 | { |
| 923 | Interest i; |
| 924 | i.setCanBePrefix(false); |
| 925 | BOOST_CHECK(i.getSignatureInfo() == nullopt); |
| 926 | BOOST_CHECK_EQUAL(i.isSigned(), false); |
| 927 | |
| 928 | // Throws because attempting to set InterestSignatureValue without set InterestSignatureInfo |
| 929 | Block sv1("2E04 01020304"_block); |
| 930 | auto svBuffer1 = make_shared<Buffer>(sv1.value(), sv1.value_size()); |
| 931 | BOOST_CHECK_THROW(i.setSignatureValue(svBuffer1), tlv::Error); |
| 932 | |
| 933 | // Simple set/get case for InterestSignatureInfo (no prior set) |
| 934 | SignatureInfo si1(tlv::SignatureSha256WithEcdsa); |
| 935 | i.setSignatureInfo(si1); |
| 936 | BOOST_CHECK(i.getSignatureInfo() == si1); |
| 937 | BOOST_CHECK_EQUAL(i.isSigned(), false); |
| 938 | |
| 939 | // Simple set/get case for InterestSignatureValue (no prior set) |
| 940 | BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false); |
| 941 | i.setSignatureValue(svBuffer1); |
| 942 | BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1); |
| 943 | BOOST_CHECK_EQUAL(i.isSigned(), true); |
| 944 | |
| 945 | // Throws because attempting to set InterestSignatureValue to nullptr |
| 946 | BOOST_CHECK_THROW(i.setSignatureValue(nullptr), std::invalid_argument); |
| 947 | BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1); |
| 948 | BOOST_CHECK_EQUAL(i.isSigned(), true); |
| 949 | |
| 950 | // Ensure that wire is not reset if specified InterestSignatureInfo is same |
| 951 | i.wireEncode(); |
| 952 | BOOST_CHECK_EQUAL(i.hasWire(), true); |
| 953 | i.setSignatureInfo(si1); |
| 954 | BOOST_CHECK_EQUAL(i.hasWire(), true); |
| 955 | BOOST_CHECK(i.getSignatureInfo() == si1); |
| 956 | BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1); |
| 957 | BOOST_CHECK_EQUAL(i.isSigned(), true); |
| 958 | |
| 959 | // Ensure that wire is reset if specified InterestSignatureInfo is different |
| 960 | i.wireEncode(); |
| 961 | BOOST_CHECK_EQUAL(i.hasWire(), true); |
| 962 | SignatureInfo si2(tlv::SignatureSha256WithRsa); |
| 963 | i.setSignatureInfo(si2); |
| 964 | BOOST_CHECK_EQUAL(i.hasWire(), false); |
| 965 | BOOST_CHECK(i.getSignatureInfo() == si2); |
| 966 | BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1); |
| 967 | BOOST_CHECK_EQUAL(i.isSigned(), true); |
| 968 | |
| 969 | // Ensure that wire is not reset if specified InterestSignatureValue is same |
| 970 | i.wireEncode(); |
| 971 | BOOST_CHECK_EQUAL(i.hasWire(), true); |
| 972 | i.setSignatureValue(svBuffer1); |
| 973 | BOOST_CHECK_EQUAL(i.hasWire(), true); |
| 974 | BOOST_CHECK(i.getSignatureInfo() == si2); |
| 975 | BOOST_CHECK_EQUAL(i.getSignatureValue(), sv1); |
| 976 | BOOST_CHECK_EQUAL(i.isSigned(), true); |
| 977 | |
| 978 | // Ensure that wire is reset if specified InterestSignatureValue is different |
| 979 | i.wireEncode(); |
| 980 | BOOST_CHECK_EQUAL(i.hasWire(), true); |
| 981 | Block sv2("2E04 99887766"_block); |
| 982 | auto svBuffer2 = make_shared<Buffer>(sv2.value(), sv2.value_size()); |
| 983 | i.setSignatureValue(svBuffer2); |
| 984 | BOOST_CHECK_EQUAL(i.hasWire(), false); |
| 985 | BOOST_CHECK(i.getSignatureInfo() == si2); |
| 986 | BOOST_CHECK_EQUAL(i.getSignatureValue(), sv2); |
| 987 | BOOST_CHECK_EQUAL(i.isSigned(), true); |
| 988 | } |
| 989 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 990 | BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent) |
| 991 | { |
| 992 | Interest i("/I"); |
| 993 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 994 | |
| 995 | i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent |
| 996 | BOOST_CHECK_EQUAL(i.getName(), |
| 997 | "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
| 998 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 999 | |
| 1000 | i.setApplicationParameters(nullptr, 0); // updates ParametersSha256DigestComponent |
| 1001 | BOOST_CHECK_EQUAL(i.getName(), |
| 1002 | "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce"); |
| 1003 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
| 1004 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 1005 | |
| 1006 | i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent |
| 1007 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 1008 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 1009 | |
| 1010 | i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q")); |
| 1011 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 1012 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 1013 | |
| 1014 | i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent |
| 1015 | BOOST_CHECK_EQUAL(i.getName(), "/P/Q"); |
| 1016 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 1017 | |
| 1018 | i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q")); |
| 1019 | i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent |
| 1020 | BOOST_CHECK_EQUAL(i.getName(), |
| 1021 | "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q"); |
| 1022 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 1023 | |
| 1024 | i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent |
| 1025 | BOOST_CHECK_EQUAL(i.getName(), |
| 1026 | "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
| 1027 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
| 1028 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
Eric Newberry | 6e262f0 | 2020-05-29 23:11:25 -0700 | [diff] [blame] | 1029 | |
| 1030 | SignatureInfo si(tlv::SignatureSha256WithEcdsa); |
| 1031 | i.setSignatureInfo(si); // updates ParametersSha256DigestComponent |
| 1032 | BOOST_CHECK_EQUAL(i.getName(), |
| 1033 | "/A/B/C/params-sha256=6400cae1730c15fd7854b26be05794d53685423c94bc61e59c49bd640d646ae8"); |
| 1034 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 1035 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block); |
| 1036 | BOOST_CHECK(i.getSignatureInfo() == si); |
| 1037 | |
| 1038 | i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent and InterestSignatureInfo |
| 1039 | BOOST_CHECK(i.getSignatureInfo() == nullopt); |
| 1040 | BOOST_CHECK_EQUAL(i.getSignatureValue().isValid(), false); |
| 1041 | BOOST_CHECK_EQUAL(i.getName(), "/A/B/C"); |
| 1042 | |
| 1043 | i.setSignatureInfo(si); // auto-adds an empty ApplicationParameters element |
| 1044 | BOOST_CHECK_EQUAL(i.getName(), |
| 1045 | "/A/B/C/params-sha256=d2ac0eb1f60f60ab206fb80bf1d0f73cfef353bbec43ba6ea626117f671ca3bb"); |
| 1046 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 1047 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 1048 | BOOST_CHECK(i.getSignatureInfo() == si); |
| 1049 | |
| 1050 | Block sv("2E04 01020304"_block); |
| 1051 | i.setSignatureValue(make_shared<Buffer>(sv.value(), sv.value_size())); // updates ParametersDigestSha256Component |
| 1052 | BOOST_CHECK_EQUAL(i.getName(), |
| 1053 | "/A/B/C/params-sha256=f649845ef944638390d1c689e2f0618ea02e471eff236110cbeb822d5932d342"); |
| 1054 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 1055 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 1056 | BOOST_CHECK(i.getSignatureInfo() == si); |
| 1057 | BOOST_CHECK_EQUAL(i.getSignatureValue(), sv); |
| 1058 | |
| 1059 | i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent |
| 1060 | BOOST_CHECK_EQUAL(i.getName(), |
| 1061 | "/A/B/C/params-sha256=c5d7e567e6b251ddf36f7a6dbed95235b2d4a0b36215bb0f3cc403ac64ad0284"); |
| 1062 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 1063 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404 C0C1C2C3"_block); |
| 1064 | BOOST_CHECK(i.getSignatureInfo() == si); |
| 1065 | BOOST_CHECK_EQUAL(i.getSignatureValue(), sv); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 1066 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 1067 | |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 1068 | BOOST_AUTO_TEST_CASE(ExtractSignedRanges) |
| 1069 | { |
| 1070 | Interest i1; |
| 1071 | i1.setCanBePrefix(false); |
| 1072 | BOOST_CHECK_EXCEPTION(i1.extractSignedRanges(), tlv::Error, [] (const auto& e) { |
| 1073 | BOOST_TEST_MESSAGE(e.what()); |
| 1074 | return e.what() == "Name has zero name components"s; |
| 1075 | }); |
| 1076 | i1.setName("/test/prefix"); |
| 1077 | i1.setNonce(0x01020304); |
| 1078 | SignatureInfo sigInfo(tlv::DigestSha256); |
| 1079 | i1.setSignatureInfo(sigInfo); |
| 1080 | |
| 1081 | // Test with previously unsigned Interest (no InterestSignatureValue) |
| 1082 | auto ranges1 = i1.extractSignedRanges(); |
| 1083 | BOOST_REQUIRE_EQUAL(ranges1.size(), 2); |
| 1084 | const Block& wire1 = i1.wireEncode(); |
| 1085 | // Ensure Name range captured properly |
| 1086 | Block nameWithoutDigest1 = i1.getName().getPrefix(-1).wireEncode(); |
| 1087 | BOOST_CHECK_EQUAL_COLLECTIONS(ranges1.front().first, ranges1.front().first + ranges1.front().second, |
| 1088 | nameWithoutDigest1.value_begin(), nameWithoutDigest1.value_end()); |
| 1089 | // Ensure parameters range captured properly |
| 1090 | const auto& appParamsWire1 = wire1.find(tlv::ApplicationParameters); |
| 1091 | BOOST_REQUIRE(appParamsWire1 != wire1.elements_end()); |
| 1092 | BOOST_CHECK_EQUAL_COLLECTIONS(ranges1.back().first, ranges1.back().first + ranges1.back().second, |
| 1093 | appParamsWire1->begin(), wire1.end()); |
| 1094 | |
| 1095 | // Test with Interest with existing InterestSignatureValue |
| 1096 | auto sigValue = make_shared<Buffer>(); |
| 1097 | i1.setSignatureValue(sigValue); |
| 1098 | auto ranges2 = i1.extractSignedRanges(); |
| 1099 | BOOST_REQUIRE_EQUAL(ranges2.size(), 2); |
| 1100 | const auto& wire2 = i1.wireEncode(); |
| 1101 | // Ensure Name range captured properly |
| 1102 | Block nameWithoutDigest2 = i1.getName().getPrefix(-1).wireEncode(); |
| 1103 | BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.front().first, ranges2.front().first + ranges2.front().second, |
| 1104 | nameWithoutDigest2.value_begin(), nameWithoutDigest2.value_end()); |
| 1105 | // Ensure parameters range captured properly |
| 1106 | const auto& appParamsWire2 = wire2.find(tlv::ApplicationParameters); |
| 1107 | BOOST_REQUIRE(appParamsWire2 != wire2.elements_end()); |
| 1108 | const auto& sigValueWire2 = wire2.find(tlv::InterestSignatureValue); |
| 1109 | BOOST_REQUIRE(sigValueWire2 != wire2.elements_end()); |
| 1110 | BOOST_CHECK_EQUAL_COLLECTIONS(ranges2.back().first, ranges2.back().first + ranges2.back().second, |
| 1111 | appParamsWire2->begin(), sigValueWire2->begin()); |
| 1112 | |
| 1113 | // Test with decoded Interest |
| 1114 | const uint8_t WIRE[] = { |
| 1115 | 0x05, 0x6f, // Interest |
| 1116 | 0x07, 0x2e, // Name |
| 1117 | 0x08, 0x04, // GenericNameComponent |
| 1118 | 0x61, 0x62, 0x63, 0x64, |
| 1119 | 0x08, 0x04, // GenericNameComponent |
| 1120 | 0x65, 0x66, 0x67, 0x68, |
| 1121 | 0x02, 0x20, // ParametersSha256DigestComponent |
| 1122 | 0x6f, 0x29, 0x58, 0x60, 0x53, 0xee, 0x9f, 0xcc, |
| 1123 | 0xd8, 0xa4, 0x22, 0x12, 0x29, 0x25, 0x28, 0x7c, |
| 1124 | 0x0a, 0x18, 0x43, 0x5f, 0x40, 0x74, 0xc4, 0x0a, |
| 1125 | 0xbb, 0x0d, 0x5b, 0x30, 0xe4, 0xaa, 0x62, 0x20, |
| 1126 | 0x12, 0x00, // MustBeFresh |
| 1127 | 0x0a, 0x04, // Nonce |
| 1128 | 0x4c, 0x1e, 0xcb, 0x4a, |
| 1129 | 0x24, 0x04, // ApplicationParameters |
| 1130 | 0xc0, 0xc1, 0xc2, 0xc3, |
| 1131 | 0x2c, 0x0d, // InterestSignatureInfo |
| 1132 | 0x1b, 0x01, // SignatureType |
| 1133 | 0x00, |
| 1134 | 0x26, 0x08, // SignatureNonce |
| 1135 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 1136 | 0x2e, 0x20, // InterestSignatureValue |
| 1137 | 0x12, 0x47, 0x1a, 0xe0, 0xf8, 0x72, 0x3a, 0xc1, |
| 1138 | 0x15, 0x6c, 0x37, 0x0a, 0x38, 0x71, 0x1e, 0xbe, |
| 1139 | 0xbf, 0x28, 0x17, 0xde, 0x9b, 0x2d, 0xd9, 0x4e, |
| 1140 | 0x9b, 0x7e, 0x62, 0xf1, 0x17, 0xb8, 0x76, 0xc1, |
| 1141 | }; |
| 1142 | Block wire3(WIRE, sizeof(WIRE)); |
| 1143 | Interest i2(wire3); |
| 1144 | auto ranges3 = i2.extractSignedRanges(); |
| 1145 | BOOST_REQUIRE_EQUAL(ranges3.size(), 2); |
| 1146 | // Ensure Name range captured properly |
| 1147 | BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.front().first, ranges3.front().first + ranges3.front().second, |
| 1148 | &WIRE[4], &WIRE[16]); |
| 1149 | // Ensure parameters range captured properly |
| 1150 | BOOST_CHECK_EQUAL_COLLECTIONS(ranges3.back().first, ranges3.back().first + ranges3.back().second, |
| 1151 | &WIRE[58], &WIRE[79]); |
| 1152 | |
| 1153 | // Test failure with missing ParametersSha256DigestComponent |
| 1154 | Interest i3("/a"); |
| 1155 | i3.setCanBePrefix(false); |
| 1156 | BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) { |
| 1157 | return e.what() == "Interest Name must end with a ParametersSha256DigestComponent"s; |
| 1158 | }); |
| 1159 | |
| 1160 | // Test failure with missing InterestSignatureInfo |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame^] | 1161 | i3.setApplicationParameters(nullptr, 0); |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 1162 | BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) { |
| 1163 | return e.what() == "Interest missing InterestSignatureInfo"s; |
| 1164 | }); |
| 1165 | } |
| 1166 | |
Davide Pesavento | 2fdb274 | 2019-07-31 23:03:35 -0400 | [diff] [blame] | 1167 | BOOST_AUTO_TEST_CASE(ToUri) |
| 1168 | { |
| 1169 | Interest i; |
| 1170 | i.setCanBePrefix(false); |
| 1171 | BOOST_CHECK_EQUAL(i.toUri(), "/"); |
| 1172 | |
| 1173 | i.setName("/foo"); |
| 1174 | BOOST_CHECK_EQUAL(i.toUri(), "/foo"); |
| 1175 | |
| 1176 | i.setCanBePrefix(true); |
| 1177 | BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix"); |
| 1178 | |
| 1179 | i.setMustBeFresh(true); |
| 1180 | BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh"); |
| 1181 | |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 1182 | i.setNonce(0xa1b2c3); |
| 1183 | BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3"); |
Davide Pesavento | 2fdb274 | 2019-07-31 23:03:35 -0400 | [diff] [blame] | 1184 | |
| 1185 | i.setInterestLifetime(2_s); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 1186 | BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000"); |
Davide Pesavento | 2fdb274 | 2019-07-31 23:03:35 -0400 | [diff] [blame] | 1187 | |
| 1188 | i.setHopLimit(18); |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 1189 | BOOST_CHECK_EQUAL(i.toUri(), "/foo?CanBePrefix&MustBeFresh&Nonce=00a1b2c3&Lifetime=2000&HopLimit=18"); |
Davide Pesavento | 2fdb274 | 2019-07-31 23:03:35 -0400 | [diff] [blame] | 1190 | |
| 1191 | i.setCanBePrefix(false); |
| 1192 | i.setMustBeFresh(false); |
| 1193 | i.setHopLimit(nullopt); |
| 1194 | i.setApplicationParameters("2402CAFE"_block); |
| 1195 | BOOST_CHECK_EQUAL(i.toUri(), |
| 1196 | "/foo/params-sha256=8621f5e8321f04104640c8d02877d7c5142cad6e203c5effda1783b1a0e476d6" |
Davide Pesavento | 5353394 | 2020-03-04 23:10:06 -0500 | [diff] [blame] | 1197 | "?Nonce=00a1b2c3&Lifetime=2000"); |
Davide Pesavento | 2fdb274 | 2019-07-31 23:03:35 -0400 | [diff] [blame] | 1198 | } |
| 1199 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 1200 | BOOST_AUTO_TEST_SUITE_END() // TestInterest |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 1201 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 1202 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 1203 | } // namespace ndn |