blob: 94d636ddb1907e46f7c4825518ddfc4a8e4c2c0d [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017, 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
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070021#include "database-fixture.hpp"
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080022#include "ca-module.hpp"
23#include "client-module.hpp"
24#include "challenge-module.hpp"
25#include <ndn-cxx/util/dummy-client-face.hpp>
26#include <ndn-cxx/security/signing-helpers.hpp>
27#include <ndn-cxx/security/transform/public-key.hpp>
28#include <ndn-cxx/security/verification-helpers.hpp>
29
30namespace ndn {
31namespace ndncert {
32namespace tests {
33
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070034BOOST_FIXTURE_TEST_SUITE(TestCaModule, DatabaseFixture)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080035
36BOOST_AUTO_TEST_CASE(Initialization)
37{
38 util::DummyClientFace face(m_io, {true, true});
39 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
40 BOOST_CHECK_EQUAL(ca.getCaConf().m_caItems.front().m_caName.toUri(), "/ndn/edu/ucla/cs/zhiyi");
41 BOOST_CHECK_EQUAL(ca.getCaConf().m_caItems.back().m_caName.toUri(), "/ndn/site1");
42
43 auto identity = addIdentity(Name("/ndn/site2"));
44 auto key = identity.getDefaultKey();
45 auto cert = key.getDefaultCertificate();
46 ca.getCaStorage()->addCertificate("111", cert);
47 BOOST_CHECK_EQUAL(ca.getCaStorage()->getCertificate("111").getIdentity(), Name("/ndn/site2"));
48
49 advanceClocks(time::milliseconds(20), 60);
50 BOOST_CHECK_EQUAL(ca.m_registeredPrefixIds.size(), 2);
51 BOOST_CHECK_EQUAL(ca.m_interestFilterIds.size(), 12);
52}
53
54BOOST_AUTO_TEST_CASE(HandleProbe)
55{
56 auto identity = addIdentity(Name("/ndn/site1"));
57 auto key = identity.getDefaultKey();
58 auto cert = key.getDefaultCertificate();
59
60 util::DummyClientFace face(m_io, {true, true});
61 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
62 ca.setProbeHandler([&] (const std::string& probeInfo) {
63 return probeInfo;
64 });
65 ca.getCaConf().m_caItems.back().m_anchor = cert.getName();
66
67 advanceClocks(time::milliseconds(20), 60);
68
69 Name interestName("/ndn/site1/CA");
70 interestName.append("_PROBE").append("zhiyi");
71 Interest interest(interestName);
72
73 int count = 0;
74 face.onSendData.connect([&] (const Data& response) {
75 count++;
76 BOOST_CHECK(security::verifySignature(response, cert));
77 JsonSection contentJson = ClientModule::getJsonFromData(response);
78 BOOST_CHECK_EQUAL(contentJson.get(JSON_IDNENTIFIER, ""), "/ndn/site1/zhiyi");
79 });
80 face.receive(interest);
81
82 advanceClocks(time::milliseconds(20), 60);
83 BOOST_CHECK_EQUAL(count, 1);
84}
85
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070086BOOST_AUTO_TEST_CASE(HandleProbeUsingDefaultHandler)
87{
88 auto identity = addIdentity(Name("/ndn/site1"));
89 auto key = identity.getDefaultKey();
90 auto cert = key.getDefaultCertificate();
91
92 util::DummyClientFace face(m_io, {true, true});
93 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
94 ca.getCaConf().m_caItems.back().m_anchor = cert.getName();
95
96 advanceClocks(time::milliseconds(20), 60);
97
98 Name interestName("/ndn/site1/CA");
99 interestName.append("_PROBE").append("zhiyi");
100 Interest interest(interestName);
101
102 int count = 0;
103 face.onSendData.connect([&] (const Data& response) {
104 count++;
105 BOOST_CHECK(security::verifySignature(response, cert));
106 JsonSection contentJson = ClientModule::getJsonFromData(response);
107 BOOST_CHECK_EQUAL(contentJson.get(JSON_IDNENTIFIER, ""), "/ndn/site1/zhiyi");
108 });
109 face.receive(interest);
110
111 advanceClocks(time::milliseconds(20), 60);
112 BOOST_CHECK_EQUAL(count, 1);
113}
114
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800115BOOST_AUTO_TEST_CASE(HandleNew)
116{
117 auto identity = addIdentity(Name("/ndn/site1"));
118 auto key = identity.getDefaultKey();
119 auto cert = key.getDefaultCertificate();
120
121 util::DummyClientFace face(m_io, {true, true});
122 util::DummyClientFace face2(m_io, {true, true});
123
124 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
125 ca.setProbeHandler([&] (const std::string& probeInfo) {
126 return probeInfo;
127 });
128 ca.getCaConf().m_caItems.back().m_anchor = cert.getName();
129 advanceClocks(time::milliseconds(20), 60);
130
131 Name identityName("/ndn/site1");
132 identityName.append("zhiyi");
133 ClientModule client(face2, m_keyChain);
134 ClientCaItem item;
135 item.m_caName = Name("/ndn/site1/CA");
136 item.m_anchor = cert;
137 client.getClientConf().m_caItems.push_back(item);
138
139 int nClientInterest = 0;
140 int nCaData = 0;
141 int nClientCallback = 0;
142
143 face.onSendData.connect([&] (const Data& data) {
144 nCaData++;
145 JsonSection contentJson = ClientModule::getJsonFromData(data);
146 BOOST_CHECK(!contentJson.get(JSON_REQUEST_ID, "").empty());
147 face2.receive(data);
148 });
149 face2.onSendInterest.connect([&] (const Interest& interest) {
150 nClientInterest++;
151 face.receive(interest);
152 });
153
154 client.sendNew(item, identityName,
155 [&] (const shared_ptr<RequestState> state) {
156 nClientCallback++;
157 BOOST_CHECK(state->m_requestId != "");
158 },
159 [] (const std::string& s) { BOOST_CHECK(false); });
160
161 advanceClocks(time::milliseconds(20), 60);
162
163 BOOST_CHECK_EQUAL(nClientCallback, 1);
164 BOOST_CHECK_EQUAL(nCaData, 1);
165 BOOST_CHECK_EQUAL(nClientInterest, 1);
166}
167
168BOOST_AUTO_TEST_SUITE_END() // TestCaModule
169
170} // namespace tests
171} // namespace ndncert
172} // namespace ndn