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