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 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 25 | #include "block-literal.hpp" |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(TestMetaInfo) |
| 32 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(EncodeDecodeEquality, 1) |
| 34 | BOOST_AUTO_TEST_CASE(EncodeDecodeEquality) |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 35 | { |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 36 | // default values |
| 37 | MetaInfo a("1406 type=180100 freshness=190100"_block); |
| 38 | BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Blob); |
| 39 | BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 0_ms); |
| 40 | BOOST_CHECK(!a.getFinalBlock()); |
| 41 | BOOST_CHECK_EQUAL(a.getFinalBlockId(), name::Component()); |
| 42 | BOOST_CHECK_EQUAL(a, a); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 43 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 44 | MetaInfo b; |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 45 | BOOST_CHECK_NE(a, b); |
| 46 | b.setType(a.getType()); |
| 47 | b.setFreshnessPeriod(a.getFreshnessPeriod()); |
| 48 | b.setFinalBlock(a.getFinalBlock()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 49 | BOOST_CHECK_EQUAL(b.wireEncode(), "1400"_block); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(a, b); // expected failure #4569 |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 51 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 52 | // non-default values |
| 53 | Block wire2 = "140C type=180101 freshness=190266B2 finalblock=1A03080141"_block; |
| 54 | a.wireDecode(wire2); |
| 55 | BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Link); |
| 56 | BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 26290_ms); |
| 57 | BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component("A")); |
| 58 | BOOST_CHECK_EQUAL(a.getFinalBlockId(), name::Component("A")); |
| 59 | BOOST_CHECK_NE(a, b); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 60 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 61 | b.setType(a.getType()); |
| 62 | b.setFreshnessPeriod(a.getFreshnessPeriod()); |
| 63 | b.setFinalBlockId(a.getFinalBlockId()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 64 | BOOST_CHECK_EQUAL(b.wireEncode(), wire2); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(a, b); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 66 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 67 | // FinalBlockId is typed name component |
| 68 | Block wire3 = "1405 finalblock=1A03DD0141"_block; |
| 69 | a.wireDecode(wire3); |
| 70 | BOOST_CHECK_EQUAL(a.getType(), tlv::ContentType_Blob); |
| 71 | BOOST_CHECK_EQUAL(a.getFreshnessPeriod(), 0_ms); |
| 72 | BOOST_CHECK_EQUAL(*a.getFinalBlock(), name::Component::fromEscapedString("221=A")); |
| 73 | BOOST_CHECK_NE(a, b); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 74 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 75 | b.setType(a.getType()); |
| 76 | b.setFreshnessPeriod(a.getFreshnessPeriod()); |
| 77 | b.setFinalBlockId(a.getFinalBlockId()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 78 | BOOST_CHECK_EQUAL(b.wireEncode(), wire3); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(a, b); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(AppMetaInfo) |
| 83 | { |
| 84 | MetaInfo info1; |
| 85 | info1.setType(196); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 86 | info1.setFreshnessPeriod(3600_ms); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 87 | info1.setFinalBlock(name::Component("/att/final")); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 88 | |
| 89 | uint32_t ints[5] = {128, 129, 130, 131, 132}; |
| 90 | std::string ss[5] = {"h", "hello", "hello, world", "hello, world, alex", |
| 91 | "hello, world, alex, I am Xiaoke Jiang"}; |
| 92 | |
| 93 | for (int i = 0; i < 5; i++) { |
| 94 | uint32_t type = 128 + i * 10; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 95 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i])); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 96 | type += 5; |
Davide Pesavento | c2e12b4 | 2017-07-09 20:35:09 -0400 | [diff] [blame] | 97 | info1.addAppMetaInfo(makeStringBlock(type, ss[i])); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 100 | BOOST_CHECK(info1.findAppMetaInfo(252) == nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 101 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 102 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 103 | BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 105 | info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 106 | BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 107 | |
| 108 | info1.removeAppMetaInfo(252); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 109 | BOOST_CHECK(info1.findAppMetaInfo(252) != nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 110 | |
| 111 | info1.removeAppMetaInfo(252); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame^] | 112 | BOOST_CHECK(info1.findAppMetaInfo(252) == nullptr); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 113 | |
| 114 | // // These octets are obtained by the snippet below. |
| 115 | // // This check is intended to detect unexpected encoding change in the future. |
| 116 | // const Block& wire = info1.wireEncode(); |
| 117 | // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) { |
| 118 | // printf("0x%02x, ", *it); |
| 119 | // } |
| 120 | |
| 121 | const uint8_t METAINFO[] = {0x14, 0x77, 0x18, 0x01, 0xc4, 0x19, 0x02, 0x0e, 0x10, 0x1a, 0x0c, |
| 122 | 0x08, 0x0a, 0x2f, 0x61, 0x74, 0x74, 0x2f, 0x66, 0x69, 0x6e, 0x61, |
| 123 | 0x6c, 0x80, 0x01, 0x80, 0x85, 0x01, 0x68, 0x8a, 0x01, 0x81, 0x8f, |
| 124 | 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x94, 0x01, 0x82, 0x99, 0x0c, |
| 125 | 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, |
| 126 | 0x64, 0x9e, 0x01, 0x83, 0xa3, 0x12, 0x68, 0x65, 0x6c, 0x6c, 0x6f, |
| 127 | 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, 0x6c, |
| 128 | 0x65, 0x78, 0xa8, 0x01, 0x84, 0xad, 0x25, 0x68, 0x65, 0x6c, 0x6c, |
| 129 | 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2c, 0x20, 0x61, |
| 130 | 0x6c, 0x65, 0x78, 0x2c, 0x20, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x58, |
| 131 | 0x69, 0x61, 0x6f, 0x6b, 0x65, 0x20, 0x4a, 0x69, 0x61, 0x6e, 0x67}; |
| 132 | |
| 133 | BOOST_REQUIRE_EQUAL_COLLECTIONS(info1.wireEncode().begin(), info1.wireEncode().end(), |
| 134 | METAINFO, METAINFO + sizeof(METAINFO)); |
| 135 | |
| 136 | MetaInfo info2; |
| 137 | info2.wireDecode(Block(METAINFO, sizeof(METAINFO))); |
| 138 | |
| 139 | for (int i = 0; i < 5; i++) { |
| 140 | uint32_t tlvType = 128 + i * 10; |
| 141 | const Block* block = info2.findAppMetaInfo(tlvType); |
| 142 | BOOST_REQUIRE(block != 0); |
| 143 | BOOST_CHECK_EQUAL(readNonNegativeInteger(*block), ints[i]); |
| 144 | tlvType += 5; |
| 145 | |
| 146 | block = info2.findAppMetaInfo(tlvType); |
| 147 | BOOST_REQUIRE(block != 0); |
| 148 | |
| 149 | std::string s3 = std::string(reinterpret_cast<const char*>(block->value()), |
| 150 | block->value_size()); |
| 151 | BOOST_CHECK_EQUAL(s3, ss[i]); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(AppMetaInfoTypeRange) |
| 156 | { |
| 157 | MetaInfo info; |
| 158 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 159 | BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000))); |
| 160 | BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000))); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 162 | BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error); |
| 163 | BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 166 | BOOST_AUTO_TEST_SUITE_END() // TestMetaInfo |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 167 | |
| 168 | } // namespace tests |
| 169 | } // namespace ndn |