Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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 | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 24 | #include "security/v1/cryptopp.hpp" |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 25 | #include "encoding/buffer-stream.hpp" |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 26 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 27 | #include "boost-test.hpp" |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace tests { |
| 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(TestMetaInfo) |
| 33 | |
| 34 | const uint8_t MetaInfo1[] = {0x14, 0x04, 0x19, 0x02, 0x27, 0x10}; |
| 35 | const 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}; |
| 38 | const 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 | |
| 42 | BOOST_AUTO_TEST_CASE(Encode) |
| 43 | { |
| 44 | MetaInfo meta; |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 45 | meta.setType(tlv::ContentType_Blob); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 46 | 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 Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 57 | meta.setType(tlv::ContentType_Link); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 58 | BOOST_REQUIRE_NO_THROW(meta.wireEncode()); |
| 59 | BOOST_REQUIRE_EQUAL_COLLECTIONS(MetaInfo3, MetaInfo3+sizeof(MetaInfo3), |
| 60 | meta.wireEncode().begin(), meta.wireEncode().end()); |
| 61 | } |
| 62 | |
| 63 | BOOST_AUTO_TEST_CASE(Decode) |
| 64 | { |
| 65 | MetaInfo meta(Block(MetaInfo1, sizeof(MetaInfo1))); |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob)); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 67 | 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 Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Blob)); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 72 | 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 Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(meta.getType(), static_cast<uint32_t>(tlv::ContentType_Link)); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 77 | BOOST_CHECK_EQUAL(meta.getFreshnessPeriod(), time::seconds(10)); |
| 78 | BOOST_CHECK_EQUAL(meta.getFinalBlockId(), name::Component("hello,world!")); |
| 79 | } |
| 80 | |
| 81 | BOOST_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 | |
| 111 | BOOST_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 Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 124 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i])); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 125 | const std::string& s = ss[i]; |
| 126 | type += 5; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 127 | info1.addAppMetaInfo(makeStringBlock(type, s)); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 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 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 135 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 136 | 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 | |
| 185 | BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange) |
| 186 | { |
| 187 | MetaInfo info; |
| 188 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 189 | BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000))); |
| 190 | BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000))); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 191 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 192 | BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error); |
| 193 | BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 196 | BOOST_AUTO_TEST_CASE(EncodeDecodeFreshnessPeriod) |
| 197 | { |
| 198 | const uint8_t expectedDefault[] = { |
| 199 | 0x14, 0x00, // MetaInfo |
| 200 | }; |
| 201 | |
| 202 | const uint8_t expected1000ms[] = { |
| 203 | 0x14, 0x04, // MetaInfo |
| 204 | 0x19, 0x02, // FreshnessPeriod |
| 205 | 0x03, 0xe8 // 1000ms |
| 206 | }; |
| 207 | |
| 208 | MetaInfo info; |
| 209 | BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(), |
| 210 | expectedDefault, expectedDefault + sizeof(expectedDefault)); |
| 211 | |
| 212 | info.setFreshnessPeriod(time::milliseconds::zero()); |
| 213 | BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(), |
| 214 | expectedDefault, expectedDefault + sizeof(expectedDefault)); |
| 215 | |
| 216 | info.setFreshnessPeriod(time::milliseconds(1000)); |
| 217 | BOOST_CHECK_EQUAL_COLLECTIONS(info.wireEncode().begin(), info.wireEncode().end(), |
| 218 | expected1000ms, expected1000ms + sizeof(expected1000ms)); |
| 219 | |
| 220 | const uint8_t inputDefault[] = { |
| 221 | 0x14, 0x03, // MetaInfo |
| 222 | 0x19, 0x01, // FreshnessPeriod |
| 223 | 0x00 // 0ms |
| 224 | }; |
| 225 | |
| 226 | const uint8_t input2000ms[] = { |
| 227 | 0x14, 0x04, // MetaInfo |
| 228 | 0x19, 0x02, // FreshnessPeriod |
| 229 | 0x07, 0xd0 // 2000ms |
| 230 | }; |
| 231 | |
| 232 | Block inputDefaultBlock(inputDefault, sizeof(inputDefault)); |
| 233 | BOOST_CHECK_NO_THROW(info.wireDecode(inputDefaultBlock)); |
| 234 | BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds::zero()); |
| 235 | |
| 236 | Block input2000msBlock(input2000ms, sizeof(input2000ms)); |
| 237 | BOOST_CHECK_NO_THROW(info.wireDecode(input2000msBlock)); |
| 238 | BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds(2000)); |
| 239 | |
| 240 | BOOST_CHECK_NO_THROW(info.setFreshnessPeriod(time::milliseconds(10000))); |
| 241 | BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds(10000)); |
| 242 | BOOST_CHECK_THROW(info.setFreshnessPeriod(time::milliseconds(-1)), std::invalid_argument); |
| 243 | BOOST_CHECK_EQUAL(info.getFreshnessPeriod(), time::milliseconds(10000)); |
| 244 | } |
| 245 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 246 | BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 247 | |
| 248 | } // namespace tests |
| 249 | } // namespace ndn |