blob: 95b892f5c7e39f7cfc5887a35ac7a54aa2b69ded [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");
tylerliua7bea662020-10-08 18:51:02 -070047 ca_profile.m_cert = std::make_shared<security::Certificate>(cert);
Suyong Won699e4692020-10-04 03:26:42 +090048
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");
tylerliua7bea662020-10-08 18:51:02 -070071 ca_profile.m_cert = std::make_shared<security::Certificate>(cert);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090072
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
tylerliub47dad72020-10-08 21:36:55 -070086 std::vector<std::pair<Name, int>> names;
87 std::vector<Name> redirects;
Suyong Won0ff8e9b2020-10-06 02:48:33 +090088 Requester::onProbeResponse(reply, ca_profile, names, redirects);
89
90 // Test names and redirects are properly stored
91 BOOST_CHECK_EQUAL(names.size(), 2);
tylerliub47dad72020-10-08 21:36:55 -070092 BOOST_CHECK_EQUAL(names[0].first.toUri(), "/site1");
93 BOOST_CHECK_EQUAL(names[0].second, 3);
94 BOOST_CHECK_EQUAL(names[1].first.toUri(), "/site2");
95 BOOST_CHECK_EQUAL(names[1].second, 3);
Suyong Won0ff8e9b2020-10-06 02:48:33 +090096
97 BOOST_CHECK_EQUAL(redirects.size(), 2);
tylerliua7bea662020-10-08 18:51:02 -070098 BOOST_CHECK_EQUAL(security::extractIdentityFromCertName(redirects[0].getPrefix(-1)), "/ndn/site1");
99 BOOST_CHECK_EQUAL(security::extractIdentityFromCertName(redirects[1].getPrefix(-1)), "/ndn/site1");
Suyong Won0ff8e9b2020-10-06 02:48:33 +0900100}
Suyong Won699e4692020-10-04 03:26:42 +0900101
tylerliu36d97f52020-09-30 22:32:54 -0700102BOOST_AUTO_TEST_CASE(ErrorHandling)
103{
104 auto identity = addIdentity(Name("/site"));
105 auto key = identity.getDefaultKey();
106 auto cert = key.getDefaultCertificate();
107
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700108 CaProfile item;
tylerliu36d97f52020-09-30 22:32:54 -0700109 item.m_caPrefix = Name("/site");
tylerliua7bea662020-10-08 18:51:02 -0700110 item.m_cert = std::make_shared<security::Certificate>(cert);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700111 RequesterState state(m_keyChain, item, RequestType::NEW);
tylerliu36d97f52020-09-30 22:32:54 -0700112
113 Data errorPacket;
114 errorPacket.setName(Name("/site/pretend/this/is/error/packet"));
115 errorPacket.setFreshnessPeriod(time::seconds(100));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700116 errorPacket.setContent(ErrorTLV::encodeDataContent(ErrorCode::INVALID_PARAMETER, "This is a test."));
tylerliu36d97f52020-09-30 22:32:54 -0700117 m_keyChain.sign(errorPacket, signingByIdentity(identity));
118
tylerliub47dad72020-10-08 21:36:55 -0700119 std::vector<std::pair<Name, int>> ids;
120 std::vector<Name> cas;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700121 BOOST_CHECK_THROW(Requester::onProbeResponse(errorPacket, item, ids, cas), std::runtime_error);
122 BOOST_CHECK_THROW(Requester::onNewRenewRevokeResponse(state, errorPacket), std::runtime_error);
123 BOOST_CHECK_THROW(Requester::onChallengeResponse(state, errorPacket), std::runtime_error);
tylerliu36d97f52020-09-30 22:32:54 -0700124}
125
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700126BOOST_AUTO_TEST_SUITE_END() // TestRequester
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800127
128} // namespace tests
129} // namespace ndncert
130} // namespace ndn