blob: 20748d9b01b25eed73abba440073c26b3dc11b0a [file] [log] [blame]
Zhiyi Zhang9829da92020-09-30 16:19:34 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Zhiyi Zhang74c61142020-10-07 21:00:49 -07003 * Copyright (c) 2017-2020, 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"
Zhiyi Zhang9829da92020-09-30 16:19:34 -070024#include "test-common.hpp"
25
26namespace ndn {
27namespace ndncert {
28namespace tests {
29
30BOOST_FIXTURE_TEST_SUITE(TestConfig, IdentityManagementFixture)
31
32BOOST_AUTO_TEST_CASE(CAConfigFile)
33{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070034 ca::CaConfig config;
Zhiyi Zhang9829da92020-09-30 16:19:34 -070035 config.load("tests/unit-tests/config-files/config-ca-1");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080036 BOOST_CHECK_EQUAL(config.caProfile.caPrefix, "/ndn");
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 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");
47 BOOST_CHECK_EQUAL(config.caProfile.caInfo, "missing max validity period, max suffix length, and probe");
48 BOOST_CHECK_EQUAL(config.caProfile.maxValidityPeriod, time::seconds(86400));
49 BOOST_CHECK(!config.caProfile.maxSuffixLength.has_value());
50 BOOST_CHECK_EQUAL(config.caProfile.probeParameterKeys.size(), 0);
51 BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.size(), 1);
52 BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.front(), "pin");
Zhiyi Zhangfde50112020-10-01 16:36:33 -070053
54 config.load("tests/unit-tests/config-files/config-ca-5");
tylerliuf2e6bb52020-12-13 13:23:05 -080055 BOOST_CHECK_EQUAL(config.redirection[0]->getName(),
Zhiyi Zhangfde50112020-10-01 16:36:33 -070056 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
tylerliuf2e6bb52020-12-13 13:23:05 -080057 BOOST_CHECK_EQUAL(config.nameAssignmentFuncs.size(), 3);
58 BOOST_CHECK_EQUAL(config.nameAssignmentFuncs[0]->m_nameFormat[0], "group");
59 BOOST_CHECK_EQUAL(config.nameAssignmentFuncs[0]->m_nameFormat[1], "email");
tylerliu40226332020-11-11 15:37:16 -080060 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 Zhang8683ec92020-10-07 18:18:35 -070064 std::vector<Name> names;
tylerliuf2e6bb52020-12-13 13:23:05 -080065 for (auto& assignment : config.nameAssignmentFuncs) {
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070066 auto results = assignment->assignName(params);
Zhiyi Zhang17064022020-10-07 18:34:44 -070067 BOOST_CHECK_EQUAL(results.size(), 1);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070068 names.insert(names.end(), results.begin(), results.end());
69 }
Zhiyi Zhang17064022020-10-07 18:34:44 -070070 BOOST_CHECK_EQUAL(names.size(), 3);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070071 BOOST_CHECK_EQUAL(names[0], Name("/irl/1@1.edu"));
72 BOOST_CHECK_EQUAL(names[1], Name("/irl/ndncert"));
Zhiyi Zhang17064022020-10-07 18:34:44 -070073 BOOST_CHECK_EQUAL(names[2].size(), 1);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070074}
75
76BOOST_AUTO_TEST_CASE(CAConfigFileWithErrors)
77{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070078 ca::CaConfig config;
Zhiyi Zhang9829da92020-09-30 16:19:34 -070079 // 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);
tylerliubfd2d6e2020-10-06 21:03:56 -070085 // unsupported name assignment
86 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-6"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070087}
88
Zhiyi Zhanga16b7582020-10-29 18:59:46 -070089BOOST_AUTO_TEST_CASE(ProfileStorageConfigFile)
Zhiyi Zhang9829da92020-09-30 16:19:34 -070090{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -070091 requester::ProfileStorage profileStorage;
92 profileStorage.load("tests/unit-tests/config-files/config-client-1");
Zhiyi Zhang84e11842020-11-19 20:03:23 -080093 BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 2);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070094
Zhiyi Zhang84e11842020-11-19 20:03:23 -080095 auto& profile1 = profileStorage.getKnownProfiles().front();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080096 BOOST_CHECK_EQUAL(profile1.caPrefix, "/ndn/edu/ucla");
97 BOOST_CHECK_EQUAL(profile1.caInfo, "ndn testbed ca");
98 BOOST_CHECK_EQUAL(profile1.maxValidityPeriod, time::seconds(864000));
99 BOOST_CHECK_EQUAL(*profile1.maxSuffixLength, 3);
100 BOOST_CHECK_EQUAL(profile1.probeParameterKeys.size(), 1);
101 BOOST_CHECK_EQUAL(profile1.probeParameterKeys.front(), "email");
102 BOOST_CHECK_EQUAL(profile1.cert->getName(),
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700103 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
104
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800105 auto& profile2 = profileStorage.getKnownProfiles().back();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800106 BOOST_CHECK_EQUAL(profile2.caPrefix, "/ndn/edu/ucla/zhiyi");
107 BOOST_CHECK_EQUAL(profile2.caInfo, "");
108 BOOST_CHECK_EQUAL(profile2.maxValidityPeriod, time::seconds(86400));
109 BOOST_CHECK(!profile2.maxSuffixLength);
110 BOOST_CHECK_EQUAL(profile2.probeParameterKeys.size(), 0);
111 BOOST_CHECK_EQUAL(profile2.cert->getName(),
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700112 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
113}
114
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700115BOOST_AUTO_TEST_CASE(ProfileStorageWithErrors)
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700116{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700117 requester::ProfileStorage profileStorage;
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700118 // nonexistent file
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700119 BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700120 // missing certificate
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700121 BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/config-client-2"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700122 // missing ca prefix
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700123 BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/config-client-3"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700124}
125
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700126BOOST_AUTO_TEST_CASE(ProfileStorageAddAndRemoveProfile)
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700127{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700128 requester::ProfileStorage profileStorage;
129 profileStorage.load("tests/unit-tests/config-files/config-client-1");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700130
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700131 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800132 item.caPrefix = Name("/test");
133 item.caInfo = "test";
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700134
tylerliu1f480be2020-11-10 13:02:53 -0800135 profileStorage.addCaProfile(item);
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800136 BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 3);
137 auto lastItem = profileStorage.getKnownProfiles().back();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800138 BOOST_CHECK_EQUAL(lastItem.caPrefix, "/test");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700139
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700140 profileStorage.removeCaProfile(Name("/test"));
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800141 BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 2);
142 lastItem = profileStorage.getKnownProfiles().back();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800143 BOOST_CHECK_EQUAL(lastItem.caPrefix, "/ndn/edu/ucla/zhiyi");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700144}
145
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700146BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
147
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700148} // namespace tests
149} // namespace ndncert
150} // namespace ndn