blob: 8ac66c9736b0ba3f629b495b540a3d389e07bece [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
tylerliu36d97f52020-09-30 22:32:54 -070021#include <protocol-detail/error.hpp>
Suyong Won0ff8e9b2020-10-06 02:48:33 +090022#include <protocol-detail/probe.hpp>
23#include <ndn-cxx/util/io.hpp>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070024#include "requester.hpp"
Zhiyi Zhangdbd9d432020-10-07 15:56:27 -070025#include "identity-challenge/challenge-module.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070026#include "ca-module.hpp"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070027#include "test-common.hpp"
Zhiyi Zhang23564c82017-03-01 10:22:22 -080028
29namespace ndn {
30namespace ndncert {
31namespace tests {
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 +090035/* PROBE */
36BOOST_AUTO_TEST_CASE(GenProbeInterest)
37{
38 auto identity = addIdentity(Name("/site"));
39 auto key = identity.getDefaultKey();
40 auto cert = key.getDefaultCertificate();
41
42 CaProfile ca_profile;
43 ca_profile.m_probeParameterKeys.push_back("email");
Zhiyi Zhang122fcfc2020-10-03 21:46:30 -070044 ca_profile.m_probeParameterKeys.push_back("uid");
Suyong Won699e4692020-10-04 03:26:42 +090045 ca_profile.m_probeParameterKeys.push_back("name");
46 ca_profile.m_caPrefix = Name("/site");
47 ca_profile.m_cert = std::make_shared<security::v2::Certificate>(cert);
48
49 std::vector<std::tuple<std::string, std::string>> probeParams;
50 probeParams.push_back(std::make_tuple("email", "zhiyi@cs.ucla.edu"));
51 probeParams.push_back(std::make_tuple("uid", "987654321"));
52 probeParams.push_back(std::make_tuple("name", "Zhiyi Zhang"));
Zhiyi Zhang122fcfc2020-10-03 21:46:30 -070053 auto firstInterest = Requester::genProbeInterest(ca_profile, std::move(probeParams));
Suyong Won699e4692020-10-04 03:26:42 +090054
55 BOOST_CHECK(firstInterest->getName().at(-1).isParametersSha256Digest());
56 // ignore the last name component (ParametersSha256Digest)
Zhiyi Zhang621a3b52020-10-03 21:55:11 -070057 BOOST_CHECK_EQUAL(firstInterest->getName().getPrefix(-1), "/site/CA/PROBE");
Suyong Won699e4692020-10-04 03:26:42 +090058 BOOST_CHECK_EQUAL(readString(firstInterest->getApplicationParameters().get(tlv_parameter_value)), "zhiyi@cs.ucla.edu");
59}
60
Suyong Won0ff8e9b2020-10-06 02:48:33 +090061BOOST_AUTO_TEST_CASE(OnProbeResponse){
62 auto identity = addIdentity(Name("/site"));
63 auto key = identity.getDefaultKey();
64 auto cert = key.getDefaultCertificate();
Suyong Won699e4692020-10-04 03:26:42 +090065
Suyong Won0ff8e9b2020-10-06 02:48:33 +090066 CaProfile ca_profile;
67 ca_profile.m_probeParameterKeys.push_back("email");
68 ca_profile.m_probeParameterKeys.push_back("uid");
69 ca_profile.m_probeParameterKeys.push_back("name");
70 ca_profile.m_caPrefix = Name("/site");
71 ca_profile.m_cert = std::make_shared<security::v2::Certificate>(cert);
72
73 std::vector<Name> availableNames;
74 availableNames.push_back(Name("/site1"));
75 availableNames.push_back(Name("/site2"));
76
77 util::DummyClientFace face(io, m_keyChain, {true, true});
78 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-5", "ca-storage-memory");
79
80 Data reply;
81 reply.setName(Name("/site/CA/PROBE"));
82 reply.setFreshnessPeriod(time::seconds(100));
83 reply.setContent(PROBE::encodeDataContent(availableNames, 3, ca.m_config.m_redirection));
84 m_keyChain.sign(reply, signingByIdentity(identity));
85
86 std::vector<Name> names, redirects;
87 Requester::onProbeResponse(reply, ca_profile, names, redirects);
88
89 // Test names and redirects are properly stored
90 BOOST_CHECK_EQUAL(names.size(), 2);
91 BOOST_CHECK_EQUAL(names[0].toUri(), "/site1");
92 BOOST_CHECK_EQUAL(names[1].toUri(), "/site2");
93
94 BOOST_CHECK_EQUAL(redirects.size(), 2);
95 BOOST_CHECK_EQUAL(security::v2::extractIdentityFromCertName(redirects[0].getPrefix(-1)), "/ndn/site1");
96 BOOST_CHECK_EQUAL(security::v2::extractIdentityFromCertName(redirects[1].getPrefix(-1)), "/ndn/site1");
97}
Suyong Won699e4692020-10-04 03:26:42 +090098
tylerliu36d97f52020-09-30 22:32:54 -070099BOOST_AUTO_TEST_CASE(ErrorHandling)
100{
101 auto identity = addIdentity(Name("/site"));
102 auto key = identity.getDefaultKey();
103 auto cert = key.getDefaultCertificate();
104
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700105 CaProfile item;
tylerliu36d97f52020-09-30 22:32:54 -0700106 item.m_caPrefix = Name("/site");
107 item.m_cert = std::make_shared<security::v2::Certificate>(cert);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700108 RequesterState state(m_keyChain, item, RequestType::NEW);
tylerliu36d97f52020-09-30 22:32:54 -0700109
110 Data errorPacket;
111 errorPacket.setName(Name("/site/pretend/this/is/error/packet"));
112 errorPacket.setFreshnessPeriod(time::seconds(100));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700113 errorPacket.setContent(ErrorTLV::encodeDataContent(ErrorCode::INVALID_PARAMETER, "This is a test."));
tylerliu36d97f52020-09-30 22:32:54 -0700114 m_keyChain.sign(errorPacket, signingByIdentity(identity));
115
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700116 std::vector<Name> ids, cas;
117 BOOST_CHECK_THROW(Requester::onProbeResponse(errorPacket, item, ids, cas), std::runtime_error);
118 BOOST_CHECK_THROW(Requester::onNewRenewRevokeResponse(state, errorPacket), std::runtime_error);
119 BOOST_CHECK_THROW(Requester::onChallengeResponse(state, errorPacket), std::runtime_error);
tylerliu36d97f52020-09-30 22:32:54 -0700120}
121
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700122BOOST_AUTO_TEST_SUITE_END() // TestRequester
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800123
124} // namespace tests
125} // namespace ndncert
126} // namespace ndn