Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03: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 | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03: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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/link.hpp" |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
| 25 | #include "tests/make-interest-data.hpp" |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace tests { |
| 29 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 30 | BOOST_AUTO_TEST_SUITE(TestLink) |
| 31 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 32 | const uint8_t GOOD_LINK[] = { |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 33 | 0x06, 0xda, // Data |
| 34 | 0x07, 0x14, // Name |
| 35 | 0x08, 0x05, |
| 36 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 37 | 0x08, 0x03, |
| 38 | 0x6e, 0x64, 0x6e, |
| 39 | 0x08, 0x06, |
| 40 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 41 | 0x14, 0x07, // MetaInfo |
| 42 | 0x18, 0x01, // ContentType |
| 43 | 0x01, |
| 44 | 0x19, 0x02, // FreshnessPeriod |
| 45 | 0x27, 0x10, |
| 46 | 0x15, 0x1a, // Content |
| 47 | 0x1f, 0x0c, // LinkDelegation |
| 48 | 0x1e, 0x01, // LinkPreference |
| 49 | 0x0a, |
| 50 | 0x07, 0x07, // Name |
| 51 | 0x08, 0x05, |
| 52 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 53 | 0x1f, 0x0a, // LinkDelegation |
| 54 | 0x1e, 0x01, // LinkPreference |
| 55 | 0x14, |
| 56 | 0x07, 0x05, // Name |
| 57 | 0x08, 0x03, |
| 58 | 0x6e, 0x64, 0x6e, |
| 59 | 0x16, 0x1b, // SignatureInfo |
| 60 | 0x1b, 0x01, // SignatureType |
| 61 | 0x01, |
| 62 | 0x1c, 0x16, // KeyLocator |
| 63 | 0x07, 0x14, // Name |
| 64 | 0x08, 0x04, |
| 65 | 0x74, 0x65, 0x73, 0x74, |
| 66 | 0x08, 0x03, |
| 67 | 0x6b, 0x65, 0x79, |
| 68 | 0x08, 0x07, |
| 69 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 70 | 0x17, 0x80, // SignatureValue |
| 71 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 72 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 73 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 74 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 75 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf, |
| 76 | 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, |
| 77 | 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, |
| 78 | 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, |
| 79 | 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3, |
| 80 | 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1 |
| 81 | }; |
| 82 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 83 | BOOST_AUTO_TEST_SUITE(EncodeDecode) |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 84 | |
| 85 | BOOST_AUTO_TEST_CASE(Decode) |
| 86 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 87 | Link link(Block(GOOD_LINK, sizeof(GOOD_LINK))); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(link.getName(), Name("/local/ndn/prefix")); |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(link.getDelegationList(), |
| 90 | DelegationList({{10, Name("/local")}, {20, Name("/ndn")}})); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 93 | BOOST_AUTO_TEST_CASE(DecodeBadContentType) |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 94 | { |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 95 | Data linkData(Block(GOOD_LINK, sizeof(GOOD_LINK))); |
| 96 | linkData.setContentType(tlv::ContentType_Key); |
| 97 | Block badLink = linkData.wireEncode(); |
| 98 | |
| 99 | BOOST_CHECK_THROW((Link(badLink)), Link::Error); |
| 100 | Link link; |
| 101 | BOOST_CHECK_THROW(link.wireDecode(badLink), Link::Error); |
| 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE(Encode) |
| 105 | { |
| 106 | Link link1("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}); |
| 107 | signData(link1); |
| 108 | Block wire = link1.wireEncode(); |
| 109 | |
| 110 | Link link2(wire); |
| 111 | BOOST_CHECK_EQUAL(link2.getName(), "/test"); |
| 112 | BOOST_CHECK_EQUAL(link2.getDelegationList(), |
| 113 | DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}})); |
| 114 | } |
| 115 | |
| 116 | BOOST_AUTO_TEST_SUITE_END() // EncodeDecode |
| 117 | |
| 118 | BOOST_AUTO_TEST_SUITE(Modify) |
| 119 | |
| 120 | BOOST_AUTO_TEST_CASE(SetDelegationList) |
| 121 | { |
| 122 | Link link("/test"); |
| 123 | BOOST_CHECK_EQUAL(link.getDelegationList(), DelegationList()); |
| 124 | |
| 125 | link.setDelegationList(DelegationList({{10, "/test1"}, {20, "/test2"}})); |
| 126 | BOOST_CHECK_EQUAL(link.getDelegationList(), DelegationList({{10, "/test1"}, {20, "/test2"}})); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(AddDelegation) |
| 130 | { |
| 131 | Link link1("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}); |
| 132 | BOOST_CHECK_EQUAL(link1.getDelegationList(), |
| 133 | DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}})); |
| 134 | |
| 135 | link1.addDelegation(30, "/test4"); |
| 136 | BOOST_CHECK_EQUAL(link1.getDelegationList(), |
| 137 | DelegationList({{10, "/test1"}, {20, "/test2"}, {30, "/test4"}, {100, "/test3"}})); |
| 138 | |
| 139 | link1.addDelegation(40, "/test2"); |
| 140 | BOOST_CHECK_EQUAL(link1.getDelegationList(), |
| 141 | DelegationList({{10, "/test1"}, {30, "/test4"}, {40, "/test2"}, {100, "/test3"}})); |
| 142 | |
| 143 | signData(link1); |
| 144 | Link link2(link1.wireEncode()); |
| 145 | BOOST_CHECK_EQUAL(link2.getDelegationList(), |
| 146 | DelegationList({{10, "/test1"}, {30, "/test4"}, {40, "/test2"}, {100, "/test3"}})); |
| 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_CASE(RemoveDelegation) |
| 150 | { |
| 151 | Link link1("/test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}); |
| 152 | BOOST_CHECK_EQUAL(link1.getDelegationList(), |
| 153 | DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}})); |
| 154 | |
| 155 | link1.removeDelegation("/test4"); // non-existent |
| 156 | BOOST_CHECK_EQUAL(link1.getDelegationList(), |
| 157 | DelegationList({{10, "/test1"}, {20, "/test2"}, {100, "/test3"}})); |
| 158 | |
| 159 | link1.removeDelegation("/test2"); |
| 160 | BOOST_CHECK_EQUAL(link1.getDelegationList(), |
| 161 | DelegationList({{10, "/test1"}, {100, "/test3"}})); |
| 162 | |
| 163 | signData(link1); |
| 164 | Link link2(link1.wireEncode()); |
| 165 | BOOST_CHECK_EQUAL(link2.getDelegationList(), |
| 166 | DelegationList({{10, "/test1"}, {100, "/test3"}})); |
| 167 | |
| 168 | link1.removeDelegation("/test1"); |
| 169 | link1.removeDelegation("/test3"); |
| 170 | BOOST_CHECK_EQUAL(link1.getDelegationList(), DelegationList()); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_SUITE_END() // Modify |
| 174 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 175 | BOOST_AUTO_TEST_SUITE_END() // TestLink |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 176 | |
| 177 | } // namespace tests |
| 178 | } // namespace ndn |