blob: ad83119f9910c35dbfbe58f09267967ae79a9930 [file] [log] [blame]
Shock Jiangca7ea702014-10-02 11:23:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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
22#include "meta-info.hpp"
Shock Jiangca7ea702014-10-02 11:23:25 -070023#include "data.hpp"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070024#include "security/v1/cryptopp.hpp"
Shock Jiangca7ea702014-10-02 11:23:25 -070025#include "encoding/buffer-stream.hpp"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080026
Shock Jiangca7ea702014-10-02 11:23:25 -070027#include "boost-test.hpp"
28
29namespace ndn {
30namespace tests {
31
32BOOST_AUTO_TEST_SUITE(TestMetaInfo)
33
34const uint8_t MetaInfo1[] = {0x14, 0x04, 0x19, 0x02, 0x27, 0x10};
35const uint8_t MetaInfo2[] = {0x14, 0x14, 0x19, 0x02, 0x27, 0x10, 0x1a, 0x0e, 0x08, 0x0c,
36 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x77, 0x6f, 0x72, 0x6c,
37 0x64, 0x21};
38const uint8_t MetaInfo3[] = {0x14, 0x17, 0x18, 0x01, 0x01, 0x19, 0x02, 0x27, 0x10, 0x1a,
39 0x0e, 0x08, 0x0c, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x77,
40 0x6f, 0x72, 0x6c, 0x64, 0x21};
41
42BOOST_AUTO_TEST_CASE(Encode)
43{
44 MetaInfo meta;
Junxiao Shia464b922014-11-12 21:13:06 -070045 meta.setType(tlv::ContentType_Blob);
Shock Jiangca7ea702014-10-02 11:23:25 -070046 meta.setFreshnessPeriod(time::seconds(10));
47
48 BOOST_REQUIRE_NO_THROW(meta.wireEncode());
49 BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo1, MetaInfo1+sizeof(MetaInfo1),
50 meta.wireEncode().begin(), meta.wireEncode().end());
51
52 meta.setFinalBlockId(name::Component("hello,world!"));
53 BOOST_REQUIRE_NO_THROW(meta.wireEncode());
54 BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo2, MetaInfo2+sizeof(MetaInfo2),
55 meta.wireEncode().begin(), meta.wireEncode().end());
56
Junxiao Shia464b922014-11-12 21:13:06 -070057 meta.setType(tlv::ContentType_Link);
Shock Jiangca7ea702014-10-02 11:23:25 -070058 BOOST_REQUIRE_NO_THROW(meta.wireEncode());
59 BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo3, MetaInfo3+sizeof(MetaInfo3),
60 meta.wireEncode().begin(), meta.wireEncode().end());
61}
62
63BOOST_AUTO_TEST_CASE(Decode)
64{
65 MetaInfo meta(Block(MetaInfo1, sizeof(MetaInfo1)));
Junxiao Shia464b922014-11-12 21:13:06 -070066 BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob));
Shock Jiangca7ea702014-10-02 11:23:25 -070067 BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), time::seconds(10));
68 BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component());
69
70 meta.wireDecode(Block(MetaInfo2, sizeof(MetaInfo2)));
Junxiao Shia464b922014-11-12 21:13:06 -070071 BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob));
Shock Jiangca7ea702014-10-02 11:23:25 -070072 BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), time::seconds(10));
73 BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!"));
74
75 meta.wireDecode(Block(MetaInfo3, sizeof(MetaInfo3)));
Junxiao Shia464b922014-11-12 21:13:06 -070076 BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Link));
Shock Jiangca7ea702014-10-02 11:23:25 -070077 BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), time::seconds(10));
78 BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!"));
79}
80
81BOOST_AUTO_TEST_CASE(EqualityChecks)
82{
83 using namespace time;
84
85 MetaInfo a;
86 MetaInfo b;
87 BOOST_CHECK_EQUAL(a == b, true);
88 BOOST_CHECK_EQUAL(a != b, false);
89
90 a.setFreshnessPeriod(seconds(10));
91 BOOST_CHECK_EQUAL(a == b, false);
92 BOOST_CHECK_EQUAL(a != b, true);
93
94 b.setFreshnessPeriod(milliseconds(90000));
95 BOOST_CHECK_EQUAL(a == b, false);
96 BOOST_CHECK_EQUAL(a != b, true);
97
98 b.setFreshnessPeriod(milliseconds(10000));
99 BOOST_CHECK_EQUAL(a == b, true);
100 BOOST_CHECK_EQUAL(a != b, false);
101
102 a.setType(10);
103 BOOST_CHECK_EQUAL(a == b, false);
104 BOOST_CHECK_EQUAL(a != b, true);
105
106 b.setType(10);
107 BOOST_CHECK_EQUAL(a == b, true);
108 BOOST_CHECK_EQUAL(a != b, false);
109}
110
111BOOST_AUTO_TEST_CASE(AppMetaInfo)
112{
113 MetaInfo info1;
114 info1.setType(196);
115 info1.setFreshnessPeriod(time::milliseconds(3600));
116 info1.setFinalBlockId(name::Component("/att/final"));
117
118 uint32_t ints[5] = {128, 129, 130, 131, 132};
119 std::string ss[5] = {"h", "hello", "hello, world", "hello, world, alex",
120 "hello, world, alex, I am Xiaoke Jiang"};
121
122 for (int i = 0; i < 5; i++) {
123 uint32_t type = 128 + i * 10;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700124 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i]));
Shock Jiangca7ea702014-10-02 11:23:25 -0700125 const std::string& s = ss[i];
126 type += 5;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700127 info1.addAppMetaInfo(makeStringBlock(type, s));
Shock Jiangca7ea702014-10-02 11:23:25 -0700128 }
129
130 BOOST_CHECK(info1.findAppMetaInfo(252) == 0);
131
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700132 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Shock Jiangca7ea702014-10-02 11:23:25 -0700133 BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
134
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700135 info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
Shock Jiangca7ea702014-10-02 11:23:25 -0700136 BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
137
138 info1.removeAppMetaInfo(252);
139 BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
140
141 info1.removeAppMetaInfo(252);
142 BOOST_CHECK(info1.findAppMetaInfo(252) == 0);
143
144 // // These octets are obtained by the snippet below.
145 // // This check is intended to detect unexpected encoding change in the future.
146 // const Block& wire = info1.wireEncode();
147 // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
148 // printf("0x%02x, ", *it);
149 // }
150
151 const uint8_t METAINFO[] = {0x14, 0x77, 0x18, 0x01, 0xc4, 0x19, 0x02, 0x0e, 0x10, 0x1a, 0x0c,
152 0x08, 0x0a, 0x2f, 0x61, 0x74, 0x74, 0x2f, 0x66, 0x69, 0x6e, 0x61,
153 0x6c, 0x80, 0x01, 0x80, 0x85, 0x01, 0x68, 0x8a, 0x01, 0x81, 0x8f,
154 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x94, 0x01, 0x82, 0x99, 0x0c,
155 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c,
156 0x64, 0x9e, 0x01, 0x83, 0xa3, 0x12, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
157 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, 0x6c,
158 0x65, 0x78, 0xa8, 0x01, 0x84, 0xad, 0x25, 0x68, 0x65, 0x6c, 0x6c,
159 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61,
160 0x6c, 0x65, 0x78, 0x2c, 0x20, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x58,
161 0x69, 0x61, 0x6f, 0x6b, 0x65, 0x20, 0x4a, 0x69, 0x61, 0x6e, 0x67};
162
163 BOOST_REQUIRE_EQUAL_COLLECTIONS(info1.wireEncode().begin(), info1.wireEncode().end(),
164 METAINFO, METAINFO + sizeof(METAINFO));
165
166 MetaInfo info2;
167 info2.wireDecode(Block(METAINFO, sizeof(METAINFO)));
168
169 for (int i = 0; i < 5; i++) {
170 uint32_t tlvType = 128 + i * 10;
171 const Block* block = info2.findAppMetaInfo(tlvType);
172 BOOST_REQUIRE(block != 0);
173 BOOST_CHECK_EQUAL(readNonNegativeInteger(*block), ints[i]);
174 tlvType += 5;
175
176 block = info2.findAppMetaInfo(tlvType);
177 BOOST_REQUIRE(block != 0);
178
179 std::string s3 = std::string(reinterpret_cast<const char*>(block->value()),
180 block->value_size());
181 BOOST_CHECK_EQUAL(s3, ss[i]);
182 }
183}
184
185BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange)
186{
187 MetaInfo info;
188
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700189 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000)));
190 BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)));
Shock Jiangca7ea702014-10-02 11:23:25 -0700191
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700192 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error);
193 BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error);
Shock Jiangca7ea702014-10-02 11:23:25 -0700194}
195
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100196BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo
Shock Jiangca7ea702014-10-02 11:23:25 -0700197
198} // namespace tests
199} // namespace ndn