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 | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 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 | |
| 22 | #include "meta-info.hpp" |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 23 | #include "data.hpp" |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 24 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestMetaInfo) |
| 31 | |
| 32 | const uint8_t MetaInfo1[] = {0x14, 0x04, 0x19, 0x02, 0x27, 0x10}; |
| 33 | const uint8_t MetaInfo2[] = {0x14, 0x14, 0x19, 0x02, 0x27, 0x10, 0x1a, 0x0e, 0x08, 0x0c, |
| 34 | 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x77, 0x6f, 0x72, 0x6c, |
| 35 | 0x64, 0x21}; |
| 36 | const uint8_t MetaInfo3[] = {0x14, 0x17, 0x18, 0x01, 0x01, 0x19, 0x02, 0x27, 0x10, 0x1a, |
| 37 | 0x0e, 0x08, 0x0c, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x77, |
| 38 | 0x6f, 0x72, 0x6c, 0x64, 0x21}; |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE(Encode) |
| 41 | { |
| 42 | MetaInfo meta; |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 43 | meta.setType(tlv::ContentType_Blob); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 44 | meta.setFreshnessPeriod(10_s); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 45 | |
| 46 | BOOST_REQUIRE_NO_THROW(meta.wireEncode()); |
| 47 | BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo1, MetaInfo1+sizeof(MetaInfo1), |
| 48 | meta.wireEncode().begin(), meta.wireEncode().end()); |
| 49 | |
| 50 | meta.setFinalBlockId(name::Component("hello,world!")); |
| 51 | BOOST_REQUIRE_NO_THROW(meta.wireEncode()); |
| 52 | BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo2, MetaInfo2+sizeof(MetaInfo2), |
| 53 | meta.wireEncode().begin(), meta.wireEncode().end()); |
| 54 | |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 55 | meta.setType(tlv::ContentType_Link); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 56 | BOOST_REQUIRE_NO_THROW(meta.wireEncode()); |
| 57 | BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo3, MetaInfo3+sizeof(MetaInfo3), |
| 58 | meta.wireEncode().begin(), meta.wireEncode().end()); |
| 59 | } |
| 60 | |
| 61 | BOOST_AUTO_TEST_CASE(Decode) |
| 62 | { |
| 63 | MetaInfo meta(Block(MetaInfo1, sizeof(MetaInfo1))); |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 65 | BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), 10_s); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component()); |
| 67 | |
| 68 | meta.wireDecode(Block(MetaInfo2, sizeof(MetaInfo2))); |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 70 | BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), 10_s); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!")); |
| 72 | |
| 73 | meta.wireDecode(Block(MetaInfo3, sizeof(MetaInfo3))); |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Link)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 75 | BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), 10_s); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!")); |
| 77 | } |
| 78 | |
| 79 | BOOST_AUTO_TEST_CASE(EqualityChecks) |
| 80 | { |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 81 | MetaInfo a; |
| 82 | MetaInfo b; |
| 83 | BOOST_CHECK_EQUAL(a == b, true); |
| 84 | BOOST_CHECK_EQUAL(a != b, false); |
| 85 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 86 | a.setFreshnessPeriod(10_s); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(a == b, false); |
| 88 | BOOST_CHECK_EQUAL(a != b, true); |
| 89 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 90 | b.setFreshnessPeriod(90_s); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(a == b, false); |
| 92 | BOOST_CHECK_EQUAL(a != b, true); |
| 93 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 94 | b.setFreshnessPeriod(10_s); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(a == b, true); |
| 96 | BOOST_CHECK_EQUAL(a != b, false); |
| 97 | |
| 98 | a.setType(10); |
| 99 | BOOST_CHECK_EQUAL(a == b, false); |
| 100 | BOOST_CHECK_EQUAL(a != b, true); |
| 101 | |
| 102 | b.setType(10); |
| 103 | BOOST_CHECK_EQUAL(a == b, true); |
| 104 | BOOST_CHECK_EQUAL(a != b, false); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(AppMetaInfo) |
| 108 | { |
| 109 | MetaInfo info1; |
| 110 | info1.setType(196); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 111 | info1.setFreshnessPeriod(3600_ms); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 112 | info1.setFinalBlockId(name::Component("/att/final")); |
| 113 | |
| 114 | uint32_t ints[5] = {128, 129, 130, 131, 132}; |
| 115 | std::string ss[5] = {"h", "hello", "hello, world", "hello, world, alex", |
| 116 | "hello, world, alex, I am Xiaoke Jiang"}; |
| 117 | |
| 118 | for (int i = 0; i < 5; i++) { |
| 119 | uint32_t type = 128 + i * 10; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 120 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i])); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 121 | type += 5; |
Davide Pesavento | c2e12b4 | 2017-07-09 20:35:09 -0400 | [diff] [blame] | 122 | info1.addAppMetaInfo(makeStringBlock(type, ss[i])); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | BOOST_CHECK(info1.findAppMetaInfo(252) == 0); |
| 126 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 127 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 128 | BOOST_CHECK(info1.findAppMetaInfo(252) != 0); |
| 129 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 130 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 131 | BOOST_CHECK(info1.findAppMetaInfo(252) != 0); |
| 132 | |
| 133 | info1.removeAppMetaInfo(252); |
| 134 | BOOST_CHECK(info1.findAppMetaInfo(252) != 0); |
| 135 | |
| 136 | info1.removeAppMetaInfo(252); |
| 137 | BOOST_CHECK(info1.findAppMetaInfo(252) == 0); |
| 138 | |
| 139 | // // These octets are obtained by the snippet below. |
| 140 | // // This check is intended to detect unexpected encoding change in the future. |
| 141 | // const Block& wire = info1.wireEncode(); |
| 142 | // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) { |
| 143 | // printf("0x%02x, ", *it); |
| 144 | // } |
| 145 | |
| 146 | const uint8_t METAINFO[] = {0x14, 0x77, 0x18, 0x01, 0xc4, 0x19, 0x02, 0x0e, 0x10, 0x1a, 0x0c, |
| 147 | 0x08, 0x0a, 0x2f, 0x61, 0x74, 0x74, 0x2f, 0x66, 0x69, 0x6e, 0x61, |
| 148 | 0x6c, 0x80, 0x01, 0x80, 0x85, 0x01, 0x68, 0x8a, 0x01, 0x81, 0x8f, |
| 149 | 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x94, 0x01, 0x82, 0x99, 0x0c, |
| 150 | 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, |
| 151 | 0x64, 0x9e, 0x01, 0x83, 0xa3, 0x12, 0x68, 0x65, 0x6c, 0x6c, 0x6f, |
| 152 | 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, 0x6c, |
| 153 | 0x65, 0x78, 0xa8, 0x01, 0x84, 0xad, 0x25, 0x68, 0x65, 0x6c, 0x6c, |
| 154 | 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, |
| 155 | 0x6c, 0x65, 0x78, 0x2c, 0x20, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x58, |
| 156 | 0x69, 0x61, 0x6f, 0x6b, 0x65, 0x20, 0x4a, 0x69, 0x61, 0x6e, 0x67}; |
| 157 | |
| 158 | BOOST_REQUIRE_EQUAL_COLLECTIONS(info1.wireEncode().begin(), info1.wireEncode().end(), |
| 159 | METAINFO, METAINFO + sizeof(METAINFO)); |
| 160 | |
| 161 | MetaInfo info2; |
| 162 | info2.wireDecode(Block(METAINFO, sizeof(METAINFO))); |
| 163 | |
| 164 | for (int i = 0; i < 5; i++) { |
| 165 | uint32_t tlvType = 128 + i * 10; |
| 166 | const Block* block = info2.findAppMetaInfo(tlvType); |
| 167 | BOOST_REQUIRE(block != 0); |
| 168 | BOOST_CHECK_EQUAL(readNonNegativeInteger(*block), ints[i]); |
| 169 | tlvType += 5; |
| 170 | |
| 171 | block = info2.findAppMetaInfo(tlvType); |
| 172 | BOOST_REQUIRE(block != 0); |
| 173 | |
| 174 | std::string s3 = std::string(reinterpret_cast<const char*>(block->value()), |
| 175 | block->value_size()); |
| 176 | BOOST_CHECK_EQUAL(s3, ss[i]); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange) |
| 181 | { |
| 182 | MetaInfo info; |
| 183 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 184 | BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000))); |
| 185 | BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000))); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 186 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 187 | BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error); |
| 188 | BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 191 | BOOST_AUTO_TEST_CASE(EncodeDecodeFreshnessPeriod) |
| 192 | { |
| 193 | const uint8_t expectedDefault[] = { |
| 194 | 0x14, 0x00, // MetaInfo |
| 195 | }; |
| 196 | |
| 197 | const uint8_t expected1000ms[] = { |
| 198 | 0x14, 0x04, // MetaInfo |
| 199 | 0x19, 0x02, // FreshnessPeriod |
| 200 | 0x03, 0xe8 // 1000ms |
| 201 | }; |
| 202 | |
| 203 | MetaInfo info; |
| 204 | BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(), |
| 205 | expectedDefault, expectedDefault + sizeof(expectedDefault)); |
| 206 | |
| 207 | info.setFreshnessPeriod(time::milliseconds::zero()); |
| 208 | BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(), |
| 209 | expectedDefault, expectedDefault + sizeof(expectedDefault)); |
| 210 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 211 | info.setFreshnessPeriod(1000_ms); |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 212 | BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(), |
| 213 | expected1000ms, expected1000ms + sizeof(expected1000ms)); |
| 214 | |
| 215 | const uint8_t inputDefault[] = { |
| 216 | 0x14, 0x03, // MetaInfo |
| 217 | 0x19, 0x01, // FreshnessPeriod |
| 218 | 0x00 // 0ms |
| 219 | }; |
| 220 | |
| 221 | const uint8_t input2000ms[] = { |
| 222 | 0x14, 0x04, // MetaInfo |
| 223 | 0x19, 0x02, // FreshnessPeriod |
| 224 | 0x07, 0xd0 // 2000ms |
| 225 | }; |
| 226 | |
| 227 | Block inputDefaultBlock(inputDefault, sizeof(inputDefault)); |
| 228 | BOOST_CHECK_NO_THROW(info.wireDecode(inputDefaultBlock)); |
| 229 | BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds::zero()); |
| 230 | |
| 231 | Block input2000msBlock(input2000ms, sizeof(input2000ms)); |
| 232 | BOOST_CHECK_NO_THROW(info.wireDecode(input2000msBlock)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 233 | BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), 2000_ms); |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 234 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 235 | BOOST_CHECK_NO_THROW(info.setFreshnessPeriod(10000_ms)); |
| 236 | BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), 10000_ms); |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 237 | BOOST_CHECK_THROW(info.setFreshnessPeriod(time::milliseconds(-1)), std::invalid_argument); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame^] | 238 | BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), 10000_ms); |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 241 | BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 242 | |
| 243 | } // namespace tests |
| 244 | } // namespace ndn |