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