blob: 709a0bc3aef4a9f1c2ebae9cbb944a3425d0cf7c [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 Zhangfc1678a2020-05-12 16:52:14 -070041 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070042 auto profileData = ca.getCaProfileData();
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070043
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) {
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070051 std::cout << interest.getName() << std::endl;
52 if (interest.getName() == profileData.getName()) {
53 face.put(profileData);
54 }
Zhiyi Zhang696cd042020-10-07 21:27:36 -070055 },
56 nullptr, nullptr);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070057 advanceClocks(time::milliseconds(20), 60);
58
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070059 int count = 0;
60 face.onSendData.connect([&](const Data& response) {
61 if (count == 0) {
62 count++;
63 std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl;
64 auto block = response.getContent();
65 block.parse();
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070066 infoInterest = make_shared<Interest>(Name(block.get(tlv::Name)).appendSegment(0));
67 infoInterest->setCanBePrefix(false);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070068 std::cout << "CA Config fetch Interest Size: " << infoInterest->wireEncode().size() << std::endl;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070069 }
70 else {
71 count++;
72 std::cout << "CA Config Data Size: " << response.wireEncode().size() << std::endl;
73 BOOST_CHECK(security::verifySignature(response, cert));
74 auto contentBlock = response.getContent();
75 contentBlock.parse();
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070076 auto caItem = INFO::decodeDataContent(contentBlock);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070077 BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070078 BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.size(), 1);
79 BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.front(), "full name");
Zhiyi Zhang9829da92020-09-30 16:19:34 -070080 BOOST_CHECK_EQUAL(caItem.m_cert->wireEncode(), cert.wireEncode());
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070081 BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
82 }
83 });
84 face.receive(interest);
85 advanceClocks(time::milliseconds(20), 60);
86 face.receive(*infoInterest);
87 advanceClocks(time::milliseconds(20), 60);
88
89 BOOST_CHECK_EQUAL(count, 2);
Suyong Won57462ca2020-05-05 22:20:09 -070090}
91
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070092BOOST_AUTO_TEST_CASE(PacketSize1)
93{
94 auto identity = addIdentity(Name("/ndn"));
95 auto key = identity.getDefaultKey();
96 auto cert = key.getDefaultCertificate();
Suyong Won57462ca2020-05-05 22:20:09 -070097
Zhiyi Zhang22998612020-09-25 14:43:23 -070098 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070099 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700100 advanceClocks(time::milliseconds(20), 60);
101
102 // generate NEW Interest
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700103 CaProfile item;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700104 item.m_caPrefix = Name("/ndn");
tylerliua7bea662020-10-08 18:51:02 -0700105 item.m_cert = std::make_shared<security::Certificate>(cert);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700106 RequesterState state(m_keyChain, item, RequestType::NEW);
107 auto newInterest = Requester::genNewInterest(state, Name("/ndn/alice"),
108 time::system_clock::now(),
109 time::system_clock::now() + time::days(1));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700110
111 std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl;
112
113 // generate CHALLENGE Interest
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700114 shared_ptr<Interest> challengeInterest = nullptr;
115 shared_ptr<Interest> challengeInterest2 = nullptr;
116 shared_ptr<Interest> challengeInterest3 = nullptr;
117
118 int count = 0;
119 face.onSendData.connect([&](const Data& response) {
120 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
121 std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700122 auto challengeList = Requester::onNewRenewRevokeResponse(state, response);
123 auto paramList = Requester::selectOrContinueChallenge(state, "pin");
124 challengeInterest = Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700125 }
126 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
127 count++;
128 BOOST_CHECK(security::verifySignature(response, cert));
129
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700130 Requester::onChallengeResponse(state, response);
131 BOOST_CHECK(state.m_status == Status::CHALLENGE);
132 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::NEED_CODE);
133 auto paramList = Requester::selectOrContinueChallenge(state, "pin");
134 challengeInterest2 = Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700135 }
136 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
137 count++;
138 BOOST_CHECK(security::verifySignature(response, cert));
139
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700140 Requester::onChallengeResponse(state, response);
141 BOOST_CHECK(state.m_status == Status::CHALLENGE);
142 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::WRONG_CODE);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700143
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700144 auto paramList = Requester::selectOrContinueChallenge(state, "pin");
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700145 auto request = ca.getCertificateRequest(*challengeInterest2);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700146 auto secret = request.m_challengeState->m_secrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
Zhiyi Zhang46049832020-09-28 17:08:12 -0700147 std::get<1>(paramList[0]) = secret;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700148 challengeInterest3 = Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700149 std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl;
150 }
151 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
152 std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl;
153 count++;
154 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700155 Requester::onChallengeResponse(state, response);
156 BOOST_CHECK(state.m_status == Status::SUCCESS);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700157 }
158 });
159
160 face.receive(*newInterest);
161 advanceClocks(time::milliseconds(20), 60);
162 face.receive(*challengeInterest);
163 advanceClocks(time::milliseconds(20), 60);
164 face.receive(*challengeInterest2);
165 advanceClocks(time::milliseconds(20), 60);
166 face.receive(*challengeInterest3);
167 advanceClocks(time::milliseconds(20), 60);
168 BOOST_CHECK_EQUAL(count, 3);
169}
170
171BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
172
173} // namespace tests
174} // namespace ndncert
175} // namespace ndn