Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 1 | /* -*- 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 "client-module.hpp" |
| 23 | #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 | |
| 29 | namespace ndn { |
| 30 | namespace ndncert { |
| 31 | namespace tests { |
| 32 | |
| 33 | BOOST_FIXTURE_TEST_SUITE(TestClientModule, IdentityManagementV2TimeFixture) |
| 34 | |
| 35 | BOOST_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 | |
| 43 | BOOST_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 | |
| 49 | auto identity = addIdentity(Name("/site/CA")); |
| 50 | 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; |
| 59 | face.onSendInterest.connect([&] (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); |
| 65 | |
| 66 | auto data = make_shared<Data>(); |
| 67 | data->setName(interest.getName()); |
| 68 | JsonSection json = genResponseProbeJson(Name("/site/CA/ucla-cs-zhiyi"), Name("")); |
| 69 | 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); |
| 80 | |
| 81 | 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)); |
| 92 | |
| 93 | face.receive(*data); |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | int nCallback = 0; |
| 98 | shared_ptr<RequestState> requestState = nullptr; |
| 99 | ClientModule::RequestCallback requestCallback = [&] (shared_ptr<RequestState> state) { |
| 100 | nCallback++; |
| 101 | BOOST_CHECK_EQUAL(state->m_requestId, "1234"); |
| 102 | BOOST_CHECK_EQUAL(state->m_challengeList.size(), 2); |
| 103 | requestState = state; |
| 104 | }; |
| 105 | client.sendProbe(item, "zhiyi@cs.ucla.edu", requestCallback, ClientModule::ErrorCallback()); |
| 106 | |
| 107 | advanceClocks(time::milliseconds(200), 20); |
| 108 | |
| 109 | BOOST_CHECK_EQUAL(nInterest, 2); |
| 110 | BOOST_CHECK_EQUAL(nCallback, 1); |
| 111 | BOOST_CHECK_EQUAL(requestState->m_ca.m_caName.toUri(), "/site/CA"); |
| 112 | BOOST_CHECK_EQUAL(requestState->m_key.getName().getPrefix(4).toUri(), "/site/CA/ucla-cs-zhiyi/KEY"); |
| 113 | } |
| 114 | |
| 115 | BOOST_AUTO_TEST_SUITE_END() // TestClientModule |
| 116 | |
| 117 | } // namespace tests |
| 118 | } // namespace ndncert |
| 119 | } // namespace ndn |