blob: b65b09bad6299e019219d1e24f9cdf229a95ecba [file] [log] [blame]
Zhiyi Zhang23564c82017-03-01 10:22:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang10130782018-02-01 18:28:49 -08003 * Copyright (c) 2017-2018, Regents of the University of California.
Zhiyi Zhang23564c82017-03-01 10:22:22 -08004 *
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 Zhang23564c82017-03-01 10:22:22 -080021#include "client-module.hpp"
Zhiyi Zhang10130782018-02-01 18:28:49 -080022#include "identity-management-fixture.hpp"
Zhiyi Zhang23564c82017-03-01 10:22:22 -080023#include "challenge-module.hpp"
24#include <ndn-cxx/util/dummy-client-face.hpp>
25#include <ndn-cxx/security/signing-helpers.hpp>
26#include <ndn-cxx/security/transform/public-key.hpp>
27#include <ndn-cxx/security/verification-helpers.hpp>
28
29namespace ndn {
30namespace ndncert {
31namespace tests {
32
33BOOST_FIXTURE_TEST_SUITE(TestClientModule, IdentityManagementV2TimeFixture)
34
35BOOST_AUTO_TEST_CASE(ClientModuleInitialize)
36{
37 util::DummyClientFace face(m_io, {true, true});
38 ClientModule client(face, m_keyChain);
39 client.getClientConf().load("tests/unit-tests/client.conf.test");
40 BOOST_CHECK_EQUAL(client.getClientConf().m_caItems.size(), 2);
41}
42
43BOOST_AUTO_TEST_CASE(ProbeAndNew)
44{
45 util::DummyClientFace face(m_io, {true, true});
46 ClientModule client(face, m_keyChain);
47 client.getClientConf().load("tests/unit-tests/client.conf.test");
48
Zhiyi Zhang10130782018-02-01 18:28:49 -080049 auto identity = addIdentity(Name("/site"));
Zhiyi Zhang23564c82017-03-01 10:22:22 -080050 auto key = identity.getDefaultKey();
51 auto cert = key.getDefaultCertificate();
52
53 ClientCaItem item;
54 item.m_caName = Name("/site/CA");
55 item.m_anchor = cert;
56 client.getClientConf().m_caItems.push_back(item);
57
58 int nInterest = 0;
Junxiao Shi7c068032017-05-28 13:40:47 +000059 auto processInterest = [&] (const Interest& interest) {
60 nInterest++;
61 if (nInterest == 1) {
62 // PROBE interest and return identifier
63 BOOST_CHECK_EQUAL(interest.getName().toUri(), "/site/CA/_PROBE/zhiyi%40cs.ucla.edu");
64 BOOST_CHECK_EQUAL(interest.getMustBeFresh(), 1);
Zhiyi Zhang23564c82017-03-01 10:22:22 -080065
Junxiao Shi7c068032017-05-28 13:40:47 +000066 auto data = make_shared<Data>();
67 data->setName(interest.getName());
Zhiyi Zhang10130782018-02-01 18:28:49 -080068 JsonSection json = genResponseProbeJson(Name("/site/ucla-cs-zhiyi"), Name(""));
Junxiao Shi7c068032017-05-28 13:40:47 +000069 std::stringstream ss;
70 boost::property_tree::write_json(ss, json);
71 Block dataContent = makeStringBlock(ndn::tlv::Content, ss.str());
72 data->setContent(dataContent);
73 m_keyChain.sign(*data, signingByCertificate(cert));
74 face.receive(*data);
75 }
76 else {
77 // NEW interest and return challenge list, request ID
78 BOOST_CHECK_EQUAL(interest.getName().getPrefix(3).toUri(), "/site/CA/_NEW");
79 BOOST_CHECK_EQUAL(interest.getName().size(), 6);
Zhiyi Zhang23564c82017-03-01 10:22:22 -080080
Junxiao Shi7c068032017-05-28 13:40:47 +000081 auto data = make_shared<Data>();
82 data->setName(interest.getName());
83 std::list<std::string> challenges;
84 challenges.push_back("EMAIL");
85 challenges.push_back("PIN");
86 JsonSection json = genResponseNewJson("1234", ChallengeModule::WAIT_SELECTION, challenges);
87 std::stringstream ss;
88 boost::property_tree::write_json(ss, json);
89 Block dataContent = makeStringBlock(ndn::tlv::Content, ss.str());
90 data->setContent(dataContent);
91 m_keyChain.sign(*data, signingByCertificate(cert));
Zhiyi Zhang23564c82017-03-01 10:22:22 -080092
Junxiao Shi7c068032017-05-28 13:40:47 +000093 face.receive(*data);
94 }
95 };
96 face.onSendInterest.connect([=] (const Interest& interest) { m_io.post([=] { processInterest(interest); }); });
Zhiyi Zhang23564c82017-03-01 10:22:22 -080097
98 int nCallback = 0;
99 shared_ptr<RequestState> requestState = nullptr;
100 ClientModule::RequestCallback requestCallback = [&] (shared_ptr<RequestState> state) {
101 nCallback++;
102 BOOST_CHECK_EQUAL(state->m_requestId, "1234");
103 BOOST_CHECK_EQUAL(state->m_challengeList.size(), 2);
104 requestState = state;
105 };
106 client.sendProbe(item, "zhiyi@cs.ucla.edu", requestCallback, ClientModule::ErrorCallback());
107
108 advanceClocks(time::milliseconds(200), 20);
109
110 BOOST_CHECK_EQUAL(nInterest, 2);
111 BOOST_CHECK_EQUAL(nCallback, 1);
112 BOOST_CHECK_EQUAL(requestState->m_ca.m_caName.toUri(), "/site/CA");
Zhiyi Zhang10130782018-02-01 18:28:49 -0800113 BOOST_CHECK_EQUAL(requestState->m_key.getName().getPrefix(3).toUri(), "/site/ucla-cs-zhiyi/KEY");
114
115 // make sure the client did not generate duplicated new keys
116 auto clientIdentity = m_keyChain.getPib().getIdentity(Name("/site/ucla-cs-zhiyi"));
117 const auto& clientKeys = clientIdentity.getKeys();
118 BOOST_CHECK_EQUAL(clientKeys.size(), 1);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800119}
120
121BOOST_AUTO_TEST_SUITE_END() // TestClientModule
122
123} // namespace tests
124} // namespace ndncert
125} // namespace ndn