blob: c2f84e8905bc951e7b577c684b07a85d76521232 [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");
36 BOOST_CHECK_EQUAL(config.m_caItem.m_caPrefix, "/ndn");
37 BOOST_CHECK_EQUAL(config.m_caItem.m_caInfo, "ndn testbed ca");
38 BOOST_CHECK_EQUAL(config.m_caItem.m_maxValidityPeriod, time::seconds(864000));
39 BOOST_CHECK_EQUAL(*config.m_caItem.m_maxSuffixLength, 3);
40 BOOST_CHECK_EQUAL(config.m_caItem.m_probeParameterKeys.size(), 1);
41 BOOST_CHECK_EQUAL(config.m_caItem.m_probeParameterKeys.front(), "full name");
42 BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.size(), 1);
43 BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.front(), "pin");
44
45 config.load("tests/unit-tests/config-files/config-ca-2");
46 BOOST_CHECK_EQUAL(config.m_caItem.m_caPrefix, "/ndn");
47 BOOST_CHECK_EQUAL(config.m_caItem.m_caInfo, "missing max validity period, max suffix length, and probe");
48 BOOST_CHECK_EQUAL(config.m_caItem.m_maxValidityPeriod, time::seconds(86400));
Zhiyi Zhang997669a2020-10-28 21:15:40 -070049 BOOST_CHECK(!config.m_caItem.m_maxSuffixLength.has_value());
Zhiyi Zhang9829da92020-09-30 16:19:34 -070050 BOOST_CHECK_EQUAL(config.m_caItem.m_probeParameterKeys.size(), 0);
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070051 BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.size(), 2);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070052 BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.front(), "pin");
53 BOOST_CHECK_EQUAL(config.m_caItem.m_supportedChallenges.back(), "email");
Zhiyi Zhangfde50112020-10-01 16:36:33 -070054
55 config.load("tests/unit-tests/config-files/config-ca-5");
Zhiyi Zhange537dd52020-10-01 18:02:24 -070056 BOOST_CHECK_EQUAL(config.m_redirection->at(0)->getName(),
Zhiyi Zhangfde50112020-10-01 16:36:33 -070057 "/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 -070058 BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs.size(), 3);
Zhiyi Zhang74c61142020-10-07 21:00:49 -070059 BOOST_CHECK_EQUAL(config.m_nameAssignmentFuncs[0]->m_nameFormat[0], "group");
60 BOOST_CHECK_EQUAL(config.m_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;
Zhiyi Zhang74c61142020-10-07 21:00:49 -070066 for (auto& assignment : config.m_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
77BOOST_AUTO_TEST_CASE(CAConfigFileWithErrors)
78{
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");
tylerliu1f480be2020-11-10 13:02:53 -080094 BOOST_CHECK_EQUAL(profileStorage.getCaItems().size(), 2);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070095
tylerliu1f480be2020-11-10 13:02:53 -080096 auto& profile1 = profileStorage.getCaItems().front();
Zhiyi Zhanga16b7582020-10-29 18:59:46 -070097 BOOST_CHECK_EQUAL(profile1.m_caPrefix, "/ndn/edu/ucla");
98 BOOST_CHECK_EQUAL(profile1.m_caInfo, "ndn testbed ca");
99 BOOST_CHECK_EQUAL(profile1.m_maxValidityPeriod, time::seconds(864000));
100 BOOST_CHECK_EQUAL(*profile1.m_maxSuffixLength, 3);
101 BOOST_CHECK_EQUAL(profile1.m_probeParameterKeys.size(), 1);
102 BOOST_CHECK_EQUAL(profile1.m_probeParameterKeys.front(), "email");
103 BOOST_CHECK_EQUAL(profile1.m_cert->getName(),
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700104 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
105
tylerliu1f480be2020-11-10 13:02:53 -0800106 auto& profile2 = profileStorage.getCaItems().back();
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700107 BOOST_CHECK_EQUAL(profile2.m_caPrefix, "/ndn/edu/ucla/zhiyi");
108 BOOST_CHECK_EQUAL(profile2.m_caInfo, "");
109 BOOST_CHECK_EQUAL(profile2.m_maxValidityPeriod, time::seconds(86400));
110 BOOST_CHECK(!profile2.m_maxSuffixLength);
111 BOOST_CHECK_EQUAL(profile2.m_probeParameterKeys.size(), 0);
112 BOOST_CHECK_EQUAL(profile2.m_cert->getName(),
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700113 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
114}
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 Zhang9829da92020-09-30 16:19:34 -0700133 item.m_caPrefix = Name("/test");
134 item.m_caInfo = "test";
135
tylerliu1f480be2020-11-10 13:02:53 -0800136 profileStorage.addCaProfile(item);
137 BOOST_CHECK_EQUAL(profileStorage.getCaItems().size(), 3);
138 auto lastItem = profileStorage.getCaItems().back();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700139 BOOST_CHECK_EQUAL(lastItem.m_caPrefix, "/test");
140
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700141 profileStorage.removeCaProfile(Name("/test"));
tylerliu1f480be2020-11-10 13:02:53 -0800142 BOOST_CHECK_EQUAL(profileStorage.getCaItems().size(), 2);
143 lastItem = profileStorage.getCaItems().back();
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700144 BOOST_CHECK_EQUAL(lastItem.m_caPrefix, "/ndn/edu/ucla/zhiyi");
145}
146
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700147BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
148
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700149} // namespace tests
150} // namespace ndncert
151} // namespace ndn