blob: f7a8bc53ab69ecde35f981eca004adb97f88a913 [file] [log] [blame]
Zhiyi Zhang9829da92020-09-30 16:19:34 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento0dc02012021-11-23 22:55:03 -05003 * Copyright (c) 2017-2021, 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
Zhiyi Zhang9829da92020-09-30 16:19:34 -070026namespace ndncert {
27namespace tests {
28
29BOOST_FIXTURE_TEST_SUITE(TestConfig, IdentityManagementFixture)
30
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");
36 BOOST_CHECK_EQUAL(config.caProfile.caInfo, "ndn testbed ca");
37 BOOST_CHECK_EQUAL(config.caProfile.maxValidityPeriod, time::seconds(864000));
38 BOOST_CHECK_EQUAL(*config.caProfile.maxSuffixLength, 3);
39 BOOST_CHECK_EQUAL(config.caProfile.probeParameterKeys.size(), 1);
40 BOOST_CHECK_EQUAL(config.caProfile.probeParameterKeys.front(), "full name");
41 BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.size(), 1);
42 BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.front(), "pin");
Zhiyi Zhang9829da92020-09-30 16:19:34 -070043
44 config.load("tests/unit-tests/config-files/config-ca-2");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080045 BOOST_CHECK_EQUAL(config.caProfile.caPrefix, "/ndn");
46 BOOST_CHECK_EQUAL(config.caProfile.caInfo, "missing max validity period, max suffix length, and probe");
47 BOOST_CHECK_EQUAL(config.caProfile.maxValidityPeriod, time::seconds(86400));
48 BOOST_CHECK(!config.caProfile.maxSuffixLength.has_value());
49 BOOST_CHECK_EQUAL(config.caProfile.probeParameterKeys.size(), 0);
50 BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.size(), 1);
51 BOOST_CHECK_EQUAL(config.caProfile.supportedChallenges.front(), "pin");
Zhiyi Zhangfde50112020-10-01 16:36:33 -070052
53 config.load("tests/unit-tests/config-files/config-ca-5");
tylerliuf2e6bb52020-12-13 13:23:05 -080054 BOOST_CHECK_EQUAL(config.redirection[0]->getName(),
Zhiyi Zhangfde50112020-10-01 16:36:33 -070055 "/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 -080056 BOOST_CHECK_EQUAL(config.nameAssignmentFuncs.size(), 3);
57 BOOST_CHECK_EQUAL(config.nameAssignmentFuncs[0]->m_nameFormat[0], "group");
58 BOOST_CHECK_EQUAL(config.nameAssignmentFuncs[0]->m_nameFormat[1], "email");
tylerliu40226332020-11-11 15:37:16 -080059 std::multimap<std::string, std::string> params;
60 params.emplace("email", "1@1.edu");
61 params.emplace("group", "irl");
62 params.emplace("name", "ndncert");
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070063 std::vector<Name> names;
tylerliuf2e6bb52020-12-13 13:23:05 -080064 for (auto& assignment : config.nameAssignmentFuncs) {
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070065 auto results = assignment->assignName(params);
Zhiyi Zhang17064022020-10-07 18:34:44 -070066 BOOST_CHECK_EQUAL(results.size(), 1);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070067 names.insert(names.end(), results.begin(), results.end());
68 }
Zhiyi Zhang17064022020-10-07 18:34:44 -070069 BOOST_CHECK_EQUAL(names.size(), 3);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070070 BOOST_CHECK_EQUAL(names[0], Name("/irl/1@1.edu"));
71 BOOST_CHECK_EQUAL(names[1], Name("/irl/ndncert"));
Zhiyi Zhang17064022020-10-07 18:34:44 -070072 BOOST_CHECK_EQUAL(names[2].size(), 1);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070073}
74
Davide Pesavento0dc02012021-11-23 22:55:03 -050075BOOST_AUTO_TEST_CASE(CaConfigFileWithErrors)
Zhiyi Zhang9829da92020-09-30 16:19:34 -070076{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070077 ca::CaConfig config;
Zhiyi Zhang9829da92020-09-30 16:19:34 -070078 // nonexistent file
79 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error);
80 // missing challenge
81 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-3"), std::runtime_error);
82 // unsupported challenge
83 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-4"), std::runtime_error);
tylerliubfd2d6e2020-10-06 21:03:56 -070084 // unsupported name assignment
85 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-6"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070086}
87
Zhiyi Zhanga16b7582020-10-29 18:59:46 -070088BOOST_AUTO_TEST_CASE(ProfileStorageConfigFile)
Zhiyi Zhang9829da92020-09-30 16:19:34 -070089{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -070090 requester::ProfileStorage profileStorage;
91 profileStorage.load("tests/unit-tests/config-files/config-client-1");
Zhiyi Zhang84e11842020-11-19 20:03:23 -080092 BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 2);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070093
Zhiyi Zhang84e11842020-11-19 20:03:23 -080094 auto& profile1 = profileStorage.getKnownProfiles().front();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080095 BOOST_CHECK_EQUAL(profile1.caPrefix, "/ndn/edu/ucla");
96 BOOST_CHECK_EQUAL(profile1.caInfo, "ndn testbed ca");
97 BOOST_CHECK_EQUAL(profile1.maxValidityPeriod, time::seconds(864000));
98 BOOST_CHECK_EQUAL(*profile1.maxSuffixLength, 3);
99 BOOST_CHECK_EQUAL(profile1.probeParameterKeys.size(), 1);
100 BOOST_CHECK_EQUAL(profile1.probeParameterKeys.front(), "email");
101 BOOST_CHECK_EQUAL(profile1.cert->getName(),
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700102 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
103
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800104 auto& profile2 = profileStorage.getKnownProfiles().back();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800105 BOOST_CHECK_EQUAL(profile2.caPrefix, "/ndn/edu/ucla/zhiyi");
106 BOOST_CHECK_EQUAL(profile2.caInfo, "");
107 BOOST_CHECK_EQUAL(profile2.maxValidityPeriod, time::seconds(86400));
108 BOOST_CHECK(!profile2.maxSuffixLength);
109 BOOST_CHECK_EQUAL(profile2.probeParameterKeys.size(), 0);
110 BOOST_CHECK_EQUAL(profile2.cert->getName(),
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700111 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
112}
113
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700114BOOST_AUTO_TEST_CASE(ProfileStorageWithErrors)
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700115{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700116 requester::ProfileStorage profileStorage;
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700117 // nonexistent file
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700118 BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700119 // missing certificate
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700120 BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/config-client-2"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700121 // missing ca prefix
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700122 BOOST_CHECK_THROW(profileStorage.load("tests/unit-tests/config-files/config-client-3"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700123}
124
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700125BOOST_AUTO_TEST_CASE(ProfileStorageAddAndRemoveProfile)
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700126{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700127 requester::ProfileStorage profileStorage;
128 profileStorage.load("tests/unit-tests/config-files/config-client-1");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700129
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700130 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800131 item.caPrefix = Name("/test");
132 item.caInfo = "test";
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700133
tylerliu1f480be2020-11-10 13:02:53 -0800134 profileStorage.addCaProfile(item);
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800135 BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 3);
136 auto lastItem = profileStorage.getKnownProfiles().back();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800137 BOOST_CHECK_EQUAL(lastItem.caPrefix, "/test");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700138
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700139 profileStorage.removeCaProfile(Name("/test"));
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800140 BOOST_CHECK_EQUAL(profileStorage.getKnownProfiles().size(), 2);
141 lastItem = profileStorage.getKnownProfiles().back();
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800142 BOOST_CHECK_EQUAL(lastItem.caPrefix, "/ndn/edu/ucla/zhiyi");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700143}
144
Davide Pesavento0dc02012021-11-23 22:55:03 -0500145BOOST_AUTO_TEST_SUITE_END() // TestConfig
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700146
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700147} // namespace tests
148} // namespace ndncert