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