blob: 03cb3dee5aa495eb7ea3371b082a29d22495d09f [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/*
Davide Pesavento842f1f72024-02-21 21:27:25 -05003 * Copyright (c) 2017-2024, 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"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040022#include "ca-module.hpp"
Zhiyi Zhang062be6d2020-10-14 17:13:43 -070023#include "detail/error-encoder.hpp"
24#include "detail/probe-encoder.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040025
Davide Pesavento829aff62022-05-15 20:30:34 -040026#include "tests/boost-test.hpp"
27#include "tests/io-key-chain-fixture.hpp"
28
Davide Pesavento842f1f72024-02-21 21:27:25 -050029#include <ndn-cxx/security/signing-helpers.hpp>
Davide Pesavento829aff62022-05-15 20:30:34 -040030#include <ndn-cxx/util/dummy-client-face.hpp>
Zhiyi Zhang23564c82017-03-01 10:22:22 -080031
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040032namespace ndncert::tests {
Zhiyi Zhang23564c82017-03-01 10:22:22 -080033
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -070034using namespace requester;
35
Davide Pesavento829aff62022-05-15 20:30:34 -040036BOOST_FIXTURE_TEST_SUITE(TestRequester, IoKeyChainFixture)
Zhiyi Zhang23564c82017-03-01 10:22:22 -080037
Suyong Won699e4692020-10-04 03:26:42 +090038BOOST_AUTO_TEST_CASE(GenProbeInterest)
39{
Davide Pesavento829aff62022-05-15 20:30:34 -040040 auto identity = m_keyChain.createIdentity(Name("/site"));
Suyong Won699e4692020-10-04 03:26:42 +090041 auto key = identity.getDefaultKey();
42 auto cert = key.getDefaultCertificate();
43
44 CaProfile ca_profile;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080045 ca_profile.probeParameterKeys.push_back("email");
46 ca_profile.probeParameterKeys.push_back("uid");
47 ca_profile.probeParameterKeys.push_back("name");
48 ca_profile.caPrefix = Name("/site");
Davide Pesavento0dc02012021-11-23 22:55:03 -050049 ca_profile.cert = std::make_shared<Certificate>(cert);
Suyong Won699e4692020-10-04 03:26:42 +090050
tylerliu40226332020-11-11 15:37:16 -080051 std::multimap<std::string, std::string> probeParams;
52 probeParams.emplace("email", "zhiyi@cs.ucla.edu");
53 probeParams.emplace("uid", "987654321");
54 probeParams.emplace("name", "Zhiyi Zhang");
tylerliu4140fe82021-01-27 15:45:44 -080055 auto firstInterest = Request::genProbeInterest(ca_profile, std::move(probeParams));
Suyong Won699e4692020-10-04 03:26:42 +090056
57 BOOST_CHECK(firstInterest->getName().at(-1).isParametersSha256Digest());
58 // ignore the last name component (ParametersSha256Digest)
Zhiyi Zhang621a3b52020-10-03 21:55:11 -070059 BOOST_CHECK_EQUAL(firstInterest->getName().getPrefix(-1), "/site/CA/PROBE");
tylerliu50d679e2020-10-14 14:08:39 -070060 BOOST_CHECK_EQUAL(readString(firstInterest->getApplicationParameters().get(tlv::ParameterValue)), "zhiyi@cs.ucla.edu");
Suyong Won699e4692020-10-04 03:26:42 +090061}
62
Davide Pesavento829aff62022-05-15 20:30:34 -040063BOOST_AUTO_TEST_CASE(OnProbeResponse)
64{
65 auto identity = m_keyChain.createIdentity(Name("/site"));
Suyong Won0ff8e9b2020-10-06 02:48:33 +090066 auto key = identity.getDefaultKey();
67 auto cert = key.getDefaultCertificate();
Suyong Won699e4692020-10-04 03:26:42 +090068
Suyong Won0ff8e9b2020-10-06 02:48:33 +090069 CaProfile ca_profile;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080070 ca_profile.probeParameterKeys.push_back("email");
71 ca_profile.probeParameterKeys.push_back("uid");
72 ca_profile.probeParameterKeys.push_back("name");
73 ca_profile.caPrefix = Name("/site");
Davide Pesavento0dc02012021-11-23 22:55:03 -050074 ca_profile.cert = std::make_shared<Certificate>(cert);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090075
76 std::vector<Name> availableNames;
Tianyuan Yu13aac732022-03-03 20:59:54 -080077 availableNames.emplace_back("/site1");
78 availableNames.emplace_back("/site2");
Suyong Won0ff8e9b2020-10-06 02:48:33 +090079
Tianyuan Yua721a4d2024-12-20 20:05:21 -080080 m_keyChain.createIdentity(Name("/ndn"));
Davide Pesavento76304d82023-08-10 23:38:06 -040081 ndn::DummyClientFace face(m_io, m_keyChain, {true, true});
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070082 ca::CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-5", "ca-storage-memory");
Suyong Won0ff8e9b2020-10-06 02:48:33 +090083
84 Data reply;
85 reply.setName(Name("/site/CA/PROBE"));
86 reply.setFreshnessPeriod(time::seconds(100));
Tianyuan Yu13aac732022-03-03 20:59:54 -080087 {
88 std::vector<Name> redirectionNames;
89 for (const auto &i : ca.m_config.redirection) redirectionNames.push_back(i.first->getFullName());
90 reply.setContent(probetlv::encodeDataContent(availableNames, 3, redirectionNames));
91 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050092 m_keyChain.sign(reply, ndn::signingByIdentity(identity));
Suyong Won0ff8e9b2020-10-06 02:48:33 +090093
tylerliub47dad72020-10-08 21:36:55 -070094 std::vector<std::pair<Name, int>> names;
95 std::vector<Name> redirects;
tylerliu4140fe82021-01-27 15:45:44 -080096 Request::onProbeResponse(reply, ca_profile, names, redirects);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090097
98 // Test names and redirects are properly stored
99 BOOST_CHECK_EQUAL(names.size(), 2);
tylerliub47dad72020-10-08 21:36:55 -0700100 BOOST_CHECK_EQUAL(names[0].first.toUri(), "/site1");
101 BOOST_CHECK_EQUAL(names[0].second, 3);
102 BOOST_CHECK_EQUAL(names[1].first.toUri(), "/site2");
103 BOOST_CHECK_EQUAL(names[1].second, 3);
Suyong Won0ff8e9b2020-10-06 02:48:33 +0900104
105 BOOST_CHECK_EQUAL(redirects.size(), 2);
Tianyuan Yu13aac732022-03-03 20:59:54 -0800106 BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirects[0].getPrefix(-1)), "/ndn/edu/ucla");
107 BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirects[1].getPrefix(-1)), "/ndn/edu/ucla/cs/irl");
Suyong Won0ff8e9b2020-10-06 02:48:33 +0900108}
Suyong Won699e4692020-10-04 03:26:42 +0900109
tylerliu36d97f52020-09-30 22:32:54 -0700110BOOST_AUTO_TEST_CASE(ErrorHandling)
111{
Davide Pesavento829aff62022-05-15 20:30:34 -0400112 auto identity = m_keyChain.createIdentity(Name("/site"));
tylerliu36d97f52020-09-30 22:32:54 -0700113 auto key = identity.getDefaultKey();
114 auto cert = key.getDefaultCertificate();
115
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700116 CaProfile item;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800117 item.caPrefix = Name("/site");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500118 item.cert = std::make_shared<Certificate>(cert);
tylerliu4140fe82021-01-27 15:45:44 -0800119 Request state(m_keyChain, item, RequestType::NEW);
tylerliu36d97f52020-09-30 22:32:54 -0700120
121 Data errorPacket;
122 errorPacket.setName(Name("/site/pretend/this/is/error/packet"));
123 errorPacket.setFreshnessPeriod(time::seconds(100));
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800124 errorPacket.setContent(errortlv::encodeDataContent(ErrorCode::INVALID_PARAMETER, "This is a test."));
Davide Pesavento0dc02012021-11-23 22:55:03 -0500125 m_keyChain.sign(errorPacket, ndn::signingByIdentity(identity));
tylerliu36d97f52020-09-30 22:32:54 -0700126
tylerliub47dad72020-10-08 21:36:55 -0700127 std::vector<std::pair<Name, int>> ids;
128 std::vector<Name> cas;
tylerliu4140fe82021-01-27 15:45:44 -0800129 BOOST_CHECK_THROW(Request::onProbeResponse(errorPacket, item, ids, cas), std::runtime_error);
130 BOOST_CHECK_THROW(state.onNewRenewRevokeResponse(errorPacket), std::runtime_error);
131 BOOST_CHECK_THROW(state.onChallengeResponse(errorPacket), std::runtime_error);
tylerliu36d97f52020-09-30 22:32:54 -0700132}
133
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700134BOOST_AUTO_TEST_SUITE_END() // TestRequester
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800135
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400136} // namespace ndncert::tests