blob: 00f8ba50ba8324b11a76c5f1d1708138bf56c0d2 [file] [log] [blame]
Zhiyi Zhang9829da92020-09-30 16:19:34 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Tianyuan Yu42bc63e2024-10-18 10:18:38 -07003 * Copyright (c) 2017-2024, Regents of the University of California.
Zhiyi Zhang9829da92020-09-30 16:19:34 -07004 *
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 Zhang3f20f952020-11-19 19:26:43 -080021#include "detail/ca-configuration.hpp"
22#include "detail/profile-storage.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070023#include "detail/info-encoder.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040024
Davide Pesavento829aff62022-05-15 20:30:34 -040025#include "tests/boost-test.hpp"
Zhiyi Zhang9829da92020-09-30 16:19:34 -070026
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040027namespace ndncert::tests {
Zhiyi Zhang9829da92020-09-30 16:19:34 -070028
Davide Pesavento829aff62022-05-15 20:30:34 -040029BOOST_AUTO_TEST_SUITE(TestConfig)
Zhiyi Zhang9829da92020-09-30 16:19:34 -070030
Davide Pesavento0dc02012021-11-23 22:55:03 -050031BOOST_AUTO_TEST_CASE(CaConfigFile)
Zhiyi Zhang9829da92020-09-30 16:19:34 -070032{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070033 ca::CaConfig config;
Zhiyi Zhang9829da92020-09-30 16:19:34 -070034 config.load("tests/unit-tests/config-files/config-ca-1");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080035 BOOST_CHECK_EQUAL(config.caProfile.caPrefix, "/ndn");
Tianyuan Yu42bc63e2024-10-18 10:18:38 -070036 BOOST_CHECK_EQUAL(config.caProfile.forwardingHint, "/repo");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080037 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 Zhang9829da92020-09-30 16:19:34 -070044
45 config.load("tests/unit-tests/config-files/config-ca-2");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080046 BOOST_CHECK_EQUAL(config.caProfile.caPrefix, "/ndn");
Tianyuan Yu42bc63e2024-10-18 10:18:38 -070047 BOOST_CHECK_EQUAL(config.caProfile.forwardingHint, "/ndn/CA");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080048 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 Zhangfde50112020-10-01 16:36:33 -070054
55 config.load("tests/unit-tests/config-files/config-ca-5");
Tianyuan Yu13aac732022-03-03 20:59:54 -080056 BOOST_CHECK_EQUAL(config.redirection[0].first->getName(),
57 "/ndn/edu/ucla/KEY/m%08%98%C2xNZ%13/self/v=1646441513929");
tylerliuf2e6bb52020-12-13 13:23:05 -080058 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");
tylerliu40226332020-11-11 15:37:16 -080061 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 Zhang8683ec92020-10-07 18:18:35 -070065 std::vector<Name> names;
tylerliuf2e6bb52020-12-13 13:23:05 -080066 for (auto& assignment : config.nameAssignmentFuncs) {
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070067 auto results = assignment->assignName(params);
Zhiyi Zhang17064022020-10-07 18:34:44 -070068 BOOST_CHECK_EQUAL(results.size(), 1);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070069 names.insert(names.end(), results.begin(), results.end());
70 }
Zhiyi Zhang17064022020-10-07 18:34:44 -070071 BOOST_CHECK_EQUAL(names.size(), 3);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070072 BOOST_CHECK_EQUAL(names[0], Name("/irl/1@1.edu"));
73 BOOST_CHECK_EQUAL(names[1], Name("/irl/ndncert"));
Zhiyi Zhang17064022020-10-07 18:34:44 -070074 BOOST_CHECK_EQUAL(names[2].size(), 1);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070075}
76
Davide Pesavento0dc02012021-11-23 22:55:03 -050077BOOST_AUTO_TEST_CASE(CaConfigFileWithErrors)
Zhiyi Zhang9829da92020-09-30 16:19:34 -070078{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070079 ca::CaConfig config;
Zhiyi Zhang9829da92020-09-30 16:19:34 -070080 // 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);
tylerliubfd2d6e2020-10-06 21:03:56 -070086 // unsupported name assignment
87 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-6"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070088}
89
Zhiyi Zhanga16b7582020-10-29 18:59:46 -070090BOOST_AUTO_TEST_CASE(ProfileStorageConfigFile)
Zhiyi Zhang9829da92020-09-30 16:19:34 -070091{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -070092 requester::ProfileStorage profileStorage;
93 profileStorage.load("tests/unit-tests/config-files/config-client-1");
Zhiyi Zhang84e11842020-11-19 20:03:23 -080094 BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 2);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070095
Zhiyi Zhang84e11842020-11-19 20:03:23 -080096 auto& profile1 = profileStorage.getKnownProfiles().front();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080097 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 Yu13aac732022-03-03 20:59:54 -0800104 "/ndn/site1/KEY/B%B2%60F%07%88%1C2/self/v=1646441889090");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700105
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800106 auto& profile2 = profileStorage.getKnownProfiles().back();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800107 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 Yu13aac732022-03-03 20:59:54 -0800113 "/ndn/site1/KEY/B%B2%60F%07%88%1C2/self/v=1646441889090");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700114}
115
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700116BOOST_AUTO_TEST_CASE(ProfileStorageWithErrors)
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700117{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700118 requester::ProfileStorage profileStorage;
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700119 // nonexistent file
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700120 BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700121 // missing certificate
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700122 BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/config-client-2"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700123 // missing ca prefix
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700124 BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/config-client-3"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700125}
126
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700127BOOST_AUTO_TEST_CASE(ProfileStorageAddAndRemoveProfile)
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700128{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700129 requester::ProfileStorage profileStorage;
130 profileStorage.load("tests/unit-tests/config-files/config-client-1");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700131
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700132 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800133 item.caPrefix = Name("/test");
134 item.caInfo = "test";
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700135
tylerliu1f480be2020-11-10 13:02:53 -0800136 profileStorage.addCaProfile(item);
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800137 BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 3);
138 auto lastItem = profileStorage.getKnownProfiles().back();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800139 BOOST_CHECK_EQUAL(lastItem.caPrefix, "/test");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700140
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700141 profileStorage.removeCaProfile(Name("/test"));
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800142 BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 2);
143 lastItem = profileStorage.getKnownProfiles().back();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800144 BOOST_CHECK_EQUAL(lastItem.caPrefix, "/ndn/edu/ucla/zhiyi");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700145}
146
Davide Pesavento0dc02012021-11-23 22:55:03 -0500147BOOST_AUTO_TEST_SUITE_END() // TestConfig
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700148
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400149} // namespace ndncert::tests