blob: 5ea7a0360a3ebf23f9732e11b2d17adbfc927119 [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 Zhangc5d93a92020-10-14 17:07:35 -070022#include "protocol-detail/error-encoder.hpp"
23#include "protocol-detail/probe-encoder.hpp"
Zhiyi Zhangdbd9d432020-10-07 15:56:27 -070024#include "identity-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 Zhang1d3dcd22020-10-01 22:25:43 -070032BOOST_FIXTURE_TEST_SUITE(TestRequester, IdentityManagementTimeFixture)
Zhiyi Zhang23564c82017-03-01 10:22:22 -080033
Suyong Won699e4692020-10-04 03:26:42 +090034/* PROBE */
35BOOST_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;
42 ca_profile.m_probeParameterKeys.push_back("email");
Zhiyi Zhang122fcfc2020-10-03 21:46:30 -070043 ca_profile.m_probeParameterKeys.push_back("uid");
Suyong Won699e4692020-10-04 03:26:42 +090044 ca_profile.m_probeParameterKeys.push_back("name");
45 ca_profile.m_caPrefix = Name("/site");
tylerliua7bea662020-10-08 18:51:02 -070046 ca_profile.m_cert = std::make_shared<security::Certificate>(cert);
Suyong Won699e4692020-10-04 03:26:42 +090047
48 std::vector<std::tuple<std::string, std::string>> probeParams;
49 probeParams.push_back(std::make_tuple("email", "zhiyi@cs.ucla.edu"));
50 probeParams.push_back(std::make_tuple("uid", "987654321"));
51 probeParams.push_back(std::make_tuple("name", "Zhiyi Zhang"));
Zhiyi Zhang122fcfc2020-10-03 21:46:30 -070052 auto firstInterest = Requester::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;
66 ca_profile.m_probeParameterKeys.push_back("email");
67 ca_profile.m_probeParameterKeys.push_back("uid");
68 ca_profile.m_probeParameterKeys.push_back("name");
69 ca_profile.m_caPrefix = Name("/site");
tylerliua7bea662020-10-08 18:51:02 -070070 ca_profile.m_cert = std::make_shared<security::Certificate>(cert);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090071
72 std::vector<Name> availableNames;
73 availableNames.push_back(Name("/site1"));
74 availableNames.push_back(Name("/site2"));
75
76 util::DummyClientFace face(io, m_keyChain, {true, true});
77 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-5", "ca-storage-memory");
78
79 Data reply;
80 reply.setName(Name("/site/CA/PROBE"));
81 reply.setFreshnessPeriod(time::seconds(100));
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070082 reply.setContent(ProbeEncoder::encodeDataContent(availableNames, 3, ca.m_config.m_redirection));
Suyong Won0ff8e9b2020-10-06 02:48:33 +090083 m_keyChain.sign(reply, signingByIdentity(identity));
84
tylerliub47dad72020-10-08 21:36:55 -070085 std::vector<std::pair<Name, int>> names;
86 std::vector<Name> redirects;
Suyong Won0ff8e9b2020-10-06 02:48:33 +090087 Requester::onProbeResponse(reply, ca_profile, names, redirects);
88
89 // Test names and redirects are properly stored
90 BOOST_CHECK_EQUAL(names.size(), 2);
tylerliub47dad72020-10-08 21:36:55 -070091 BOOST_CHECK_EQUAL(names[0].first.toUri(), "/site1");
92 BOOST_CHECK_EQUAL(names[0].second, 3);
93 BOOST_CHECK_EQUAL(names[1].first.toUri(), "/site2");
94 BOOST_CHECK_EQUAL(names[1].second, 3);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090095
96 BOOST_CHECK_EQUAL(redirects.size(), 2);
tylerliua7bea662020-10-08 18:51:02 -070097 BOOST_CHECK_EQUAL(security::extractIdentityFromCertName(redirects[0].getPrefix(-1)), "/ndn/site1");
98 BOOST_CHECK_EQUAL(security::extractIdentityFromCertName(redirects[1].getPrefix(-1)), "/ndn/site1");
Suyong Won0ff8e9b2020-10-06 02:48:33 +090099}
Suyong Won699e4692020-10-04 03:26:42 +0900100
tylerliu36d97f52020-09-30 22:32:54 -0700101BOOST_AUTO_TEST_CASE(ErrorHandling)
102{
103 auto identity = addIdentity(Name("/site"));
104 auto key = identity.getDefaultKey();
105 auto cert = key.getDefaultCertificate();
106
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700107 CaProfile item;
tylerliu36d97f52020-09-30 22:32:54 -0700108 item.m_caPrefix = Name("/site");
tylerliua7bea662020-10-08 18:51:02 -0700109 item.m_cert = std::make_shared<security::Certificate>(cert);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700110 RequesterState state(m_keyChain, item, RequestType::NEW);
tylerliu36d97f52020-09-30 22:32:54 -0700111
112 Data errorPacket;
113 errorPacket.setName(Name("/site/pretend/this/is/error/packet"));
114 errorPacket.setFreshnessPeriod(time::seconds(100));
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700115 errorPacket.setContent(ErrorEncoder::encodeDataContent(ErrorCode::INVALID_PARAMETER, "This is a test."));
tylerliu36d97f52020-09-30 22:32:54 -0700116 m_keyChain.sign(errorPacket, signingByIdentity(identity));
117
tylerliub47dad72020-10-08 21:36:55 -0700118 std::vector<std::pair<Name, int>> ids;
119 std::vector<Name> cas;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700120 BOOST_CHECK_THROW(Requester::onProbeResponse(errorPacket, item, ids, cas), std::runtime_error);
121 BOOST_CHECK_THROW(Requester::onNewRenewRevokeResponse(state, errorPacket), std::runtime_error);
122 BOOST_CHECK_THROW(Requester::onChallengeResponse(state, errorPacket), std::runtime_error);
tylerliu36d97f52020-09-30 22:32:54 -0700123}
124
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700125BOOST_AUTO_TEST_SUITE_END() // TestRequester
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800126
127} // namespace tests
128} // namespace ndncert
129} // namespace ndn