blob: e6ad757713e296cf270be8ca58be851065692def [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 Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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"
23#include "ndn-cxx/data.hpp"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Shock Jiangca7ea702014-10-02 11:23:25 -070026
27namespace ndn {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(TestMetaInfo)
31
Junxiao Shiebfe4a22018-04-01 23:53:40 +000032BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(EncodeDecodeEquality, 1)
33BOOST_AUTO_TEST_CASE(EncodeDecodeEquality)
Shock Jiangca7ea702014-10-02 11:23:25 -070034{
Junxiao Shiebfe4a22018-04-01 23:53:40 +000035 // default values
36 MetaInfo a("1406 type=180100 freshness=190100"_block);
37 BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Blob);
38 BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 0_ms);
39 BOOST_CHECK(!a.getFinalBlock());
Junxiao Shiebfe4a22018-04-01 23:53:40 +000040 BOOST_CHECK_EQUAL(a, a);
Shock Jiangca7ea702014-10-02 11:23:25 -070041
Shock Jiangca7ea702014-10-02 11:23:25 -070042 MetaInfo b;
Junxiao Shiebfe4a22018-04-01 23:53:40 +000043 BOOST_CHECK_NE(a, b);
44 b.setType(a.getType());
45 b.setFreshnessPeriod(a.getFreshnessPeriod());
46 b.setFinalBlock(a.getFinalBlock());
Junxiao Shi72c0c642018-04-20 15:41:09 +000047 BOOST_CHECK_EQUAL(b.wireEncode(), "1400"_block);
Junxiao Shiebfe4a22018-04-01 23:53:40 +000048 BOOST_CHECK_EQUAL(a, b); // expected failure #4569
Shock Jiangca7ea702014-10-02 11:23:25 -070049
Junxiao Shiebfe4a22018-04-01 23:53:40 +000050 // non-default values
51 Block wire2 = "140C type=180101 freshness=190266B2 finalblock=1A03080141"_block;
52 a.wireDecode(wire2);
53 BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Link);
54 BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 26290_ms);
55 BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component("A"));
Junxiao Shiebfe4a22018-04-01 23:53:40 +000056 BOOST_CHECK_NE(a, b);
Shock Jiangca7ea702014-10-02 11:23:25 -070057
Junxiao Shiebfe4a22018-04-01 23:53:40 +000058 b.setType(a.getType());
59 b.setFreshnessPeriod(a.getFreshnessPeriod());
Davide Pesavento8dfd9132018-10-28 18:11:24 -040060 b.setFinalBlock(a.getFinalBlock());
Junxiao Shi72c0c642018-04-20 15:41:09 +000061 BOOST_CHECK_EQUAL(b.wireEncode(), wire2);
Junxiao Shiebfe4a22018-04-01 23:53:40 +000062 BOOST_CHECK_EQUAL(a, b);
Shock Jiangca7ea702014-10-02 11:23:25 -070063
Junxiao Shiebfe4a22018-04-01 23:53:40 +000064 // FinalBlockId is typed name component
65 Block wire3 = "1405 finalblock=1A03DD0141"_block;
66 a.wireDecode(wire3);
67 BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Blob);
68 BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 0_ms);
69 BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component::fromEscapedString("221=A"));
70 BOOST_CHECK_NE(a, b);
Shock Jiangca7ea702014-10-02 11:23:25 -070071
Junxiao Shiebfe4a22018-04-01 23:53:40 +000072 b.setType(a.getType());
73 b.setFreshnessPeriod(a.getFreshnessPeriod());
Davide Pesavento8dfd9132018-10-28 18:11:24 -040074 b.setFinalBlock(a.getFinalBlock());
Junxiao Shi72c0c642018-04-20 15:41:09 +000075 BOOST_CHECK_EQUAL(b.wireEncode(), wire3);
Junxiao Shiebfe4a22018-04-01 23:53:40 +000076 BOOST_CHECK_EQUAL(a, b);
Shock Jiangca7ea702014-10-02 11:23:25 -070077}
78
79BOOST_AUTO_TEST_CASE(AppMetaInfo)
80{
81 MetaInfo info1;
82 info1.setType(196);
Davide Pesavento0f830802018-01-16 23:58:58 -050083 info1.setFreshnessPeriod(3600_ms);
Junxiao Shiebfe4a22018-04-01 23:53:40 +000084 info1.setFinalBlock(name::Component("/att/final"));
Shock Jiangca7ea702014-10-02 11:23:25 -070085
86 uint32_t ints[5] = {128, 129, 130, 131, 132};
87 std::string ss[5] = {"h", "hello", "hello, world", "hello, world, alex",
88 "hello, world, alex, I am Xiaoke Jiang"};
89
Davide Pesavento8dfd9132018-10-28 18:11:24 -040090 for (size_t i = 0; i < 5; i++) {
Shock Jiangca7ea702014-10-02 11:23:25 -070091 uint32_t type = 128 + i * 10;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070092 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i]));
Shock Jiangca7ea702014-10-02 11:23:25 -070093 type += 5;
Davide Pesaventoc2e12b42017-07-09 20:35:09 -040094 info1.addAppMetaInfo(makeStringBlock(type, ss[i]));
Shock Jiangca7ea702014-10-02 11:23:25 -070095 }
96
Junxiao Shi72c0c642018-04-20 15:41:09 +000097 BOOST_CHECK(info1.findAppMetaInfo(252) == nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -070098
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070099 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000100 BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700101
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700102 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Junxiao Shi72c0c642018-04-20 15:41:09 +0000103 BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700104
105 info1.removeAppMetaInfo(252);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000106 BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700107
108 info1.removeAppMetaInfo(252);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000109 BOOST_CHECK(info1.findAppMetaInfo(252) == nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700110
111 // // These octets are obtained by the snippet below.
112 // // This check is intended to detect unexpected encoding change in the future.
113 // const Block& wire = info1.wireEncode();
114 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
115 // printf("0x%02x, ", *it);
116 // }
Shock Jiangca7ea702014-10-02 11:23:25 -0700117 const uint8_t METAINFO[] = {0x14, 0x77, 0x18, 0x01, 0xc4, 0x19, 0x02, 0x0e, 0x10, 0x1a, 0x0c,
118 0x08, 0x0a, 0x2f, 0x61, 0x74, 0x74, 0x2f, 0x66, 0x69, 0x6e, 0x61,
119 0x6c, 0x80, 0x01, 0x80, 0x85, 0x01, 0x68, 0x8a, 0x01, 0x81, 0x8f,
120 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x94, 0x01, 0x82, 0x99, 0x0c,
121 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c,
122 0x64, 0x9e, 0x01, 0x83, 0xa3, 0x12, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
123 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, 0x6c,
124 0x65, 0x78, 0xa8, 0x01, 0x84, 0xad, 0x25, 0x68, 0x65, 0x6c, 0x6c,
125 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61,
126 0x6c, 0x65, 0x78, 0x2c, 0x20, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x58,
127 0x69, 0x61, 0x6f, 0x6b, 0x65, 0x20, 0x4a, 0x69, 0x61, 0x6e, 0x67};
128
129 BOOST_REQUIRE_EQUAL_COLLECTIONS(info1.wireEncode().begin(), info1.wireEncode().end(),
130 METAINFO, METAINFO + sizeof(METAINFO));
131
132 MetaInfo info2;
133 info2.wireDecode(Block(METAINFO, sizeof(METAINFO)));
134
Davide Pesavento8dfd9132018-10-28 18:11:24 -0400135 for (size_t i = 0; i < 5; i++) {
Shock Jiangca7ea702014-10-02 11:23:25 -0700136 uint32_t tlvType = 128 + i * 10;
137 const Block* block = info2.findAppMetaInfo(tlvType);
Davide Pesavento8dfd9132018-10-28 18:11:24 -0400138 BOOST_REQUIRE(block != nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700139 BOOST_CHECK_EQUAL(readNonNegativeInteger(*block), ints[i]);
140 tlvType += 5;
141
142 block = info2.findAppMetaInfo(tlvType);
Davide Pesavento8dfd9132018-10-28 18:11:24 -0400143 BOOST_REQUIRE(block != nullptr);
Shock Jiangca7ea702014-10-02 11:23:25 -0700144
Davide Pesavento8dfd9132018-10-28 18:11:24 -0400145 std::string s3(reinterpret_cast<const char*>(block->value()), block->value_size());
Shock Jiangca7ea702014-10-02 11:23:25 -0700146 BOOST_CHECK_EQUAL(s3, ss[i]);
147 }
148}
149
150BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange)
151{
152 MetaInfo info;
153
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700154 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000)));
155 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)));
Shock Jiangca7ea702014-10-02 11:23:25 -0700156
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700157 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error);
158 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error);
Shock Jiangca7ea702014-10-02 11:23:25 -0700159}
160
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100161BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo
Shock Jiangca7ea702014-10-02 11:23:25 -0700162
163} // namespace tests
164} // namespace ndn