blob: 8fc2fe5050f5cdd92706a84e322ce0b54a3bbf3c [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/*
swa770de007bc2020-03-24 21:26:21 -07003 * Copyright (c) 2017-2020, 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
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070021#include "requester.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
28namespace ndn {
29namespace ndncert {
30namespace tests {
31
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -070032using namespace requester;
33
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070034BOOST_FIXTURE_TEST_SUITE(TestRequester, IdentityManagementTimeFixture)
Zhiyi Zhang23564c82017-03-01 10:22:22 -080035
Suyong Won699e4692020-10-04 03:26:42 +090036/* PROBE */
37BOOST_AUTO_TEST_CASE(GenProbeInterest)
38{
39 auto identity = addIdentity(Name("/site"));
40 auto key = identity.getDefaultKey();
41 auto cert = key.getDefaultCertificate();
42
43 CaProfile ca_profile;
44 ca_profile.m_probeParameterKeys.push_back("email");
Zhiyi Zhang122fcfc2020-10-03 21:46:30 -070045 ca_profile.m_probeParameterKeys.push_back("uid");
Suyong Won699e4692020-10-04 03:26:42 +090046 ca_profile.m_probeParameterKeys.push_back("name");
47 ca_profile.m_caPrefix = Name("/site");
tylerliua7bea662020-10-08 18:51:02 -070048 ca_profile.m_cert = std::make_shared<security::Certificate>(cert);
Suyong Won699e4692020-10-04 03:26:42 +090049
tylerliu40226332020-11-11 15:37:16 -080050 std::multimap<std::string, std::string> probeParams;
51 probeParams.emplace("email", "zhiyi@cs.ucla.edu");
52 probeParams.emplace("uid", "987654321");
53 probeParams.emplace("name", "Zhiyi Zhang");
Zhiyi Zhang122fcfc2020-10-03 21:46:30 -070054 auto firstInterest = Requester::genProbeInterest(ca_profile, std::move(probeParams));
Suyong Won699e4692020-10-04 03:26:42 +090055
56 BOOST_CHECK(firstInterest->getName().at(-1).isParametersSha256Digest());
57 // ignore the last name component (ParametersSha256Digest)
Zhiyi Zhang621a3b52020-10-03 21:55:11 -070058 BOOST_CHECK_EQUAL(firstInterest->getName().getPrefix(-1), "/site/CA/PROBE");
tylerliu50d679e2020-10-14 14:08:39 -070059 BOOST_CHECK_EQUAL(readString(firstInterest->getApplicationParameters().get(tlv::ParameterValue)), "zhiyi@cs.ucla.edu");
Suyong Won699e4692020-10-04 03:26:42 +090060}
61
Suyong Won0ff8e9b2020-10-06 02:48:33 +090062BOOST_AUTO_TEST_CASE(OnProbeResponse){
63 auto identity = addIdentity(Name("/site"));
64 auto key = identity.getDefaultKey();
65 auto cert = key.getDefaultCertificate();
Suyong Won699e4692020-10-04 03:26:42 +090066
Suyong Won0ff8e9b2020-10-06 02:48:33 +090067 CaProfile ca_profile;
68 ca_profile.m_probeParameterKeys.push_back("email");
69 ca_profile.m_probeParameterKeys.push_back("uid");
70 ca_profile.m_probeParameterKeys.push_back("name");
71 ca_profile.m_caPrefix = Name("/site");
tylerliua7bea662020-10-08 18:51:02 -070072 ca_profile.m_cert = std::make_shared<security::Certificate>(cert);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090073
74 std::vector<Name> availableNames;
75 availableNames.push_back(Name("/site1"));
76 availableNames.push_back(Name("/site2"));
77
78 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070079 ca::CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-5", "ca-storage-memory");
Suyong Won0ff8e9b2020-10-06 02:48:33 +090080
81 Data reply;
82 reply.setName(Name("/site/CA/PROBE"));
83 reply.setFreshnessPeriod(time::seconds(100));
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080084 reply.setContent(probetlv::encodeDataContent(availableNames, 3, ca.m_config.m_redirection));
Suyong Won0ff8e9b2020-10-06 02:48:33 +090085 m_keyChain.sign(reply, signingByIdentity(identity));
86
tylerliub47dad72020-10-08 21:36:55 -070087 std::vector<std::pair<Name, int>> names;
88 std::vector<Name> redirects;
Suyong Won0ff8e9b2020-10-06 02:48:33 +090089 Requester::onProbeResponse(reply, ca_profile, names, redirects);
90
91 // Test names and redirects are properly stored
92 BOOST_CHECK_EQUAL(names.size(), 2);
tylerliub47dad72020-10-08 21:36:55 -070093 BOOST_CHECK_EQUAL(names[0].first.toUri(), "/site1");
94 BOOST_CHECK_EQUAL(names[0].second, 3);
95 BOOST_CHECK_EQUAL(names[1].first.toUri(), "/site2");
96 BOOST_CHECK_EQUAL(names[1].second, 3);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090097
98 BOOST_CHECK_EQUAL(redirects.size(), 2);
tylerliua7bea662020-10-08 18:51:02 -070099 BOOST_CHECK_EQUAL(security::extractIdentityFromCertName(redirects[0].getPrefix(-1)), "/ndn/site1");
100 BOOST_CHECK_EQUAL(security::extractIdentityFromCertName(redirects[1].getPrefix(-1)), "/ndn/site1");
Suyong Won0ff8e9b2020-10-06 02:48:33 +0900101}
Suyong Won699e4692020-10-04 03:26:42 +0900102
tylerliu36d97f52020-09-30 22:32:54 -0700103BOOST_AUTO_TEST_CASE(ErrorHandling)
104{
105 auto identity = addIdentity(Name("/site"));
106 auto key = identity.getDefaultKey();
107 auto cert = key.getDefaultCertificate();
108
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700109 CaProfile item;
tylerliu36d97f52020-09-30 22:32:54 -0700110 item.m_caPrefix = Name("/site");
tylerliua7bea662020-10-08 18:51:02 -0700111 item.m_cert = std::make_shared<security::Certificate>(cert);
tylerliubb630362020-11-10 11:31:35 -0800112 RequestState state(m_keyChain, item, RequestType::NEW);
tylerliu36d97f52020-09-30 22:32:54 -0700113
114 Data errorPacket;
115 errorPacket.setName(Name("/site/pretend/this/is/error/packet"));
116 errorPacket.setFreshnessPeriod(time::seconds(100));
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800117 errorPacket.setContent(errortlv::encodeDataContent(ErrorCode::INVALID_PARAMETER, "This is a test."));
tylerliu36d97f52020-09-30 22:32:54 -0700118 m_keyChain.sign(errorPacket, signingByIdentity(identity));
119
tylerliub47dad72020-10-08 21:36:55 -0700120 std::vector<std::pair<Name, int>> ids;
121 std::vector<Name> cas;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700122 BOOST_CHECK_THROW(Requester::onProbeResponse(errorPacket, item, ids, cas), std::runtime_error);
123 BOOST_CHECK_THROW(Requester::onNewRenewRevokeResponse(state, errorPacket), std::runtime_error);
124 BOOST_CHECK_THROW(Requester::onChallengeResponse(state, errorPacket), std::runtime_error);
tylerliu36d97f52020-09-30 22:32:54 -0700125}
126
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700127BOOST_AUTO_TEST_SUITE_END() // TestRequester
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800128
129} // namespace tests
130} // namespace ndncert
131} // namespace ndn