Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2017-2019, 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 "ca-module.hpp" |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame^] | 22 | #include "requester.hpp" |
Zhiyi Zhang | 8bd8e5b | 2020-09-29 17:40:40 -0700 | [diff] [blame] | 23 | #include "challenge-modules/challenge-pin.hpp" |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 24 | #include "protocol-detail/info.hpp" |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 25 | #include "test-common.hpp" |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | namespace tests { |
| 30 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 31 | BOOST_FIXTURE_TEST_SUITE(TestForBenchmark, IdentityManagementTimeFixture) |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 32 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE(PacketSize0) |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 34 | { |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 35 | auto identity = addIdentity(Name("/ndn")); |
| 36 | auto key = identity.getDefaultKey(); |
| 37 | auto cert = key.getDefaultCertificate(); |
| 38 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 39 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 40 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 41 | advanceClocks(time::milliseconds(20), 60); |
| 42 | |
| 43 | Interest interest = MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO")); |
| 44 | std::cout << "CA Config discovery Interest Size: " << interest.wireEncode().size() << std::endl; |
| 45 | shared_ptr<Interest> infoInterest = nullptr; |
| 46 | |
| 47 | int count = 0; |
| 48 | face.onSendData.connect([&](const Data& response) { |
| 49 | if (count == 0) { |
| 50 | count++; |
| 51 | std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl; |
| 52 | auto block = response.getContent(); |
| 53 | block.parse(); |
| 54 | Interest interest(Name(block.get(tlv::Name))); |
| 55 | interest.setCanBePrefix(true); |
| 56 | infoInterest = make_shared<Interest>(interest); |
| 57 | std::cout << "CA Config fetch Interest Size: " << infoInterest->wireEncode().size() << std::endl; |
| 58 | |
| 59 | } |
| 60 | else { |
| 61 | count++; |
| 62 | std::cout << "CA Config Data Size: " << response.wireEncode().size() << std::endl; |
| 63 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 64 | auto contentBlock = response.getContent(); |
| 65 | contentBlock.parse(); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 66 | auto caItem = INFO::decodeDataContent(contentBlock); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn"); |
Zhiyi Zhang | b940aa1 | 2020-09-30 16:38:57 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.size(), 1); |
| 69 | BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.front(), "full name"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(caItem.m_cert->wireEncode(), cert.wireEncode()); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca"); |
| 72 | } |
| 73 | }); |
| 74 | face.receive(interest); |
| 75 | advanceClocks(time::milliseconds(20), 60); |
| 76 | face.receive(*infoInterest); |
| 77 | advanceClocks(time::milliseconds(20), 60); |
| 78 | |
| 79 | BOOST_CHECK_EQUAL(count, 2); |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 82 | BOOST_AUTO_TEST_CASE(PacketSize1) |
| 83 | { |
| 84 | auto identity = addIdentity(Name("/ndn")); |
| 85 | auto key = identity.getDefaultKey(); |
| 86 | auto cert = key.getDefaultCertificate(); |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 87 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 88 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 89 | CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory"); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 90 | advanceClocks(time::milliseconds(20), 60); |
| 91 | |
| 92 | // generate NEW Interest |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame^] | 93 | CaProfile item; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 94 | item.m_caPrefix = Name("/ndn"); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 95 | item.m_cert = std::make_shared<security::v2::Certificate>(cert); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame^] | 96 | RequesterState state(m_keyChain, item, RequestType::NEW); |
| 97 | auto newInterest = Requester::genNewInterest(state, Name("/ndn/alice"), |
| 98 | time::system_clock::now(), |
| 99 | time::system_clock::now() + time::days(1)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 100 | |
| 101 | std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl; |
| 102 | |
| 103 | // generate CHALLENGE Interest |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 104 | shared_ptr<Interest> challengeInterest = nullptr; |
| 105 | shared_ptr<Interest> challengeInterest2 = nullptr; |
| 106 | shared_ptr<Interest> challengeInterest3 = nullptr; |
| 107 | |
| 108 | int count = 0; |
| 109 | face.onSendData.connect([&](const Data& response) { |
| 110 | if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) { |
| 111 | std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl; |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame^] | 112 | auto challengeList = Requester::onNewRenewRevokeResponse(state, response); |
| 113 | auto paramList = Requester::selectOrContinueChallenge(state, "pin"); |
| 114 | challengeInterest = Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 115 | } |
| 116 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) { |
| 117 | count++; |
| 118 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 119 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame^] | 120 | Requester::onChallengeResponse(state, response); |
| 121 | BOOST_CHECK(state.m_status == Status::CHALLENGE); |
| 122 | BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::NEED_CODE); |
| 123 | auto paramList = Requester::selectOrContinueChallenge(state, "pin"); |
| 124 | challengeInterest2 = Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 125 | } |
| 126 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) { |
| 127 | count++; |
| 128 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 129 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame^] | 130 | Requester::onChallengeResponse(state, response); |
| 131 | BOOST_CHECK(state.m_status == Status::CHALLENGE); |
| 132 | BOOST_CHECK_EQUAL(state.m_challengeStatus, ChallengePin::WRONG_CODE); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 133 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame^] | 134 | auto paramList = Requester::selectOrContinueChallenge(state, "pin"); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 135 | auto request = ca.getCertificateRequest(*challengeInterest2); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 136 | auto secret = request.m_challengeState->m_secrets.get(ChallengePin::PARAMETER_KEY_CODE, ""); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 137 | std::get<1>(paramList[0]) = secret; |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame^] | 138 | challengeInterest3 = Requester::genChallengeInterest(state, std::move(paramList)); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 139 | std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl; |
| 140 | } |
| 141 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) { |
| 142 | std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl; |
| 143 | count++; |
| 144 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame^] | 145 | Requester::onChallengeResponse(state, response); |
| 146 | BOOST_CHECK(state.m_status == Status::SUCCESS); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 147 | } |
| 148 | }); |
| 149 | |
| 150 | face.receive(*newInterest); |
| 151 | advanceClocks(time::milliseconds(20), 60); |
| 152 | face.receive(*challengeInterest); |
| 153 | advanceClocks(time::milliseconds(20), 60); |
| 154 | face.receive(*challengeInterest2); |
| 155 | advanceClocks(time::milliseconds(20), 60); |
| 156 | face.receive(*challengeInterest3); |
| 157 | advanceClocks(time::milliseconds(20), 60); |
| 158 | BOOST_CHECK_EQUAL(count, 3); |
| 159 | } |
| 160 | |
| 161 | BOOST_AUTO_TEST_SUITE_END() // TestCaConfig |
| 162 | |
| 163 | } // namespace tests |
| 164 | } // namespace ndncert |
| 165 | } // namespace ndn |