blob: 27798168b5ccefc77589a68bc0f10bc489669121 [file] [log] [blame]
Yingdi Yuea382942015-07-17 23:20:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi72c0c642018-04-20 15:41:09 +00002/*
Davide Pesaventofbea4fc2022-02-08 07:26:04 -05003 * Copyright (c) 2013-2022 Regents of the University of California.
Yingdi Yuea382942015-07-17 23:20:44 -07004 *
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
Alexander Afanasyev09236c22020-06-03 13:42:38 -040022#include "ndn-cxx/security/additional-description.hpp"
Yingdi Yuea382942015-07-17 23:20:44 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Yingdi Yuea382942015-07-17 23:20:44 -070025
26namespace ndn {
27namespace security {
28namespace tests {
29
Davide Pesaventoeee3e822016-11-26 19:19:34 +010030BOOST_AUTO_TEST_SUITE(Security)
31BOOST_AUTO_TEST_SUITE(TestAdditionalDescription)
Yingdi Yuea382942015-07-17 23:20:44 -070032
Davide Pesaventofbea4fc2022-02-08 07:26:04 -050033const uint8_t DESC[] = {
Yingdi Yuea382942015-07-17 23:20:44 -070034 0xfd, 0x01, 0x02, 0x28,
35 0xfd, 0x02, 0x00, 0x10, // DescriptionEntry
36 0xfd, 0x02, 0x01, 0x04, // DescriptionKey
37 0x6b, 0x65, 0x79, 0x31, // "key1"
38 0xfd, 0x02, 0x02, 0x04, // DescriptionValue
39 0x76, 0x61, 0x6c, 0x31, // "val1"
40 0xfd, 0x02, 0x00, 0x10, // DescriptionEntry
41 0xfd, 0x02, 0x01, 0x04, // DescriptionKey
42 0x6b, 0x65, 0x79, 0x32, // "key2"
43 0xfd, 0x02, 0x02, 0x04, // DescriptionValue
44 0x76, 0x61, 0x6c, 0x32, // "val2"
45};
46
Yingdi Yuea382942015-07-17 23:20:44 -070047BOOST_AUTO_TEST_CASE(Basic)
48{
49 AdditionalDescription aDescription;
50
51 aDescription.set("key2", "val2");
52 aDescription.set("key1", "val1");
53
54 BOOST_REQUIRE_NO_THROW(aDescription.get("key1"));
55 BOOST_REQUIRE_NO_THROW(aDescription.get("key2"));
56 BOOST_REQUIRE_THROW(aDescription.get("key3"), AdditionalDescription::Error);
57
58 BOOST_CHECK_EQUAL(aDescription.has("key1"), true);
59 BOOST_CHECK_EQUAL(aDescription.has("key2"), true);
60 BOOST_CHECK_EQUAL(aDescription.has("key3"), false);
61
62 auto val1 = aDescription.get("key1");
63 auto val2 = aDescription.get("key2");
64
65 BOOST_CHECK_EQUAL(val1, "val1");
66 BOOST_CHECK_EQUAL(val2, "val2");
67
68 auto it = aDescription.begin();
69 BOOST_CHECK_EQUAL(it->second, "val1");
70 it++;
71 BOOST_CHECK_EQUAL(it->second, "val2");
72 it++;
73 BOOST_CHECK(it == aDescription.end());
74
Davide Pesavento258d51a2022-02-27 21:26:28 -050075 BOOST_TEST(aDescription.wireEncode() == DESC, boost::test_tools::per_element());
Yingdi Yuea382942015-07-17 23:20:44 -070076
Davide Pesaventofbea4fc2022-02-08 07:26:04 -050077 AdditionalDescription aDescription2(Block{DESC});
Junxiao Shi72c0c642018-04-20 15:41:09 +000078 BOOST_CHECK_EQUAL(aDescription2, aDescription);
Yingdi Yuea382942015-07-17 23:20:44 -070079
80 AdditionalDescription aDescription3;
81 aDescription3.set("key3", "val3");
82 aDescription3.set("key2", "val2");
83
Junxiao Shi72c0c642018-04-20 15:41:09 +000084 BOOST_CHECK_NE(aDescription2, aDescription3);
Yingdi Yuea382942015-07-17 23:20:44 -070085
86 std::ostringstream os;
87 os << aDescription;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -050088 BOOST_CHECK_EQUAL(os.str(), "[(key1:val1), (key2:val2)]");
Yingdi Yuea382942015-07-17 23:20:44 -070089}
90
Davide Pesaventoeee3e822016-11-26 19:19:34 +010091BOOST_AUTO_TEST_SUITE_END() // TestAdditionalDescription
92BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yuea382942015-07-17 23:20:44 -070093
94} // namespace tests
95} // namespace security
96} // namespace ndn