blob: da7e699e9fd9de669fd4893c90f12e7cc7f9c899 [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"
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
93 ClientModule client(m_keyChain);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070094 CaConfigItem item;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070095 item.m_caPrefix = Name("/ndn");
Zhiyi Zhang9829da92020-09-30 16:19:34 -070096 item.m_cert = std::make_shared<security::v2::Certificate>(cert);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -070097 client.getClientConf().m_caItems.push_back(item);
98 auto newInterest = client.generateNewInterest(time::system_clock::now(),
99 time::system_clock::now() + time::days(1), Name("/ndn/alice"));
100
101 std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl;
102
103 // generate CHALLENGE Interest
104 ChallengePin pinChallenge;
105 shared_ptr<Interest> challengeInterest = nullptr;
106 shared_ptr<Interest> challengeInterest2 = nullptr;
107 shared_ptr<Interest> challengeInterest3 = nullptr;
108
109 int count = 0;
110 face.onSendData.connect([&](const Data& response) {
111 if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
112 std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl;
tylerliu0e176c32020-09-29 11:39:46 -0700113 client.onNewRenewRevokeResponse(response);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700114 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700115 challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
116 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700117 std::move(paramList)));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700118 }
119 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
120 count++;
121 BOOST_CHECK(security::verifySignature(response, cert));
122
123 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700124 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700125 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
126
Zhiyi Zhang46049832020-09-28 17:08:12 -0700127 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700128 challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
129 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700130 std::move(paramList)));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700131 }
132 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
133 count++;
134 BOOST_CHECK(security::verifySignature(response, cert));
135
136 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700137 BOOST_CHECK(client.m_status == Status::CHALLENGE);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700138 BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
139
Zhiyi Zhang46049832020-09-28 17:08:12 -0700140 auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700141 auto request = ca.getCertificateRequest(*challengeInterest2);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700142 auto secret = request.m_challengeState->m_secrets.get(ChallengePin::PARAMETER_KEY_CODE, "");
Zhiyi Zhang46049832020-09-28 17:08:12 -0700143 std::get<1>(paramList[0]) = secret;
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700144 challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
145 client.m_challengeStatus,
Zhiyi Zhang46049832020-09-28 17:08:12 -0700146 std::move(paramList)));
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700147 std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl;
148 }
149 else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
150 std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl;
151 count++;
152 BOOST_CHECK(security::verifySignature(response, cert));
153
154 client.onChallengeResponse(response);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700155 BOOST_CHECK(client.m_status == Status::SUCCESS);
Zhiyi Zhangfc1678a2020-05-12 16:52:14 -0700156 }
157 });
158
159 face.receive(*newInterest);
160 advanceClocks(time::milliseconds(20), 60);
161 face.receive(*challengeInterest);
162 advanceClocks(time::milliseconds(20), 60);
163 face.receive(*challengeInterest2);
164 advanceClocks(time::milliseconds(20), 60);
165 face.receive(*challengeInterest3);
166 advanceClocks(time::milliseconds(20), 60);
167 BOOST_CHECK_EQUAL(count, 3);
168}
169
170BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
171
172} // namespace tests
173} // namespace ndncert
174} // namespace ndn