Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 2 | /* |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, Regents of the University of California. |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [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 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 21 | #include "requester-request.hpp" |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 22 | #include "ca-module.hpp" |
| 23 | #include "challenge/challenge-module.hpp" |
Zhiyi Zhang | 062be6d | 2020-10-14 17:13:43 -0700 | [diff] [blame] | 24 | #include "detail/error-encoder.hpp" |
| 25 | #include "detail/probe-encoder.hpp" |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 26 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 27 | #include "tests/boost-test.hpp" |
| 28 | #include "tests/io-key-chain-fixture.hpp" |
| 29 | |
| 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 32 | namespace ndncert::tests { |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 33 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 34 | using namespace requester; |
| 35 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(TestRequester, IoKeyChainFixture) |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 37 | |
Suyong Won | 699e469 | 2020-10-04 03:26:42 +0900 | [diff] [blame] | 38 | BOOST_AUTO_TEST_CASE(GenProbeInterest) |
| 39 | { |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 40 | auto identity = m_keyChain.createIdentity(Name("/site")); |
Suyong Won | 699e469 | 2020-10-04 03:26:42 +0900 | [diff] [blame] | 41 | auto key = identity.getDefaultKey(); |
| 42 | auto cert = key.getDefaultCertificate(); |
| 43 | |
| 44 | CaProfile ca_profile; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 45 | ca_profile.probeParameterKeys.push_back("email"); |
| 46 | ca_profile.probeParameterKeys.push_back("uid"); |
| 47 | ca_profile.probeParameterKeys.push_back("name"); |
| 48 | ca_profile.caPrefix = Name("/site"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 49 | ca_profile.cert = std::make_shared<Certificate>(cert); |
Suyong Won | 699e469 | 2020-10-04 03:26:42 +0900 | [diff] [blame] | 50 | |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 51 | std::multimap<std::string, std::string> probeParams; |
| 52 | probeParams.emplace("email", "zhiyi@cs.ucla.edu"); |
| 53 | probeParams.emplace("uid", "987654321"); |
| 54 | probeParams.emplace("name", "Zhiyi Zhang"); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 55 | auto firstInterest = Request::genProbeInterest(ca_profile, std::move(probeParams)); |
Suyong Won | 699e469 | 2020-10-04 03:26:42 +0900 | [diff] [blame] | 56 | |
| 57 | BOOST_CHECK(firstInterest->getName().at(-1).isParametersSha256Digest()); |
| 58 | // ignore the last name component (ParametersSha256Digest) |
Zhiyi Zhang | 621a3b5 | 2020-10-03 21:55:11 -0700 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(firstInterest->getName().getPrefix(-1), "/site/CA/PROBE"); |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(readString(firstInterest->getApplicationParameters().get(tlv::ParameterValue)), "zhiyi@cs.ucla.edu"); |
Suyong Won | 699e469 | 2020-10-04 03:26:42 +0900 | [diff] [blame] | 61 | } |
| 62 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 63 | BOOST_AUTO_TEST_CASE(OnProbeResponse) |
| 64 | { |
| 65 | auto identity = m_keyChain.createIdentity(Name("/site")); |
Suyong Won | 0ff8e9b | 2020-10-06 02:48:33 +0900 | [diff] [blame] | 66 | auto key = identity.getDefaultKey(); |
| 67 | auto cert = key.getDefaultCertificate(); |
Suyong Won | 699e469 | 2020-10-04 03:26:42 +0900 | [diff] [blame] | 68 | |
Suyong Won | 0ff8e9b | 2020-10-06 02:48:33 +0900 | [diff] [blame] | 69 | CaProfile ca_profile; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 70 | ca_profile.probeParameterKeys.push_back("email"); |
| 71 | ca_profile.probeParameterKeys.push_back("uid"); |
| 72 | ca_profile.probeParameterKeys.push_back("name"); |
| 73 | ca_profile.caPrefix = Name("/site"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 74 | ca_profile.cert = std::make_shared<Certificate>(cert); |
Suyong Won | 0ff8e9b | 2020-10-06 02:48:33 +0900 | [diff] [blame] | 75 | |
| 76 | std::vector<Name> availableNames; |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 77 | availableNames.emplace_back("/site1"); |
| 78 | availableNames.emplace_back("/site2"); |
Suyong Won | 0ff8e9b | 2020-10-06 02:48:33 +0900 | [diff] [blame] | 79 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 80 | ndn::util::DummyClientFace face(m_io, m_keyChain, {true, true}); |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 81 | ca::CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-5", "ca-storage-memory"); |
Suyong Won | 0ff8e9b | 2020-10-06 02:48:33 +0900 | [diff] [blame] | 82 | |
| 83 | Data reply; |
| 84 | reply.setName(Name("/site/CA/PROBE")); |
| 85 | reply.setFreshnessPeriod(time::seconds(100)); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 86 | { |
| 87 | std::vector<Name> redirectionNames; |
| 88 | for (const auto &i : ca.m_config.redirection) redirectionNames.push_back(i.first->getFullName()); |
| 89 | reply.setContent(probetlv::encodeDataContent(availableNames, 3, redirectionNames)); |
| 90 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 91 | m_keyChain.sign(reply, ndn::signingByIdentity(identity)); |
Suyong Won | 0ff8e9b | 2020-10-06 02:48:33 +0900 | [diff] [blame] | 92 | |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 93 | std::vector<std::pair<Name, int>> names; |
| 94 | std::vector<Name> redirects; |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 95 | Request::onProbeResponse(reply, ca_profile, names, redirects); |
Suyong Won | 0ff8e9b | 2020-10-06 02:48:33 +0900 | [diff] [blame] | 96 | |
| 97 | // Test names and redirects are properly stored |
| 98 | BOOST_CHECK_EQUAL(names.size(), 2); |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(names[0].first.toUri(), "/site1"); |
| 100 | BOOST_CHECK_EQUAL(names[0].second, 3); |
| 101 | BOOST_CHECK_EQUAL(names[1].first.toUri(), "/site2"); |
| 102 | BOOST_CHECK_EQUAL(names[1].second, 3); |
Suyong Won | 0ff8e9b | 2020-10-06 02:48:33 +0900 | [diff] [blame] | 103 | |
| 104 | BOOST_CHECK_EQUAL(redirects.size(), 2); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirects[0].getPrefix(-1)), "/ndn/edu/ucla"); |
| 106 | BOOST_CHECK_EQUAL(ndn::security::extractIdentityFromCertName(redirects[1].getPrefix(-1)), "/ndn/edu/ucla/cs/irl"); |
Suyong Won | 0ff8e9b | 2020-10-06 02:48:33 +0900 | [diff] [blame] | 107 | } |
Suyong Won | 699e469 | 2020-10-04 03:26:42 +0900 | [diff] [blame] | 108 | |
tylerliu | 36d97f5 | 2020-09-30 22:32:54 -0700 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(ErrorHandling) |
| 110 | { |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 111 | auto identity = m_keyChain.createIdentity(Name("/site")); |
tylerliu | 36d97f5 | 2020-09-30 22:32:54 -0700 | [diff] [blame] | 112 | auto key = identity.getDefaultKey(); |
| 113 | auto cert = key.getDefaultCertificate(); |
| 114 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 115 | CaProfile item; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 116 | item.caPrefix = Name("/site"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 117 | item.cert = std::make_shared<Certificate>(cert); |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 118 | Request state(m_keyChain, item, RequestType::NEW); |
tylerliu | 36d97f5 | 2020-09-30 22:32:54 -0700 | [diff] [blame] | 119 | |
| 120 | Data errorPacket; |
| 121 | errorPacket.setName(Name("/site/pretend/this/is/error/packet")); |
| 122 | errorPacket.setFreshnessPeriod(time::seconds(100)); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 123 | errorPacket.setContent(errortlv::encodeDataContent(ErrorCode::INVALID_PARAMETER, "This is a test.")); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 124 | m_keyChain.sign(errorPacket, ndn::signingByIdentity(identity)); |
tylerliu | 36d97f5 | 2020-09-30 22:32:54 -0700 | [diff] [blame] | 125 | |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 126 | std::vector<std::pair<Name, int>> ids; |
| 127 | std::vector<Name> cas; |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 128 | BOOST_CHECK_THROW(Request::onProbeResponse(errorPacket, item, ids, cas), std::runtime_error); |
| 129 | BOOST_CHECK_THROW(state.onNewRenewRevokeResponse(errorPacket), std::runtime_error); |
| 130 | BOOST_CHECK_THROW(state.onChallengeResponse(errorPacket), std::runtime_error); |
tylerliu | 36d97f5 | 2020-09-30 22:32:54 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 133 | BOOST_AUTO_TEST_SUITE_END() // TestRequester |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 134 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 135 | } // namespace ndncert::tests |