blob: 28850a8ee047b6aaefcf9645e7860ed1ed5a48eb [file] [log] [blame]
Suyong Won57462ca2020-05-05 22:20:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2017-2019, Regents of the University of California.
4 *
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 Zhang1d3dcd22020-10-01 22:25:43 -070022#include "requester.hpp"
Zhiyi Zhang8bd8e5b2020-09-29 17:40:40 -070023#include "challenge-modules/challenge-pin.hpp"
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070024#include "protocol-detail/info.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);
42
43 Interest interest = MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO"));
44 std::cout << "CA Config discovery Interest Size: " << interest.wireEncode().size() << std::endl;
45 shared_ptr<Interest> infoInterest = nullptr;
46
47 int count = 0;
48 face.onSendData.connect([&](const Data& response) {
49 if (count == 0) {
50 count++;
51 std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl;
52 auto block = response.getContent();
53 block.parse();
54 Interest interest(Name(block.get(tlv::Name)));
55 interest.setCanBePrefix(true);
56 infoInterest = make_shared<Interest>(interest);
57 std::cout << "CA Config fetch Interest Size: " << infoInterest->wireEncode().size() << std::endl;
58
59 }
60 else {
61 count++;
62 std::cout << "CA Config Data Size: " << response.wireEncode().size() << std::endl;
63 BOOST_CHECK(security::verifySignature(response, cert));
64 auto contentBlock = response.getContent();
65 contentBlock.parse();
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070066 auto caItem = INFO::decodeDataContent(contentBlock);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070067 BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
Zhiyi Zhangb940aa12020-09-30 16:38:57 -070068 BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.size(), 1);
69 BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.front(), "full name");
Zhiyi Zhang9829da92020-09-30 16:19:34 -070070 BOOST_CHECK_EQUAL(caItem.m_cert->wireEncode(), cert.wireEncode());
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070071 BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
72 }
73 });
74 face.receive(interest);
75 advanceClocks(time::milliseconds(20), 60);
76 face.receive(*infoInterest);
77 advanceClocks(time::milliseconds(20), 60);
78
79 BOOST_CHECK_EQUAL(count, 2);
Suyong Won57462ca2020-05-05 22:20:09 -070080}
81
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070082BOOST_AUTO_TEST_CASE(PacketSize1)
83{
84 auto identity = addIdentity(Name("/ndn"));
85 auto key = identity.getDefaultKey();
86 auto cert = key.getDefaultCertificate();
Suyong Won57462ca2020-05-05 22:20:09 -070087
Zhiyi Zhang22998612020-09-25 14:43:23 -070088 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhang9829da92020-09-30 16:19:34 -070089 CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070090 advanceClocks(time::milliseconds(20), 60);
91
92 // generate NEW Interest
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070093 CaProfile item;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070094 item.m_caPrefix = Name("/ndn");
Zhiyi Zhang9829da92020-09-30 16:19:34 -070095 item.m_cert = std::make_shared<security::v2::Certificate>(cert);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070096 RequesterState state(m_keyChain, item, RequestType::NEW);
97 auto newInterest = Requester::genNewInterest(state, Name("/ndn/alice"),
98 time::system_clock::now(),
99 time::system_clock::now() + time::days(1));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700100
101 std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl;
102
103 // generate CHALLENGE Interest
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700104 shared_ptr<Interest> challengeInterest = nullptr;
105 shared_ptr<Interest> challengeInterest2 = nullptr;
106 shared_ptr<Interest> challengeInterest3 = nullptr;
107
108 int count = 0;
109 face.onSendData.connect([&](const Data& response) {
110 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
111 std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700112 auto challengeList = Requester::onNewRenewRevokeResponse(state, response);
113 auto paramList = Requester::selectOrContinueChallenge(state, "pin");
114 challengeInterest = Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700115 }
116 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
117 count++;
118 BOOST_CHECK(security::verifySignature(response, cert));
119
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700120 Requester::onChallengeResponse(state, response);
121 BOOST_CHECK(state.m_status == Status::CHALLENGE);
122 BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::NEED_CODE);
123 auto paramList = Requester::selectOrContinueChallenge(state, "pin");
124 challengeInterest2 = 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 == 1) {
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::WRONG_CODE);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700133
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700134 auto paramList = Requester::selectOrContinueChallenge(state, "pin");
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700135 auto request = ca.getCertificateRequest(*challengeInterest2);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700136 auto secret = request.m_challengeState->m_secrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
Zhiyi Zhang46049832020-09-28 17:08:12 -0700137 std::get<1>(paramList[0]) = secret;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700138 challengeInterest3 = Requester::genChallengeInterest(state, std::move(paramList));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700139 std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl;
140 }
141 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
142 std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl;
143 count++;
144 BOOST_CHECK(security::verifySignature(response, cert));
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700145 Requester::onChallengeResponse(state, response);
146 BOOST_CHECK(state.m_status == Status::SUCCESS);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700147 }
148 });
149
150 face.receive(*newInterest);
151 advanceClocks(time::milliseconds(20), 60);
152 face.receive(*challengeInterest);
153 advanceClocks(time::milliseconds(20), 60);
154 face.receive(*challengeInterest2);
155 advanceClocks(time::milliseconds(20), 60);
156 face.receive(*challengeInterest3);
157 advanceClocks(time::milliseconds(20), 60);
158 BOOST_CHECK_EQUAL(count, 3);
159}
160
161BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
162
163} // namespace tests
164} // namespace ndncert
165} // namespace ndn