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 | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 22 | #include "client-module.hpp" |
| 23 | #include "challenge-module/challenge-pin.hpp" |
| 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 | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 40 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "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(); |
| 66 | auto caItem = INFO::decodeClientConfigFromContent(contentBlock); |
| 67 | BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn"); |
| 68 | BOOST_CHECK_EQUAL(caItem.m_probe, ""); |
| 69 | BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode()); |
| 70 | BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca"); |
| 71 | } |
| 72 | }); |
| 73 | face.receive(interest); |
| 74 | advanceClocks(time::milliseconds(20), 60); |
| 75 | face.receive(*infoInterest); |
| 76 | advanceClocks(time::milliseconds(20), 60); |
| 77 | |
| 78 | BOOST_CHECK_EQUAL(count, 2); |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 81 | BOOST_AUTO_TEST_CASE(PacketSize1) |
| 82 | { |
| 83 | auto identity = addIdentity(Name("/ndn")); |
| 84 | auto key = identity.getDefaultKey(); |
| 85 | auto cert = key.getDefaultCertificate(); |
Suyong Won | 57462ca | 2020-05-05 22:20:09 -0700 | [diff] [blame] | 86 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 87 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 88 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
| 89 | advanceClocks(time::milliseconds(20), 60); |
| 90 | |
| 91 | // generate NEW Interest |
| 92 | ClientModule client(m_keyChain); |
| 93 | ClientCaItem item; |
| 94 | item.m_caPrefix = Name("/ndn"); |
| 95 | item.m_anchor = cert; |
| 96 | client.getClientConf().m_caItems.push_back(item); |
| 97 | auto newInterest = client.generateNewInterest(time::system_clock::now(), |
| 98 | time::system_clock::now() + time::days(1), Name("/ndn/alice")); |
| 99 | |
| 100 | std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl; |
| 101 | |
| 102 | // generate CHALLENGE Interest |
| 103 | ChallengePin pinChallenge; |
| 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; |
tylerliu | 0e176c3 | 2020-09-29 11:39:46 -0700 | [diff] [blame] | 112 | client.onNewRenewRevokeResponse(response); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 113 | auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 114 | challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status, |
| 115 | client.m_challengeStatus, |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 116 | std::move(paramList))); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 117 | } |
| 118 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) { |
| 119 | count++; |
| 120 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 121 | |
| 122 | client.onChallengeResponse(response); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 123 | BOOST_CHECK(client.m_status == Status::CHALLENGE); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE); |
| 125 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 126 | auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 127 | challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status, |
| 128 | client.m_challengeStatus, |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 129 | std::move(paramList))); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 130 | } |
| 131 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) { |
| 132 | count++; |
| 133 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 134 | |
| 135 | client.onChallengeResponse(response); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 136 | BOOST_CHECK(client.m_status == Status::CHALLENGE); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE); |
| 138 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 139 | auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 140 | auto request = ca.getCertificateRequest(*challengeInterest2); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 141 | auto secret = request.m_challengeState->m_secrets.get(ChallengePin::PARAMETER_KEY_CODE, ""); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 142 | std::get<1>(paramList[0]) = secret; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 143 | challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status, |
| 144 | client.m_challengeStatus, |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 145 | std::move(paramList))); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 146 | std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl; |
| 147 | } |
| 148 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) { |
| 149 | std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl; |
| 150 | count++; |
| 151 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 152 | |
| 153 | client.onChallengeResponse(response); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 154 | BOOST_CHECK(client.m_status == Status::SUCCESS); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 155 | } |
| 156 | }); |
| 157 | |
| 158 | face.receive(*newInterest); |
| 159 | advanceClocks(time::milliseconds(20), 60); |
| 160 | face.receive(*challengeInterest); |
| 161 | advanceClocks(time::milliseconds(20), 60); |
| 162 | face.receive(*challengeInterest2); |
| 163 | advanceClocks(time::milliseconds(20), 60); |
| 164 | face.receive(*challengeInterest3); |
| 165 | advanceClocks(time::milliseconds(20), 60); |
| 166 | BOOST_CHECK_EQUAL(count, 3); |
| 167 | } |
| 168 | |
| 169 | BOOST_AUTO_TEST_SUITE_END() // TestCaConfig |
| 170 | |
| 171 | } // namespace tests |
| 172 | } // namespace ndncert |
| 173 | } // namespace ndn |