blob: 33c81ee7a6f85c8460bff3189dec192c0fb0165a [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
21#include "identity-management-fixture.hpp"
22#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
34BOOST_FIXTURE_TEST_SUITE(TestCaModule, IdentityManagementV2TimeFixture)
35
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
86BOOST_AUTO_TEST_CASE(HandleNew)
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 util::DummyClientFace face2(m_io, {true, true});
94
95 CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
96 ca.setProbeHandler([&] (const std::string& probeInfo) {
97 return probeInfo;
98 });
99 ca.getCaConf().m_caItems.back().m_anchor = cert.getName();
100 advanceClocks(time::milliseconds(20), 60);
101
102 Name identityName("/ndn/site1");
103 identityName.append("zhiyi");
104 ClientModule client(face2, m_keyChain);
105 ClientCaItem item;
106 item.m_caName = Name("/ndn/site1/CA");
107 item.m_anchor = cert;
108 client.getClientConf().m_caItems.push_back(item);
109
110 int nClientInterest = 0;
111 int nCaData = 0;
112 int nClientCallback = 0;
113
114 face.onSendData.connect([&] (const Data& data) {
115 nCaData++;
116 JsonSection contentJson = ClientModule::getJsonFromData(data);
117 BOOST_CHECK(!contentJson.get(JSON_REQUEST_ID, "").empty());
118 face2.receive(data);
119 });
120 face2.onSendInterest.connect([&] (const Interest& interest) {
121 nClientInterest++;
122 face.receive(interest);
123 });
124
125 client.sendNew(item, identityName,
126 [&] (const shared_ptr<RequestState> state) {
127 nClientCallback++;
128 BOOST_CHECK(state->m_requestId != "");
129 },
130 [] (const std::string& s) { BOOST_CHECK(false); });
131
132 advanceClocks(time::milliseconds(20), 60);
133
134 BOOST_CHECK_EQUAL(nClientCallback, 1);
135 BOOST_CHECK_EQUAL(nCaData, 1);
136 BOOST_CHECK_EQUAL(nClientInterest, 1);
137}
138
139BOOST_AUTO_TEST_SUITE_END() // TestCaModule
140
141} // namespace tests
142} // namespace ndncert
143} // namespace ndn