blob: 038e97ba10d663c2ca7b9cd8b289bcd673f9fbc7 [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"
22#include "identity-management-fixture.hpp"
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070023#include "client-module.hpp"
24#include "challenge-module/challenge-pin.hpp"
25#include "protocol-detail/info.hpp"
Suyong Won57462ca2020-05-05 22:20:09 -070026
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070027#include <ndn-cxx/util/dummy-client-face.hpp>
28#include <ndn-cxx/security/signing-helpers.hpp>
29#include <ndn-cxx/security/transform/public-key.hpp>
30#include <ndn-cxx/security/verification-helpers.hpp>
31#include <ndn-cxx/metadata-object.hpp>
32#include <iostream>
Suyong Won57462ca2020-05-05 22:20:09 -070033
34namespace ndn {
35namespace ndncert {
36namespace tests {
37
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070038BOOST_FIXTURE_TEST_SUITE(TestForBenchmark, IdentityManagementTimeFixture)
Suyong Won57462ca2020-05-05 22:20:09 -070039
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070040BOOST_AUTO_TEST_CASE(PacketSize0)
Suyong Won57462ca2020-05-05 22:20:09 -070041{
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070042 auto identity = addIdentity(Name("/ndn"));
43 auto key = identity.getDefaultKey();
44 auto cert = key.getDefaultCertificate();
45
46 util::DummyClientFace face(io, {true, true});
47 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
48 ca.setProbeHandler([&](const Block& probeInfo) {
49 return "example";
50 });
51 advanceClocks(time::milliseconds(20), 60);
52
53 Interest interest = MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO"));
54 std::cout << "CA Config discovery Interest Size: " << interest.wireEncode().size() << std::endl;
55 shared_ptr<Interest> infoInterest = nullptr;
56
57 int count = 0;
58 face.onSendData.connect([&](const Data& response) {
59 if (count == 0) {
60 count++;
61 std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl;
62 auto block = response.getContent();
63 block.parse();
64 Interest interest(Name(block.get(tlv::Name)));
65 interest.setCanBePrefix(true);
66 infoInterest = make_shared<Interest>(interest);
67 std::cout << "CA Config fetch Interest Size: " << infoInterest->wireEncode().size() << std::endl;
68
69 }
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();
76 auto caItem = INFO::decodeClientConfigFromContent(contentBlock);
77 BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
78 BOOST_CHECK_EQUAL(caItem.m_probe, "");
79 BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode());
80 BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
81 }
82 });
83 face.receive(interest);
84 advanceClocks(time::milliseconds(20), 60);
85 face.receive(*infoInterest);
86 advanceClocks(time::milliseconds(20), 60);
87
88 BOOST_CHECK_EQUAL(count, 2);
Suyong Won57462ca2020-05-05 22:20:09 -070089}
90
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070091BOOST_AUTO_TEST_CASE(PacketSize1)
92{
93 auto identity = addIdentity(Name("/ndn"));
94 auto key = identity.getDefaultKey();
95 auto cert = key.getDefaultCertificate();
Suyong Won57462ca2020-05-05 22:20:09 -070096
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070097 util::DummyClientFace face(io, {true, true});
98 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
99 advanceClocks(time::milliseconds(20), 60);
100
101 // generate NEW Interest
102 ClientModule client(m_keyChain);
103 ClientCaItem item;
104 item.m_caPrefix = Name("/ndn");
105 item.m_anchor = cert;
106 client.getClientConf().m_caItems.push_back(item);
107 auto newInterest = client.generateNewInterest(time::system_clock::now(),
108 time::system_clock::now() + time::days(1), Name("/ndn/alice"));
109
110 std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl;
111
112 // generate CHALLENGE Interest
113 ChallengePin pinChallenge;
114 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;
122 client.onNewResponse(response);
123 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
124 challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
125 client.m_challengeStatus,
126 paramJson));
127 }
128 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
129 count++;
130 BOOST_CHECK(security::verifySignature(response, cert));
131
132 client.onChallengeResponse(response);
133 BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
134 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
135
136 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
137 challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
138 client.m_challengeStatus,
139 paramJson));
140 }
141 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
142 count++;
143 BOOST_CHECK(security::verifySignature(response, cert));
144
145 client.onChallengeResponse(response);
146 BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
147 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
148
149 auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
150 auto request = ca.getCertificateRequest(*challengeInterest2);
151 auto secret = request.m_challengeSecrets.get(ChallengePin::JSON_PIN_CODE, "");
152 for (auto& i : paramJson) {
153 if (i.first == ChallengePin::JSON_PIN_CODE)
154 i.second.put("", secret);
155 }
156 challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
157 client.m_challengeStatus,
158 paramJson));
159 std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl;
160 }
161 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
162 std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl;
163 count++;
164 BOOST_CHECK(security::verifySignature(response, cert));
165
166 client.onChallengeResponse(response);
167 BOOST_CHECK_EQUAL(client.m_status, STATUS_SUCCESS);
168 BOOST_CHECK_EQUAL(client.m_challengeStatus, CHALLENGE_STATUS_SUCCESS);
169 }
170 });
171
172 face.receive(*newInterest);
173 advanceClocks(time::milliseconds(20), 60);
174 face.receive(*challengeInterest);
175 advanceClocks(time::milliseconds(20), 60);
176 face.receive(*challengeInterest2);
177 advanceClocks(time::milliseconds(20), 60);
178 face.receive(*challengeInterest3);
179 advanceClocks(time::milliseconds(20), 60);
180 BOOST_CHECK_EQUAL(count, 3);
181}
182
183BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
184
185} // namespace tests
186} // namespace ndncert
187} // namespace ndn