blob: 13adf8c6e43a4e6b20fd9503415b01de9e4f75e4 [file] [log] [blame]
Suyong Won57462ca2020-05-05 22:20:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Zhiyi Zhang74c61142020-10-07 21:00:49 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Suyong Won57462ca2020-05-05 22:20:09 -07004 *
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
21#include "ca-module.hpp"
Zhiyi Zhangdbd9d432020-10-07 15:56:27 -070022#include "identity-challenge/challenge-pin.hpp"
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070023#include "protocol-detail/info.hpp"
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070024#include "requester.hpp"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070025#include "test-common.hpp"
Suyong Won57462ca2020-05-05 22:20:09 -070026
27namespace ndn {
28namespace ndncert {
29namespace tests {
30
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070031BOOST_FIXTURE_TEST_SUITE(TestForBenchmark, IdentityManagementTimeFixture)
Suyong Won57462ca2020-05-05 22:20:09 -070032
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070033BOOST_AUTO_TEST_CASE(PacketSize0)
Suyong Won57462ca2020-05-05 22:20:09 -070034{
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070035 auto identity = addIdentity(Name("/ndn"));
36 auto key = identity.getDefaultKey();
37 auto cert = key.getDefaultCertificate();
38
Zhiyi Zhang22998612020-09-25 14:43:23 -070039 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070040 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhang696cd042020-10-07 21:27:36 -070041 auto profileData = ca.getCaProfileData();
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070042 advanceClocks(time::milliseconds(20), 60);
43
44 Interest interest = MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO"));
45 std::cout << "CA Config discovery Interest Size: " << interest.wireEncode().size() << std::endl;
46 shared_ptr<Interest> infoInterest = nullptr;
47
Zhiyi Zhang696cd042020-10-07 21:27:36 -070048 face.setInterestFilter(
49 InterestFilter("/ndn/CA/INFO"),
50 [&](const auto&, const Interest& interest) {
51 BOOST_CHECK(interest.matchesData(profileData));
52 face.put(profileData);
53 },
54 nullptr, nullptr);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070055 advanceClocks(time::milliseconds(20), 60);
56
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070057 int count = 0;
58 face.onSendData.connect([&](const Data& response) {
59 if (count == 0) {
60 count++;
61 std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl;
62 auto block = response.getContent();
63 block.parse();
64 Interest interest(Name(block.get(tlv::Name)));
65 interest.setCanBePrefix(true);
66 infoInterest = make_shared<Interest>(interest);
67 std::cout << "CA Config fetch Interest Size: " << infoInterest->wireEncode().size() << std::endl;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070068 }
69 else {
70 count++;
71 std::cout << "CA Config Data Size: " << response.wireEncode().size() << std::endl;
72 BOOST_CHECK(security::verifySignature(response, cert));
73 auto contentBlock = response.getContent();
74 contentBlock.parse();
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070075 auto caItem = INFO::decodeDataContent(contentBlock);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070076 BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070077 BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.size(), 1);
78 BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.front(), "full name");
Zhiyi Zhang9829da92020-09-30 16:19:34 -070079 BOOST_CHECK_EQUAL(caItem.m_cert->wireEncode(), cert.wireEncode());
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070080 BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
81 }
82 });
83 face.receive(interest);
84 advanceClocks(time::milliseconds(20), 60);
85 face.receive(*infoInterest);
86 advanceClocks(time::milliseconds(20), 60);
87
88 BOOST_CHECK_EQUAL(count, 2);
Suyong Won57462ca2020-05-05 22:20:09 -070089}
90
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070091BOOST_AUTO_TEST_CASE(PacketSize1)
92{
93 auto identity = addIdentity(Name("/ndn"));
94 auto key = identity.getDefaultKey();
95 auto cert = key.getDefaultCertificate();
Suyong Won57462ca2020-05-05 22:20:09 -070096
Zhiyi Zhang22998612020-09-25 14:43:23 -070097 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070098 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070099 advanceClocks(time::milliseconds(20), 60);
100
101 // generate NEW Interest
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700102 CaProfile item;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700103 item.m_caPrefix = Name("/ndn");
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700104 item.m_cert = std::make_shared<security::v2::Certificate>(cert);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700105 RequesterState state(m_keyChain, item, RequestType::NEW);
106 auto newInterest = Requester::genNewInterest(state, Name("/ndn/alice"),
107 time::system_clock::now(),
108 time::system_clock::now() + time::days(1));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700109
110 std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl;
111
112 // generate CHALLENGE Interest
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700113 shared_ptr<Interest> challengeInterest = nullptr;
114 shared_ptr<Interest> challengeInterest2 = nullptr;
115 shared_ptr<Interest> challengeInterest3 = nullptr;
116
117 int count = 0;
118 face.onSendData.connect([&](const Data& response) {
119 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
120 std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700121 auto challengeList = Requester::onNewRenewRevokeResponse(state, response);
122 auto paramList = Requester::selectOrContinueChallenge(state, "pin");
123 challengeInterest = Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700124 }
125 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
126 count++;
127 BOOST_CHECK(security::verifySignature(response, cert));
128
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700129 Requester::onChallengeResponse(state, response);
130 BOOST_CHECK(state.m_status == Status::CHALLENGE);
131 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::NEED_CODE);
132 auto paramList = Requester::selectOrContinueChallenge(state, "pin");
133 challengeInterest2 = Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700134 }
135 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
136 count++;
137 BOOST_CHECK(security::verifySignature(response, cert));
138
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700139 Requester::onChallengeResponse(state, response);
140 BOOST_CHECK(state.m_status == Status::CHALLENGE);
141 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::WRONG_CODE);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700142
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700143 auto paramList = Requester::selectOrContinueChallenge(state, "pin");
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700144 auto request = ca.getCertificateRequest(*challengeInterest2);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700145 auto secret = request.m_challengeState->m_secrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
Zhiyi Zhang46049832020-09-28 17:08:12 -0700146 std::get<1>(paramList[0]) = secret;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700147 challengeInterest3 = Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700148 std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl;
149 }
150 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
151 std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl;
152 count++;
153 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700154 Requester::onChallengeResponse(state, response);
155 BOOST_CHECK(state.m_status == Status::SUCCESS);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700156 }
157 });
158
159 face.receive(*newInterest);
160 advanceClocks(time::milliseconds(20), 60);
161 face.receive(*challengeInterest);
162 advanceClocks(time::milliseconds(20), 60);
163 face.receive(*challengeInterest2);
164 advanceClocks(time::milliseconds(20), 60);
165 face.receive(*challengeInterest3);
166 advanceClocks(time::milliseconds(20), 60);
167 BOOST_CHECK_EQUAL(count, 3);
168}
169
170BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
171
172} // namespace tests
173} // namespace ndncert
174} // namespace ndn