Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -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 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 22 | #include "security/v2/additional-description.hpp" |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
| 28 | namespace tests { |
| 29 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame^] | 30 | BOOST_AUTO_TEST_SUITE(Security) |
| 31 | BOOST_AUTO_TEST_SUITE(TestAdditionalDescription) |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame^] | 33 | const uint8_t description[] = { |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 34 | 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 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame^] | 47 | const std::string text = "((key1:val1), (key2:val2))"; |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 48 | |
| 49 | BOOST_AUTO_TEST_CASE(Basic) |
| 50 | { |
| 51 | AdditionalDescription aDescription; |
| 52 | |
| 53 | aDescription.set("key2", "val2"); |
| 54 | aDescription.set("key1", "val1"); |
| 55 | |
| 56 | BOOST_REQUIRE_NO_THROW(aDescription.get("key1")); |
| 57 | BOOST_REQUIRE_NO_THROW(aDescription.get("key2")); |
| 58 | BOOST_REQUIRE_THROW(aDescription.get("key3"), AdditionalDescription::Error); |
| 59 | |
| 60 | BOOST_CHECK_EQUAL(aDescription.has("key1"), true); |
| 61 | BOOST_CHECK_EQUAL(aDescription.has("key2"), true); |
| 62 | BOOST_CHECK_EQUAL(aDescription.has("key3"), false); |
| 63 | |
| 64 | auto val1 = aDescription.get("key1"); |
| 65 | auto val2 = aDescription.get("key2"); |
| 66 | |
| 67 | BOOST_CHECK_EQUAL(val1, "val1"); |
| 68 | BOOST_CHECK_EQUAL(val2, "val2"); |
| 69 | |
| 70 | auto it = aDescription.begin(); |
| 71 | BOOST_CHECK_EQUAL(it->second, "val1"); |
| 72 | it++; |
| 73 | BOOST_CHECK_EQUAL(it->second, "val2"); |
| 74 | it++; |
| 75 | BOOST_CHECK(it == aDescription.end()); |
| 76 | |
| 77 | BOOST_CHECK_EQUAL_COLLECTIONS(aDescription.wireEncode().wire(), |
| 78 | aDescription.wireEncode().wire() + aDescription.wireEncode().size(), |
| 79 | description, |
| 80 | description + sizeof(description)); |
| 81 | |
| 82 | BOOST_REQUIRE_NO_THROW(AdditionalDescription(Block(description, sizeof(description)))); |
| 83 | AdditionalDescription aDescription2(Block(description, sizeof(description))); |
| 84 | |
| 85 | BOOST_CHECK(aDescription2 == aDescription); |
| 86 | |
| 87 | AdditionalDescription aDescription3; |
| 88 | aDescription3.set("key3", "val3"); |
| 89 | aDescription3.set("key2", "val2"); |
| 90 | |
| 91 | BOOST_CHECK(aDescription2 != aDescription3); |
| 92 | |
| 93 | std::ostringstream os; |
| 94 | os << aDescription; |
| 95 | BOOST_CHECK_EQUAL(os.str(), text); |
| 96 | } |
| 97 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame^] | 98 | BOOST_AUTO_TEST_SUITE_END() // TestAdditionalDescription |
| 99 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 100 | |
| 101 | } // namespace tests |
| 102 | } // namespace security |
| 103 | } // namespace ndn |