Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2017-2019, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert 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 General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "configuration.hpp" |
| 22 | #include "protocol-detail/info.hpp" |
| 23 | #include "test-common.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | namespace tests { |
| 28 | |
| 29 | BOOST_FIXTURE_TEST_SUITE(TestConfig, IdentityManagementFixture) |
| 30 | |
| 31 | BOOST_AUTO_TEST_CASE(CAConfigFile) |
| 32 | { |
| 33 | CaConfig config; |
| 34 | config.load("tests/unit-tests/config-files/config-ca-1"); |
| 35 | BOOST_CHECK_EQUAL(config.m_caItem.m_caPrefix, "/ndn"); |
| 36 | BOOST_CHECK_EQUAL(config.m_caItem.m_caInfo, "ndn testbed ca"); |
| 37 | BOOST_CHECK_EQUAL(config.m_caItem.m_maxValidityPeriod, time::seconds(864000)); |
| 38 | BOOST_CHECK_EQUAL(*config.m_caItem.m_maxSuffixLength, 3); |
| 39 | BOOST_CHECK_EQUAL(config.m_caItem.m_probeParameterKeys.size(), 1); |
| 40 | BOOST_CHECK_EQUAL(config.m_caItem.m_probeParameterKeys.front(), "full name"); |
| 41 | BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.size(), 1); |
| 42 | BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.front(), "pin"); |
| 43 | |
| 44 | config.load("tests/unit-tests/config-files/config-ca-2"); |
| 45 | BOOST_CHECK_EQUAL(config.m_caItem.m_caPrefix, "/ndn"); |
| 46 | BOOST_CHECK_EQUAL(config.m_caItem.m_caInfo, "missing max validity period, max suffix length, and probe"); |
| 47 | BOOST_CHECK_EQUAL(config.m_caItem.m_maxValidityPeriod, time::seconds(86400)); |
| 48 | BOOST_CHECK(!config.m_caItem.m_maxSuffixLength); |
| 49 | BOOST_CHECK_EQUAL(config.m_caItem.m_probeParameterKeys.size(), 0); |
Zhiyi Zhang | b940aa1 | 2020-09-30 16:38:57 -0700 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.size(), 2); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.front(), "pin"); |
| 52 | BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.back(), "email"); |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 53 | |
| 54 | config.load("tests/unit-tests/config-files/config-ca-5"); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(config.m_redirection->at(0)->getName(), |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 56 | "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5"); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame^] | 57 | BOOST_CHECK_EQUAL(config.m_heuristic.size(), 3); |
tylerliu | bfd2d6e | 2020-10-06 21:03:56 -0700 | [diff] [blame] | 58 | std::vector<std::tuple<std::string, std::string>> params; |
| 59 | params.emplace_back("email", "1@1.edu"); |
tylerliu | 3f63c02 | 2020-10-07 11:08:29 -0700 | [diff] [blame] | 60 | params.emplace_back("group", "irl"); |
tylerliu | bcd8348 | 2020-10-07 14:45:28 -0700 | [diff] [blame] | 61 | params.emplace_back("name", "ndncert"); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame^] | 62 | std::vector<Name> names; |
| 63 | for (auto& assignment : config.m_heuristic) { |
| 64 | auto results = assignment->assignName(params); |
| 65 | names.insert(names.end(), results.begin(), results.end()); |
| 66 | } |
| 67 | BOOST_CHECK_EQUAL(names[0], Name("/irl/1@1.edu")); |
| 68 | BOOST_CHECK_EQUAL(names[1], Name("/irl/ndncert")); |
| 69 | BOOST_CHECK_EQUAL(names[2].size(), 2); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | BOOST_AUTO_TEST_CASE(CAConfigFileWithErrors) |
| 73 | { |
| 74 | CaConfig config; |
| 75 | // nonexistent file |
| 76 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error); |
| 77 | // missing challenge |
| 78 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-3"), std::runtime_error); |
| 79 | // unsupported challenge |
| 80 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-4"), std::runtime_error); |
tylerliu | bfd2d6e | 2020-10-06 21:03:56 -0700 | [diff] [blame] | 81 | // unsupported name assignment |
| 82 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-6"), std::runtime_error); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 85 | BOOST_AUTO_TEST_CASE(RequesterCaCacheFile) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 86 | { |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 87 | RequesterCaCache config; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 88 | config.load("tests/unit-tests/config-files/config-client-1"); |
| 89 | BOOST_CHECK_EQUAL(config.m_caItems.size(), 2); |
| 90 | |
| 91 | auto& config1 = config.m_caItems.front(); |
Zhiyi Zhang | b940aa1 | 2020-09-30 16:38:57 -0700 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(config1.m_caPrefix, "/ndn/edu/ucla"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL(config1.m_caInfo, "ndn testbed ca"); |
| 94 | BOOST_CHECK_EQUAL(config1.m_maxValidityPeriod, time::seconds(864000)); |
| 95 | BOOST_CHECK_EQUAL(*config1.m_maxSuffixLength, 3); |
| 96 | BOOST_CHECK_EQUAL(config1.m_probeParameterKeys.size(), 1); |
| 97 | BOOST_CHECK_EQUAL(config1.m_probeParameterKeys.front(), "email"); |
| 98 | BOOST_CHECK_EQUAL(config1.m_cert->getName(), |
| 99 | "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5"); |
| 100 | |
Zhiyi Zhang | b940aa1 | 2020-09-30 16:38:57 -0700 | [diff] [blame] | 101 | auto& config2 = config.m_caItems.back(); |
| 102 | BOOST_CHECK_EQUAL(config2.m_caPrefix, "/ndn/edu/ucla/zhiyi"); |
| 103 | BOOST_CHECK_EQUAL(config2.m_caInfo, ""); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(config2.m_maxValidityPeriod, time::seconds(86400)); |
| 105 | BOOST_CHECK(!config2.m_maxSuffixLength); |
| 106 | BOOST_CHECK_EQUAL(config2.m_probeParameterKeys.size(), 0); |
| 107 | BOOST_CHECK_EQUAL(config2.m_cert->getName(), |
| 108 | "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5"); |
| 109 | } |
| 110 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 111 | BOOST_AUTO_TEST_CASE(RequesterCaCacheFileWithErrors) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 112 | { |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 113 | RequesterCaCache config; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 114 | // nonexistent file |
| 115 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error); |
| 116 | // missing certificate |
| 117 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-client-2"), std::runtime_error); |
| 118 | // missing ca prefix |
| 119 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-client-3"), std::runtime_error); |
| 120 | } |
| 121 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 122 | BOOST_AUTO_TEST_CASE(RequesterCaCacheFileAddAndremoveCaProfile) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 123 | { |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 124 | RequesterCaCache config; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 125 | config.load("tests/unit-tests/config-files/config-client-1"); |
| 126 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 127 | CaProfile item; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 128 | item.m_caPrefix = Name("/test"); |
| 129 | item.m_caInfo = "test"; |
| 130 | |
| 131 | config.m_caItems.push_back(item); |
| 132 | BOOST_CHECK_EQUAL(config.m_caItems.size(), 3); |
| 133 | auto lastItem = config.m_caItems.back(); |
| 134 | BOOST_CHECK_EQUAL(lastItem.m_caPrefix, "/test"); |
| 135 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 136 | config.removeCaProfile(Name("/test")); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(config.m_caItems.size(), 2); |
| 138 | lastItem = config.m_caItems.back(); |
| 139 | BOOST_CHECK_EQUAL(lastItem.m_caPrefix, "/ndn/edu/ucla/zhiyi"); |
| 140 | } |
| 141 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 142 | BOOST_AUTO_TEST_SUITE_END() // TestCaConfig |
| 143 | |
| 144 | } // namespace tests |
| 145 | } // namespace ndncert |
| 146 | } // namespace ndn |