blob: e887165ace99d7a9e0f510217cf8e3db66031119 [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
21#include "configuration.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070022#include "detail/info-encoder.hpp"
Zhiyi Zhang9829da92020-09-30 16:19:34 -070023#include "test-common.hpp"
24
25namespace ndn {
26namespace ndncert {
27namespace tests {
28
29BOOST_FIXTURE_TEST_SUITE(TestConfig, IdentityManagementFixture)
30
31BOOST_AUTO_TEST_CASE(CAConfigFile)
32{
33 CaConfig config;
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 Zhang997669a2020-10-28 21:15:40 -070048 BOOST_CHECK(!config.m_caItem.m_maxSuffixLength.has_value());
Zhiyi Zhang9829da92020-09-30 16:19:34 -070049 BOOST_CHECK_EQUAL(config.m_caItem.m_probeParameterKeys.size(), 0);
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070050 BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.size(), 2);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070051 BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.front(), "pin");
52 BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.back(), "email");
Zhiyi Zhangfde50112020-10-01 16:36:33 -070053
54 config.load("tests/unit-tests/config-files/config-ca-5");
Zhiyi Zhange537dd52020-10-01 18:02:24 -070055 BOOST_CHECK_EQUAL(config.m_redirection->at(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");
Zhiyi Zhang74c61142020-10-07 21:00:49 -070057 BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs.size(), 3);
58 BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs[0]->FACTORY_TYPE, "param");
59 BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs[1]->FACTORY_TYPE, "param");
60 BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs[2]->FACTORY_TYPE, "random");
61 BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs[0]->m_nameFormat[0], "group");
62 BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs[0]->m_nameFormat[1], "email");
tylerliubfd2d6e2020-10-06 21:03:56 -070063 std::vector<std::tuple<std::string, std::string>> params;
64 params.emplace_back("email", "1@1.edu");
tylerliu3f63c022020-10-07 11:08:29 -070065 params.emplace_back("group", "irl");
tylerliubcd83482020-10-07 14:45:28 -070066 params.emplace_back("name", "ndncert");
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070067 std::vector<Name> names;
Zhiyi Zhang74c61142020-10-07 21:00:49 -070068 for (auto& assignment : config.m_nameAssignmentFuncs) {
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070069 auto results = assignment->assignName(params);
Zhiyi Zhang17064022020-10-07 18:34:44 -070070 BOOST_CHECK_EQUAL(results.size(), 1);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070071 names.insert(names.end(), results.begin(), results.end());
72 }
Zhiyi Zhang17064022020-10-07 18:34:44 -070073 BOOST_CHECK_EQUAL(names.size(), 3);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070074 BOOST_CHECK_EQUAL(names[0], Name("/irl/1@1.edu"));
75 BOOST_CHECK_EQUAL(names[1], Name("/irl/ndncert"));
Zhiyi Zhang17064022020-10-07 18:34:44 -070076 BOOST_CHECK_EQUAL(names[2].size(), 1);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070077}
78
79BOOST_AUTO_TEST_CASE(CAConfigFileWithErrors)
80{
81 CaConfig config;
82 // nonexistent file
83 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error);
84 // missing challenge
85 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-3"), std::runtime_error);
86 // unsupported challenge
87 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-4"), std::runtime_error);
tylerliubfd2d6e2020-10-06 21:03:56 -070088 // unsupported name assignment
89 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-ca-6"), std::runtime_error);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070090}
91
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070092BOOST_AUTO_TEST_CASE(RequesterCaCacheFile)
Zhiyi Zhang9829da92020-09-30 16:19:34 -070093{
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070094 RequesterCaCache config;
Zhiyi Zhang9829da92020-09-30 16:19:34 -070095 config.load("tests/unit-tests/config-files/config-client-1");
96 BOOST_CHECK_EQUAL(config.m_caItems.size(), 2);
97
98 auto& config1 = config.m_caItems.front();
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070099 BOOST_CHECK_EQUAL(config1.m_caPrefix, "/ndn/edu/ucla");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700100 BOOST_CHECK_EQUAL(config1.m_caInfo, "ndn testbed ca");
101 BOOST_CHECK_EQUAL(config1.m_maxValidityPeriod, time::seconds(864000));
102 BOOST_CHECK_EQUAL(*config1.m_maxSuffixLength, 3);
103 BOOST_CHECK_EQUAL(config1.m_probeParameterKeys.size(), 1);
104 BOOST_CHECK_EQUAL(config1.m_probeParameterKeys.front(), "email");
105 BOOST_CHECK_EQUAL(config1.m_cert->getName(),
106 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
107
Zhiyi Zhangb940aa12020-09-30 16:38:57 -0700108 auto& config2 = config.m_caItems.back();
109 BOOST_CHECK_EQUAL(config2.m_caPrefix, "/ndn/edu/ucla/zhiyi");
110 BOOST_CHECK_EQUAL(config2.m_caInfo, "");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700111 BOOST_CHECK_EQUAL(config2.m_maxValidityPeriod, time::seconds(86400));
112 BOOST_CHECK(!config2.m_maxSuffixLength);
113 BOOST_CHECK_EQUAL(config2.m_probeParameterKeys.size(), 0);
114 BOOST_CHECK_EQUAL(config2.m_cert->getName(),
115 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
116}
117
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700118BOOST_AUTO_TEST_CASE(RequesterCaCacheFileWithErrors)
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700119{
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700120 RequesterCaCache config;
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700121 // nonexistent file
122 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/Nonexist"), std::runtime_error);
123 // missing certificate
124 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-client-2"), std::runtime_error);
125 // missing ca prefix
126 BOOST_CHECK_THROW(config.load("tests/unit-tests/config-files/config-client-3"), std::runtime_error);
127}
128
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700129BOOST_AUTO_TEST_CASE(RequesterCaCacheFileAddAndremoveCaProfile)
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700130{
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700131 RequesterCaCache config;
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700132 config.load("tests/unit-tests/config-files/config-client-1");
133
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700134 CaProfile item;
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700135 item.m_caPrefix = Name("/test");
136 item.m_caInfo = "test";
137
138 config.m_caItems.push_back(item);
139 BOOST_CHECK_EQUAL(config.m_caItems.size(), 3);
140 auto lastItem = config.m_caItems.back();
141 BOOST_CHECK_EQUAL(lastItem.m_caPrefix, "/test");
142
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700143 config.removeCaProfile(Name("/test"));
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700144 BOOST_CHECK_EQUAL(config.m_caItems.size(), 2);
145 lastItem = config.m_caItems.back();
146 BOOST_CHECK_EQUAL(lastItem.m_caPrefix, "/ndn/edu/ucla/zhiyi");
147}
148
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700149BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
150
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700151} // namespace tests
152} // namespace ndncert
153} // namespace ndn