blob: bd5aee4194dd0a892c9f421fad5bf75e24bd6fb6 [file] [log] [blame]
Shock Jiangca7ea702014-10-02 11:23:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc2e12b42017-07-09 20:35:09 -04002/*
Davide Pesaventof6b45892023-03-13 15:00:51 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Shock Jiangca7ea702014-10-02 11:23:25 -07004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/meta-info.hpp"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Shock Jiangca7ea702014-10-02 11:23:25 -070025
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040026namespace ndn::tests {
Shock Jiangca7ea702014-10-02 11:23:25 -070027
Davide Pesavento152ef442023-04-22 02:02:29 -040028BOOST_CONCEPT_ASSERT((WireEncodable<MetaInfo>));
29BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<MetaInfo>));
30BOOST_CONCEPT_ASSERT((WireDecodable<MetaInfo>));
31static_assert(std::is_convertible_v<MetaInfo::Error*, tlv::Error*>,
32 "MetaInfo::Error must inherit from tlv::Error");
33
Shock Jiangca7ea702014-10-02 11:23:25 -070034BOOST_AUTO_TEST_SUITE(TestMetaInfo)
35
Davide Pesaventoef654dd2019-07-18 20:02:44 -040036BOOST_AUTO_TEST_CASE(EncodeDecode)
Shock Jiangca7ea702014-10-02 11:23:25 -070037{
Junxiao Shiebfe4a22018-04-01 23:53:40 +000038 // 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 Pesaventof6b45892023-03-13 15:00:51 -040042 BOOST_CHECK(a.getFinalBlock() == std::nullopt);
Shock Jiangca7ea702014-10-02 11:23:25 -070043
Shock Jiangca7ea702014-10-02 11:23:25 -070044 MetaInfo b;
Davide Pesaventoef654dd2019-07-18 20:02:44 -040045 BOOST_CHECK_NE(a.wireEncode(), b.wireEncode());
Junxiao Shiebfe4a22018-04-01 23:53:40 +000046 b.setType(a.getType());
47 b.setFreshnessPeriod(a.getFreshnessPeriod());
48 b.setFinalBlock(a.getFinalBlock());
Davide Pesaventoef654dd2019-07-18 20:02:44 -040049 BOOST_CHECK_NE(a.wireEncode(), b.wireEncode());
Junxiao Shi72c0c642018-04-20 15:41:09 +000050 BOOST_CHECK_EQUAL(b.wireEncode(), "1400"_block);
Shock Jiangca7ea702014-10-02 11:23:25 -070051
Junxiao Shiebfe4a22018-04-01 23:53:40 +000052 // non-default values
Davide Pesaventoef654dd2019-07-18 20:02:44 -040053 Block wire2("140C type=180101 freshness=190266B2 finalblock=1A03080141"_block);
Junxiao Shiebfe4a22018-04-01 23:53:40 +000054 a.wireDecode(wire2);
55 BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Link);
56 BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 26290_ms);
Davide Pesaventoef654dd2019-07-18 20:02:44 -040057 BOOST_REQUIRE(a.getFinalBlock().has_value());
Junxiao Shiebfe4a22018-04-01 23:53:40 +000058 BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component("A"));
Davide Pesaventoef654dd2019-07-18 20:02:44 -040059 BOOST_CHECK_NE(a.wireEncode(), b.wireEncode());
Shock Jiangca7ea702014-10-02 11:23:25 -070060
Junxiao Shiebfe4a22018-04-01 23:53:40 +000061 b.setType(a.getType());
62 b.setFreshnessPeriod(a.getFreshnessPeriod());
Davide Pesavento8dfd9132018-10-28 18:11:24 -040063 b.setFinalBlock(a.getFinalBlock());
Junxiao Shi72c0c642018-04-20 15:41:09 +000064 BOOST_CHECK_EQUAL(b.wireEncode(), wire2);
Shock Jiangca7ea702014-10-02 11:23:25 -070065
Davide Pesaventoef654dd2019-07-18 20:02:44 -040066 // FinalBlockId is a typed name component
Junxiao Shiebfe4a22018-04-01 23:53:40 +000067 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 Pesaventoef654dd2019-07-18 20:02:44 -040071 BOOST_REQUIRE(a.getFinalBlock().has_value());
Junxiao Shiebfe4a22018-04-01 23:53:40 +000072 BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component::fromEscapedString("221=A"));
Davide Pesaventoef654dd2019-07-18 20:02:44 -040073 BOOST_CHECK_NE(a.wireEncode(), b.wireEncode());
Shock Jiangca7ea702014-10-02 11:23:25 -070074
Junxiao Shiebfe4a22018-04-01 23:53:40 +000075 b.setType(a.getType());
76 b.setFreshnessPeriod(a.getFreshnessPeriod());
Davide Pesavento8dfd9132018-10-28 18:11:24 -040077 b.setFinalBlock(a.getFinalBlock());
Junxiao Shi72c0c642018-04-20 15:41:09 +000078 BOOST_CHECK_EQUAL(b.wireEncode(), wire3);
Shock Jiangca7ea702014-10-02 11:23:25 -070079}
80
Junxiao Shice442c42023-08-18 02:35:25 +000081BOOST_AUTO_TEST_CASE(FreshnessPeriodOverflow)
82{
83 // Bug #4997
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());
92}
93
Shock Jiangca7ea702014-10-02 11:23:25 -070094BOOST_AUTO_TEST_CASE(AppMetaInfo)
95{
96 MetaInfo info1;
97 info1.setType(196);
Davide Pesavento0f830802018-01-16 23:58:58 -050098 info1.setFreshnessPeriod(3600_ms);
Junxiao Shiebfe4a22018-04-01 23:53:40 +000099 info1.setFinalBlock(name::Component("/att/final"));
Shock Jiangca7ea702014-10-02 11:23:25 -0700100
101 uint32_t ints[5] = {128, 129, 130, 131, 132};
102 std::string ss[5] = {"h", "hello", "hello, world", "hello, world, alex",
103 "hello, world, alex, I am Xiaoke Jiang"};
104
Davide Pesavento8dfd9132018-10-28 18:11:24 -0400105 for (size_t i = 0; i < 5; i++) {
Shock Jiangca7ea702014-10-02 11:23:25 -0700106 uint32_t type = 128 + i * 10;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700107 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i]));
Shock Jiangca7ea702014-10-02 11:23:25 -0700108 type += 5;
Davide Pesaventoc2e12b42017-07-09 20:35:09 -0400109 info1.addAppMetaInfo(makeStringBlock(type, ss[i]));
Shock Jiangca7ea702014-10-02 11:23:25 -0700110 }
111
Junxiao Shi72c0c642018-04-20 15:41:09 +0000112 BOOST_CHECK(info1.findAppMetaInfo(252) == nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700113
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700114 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000115 BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700116
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700117 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000118 BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700119
120 info1.removeAppMetaInfo(252);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000121 BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700122
123 info1.removeAppMetaInfo(252);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000124 BOOST_CHECK(info1.findAppMetaInfo(252) == nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700125
126 // // These octets are obtained by the snippet below.
127 // // This check is intended to detect unexpected encoding change in the future.
128 // const Block& wire = info1.wireEncode();
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500129 // for (auto it = wire.begin(); it != wire.end(); ++it) {
Shock Jiangca7ea702014-10-02 11:23:25 -0700130 // printf("0x%02x, ", *it);
131 // }
Shock Jiangca7ea702014-10-02 11:23:25 -0700132 const uint8_t METAINFO[] = {0x14, 0x77, 0x18, 0x01, 0xc4, 0x19, 0x02, 0x0e, 0x10, 0x1a, 0x0c,
133 0x08, 0x0a, 0x2f, 0x61, 0x74, 0x74, 0x2f, 0x66, 0x69, 0x6e, 0x61,
134 0x6c, 0x80, 0x01, 0x80, 0x85, 0x01, 0x68, 0x8a, 0x01, 0x81, 0x8f,
135 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x94, 0x01, 0x82, 0x99, 0x0c,
136 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c,
137 0x64, 0x9e, 0x01, 0x83, 0xa3, 0x12, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
138 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, 0x6c,
139 0x65, 0x78, 0xa8, 0x01, 0x84, 0xad, 0x25, 0x68, 0x65, 0x6c, 0x6c,
140 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61,
141 0x6c, 0x65, 0x78, 0x2c, 0x20, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x58,
142 0x69, 0x61, 0x6f, 0x6b, 0x65, 0x20, 0x4a, 0x69, 0x61, 0x6e, 0x67};
143
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500144 BOOST_CHECK_EQUAL_COLLECTIONS(info1.wireEncode().begin(), info1.wireEncode().end(),
145 METAINFO, METAINFO + sizeof(METAINFO));
Shock Jiangca7ea702014-10-02 11:23:25 -0700146
147 MetaInfo info2;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500148 info2.wireDecode(Block(METAINFO));
Shock Jiangca7ea702014-10-02 11:23:25 -0700149
Davide Pesavento8dfd9132018-10-28 18:11:24 -0400150 for (size_t i = 0; i < 5; i++) {
Shock Jiangca7ea702014-10-02 11:23:25 -0700151 uint32_t tlvType = 128 + i * 10;
152 const Block* block = info2.findAppMetaInfo(tlvType);
Davide Pesavento8dfd9132018-10-28 18:11:24 -0400153 BOOST_REQUIRE(block != nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700154 BOOST_CHECK_EQUAL(readNonNegativeInteger(*block), ints[i]);
155 tlvType += 5;
156
157 block = info2.findAppMetaInfo(tlvType);
Davide Pesavento8dfd9132018-10-28 18:11:24 -0400158 BOOST_REQUIRE(block != nullptr);
Davide Pesavento258d51a2022-02-27 21:26:28 -0500159 BOOST_CHECK_EQUAL(readString(*block), ss[i]);
Shock Jiangca7ea702014-10-02 11:23:25 -0700160 }
161}
162
163BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange)
164{
165 MetaInfo info;
166
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700167 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000)));
168 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)));
Shock Jiangca7ea702014-10-02 11:23:25 -0700169
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700170 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error);
171 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error);
Shock Jiangca7ea702014-10-02 11:23:25 -0700172}
173
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100174BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo
Shock Jiangca7ea702014-10-02 11:23:25 -0700175
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400176} // namespace ndn::tests