blob: f21b06d1508284c2635e41e5b89c9663137baab3 [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");
41 ca.setProbeHandler([&](const Block& probeInfo) {
42 return "example";
43 });
44 advanceClocks(time::milliseconds(20), 60);
45
46 Interest interest = MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO"));
47 std::cout << "CA Config discovery Interest Size: " << interest.wireEncode().size() << std::endl;
48 shared_ptr<Interest> infoInterest = nullptr;
49
50 int count = 0;
51 face.onSendData.connect([&](const Data& response) {
52 if (count == 0) {
53 count++;
54 std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl;
55 auto block = response.getContent();
56 block.parse();
57 Interest interest(Name(block.get(tlv::Name)));
58 interest.setCanBePrefix(true);
59 infoInterest = make_shared<Interest>(interest);
60 std::cout << "CA Config fetch Interest Size: " << infoInterest->wireEncode().size() << std::endl;
61
62 }
63 else {
64 count++;
65 std::cout << "CA Config Data Size: " << response.wireEncode().size() << std::endl;
66 BOOST_CHECK(security::verifySignature(response, cert));
67 auto contentBlock = response.getContent();
68 contentBlock.parse();
69 auto caItem = INFO::decodeClientConfigFromContent(contentBlock);
70 BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
71 BOOST_CHECK_EQUAL(caItem.m_probe, "");
72 BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode());
73 BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
74 }
75 });
76 face.receive(interest);
77 advanceClocks(time::milliseconds(20), 60);
78 face.receive(*infoInterest);
79 advanceClocks(time::milliseconds(20), 60);
80
81 BOOST_CHECK_EQUAL(count, 2);
Suyong Won57462ca2020-05-05 22:20:09 -070082}
83
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070084BOOST_AUTO_TEST_CASE(PacketSize1)
85{
86 auto identity = addIdentity(Name("/ndn"));
87 auto key = identity.getDefaultKey();
88 auto cert = key.getDefaultCertificate();
Suyong Won57462ca2020-05-05 22:20:09 -070089
Zhiyi Zhang22998612020-09-25 14:43:23 -070090 util::DummyClientFace face(io, m_keyChain, {true, true});
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070091 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
92 advanceClocks(time::milliseconds(20), 60);
93
94 // generate NEW Interest
95 ClientModule client(m_keyChain);
96 ClientCaItem item;
97 item.m_caPrefix = Name("/ndn");
98 item.m_anchor = cert;
99 client.getClientConf().m_caItems.push_back(item);
100 auto newInterest = client.generateNewInterest(time::system_clock::now(),
101 time::system_clock::now() + time::days(1), Name("/ndn/alice"));
102
103 std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl;
104
105 // generate CHALLENGE Interest
106 ChallengePin pinChallenge;
107 shared_ptr<Interest> challengeInterest = nullptr;
108 shared_ptr<Interest> challengeInterest2 = nullptr;
109 shared_ptr<Interest> challengeInterest3 = nullptr;
110
111 int count = 0;
112 face.onSendData.connect([&](const Data& response) {
113 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
114 std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl;
115 client.onNewResponse(response);
116 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
117 challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
118 client.m_challengeStatus,
119 paramJson));
120 }
121 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
122 count++;
123 BOOST_CHECK(security::verifySignature(response, cert));
124
125 client.onChallengeResponse(response);
126 BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
127 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
128
129 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
130 challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
131 client.m_challengeStatus,
132 paramJson));
133 }
134 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
135 count++;
136 BOOST_CHECK(security::verifySignature(response, cert));
137
138 client.onChallengeResponse(response);
139 BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
140 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
141
142 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
143 auto request = ca.getCertificateRequest(*challengeInterest2);
144 auto secret = request.m_challengeSecrets.get(ChallengePin::JSON_PIN_CODE, "");
145 for (auto& i : paramJson) {
146 if (i.first == ChallengePin::JSON_PIN_CODE)
147 i.second.put("", secret);
148 }
149 challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
150 client.m_challengeStatus,
151 paramJson));
152 std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl;
153 }
154 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
155 std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl;
156 count++;
157 BOOST_CHECK(security::verifySignature(response, cert));
158
159 client.onChallengeResponse(response);
160 BOOST_CHECK_EQUAL(client.m_status, STATUS_SUCCESS);
161 BOOST_CHECK_EQUAL(client.m_challengeStatus, CHALLENGE_STATUS_SUCCESS);
162 }
163 });
164
165 face.receive(*newInterest);
166 advanceClocks(time::milliseconds(20), 60);
167 face.receive(*challengeInterest);
168 advanceClocks(time::milliseconds(20), 60);
169 face.receive(*challengeInterest2);
170 advanceClocks(time::milliseconds(20), 60);
171 face.receive(*challengeInterest3);
172 advanceClocks(time::milliseconds(20), 60);
173 BOOST_CHECK_EQUAL(count, 3);
174}
175
176BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
177
178} // namespace tests
179} // namespace ndncert
180} // namespace ndn