Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 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" |
Zhiyi Zhang | 062be6d | 2020-10-14 17:13:43 -0700 | [diff] [blame] | 22 | #include "detail/info-encoder.hpp" |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 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 | { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 33 | ca::CaConfig config; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 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)); |
Zhiyi Zhang | 997669a | 2020-10-28 21:15:40 -0700 | [diff] [blame] | 48 | BOOST_CHECK(!config.m_caItem.m_maxSuffixLength.has_value()); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 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 | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs.size(), 3); |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs[0]->m_nameFormat[0], "group"); |
| 59 | BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs[0]->m_nameFormat[1], "email"); |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame^] | 60 | std::multimap<std::string, std::string> params; |
| 61 | params.emplace("email", "1@1.edu"); |
| 62 | params.emplace("group", "irl"); |
| 63 | params.emplace("name", "ndncert"); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 64 | std::vector<Name> names; |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 65 | for (auto& assignment : config.m_nameAssignmentFuncs) { |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 66 | auto results = assignment->assignName(params); |
Zhiyi Zhang | 1706402 | 2020-10-07 18:34:44 -0700 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL(results.size(), 1); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 68 | names.insert(names.end(), results.begin(), results.end()); |
| 69 | } |
Zhiyi Zhang | 1706402 | 2020-10-07 18:34:44 -0700 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(names.size(), 3); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(names[0], Name("/irl/1@1.edu")); |
| 72 | BOOST_CHECK_EQUAL(names[1], Name("/irl/ndncert")); |
Zhiyi Zhang | 1706402 | 2020-10-07 18:34:44 -0700 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(names[2].size(), 1); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | BOOST_AUTO_TEST_CASE(CAConfigFileWithErrors) |
| 77 | { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 78 | ca::CaConfig config; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 79 | // nonexistent file |
| 80 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error); |
| 81 | // missing challenge |
| 82 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-3"), std::runtime_error); |
| 83 | // unsupported challenge |
| 84 | 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] | 85 | // unsupported name assignment |
| 86 | 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] | 87 | } |
| 88 | |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(ProfileStorageConfigFile) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 90 | { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 91 | requester::ProfileStorage profileStorage; |
| 92 | profileStorage.load("tests/unit-tests/config-files/config-client-1"); |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL(profileStorage.getCaItems().size(), 2); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 94 | |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 95 | auto& profile1 = profileStorage.getCaItems().front(); |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(profile1.m_caPrefix, "/ndn/edu/ucla"); |
| 97 | BOOST_CHECK_EQUAL(profile1.m_caInfo, "ndn testbed ca"); |
| 98 | BOOST_CHECK_EQUAL(profile1.m_maxValidityPeriod, time::seconds(864000)); |
| 99 | BOOST_CHECK_EQUAL(*profile1.m_maxSuffixLength, 3); |
| 100 | BOOST_CHECK_EQUAL(profile1.m_probeParameterKeys.size(), 1); |
| 101 | BOOST_CHECK_EQUAL(profile1.m_probeParameterKeys.front(), "email"); |
| 102 | BOOST_CHECK_EQUAL(profile1.m_cert->getName(), |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 103 | "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5"); |
| 104 | |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 105 | auto& profile2 = profileStorage.getCaItems().back(); |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(profile2.m_caPrefix, "/ndn/edu/ucla/zhiyi"); |
| 107 | BOOST_CHECK_EQUAL(profile2.m_caInfo, ""); |
| 108 | BOOST_CHECK_EQUAL(profile2.m_maxValidityPeriod, time::seconds(86400)); |
| 109 | BOOST_CHECK(!profile2.m_maxSuffixLength); |
| 110 | BOOST_CHECK_EQUAL(profile2.m_probeParameterKeys.size(), 0); |
| 111 | BOOST_CHECK_EQUAL(profile2.m_cert->getName(), |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 112 | "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5"); |
| 113 | } |
| 114 | |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 115 | BOOST_AUTO_TEST_CASE(ProfileStorageWithErrors) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 116 | { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 117 | requester::ProfileStorage profileStorage; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 118 | // nonexistent file |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 119 | BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 120 | // missing certificate |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 121 | BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/config-client-2"), std::runtime_error); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 122 | // missing ca prefix |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 123 | BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/config-client-3"), std::runtime_error); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE(ProfileStorageAddAndRemoveProfile) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 127 | { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 128 | requester::ProfileStorage profileStorage; |
| 129 | profileStorage.load("tests/unit-tests/config-files/config-client-1"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 130 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 131 | CaProfile item; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 132 | item.m_caPrefix = Name("/test"); |
| 133 | item.m_caInfo = "test"; |
| 134 | |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 135 | profileStorage.addCaProfile(item); |
| 136 | BOOST_CHECK_EQUAL(profileStorage.getCaItems().size(), 3); |
| 137 | auto lastItem = profileStorage.getCaItems().back(); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL(lastItem.m_caPrefix, "/test"); |
| 139 | |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 140 | profileStorage.removeCaProfile(Name("/test")); |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 141 | BOOST_CHECK_EQUAL(profileStorage.getCaItems().size(), 2); |
| 142 | lastItem = profileStorage.getCaItems().back(); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(lastItem.m_caPrefix, "/ndn/edu/ucla/zhiyi"); |
| 144 | } |
| 145 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 146 | BOOST_AUTO_TEST_SUITE_END() // TestCaConfig |
| 147 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 148 | } // namespace tests |
| 149 | } // namespace ndncert |
| 150 | } // namespace ndn |