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