blob: 9fba0e82809ae0c03615b6f6281764690382ae47 [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 Zhang062be6d2020-10-14 17:13:43 -070023#include "detail/info-encoder.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 Zhang32d4b4e2020-10-28 22:10:49 -070040 ca::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"));
Zhiyi Zhangaeab4972020-10-22 22:20:40 -070045 // std::cout << "CA Config discovery Interest Size: " << interest.wireEncode().size() << std::endl;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070046 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 Zhangaeab4972020-10-22 22:20:40 -070051 // std::cout << interest.getName() << std::endl;
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070052 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++;
Zhiyi Zhangaeab4972020-10-22 22:20:40 -070063 // std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070064 auto block = response.getContent();
65 block.parse();
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070066 infoInterest =std::make_shared<Interest>(Name(block.get(ndn::tlv::Name)).appendSegment(0));
Zhiyi Zhangcd57da82020-10-08 20:35:40 -070067 infoInterest->setCanBePrefix(false);
Zhiyi Zhangaeab4972020-10-22 22:20:40 -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++;
Zhiyi Zhangaeab4972020-10-22 22:20:40 -070072 // std::cout << "CA Config Data Size: " << response.wireEncode().size() << std::endl;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070073 BOOST_CHECK(security::verifySignature(response, cert));
74 auto contentBlock = response.getContent();
75 contentBlock.parse();
tylerliuc4cf1862020-11-10 11:49:59 -080076 auto caItem = infoEncoder::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 Zhang32d4b4e2020-10-28 22:10:49 -070099 ca::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);
tylerliubb630362020-11-10 11:31:35 -0800106 requester::RequestState state(m_keyChain, item, RequestType::NEW);
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700107 auto newInterest = requester::Requester::genNewInterest(state, Name("/ndn/alice"),
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700108 time::system_clock::now(),
109 time::system_clock::now() + time::days(1));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700110
Zhiyi Zhangaeab4972020-10-22 22:20:40 -0700111 // std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700112
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())) {
Zhiyi Zhangaeab4972020-10-22 22:20:40 -0700121 // std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl;
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700122 auto challengeList = requester::Requester::onNewRenewRevokeResponse(state, response);
123 auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin");
124 challengeInterest = requester::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 Zhang3002e6b2020-10-29 18:54:07 -0700130 requester::Requester::onChallengeResponse(state, response);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700131 BOOST_CHECK(state.m_status == Status::CHALLENGE);
132 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::NEED_CODE);
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700133 auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin");
134 challengeInterest2 = requester::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 Zhang3002e6b2020-10-29 18:54:07 -0700140 requester::Requester::onChallengeResponse(state, response);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700141 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 Zhang3002e6b2020-10-29 18:54:07 -0700144 auto paramList = requester::Requester::selectOrContinueChallenge(state, "pin");
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700145 auto request = ca.getCertificateRequest(*challengeInterest2);
Zhiyi Zhang8fdb36b2020-10-18 11:58: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 Zhang3002e6b2020-10-29 18:54:07 -0700148 challengeInterest3 = requester::Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangaeab4972020-10-22 22:20:40 -0700149 // std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700150 }
151 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
Zhiyi Zhangaeab4972020-10-22 22:20:40 -0700152 // std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700153 count++;
154 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700155 requester::Requester::onChallengeResponse(state, response);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700156 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
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700173} // namespace tests
174} // namespace ndncert
175} // namespace ndn