blob: 73da390b54e734c0aef5a30edc15f265e0c83a05 [file] [log] [blame]
Zhiyi Zhang23564c82017-03-01 10:22:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento914d05f2019-07-13 16:20:19 -04002/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhang23564c82017-03-01 10:22:22 -08004 *
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
tylerliu4140fe82021-01-27 15:45:44 -080021#include "requester-request.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070022#include "detail/error-encoder.hpp"
23#include "detail/probe-encoder.hpp"
Zhiyi Zhang84e11842020-11-19 20:03:23 -080024#include "challenge/challenge-module.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070025#include "ca-module.hpp"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070026#include "test-common.hpp"
Zhiyi Zhang23564c82017-03-01 10:22:22 -080027
Zhiyi Zhang23564c82017-03-01 10:22:22 -080028namespace ndncert {
29namespace tests {
30
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -070031using namespace requester;
32
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070033BOOST_FIXTURE_TEST_SUITE(TestRequester, IdentityManagementTimeFixture)
Zhiyi Zhang23564c82017-03-01 10:22:22 -080034
Suyong Won699e4692020-10-04 03:26:42 +090035BOOST_AUTO_TEST_CASE(GenProbeInterest)
36{
37 auto identity = addIdentity(Name("/site"));
38 auto key = identity.getDefaultKey();
39 auto cert = key.getDefaultCertificate();
40
41 CaProfile ca_profile;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080042 ca_profile.probeParameterKeys.push_back("email");
43 ca_profile.probeParameterKeys.push_back("uid");
44 ca_profile.probeParameterKeys.push_back("name");
45 ca_profile.caPrefix = Name("/site");
Davide Pesavento0dc02012021-11-23 22:55:03 -050046 ca_profile.cert = std::make_shared<Certificate>(cert);
Suyong Won699e4692020-10-04 03:26:42 +090047
tylerliu40226332020-11-11 15:37:16 -080048 std::multimap<std::string, std::string> probeParams;
49 probeParams.emplace("email", "zhiyi@cs.ucla.edu");
50 probeParams.emplace("uid", "987654321");
51 probeParams.emplace("name", "Zhiyi Zhang");
tylerliu4140fe82021-01-27 15:45:44 -080052 auto firstInterest = Request::genProbeInterest(ca_profile, std::move(probeParams));
Suyong Won699e4692020-10-04 03:26:42 +090053
54 BOOST_CHECK(firstInterest->getName().at(-1).isParametersSha256Digest());
55 // ignore the last name component (ParametersSha256Digest)
Zhiyi Zhang621a3b52020-10-03 21:55:11 -070056 BOOST_CHECK_EQUAL(firstInterest->getName().getPrefix(-1), "/site/CA/PROBE");
tylerliu50d679e2020-10-14 14:08:39 -070057 BOOST_CHECK_EQUAL(readString(firstInterest->getApplicationParameters().get(tlv::ParameterValue)), "zhiyi@cs.ucla.edu");
Suyong Won699e4692020-10-04 03:26:42 +090058}
59
Suyong Won0ff8e9b2020-10-06 02:48:33 +090060BOOST_AUTO_TEST_CASE(OnProbeResponse){
61 auto identity = addIdentity(Name("/site"));
62 auto key = identity.getDefaultKey();
63 auto cert = key.getDefaultCertificate();
Suyong Won699e4692020-10-04 03:26:42 +090064
Suyong Won0ff8e9b2020-10-06 02:48:33 +090065 CaProfile ca_profile;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080066 ca_profile.probeParameterKeys.push_back("email");
67 ca_profile.probeParameterKeys.push_back("uid");
68 ca_profile.probeParameterKeys.push_back("name");
69 ca_profile.caPrefix = Name("/site");
Davide Pesavento0dc02012021-11-23 22:55:03 -050070 ca_profile.cert = std::make_shared<Certificate>(cert);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090071
72 std::vector<Name> availableNames;
Tianyuan Yu13aac732022-03-03 20:59:54 -080073 availableNames.emplace_back("/site1");
74 availableNames.emplace_back("/site2");
Suyong Won0ff8e9b2020-10-06 02:48:33 +090075
Davide Pesavento0dc02012021-11-23 22:55:03 -050076 ndn::util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070077 ca::CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-5", "ca-storage-memory");
Suyong Won0ff8e9b2020-10-06 02:48:33 +090078
79 Data reply;
80 reply.setName(Name("/site/CA/PROBE"));
81 reply.setFreshnessPeriod(time::seconds(100));
Tianyuan Yu13aac732022-03-03 20:59:54 -080082 {
83 std::vector<Name> redirectionNames;
84 for (const auto &i : ca.m_config.redirection) redirectionNames.push_back(i.first->getFullName());
85 reply.setContent(probetlv::encodeDataContent(availableNames, 3, redirectionNames));
86 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050087 m_keyChain.sign(reply, ndn::signingByIdentity(identity));
Suyong Won0ff8e9b2020-10-06 02:48:33 +090088
tylerliub47dad72020-10-08 21:36:55 -070089 std::vector<std::pair<Name, int>> names;
90 std::vector<Name> redirects;
tylerliu4140fe82021-01-27 15:45:44 -080091 Request::onProbeResponse(reply, ca_profile, names, redirects);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090092
93 // Test names and redirects are properly stored
94 BOOST_CHECK_EQUAL(names.size(), 2);
tylerliub47dad72020-10-08 21:36:55 -070095 BOOST_CHECK_EQUAL(names[0].first.toUri(), "/site1");
96 BOOST_CHECK_EQUAL(names[0].second, 3);
97 BOOST_CHECK_EQUAL(names[1].first.toUri(), "/site2");
98 BOOST_CHECK_EQUAL(names[1].second, 3);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090099
100 BOOST_CHECK_EQUAL(redirects.size(), 2);
Tianyuan Yu13aac732022-03-03 20:59:54 -0800101 BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirects[0].getPrefix(-1)), "/ndn/edu/ucla");
102 BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirects[1].getPrefix(-1)), "/ndn/edu/ucla/cs/irl");
Suyong Won0ff8e9b2020-10-06 02:48:33 +0900103}
Suyong Won699e4692020-10-04 03:26:42 +0900104
tylerliu36d97f52020-09-30 22:32:54 -0700105BOOST_AUTO_TEST_CASE(ErrorHandling)
106{
107 auto identity = addIdentity(Name("/site"));
108 auto key = identity.getDefaultKey();
109 auto cert = key.getDefaultCertificate();
110
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700111 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800112 item.caPrefix = Name("/site");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500113 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800114 Request state(m_keyChain, item, RequestType::NEW);
tylerliu36d97f52020-09-30 22:32:54 -0700115
116 Data errorPacket;
117 errorPacket.setName(Name("/site/pretend/this/is/error/packet"));
118 errorPacket.setFreshnessPeriod(time::seconds(100));
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800119 errorPacket.setContent(errortlv::encodeDataContent(ErrorCode::INVALID_PARAMETER, "This is a test."));
Davide Pesavento0dc02012021-11-23 22:55:03 -0500120 m_keyChain.sign(errorPacket, ndn::signingByIdentity(identity));
tylerliu36d97f52020-09-30 22:32:54 -0700121
tylerliub47dad72020-10-08 21:36:55 -0700122 std::vector<std::pair<Name, int>> ids;
123 std::vector<Name> cas;
tylerliu4140fe82021-01-27 15:45:44 -0800124 BOOST_CHECK_THROW(Request::onProbeResponse(errorPacket, item, ids, cas), std::runtime_error);
125 BOOST_CHECK_THROW(state.onNewRenewRevokeResponse(errorPacket), std::runtime_error);
126 BOOST_CHECK_THROW(state.onChallengeResponse(errorPacket), std::runtime_error);
tylerliu36d97f52020-09-30 22:32:54 -0700127}
128
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700129BOOST_AUTO_TEST_SUITE_END() // TestRequester
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800130
131} // namespace tests
132} // namespace ndncert