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