Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Tianyuan Yu | 42bc63e | 2024-10-18 10:18:38 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2024, 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 | |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 21 | #include "detail/ca-configuration.hpp" |
| 22 | #include "detail/profile-storage.hpp" |
Zhiyi Zhang | 062be6d | 2020-10-14 17:13:43 -0700 | [diff] [blame] | 23 | #include "detail/info-encoder.hpp" |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 24 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 27 | namespace ndncert::tests { |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 29 | BOOST_AUTO_TEST_SUITE(TestConfig) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 31 | BOOST_AUTO_TEST_CASE(CaConfigFile) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 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"); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 35 | BOOST_CHECK_EQUAL(config.caProfile.caPrefix, "/ndn"); |
Tianyuan Yu | 42bc63e | 2024-10-18 10:18:38 -0700 | [diff] [blame] | 36 | BOOST_CHECK_EQUAL(config.caProfile.forwardingHint, "/repo"); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 37 | BOOST_CHECK_EQUAL(config.caProfile.caInfo, "ndn testbed ca"); |
| 38 | BOOST_CHECK_EQUAL(config.caProfile.maxValidityPeriod, time::seconds(864000)); |
| 39 | BOOST_CHECK_EQUAL(*config.caProfile.maxSuffixLength, 3); |
| 40 | BOOST_CHECK_EQUAL(config.caProfile.probeParameterKeys.size(), 1); |
| 41 | BOOST_CHECK_EQUAL(config.caProfile.probeParameterKeys.front(), "full name"); |
| 42 | BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.size(), 1); |
| 43 | BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.front(), "pin"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 44 | |
| 45 | config.load("tests/unit-tests/config-files/config-ca-2"); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(config.caProfile.caPrefix, "/ndn"); |
Tianyuan Yu | 42bc63e | 2024-10-18 10:18:38 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(config.caProfile.forwardingHint, "/ndn/CA"); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(config.caProfile.caInfo, "missing max validity period, max suffix length, and probe"); |
| 49 | BOOST_CHECK_EQUAL(config.caProfile.maxValidityPeriod, time::seconds(86400)); |
| 50 | BOOST_CHECK(!config.caProfile.maxSuffixLength.has_value()); |
| 51 | BOOST_CHECK_EQUAL(config.caProfile.probeParameterKeys.size(), 0); |
| 52 | BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.size(), 1); |
| 53 | BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.front(), "pin"); |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 54 | |
| 55 | config.load("tests/unit-tests/config-files/config-ca-5"); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(config.redirection[0].first->getName(), |
| 57 | "/ndn/edu/ucla/KEY/m%08%98%C2xNZ%13/self/v=1646441513929"); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(config.nameAssignmentFuncs.size(), 3); |
| 59 | BOOST_CHECK_EQUAL(config.nameAssignmentFuncs[0]->m_nameFormat[0], "group"); |
| 60 | BOOST_CHECK_EQUAL(config.nameAssignmentFuncs[0]->m_nameFormat[1], "email"); |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 61 | std::multimap<std::string, std::string> params; |
| 62 | params.emplace("email", "1@1.edu"); |
| 63 | params.emplace("group", "irl"); |
| 64 | params.emplace("name", "ndncert"); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 65 | std::vector<Name> names; |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 66 | for (auto& assignment : config.nameAssignmentFuncs) { |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 67 | auto results = assignment->assignName(params); |
Zhiyi Zhang | 1706402 | 2020-10-07 18:34:44 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(results.size(), 1); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 69 | names.insert(names.end(), results.begin(), results.end()); |
| 70 | } |
Zhiyi Zhang | 1706402 | 2020-10-07 18:34:44 -0700 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(names.size(), 3); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(names[0], Name("/irl/1@1.edu")); |
| 73 | BOOST_CHECK_EQUAL(names[1], Name("/irl/ndncert")); |
Zhiyi Zhang | 1706402 | 2020-10-07 18:34:44 -0700 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(names[2].size(), 1); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 77 | BOOST_AUTO_TEST_CASE(CaConfigFileWithErrors) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 78 | { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 79 | ca::CaConfig config; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 80 | // nonexistent file |
| 81 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error); |
| 82 | // missing challenge |
| 83 | BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-3"), std::runtime_error); |
| 84 | // unsupported challenge |
| 85 | 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] | 86 | // unsupported name assignment |
| 87 | 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] | 88 | } |
| 89 | |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 90 | BOOST_AUTO_TEST_CASE(ProfileStorageConfigFile) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 91 | { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 92 | requester::ProfileStorage profileStorage; |
| 93 | profileStorage.load("tests/unit-tests/config-files/config-client-1"); |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 2); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 95 | |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 96 | auto& profile1 = profileStorage.getKnownProfiles().front(); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(profile1.caPrefix, "/ndn/edu/ucla"); |
| 98 | BOOST_CHECK_EQUAL(profile1.caInfo, "ndn testbed ca"); |
| 99 | BOOST_CHECK_EQUAL(profile1.maxValidityPeriod, time::seconds(864000)); |
| 100 | BOOST_CHECK_EQUAL(*profile1.maxSuffixLength, 3); |
| 101 | BOOST_CHECK_EQUAL(profile1.probeParameterKeys.size(), 1); |
| 102 | BOOST_CHECK_EQUAL(profile1.probeParameterKeys.front(), "email"); |
| 103 | BOOST_CHECK_EQUAL(profile1.cert->getName(), |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 104 | "/ndn/site1/KEY/B%B2%60F%07%88%1C2/self/v=1646441889090"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 105 | |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 106 | auto& profile2 = profileStorage.getKnownProfiles().back(); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(profile2.caPrefix, "/ndn/edu/ucla/zhiyi"); |
| 108 | BOOST_CHECK_EQUAL(profile2.caInfo, ""); |
| 109 | BOOST_CHECK_EQUAL(profile2.maxValidityPeriod, time::seconds(86400)); |
| 110 | BOOST_CHECK(!profile2.maxSuffixLength); |
| 111 | BOOST_CHECK_EQUAL(profile2.probeParameterKeys.size(), 0); |
| 112 | BOOST_CHECK_EQUAL(profile2.cert->getName(), |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 113 | "/ndn/site1/KEY/B%B2%60F%07%88%1C2/self/v=1646441889090"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 116 | BOOST_AUTO_TEST_CASE(ProfileStorageWithErrors) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 117 | { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 118 | requester::ProfileStorage profileStorage; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 119 | // nonexistent file |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 120 | 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] | 121 | // missing certificate |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 122 | 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] | 123 | // missing ca prefix |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 124 | 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] | 125 | } |
| 126 | |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 127 | BOOST_AUTO_TEST_CASE(ProfileStorageAddAndRemoveProfile) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 128 | { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 129 | requester::ProfileStorage profileStorage; |
| 130 | profileStorage.load("tests/unit-tests/config-files/config-client-1"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 131 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 132 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 133 | item.caPrefix = Name("/test"); |
| 134 | item.caInfo = "test"; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 135 | |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 136 | profileStorage.addCaProfile(item); |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 3); |
| 138 | auto lastItem = profileStorage.getKnownProfiles().back(); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(lastItem.caPrefix, "/test"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 140 | |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 141 | profileStorage.removeCaProfile(Name("/test")); |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 2); |
| 143 | lastItem = profileStorage.getKnownProfiles().back(); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(lastItem.caPrefix, "/ndn/edu/ucla/zhiyi"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 147 | BOOST_AUTO_TEST_SUITE_END() // TestConfig |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 148 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 149 | } // namespace ndncert::tests |