blob: 0a738d73f79faa97ad39f75abe2ddf8f42046571 [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 Zhangfc1678a2020-05-12 16:52:14 -070022#include "client-module.hpp"
23#include "challenge-module/challenge-pin.hpp"
24#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 Zhangfc1678a2020-05-12 16:52:14 -070040 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "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();
66 auto caItem = INFO::decodeClientConfigFromContent(contentBlock);
67 BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
68 BOOST_CHECK_EQUAL(caItem.m_probe, "");
69 BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode());
70 BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
71 }
72 });
73 face.receive(interest);
74 advanceClocks(time::milliseconds(20), 60);
75 face.receive(*infoInterest);
76 advanceClocks(time::milliseconds(20), 60);
77
78 BOOST_CHECK_EQUAL(count, 2);
Suyong Won57462ca2020-05-05 22:20:09 -070079}
80
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070081BOOST_AUTO_TEST_CASE(PacketSize1)
82{
83 auto identity = addIdentity(Name("/ndn"));
84 auto key = identity.getDefaultKey();
85 auto cert = key.getDefaultCertificate();
Suyong Won57462ca2020-05-05 22:20:09 -070086
Zhiyi Zhang22998612020-09-25 14:43:23 -070087 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070088 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
89 advanceClocks(time::milliseconds(20), 60);
90
91 // generate NEW Interest
92 ClientModule client(m_keyChain);
93 ClientCaItem item;
94 item.m_caPrefix = Name("/ndn");
95 item.m_anchor = cert;
96 client.getClientConf().m_caItems.push_back(item);
97 auto newInterest = client.generateNewInterest(time::system_clock::now(),
98 time::system_clock::now() + time::days(1), Name("/ndn/alice"));
99
100 std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl;
101
102 // generate CHALLENGE Interest
103 ChallengePin pinChallenge;
104 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;
tylerliu0e176c32020-09-29 11:39:46 -0700112 client.onNewRenewRevokeResponse(response);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700113 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700114 challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
115 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700116 std::move(paramList)));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700117 }
118 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
119 count++;
120 BOOST_CHECK(security::verifySignature(response, cert));
121
122 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700123 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700124 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
125
Zhiyi Zhang46049832020-09-28 17:08:12 -0700126 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700127 challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
128 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700129 std::move(paramList)));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700130 }
131 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
132 count++;
133 BOOST_CHECK(security::verifySignature(response, cert));
134
135 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700136 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700137 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
138
Zhiyi Zhang46049832020-09-28 17:08:12 -0700139 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700140 auto request = ca.getCertificateRequest(*challengeInterest2);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700141 auto secret = request.m_challengeState->m_secrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
Zhiyi Zhang46049832020-09-28 17:08:12 -0700142 std::get<1>(paramList[0]) = secret;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700143 challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
144 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700145 std::move(paramList)));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700146 std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl;
147 }
148 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
149 std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl;
150 count++;
151 BOOST_CHECK(security::verifySignature(response, cert));
152
153 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700154 BOOST_CHECK(client.m_status == Status::SUCCESS);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700155 }
156 });
157
158 face.receive(*newInterest);
159 advanceClocks(time::milliseconds(20), 60);
160 face.receive(*challengeInterest);
161 advanceClocks(time::milliseconds(20), 60);
162 face.receive(*challengeInterest2);
163 advanceClocks(time::milliseconds(20), 60);
164 face.receive(*challengeInterest3);
165 advanceClocks(time::milliseconds(20), 60);
166 BOOST_CHECK_EQUAL(count, 3);
167}
168
169BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
170
171} // namespace tests
172} // namespace ndncert
173} // namespace ndn