Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c2e12b4 | 2017-07-09 20:35:09 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 8e047e1 | 2024-02-12 16:50:23 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/meta-info.hpp" |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 26 | namespace ndn::tests { |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 28 | BOOST_CONCEPT_ASSERT((WireEncodable<MetaInfo>)); |
| 29 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<MetaInfo>)); |
| 30 | BOOST_CONCEPT_ASSERT((WireDecodable<MetaInfo>)); |
| 31 | static_assert(std::is_convertible_v<MetaInfo::Error*, tlv::Error*>, |
| 32 | "MetaInfo::Error must inherit from tlv::Error"); |
| 33 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(TestMetaInfo) |
| 35 | |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 36 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 37 | { |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 38 | // default values |
| 39 | MetaInfo a("1406 type=180100 freshness=190100"_block); |
| 40 | BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Blob); |
| 41 | BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 0_ms); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 42 | BOOST_CHECK(a.getFinalBlock() == std::nullopt); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 43 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 44 | MetaInfo b; |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 45 | BOOST_CHECK_NE(a.wireEncode(), b.wireEncode()); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 46 | b.setType(a.getType()); |
| 47 | b.setFreshnessPeriod(a.getFreshnessPeriod()); |
| 48 | b.setFinalBlock(a.getFinalBlock()); |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 49 | BOOST_CHECK_NE(a.wireEncode(), b.wireEncode()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(b.wireEncode(), "1400"_block); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 51 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 52 | // non-default values |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 53 | Block wire2("140C type=180101 freshness=190266B2 finalblock=1A03080141"_block); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 54 | a.wireDecode(wire2); |
| 55 | BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Link); |
| 56 | BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 26290_ms); |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 57 | BOOST_REQUIRE(a.getFinalBlock().has_value()); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component("A")); |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 59 | BOOST_CHECK_NE(a.wireEncode(), b.wireEncode()); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 60 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 61 | b.setType(a.getType()); |
| 62 | b.setFreshnessPeriod(a.getFreshnessPeriod()); |
Davide Pesavento | 8dfd913 | 2018-10-28 18:11:24 -0400 | [diff] [blame] | 63 | b.setFinalBlock(a.getFinalBlock()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(b.wireEncode(), wire2); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 65 | |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 66 | // FinalBlockId is a typed name component |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 67 | Block wire3 = "1405 finalblock=1A03DD0141"_block; |
| 68 | a.wireDecode(wire3); |
| 69 | BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Blob); |
| 70 | BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 0_ms); |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 71 | BOOST_REQUIRE(a.getFinalBlock().has_value()); |
Davide Pesavento | 8e047e1 | 2024-02-12 16:50:23 -0500 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component::fromUri("221=A")); |
Davide Pesavento | ef654dd | 2019-07-18 20:02:44 -0400 | [diff] [blame] | 73 | BOOST_CHECK_NE(a.wireEncode(), b.wireEncode()); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 74 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 75 | b.setType(a.getType()); |
| 76 | b.setFreshnessPeriod(a.getFreshnessPeriod()); |
Davide Pesavento | 8dfd913 | 2018-10-28 18:11:24 -0400 | [diff] [blame] | 77 | b.setFinalBlock(a.getFinalBlock()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(b.wireEncode(), wire3); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Davide Pesavento | 7b540d2 | 2024-02-12 23:02:42 -0500 | [diff] [blame] | 81 | BOOST_AUTO_TEST_CASE(FreshnessPeriodOverflow, |
| 82 | * ut::description("test for bug #4997")) |
Junxiao Shi | ce442c4 | 2023-08-18 02:35:25 +0000 | [diff] [blame] | 83 | { |
Junxiao Shi | ce442c4 | 2023-08-18 02:35:25 +0000 | [diff] [blame] | 84 | MetaInfo mi0("140A 19087FFFFFFFFFFFFFFF"_block); |
| 85 | BOOST_CHECK_EQUAL(mi0.getFreshnessPeriod(), 0x7FFFFFFFFFFFFFFF_ms); |
| 86 | |
| 87 | MetaInfo mi1("140A 19088000000000000000"_block); |
| 88 | BOOST_CHECK_EQUAL(mi1.getFreshnessPeriod(), time::milliseconds::max()); |
| 89 | |
| 90 | MetaInfo mi2("140A 1908FFFFFFFFFFFFFFFF"_block); |
| 91 | BOOST_CHECK_EQUAL(mi2.getFreshnessPeriod(), time::milliseconds::max()); |
Davide Pesavento | 7b540d2 | 2024-02-12 23:02:42 -0500 | [diff] [blame] | 92 | |
| 93 | // force re-encoding |
| 94 | mi2.setType(tlv::ContentType_Key); |
| 95 | BOOST_CHECK_EQUAL(mi2.wireEncode(), "140D 180102 1908FFFFFFFFFFFFFFFF"_block); |
Junxiao Shi | ce442c4 | 2023-08-18 02:35:25 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(AppMetaInfo) |
| 99 | { |
| 100 | MetaInfo info1; |
| 101 | info1.setType(196); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 102 | info1.setFreshnessPeriod(3600_ms); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 103 | info1.setFinalBlock(name::Component("/att/final")); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 104 | |
| 105 | uint32_t ints[5] = {128, 129, 130, 131, 132}; |
| 106 | std::string ss[5] = {"h", "hello", "hello, world", "hello, world, alex", |
| 107 | "hello, world, alex, I am Xiaoke Jiang"}; |
| 108 | |
Davide Pesavento | 8dfd913 | 2018-10-28 18:11:24 -0400 | [diff] [blame] | 109 | for (size_t i = 0; i < 5; i++) { |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 110 | uint32_t type = 128 + i * 10; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 111 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i])); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 112 | type += 5; |
Davide Pesavento | c2e12b4 | 2017-07-09 20:35:09 -0400 | [diff] [blame] | 113 | info1.addAppMetaInfo(makeStringBlock(type, ss[i])); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 116 | BOOST_CHECK(info1.findAppMetaInfo(252) == nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 118 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 119 | BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 120 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 121 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 122 | BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 123 | |
| 124 | info1.removeAppMetaInfo(252); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 125 | BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 126 | |
| 127 | info1.removeAppMetaInfo(252); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 128 | BOOST_CHECK(info1.findAppMetaInfo(252) == nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 129 | |
| 130 | // // These octets are obtained by the snippet below. |
| 131 | // // This check is intended to detect unexpected encoding change in the future. |
| 132 | // const Block& wire = info1.wireEncode(); |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 133 | // for (auto it = wire.begin(); it != wire.end(); ++it) { |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 134 | // printf("0x%02x, ", *it); |
| 135 | // } |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 136 | const uint8_t METAINFO[] = {0x14, 0x77, 0x18, 0x01, 0xc4, 0x19, 0x02, 0x0e, 0x10, 0x1a, 0x0c, |
| 137 | 0x08, 0x0a, 0x2f, 0x61, 0x74, 0x74, 0x2f, 0x66, 0x69, 0x6e, 0x61, |
| 138 | 0x6c, 0x80, 0x01, 0x80, 0x85, 0x01, 0x68, 0x8a, 0x01, 0x81, 0x8f, |
| 139 | 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x94, 0x01, 0x82, 0x99, 0x0c, |
| 140 | 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, |
| 141 | 0x64, 0x9e, 0x01, 0x83, 0xa3, 0x12, 0x68, 0x65, 0x6c, 0x6c, 0x6f, |
| 142 | 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, 0x6c, |
| 143 | 0x65, 0x78, 0xa8, 0x01, 0x84, 0xad, 0x25, 0x68, 0x65, 0x6c, 0x6c, |
| 144 | 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, |
| 145 | 0x6c, 0x65, 0x78, 0x2c, 0x20, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x58, |
| 146 | 0x69, 0x61, 0x6f, 0x6b, 0x65, 0x20, 0x4a, 0x69, 0x61, 0x6e, 0x67}; |
| 147 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 148 | BOOST_CHECK_EQUAL_COLLECTIONS(info1.wireEncode().begin(), info1.wireEncode().end(), |
| 149 | METAINFO, METAINFO + sizeof(METAINFO)); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 150 | |
| 151 | MetaInfo info2; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 152 | info2.wireDecode(Block(METAINFO)); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 153 | |
Davide Pesavento | 8dfd913 | 2018-10-28 18:11:24 -0400 | [diff] [blame] | 154 | for (size_t i = 0; i < 5; i++) { |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 155 | uint32_t tlvType = 128 + i * 10; |
| 156 | const Block* block = info2.findAppMetaInfo(tlvType); |
Davide Pesavento | 8dfd913 | 2018-10-28 18:11:24 -0400 | [diff] [blame] | 157 | BOOST_REQUIRE(block != nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(readNonNegativeInteger(*block), ints[i]); |
| 159 | tlvType += 5; |
| 160 | |
| 161 | block = info2.findAppMetaInfo(tlvType); |
Davide Pesavento | 8dfd913 | 2018-10-28 18:11:24 -0400 | [diff] [blame] | 162 | BOOST_REQUIRE(block != nullptr); |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(readString(*block), ss[i]); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange) |
| 168 | { |
| 169 | MetaInfo info; |
| 170 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 171 | BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000))); |
| 172 | BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000))); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 173 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 174 | BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error); |
| 175 | BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 178 | BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 179 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 180 | } // namespace ndn::tests |