blob: 27cb9a7095e3f57c3ba5b97ba2630ef408e49537 [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 Zhangf5246c42017-01-26 09:39:20 -080021#include "ca-module.hpp"
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070022
23#include "database-fixture.hpp"
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080024#include "client-module.hpp"
25#include "challenge-module.hpp"
26#include <ndn-cxx/util/dummy-client-face.hpp>
27#include <ndn-cxx/security/signing-helpers.hpp>
28#include <ndn-cxx/security/transform/public-key.hpp>
29#include <ndn-cxx/security/verification-helpers.hpp>
30
31namespace ndn {
32namespace ndncert {
33namespace tests {
34
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070035BOOST_FIXTURE_TEST_SUITE(TestCaModule, DatabaseFixture)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080036
37BOOST_AUTO_TEST_CASE(Initialization)
38{
39 util::DummyClientFace face(m_io, {true, true});
40 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070041 BOOST_CHECK_EQUAL(ca.getCaConf().m_caItems.front().m_caName.toUri(), "/ndn");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080042 BOOST_CHECK_EQUAL(ca.getCaConf().m_caItems.back().m_caName.toUri(), "/ndn/site1");
43
44 auto identity = addIdentity(Name("/ndn/site2"));
45 auto key = identity.getDefaultKey();
46 auto cert = key.getDefaultCertificate();
47 ca.getCaStorage()->addCertificate("111", cert);
48 BOOST_CHECK_EQUAL(ca.getCaStorage()->getCertificate("111").getIdentity(), Name("/ndn/site2"));
49
50 advanceClocks(time::milliseconds(20), 60);
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070051 BOOST_CHECK_EQUAL(ca.m_registeredPrefixIds.size(), 3);
52 BOOST_CHECK_EQUAL(ca.m_interestFilterIds.size(), 17);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080053}
54
55BOOST_AUTO_TEST_CASE(HandleProbe)
56{
57 auto identity = addIdentity(Name("/ndn/site1"));
58 auto key = identity.getDefaultKey();
59 auto cert = key.getDefaultCertificate();
60
61 util::DummyClientFace face(m_io, {true, true});
62 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080063 ca.setProbeHandler(Name("/ndn/site1"), [&] (const std::string& probeInfo) {
64 return probeInfo + "example";
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080065 });
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080066
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);
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080078 BOOST_CHECK_EQUAL(contentJson.get(JSON_IDNENTIFIER, ""), "/ndn/site1/zhiyiexample");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080079 });
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");
Zhiyi Zhanga63b7372017-05-17 14:14:34 -070094
95 advanceClocks(time::milliseconds(20), 60);
96
97 Name interestName("/ndn/site1/CA");
98 interestName.append("_PROBE").append("zhiyi");
99 Interest interest(interestName);
100
101 int count = 0;
102 face.onSendData.connect([&] (const Data& response) {
103 count++;
104 BOOST_CHECK(security::verifySignature(response, cert));
105 JsonSection contentJson = ClientModule::getJsonFromData(response);
106 BOOST_CHECK_EQUAL(contentJson.get(JSON_IDNENTIFIER, ""), "/ndn/site1/zhiyi");
107 });
108 face.receive(interest);
109
110 advanceClocks(time::milliseconds(20), 60);
111 BOOST_CHECK_EQUAL(count, 1);
112}
113
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800114BOOST_AUTO_TEST_CASE(HandleNew)
115{
116 auto identity = addIdentity(Name("/ndn/site1"));
117 auto key = identity.getDefaultKey();
118 auto cert = key.getDefaultCertificate();
119
120 util::DummyClientFace face(m_io, {true, true});
121 util::DummyClientFace face2(m_io, {true, true});
122
123 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800124 advanceClocks(time::milliseconds(20), 60);
125
126 Name identityName("/ndn/site1");
127 identityName.append("zhiyi");
128 ClientModule client(face2, m_keyChain);
129 ClientCaItem item;
130 item.m_caName = Name("/ndn/site1/CA");
131 item.m_anchor = cert;
132 client.getClientConf().m_caItems.push_back(item);
133
134 int nClientInterest = 0;
135 int nCaData = 0;
136 int nClientCallback = 0;
137
138 face.onSendData.connect([&] (const Data& data) {
139 nCaData++;
140 JsonSection contentJson = ClientModule::getJsonFromData(data);
141 BOOST_CHECK(!contentJson.get(JSON_REQUEST_ID, "").empty());
142 face2.receive(data);
143 });
144 face2.onSendInterest.connect([&] (const Interest& interest) {
145 nClientInterest++;
146 face.receive(interest);
147 });
148
149 client.sendNew(item, identityName,
150 [&] (const shared_ptr<RequestState> state) {
151 nClientCallback++;
152 BOOST_CHECK(state->m_requestId != "");
153 },
154 [] (const std::string& s) { BOOST_CHECK(false); });
155
156 advanceClocks(time::milliseconds(20), 60);
157
158 BOOST_CHECK_EQUAL(nClientCallback, 1);
159 BOOST_CHECK_EQUAL(nCaData, 1);
160 BOOST_CHECK_EQUAL(nClientInterest, 1);
161}
162
163BOOST_AUTO_TEST_SUITE_END() // TestCaModule
164
165} // namespace tests
166} // namespace ndncert
167} // namespace ndn