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