Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2020, 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 | |
Zhiyi Zhang | 062be6d | 2020-10-14 17:13:43 -0700 | [diff] [blame] | 21 | #include "detail/info-encoder.hpp" |
| 22 | #include "detail/probe-encoder.hpp" |
| 23 | #include "detail/new-renew-revoke-encoder.hpp" |
| 24 | #include "detail/challenge-encoder.hpp" |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 25 | #include "test-common.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_FIXTURE_TEST_SUITE(TestProtocolDetail, IdentityManagementTimeFixture) |
| 32 | |
| 33 | BOOST_AUTO_TEST_CASE(TestInfo) |
| 34 | { |
| 35 | CaConfig config; |
| 36 | config.load("tests/unit-tests/config-files/config-ca-1"); |
| 37 | |
| 38 | const auto& identity = addIdentity("/test"); |
| 39 | const auto& cert = identity.getDefaultKey().getDefaultCertificate(); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 40 | auto encoded = InfoEncoder::encodeDataContent(config.m_caItem, cert); |
| 41 | auto decoded = InfoEncoder::decodeDataContent(encoded); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(config.m_caItem.m_caPrefix, decoded.m_caPrefix); |
| 43 | BOOST_CHECK_EQUAL(config.m_caItem.m_caInfo, decoded.m_caInfo); |
| 44 | BOOST_CHECK_EQUAL(config.m_caItem.m_maxValidityPeriod, decoded.m_maxValidityPeriod); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(config.m_caItem.m_probeParameterKeys.size(), decoded.m_probeParameterKeys.size()); |
| 46 | BOOST_CHECK_EQUAL(config.m_caItem.m_probeParameterKeys.front(), decoded.m_probeParameterKeys.front()); |
| 47 | BOOST_CHECK_EQUAL(cert.wireEncode(), decoded.m_cert->wireEncode()); |
| 48 | } |
| 49 | |
| 50 | BOOST_AUTO_TEST_CASE(TestProbe) |
| 51 | { |
| 52 | std::vector<std::tuple<std::string, std::string>> parameters; |
| 53 | parameters.push_back(std::make_tuple("email", "zhiyi@cs.ucla.edu")); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 54 | auto appParametersTlv = ProbeEncoder::encodeApplicationParameters(std::move(parameters)); |
| 55 | auto decodedParameters = ProbeEncoder::decodeApplicationParameters(appParametersTlv); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(std::get<0>(decodedParameters[0]), "email"); |
| 57 | BOOST_CHECK_EQUAL(std::get<1>(decodedParameters[0]), "zhiyi@cs.ucla.edu"); |
| 58 | BOOST_CHECK_EQUAL(decodedParameters.size(), 1); |
| 59 | |
| 60 | CaConfig config; |
| 61 | config.load("tests/unit-tests/config-files/config-ca-5"); |
| 62 | std::vector<Name> ids; |
| 63 | ids.push_back(Name("/example")); |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 64 | auto contentTlv = ProbeEncoder::encodeDataContent(ids, 2, config.m_redirection); |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 65 | std::vector<Name> decodedRedirectionItems; |
| 66 | std::vector<std::pair<Name, int>> decodedIds; |
Zhiyi Zhang | 8f1ade3 | 2020-10-14 16:42:57 -0700 | [diff] [blame] | 67 | ProbeEncoder::decodeDataContent(contentTlv, decodedIds, decodedRedirectionItems); |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(decodedIds[0].first, Name("/example")); |
| 69 | BOOST_CHECK_EQUAL(decodedIds[0].second, 2); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(decodedRedirectionItems[0], config.m_redirection->at(0)->getFullName()); |
| 71 | } |
| 72 | |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame^] | 73 | BOOST_AUTO_TEST_CASE(TestNewRenewRevoke) |
| 74 | { |
| 75 | auto identity = addIdentity(Name("/ndn")); |
| 76 | auto key = identity.getDefaultKey(); |
| 77 | auto cert = key.getDefaultCertificate(); |
| 78 | |
| 79 | ECDHState ecdhState; |
| 80 | auto block = NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::NEW, |
| 81 | ecdhState.getBase64PubKey(), |
| 82 | cert); |
| 83 | std::string ecdhPub; |
| 84 | shared_ptr<security::Certificate> clientCert; |
| 85 | NewRenewRevokeEncoder::decodeApplicationParameters(block, RequestType::NEW, ecdhPub, clientCert); |
| 86 | BOOST_CHECK_EQUAL(ecdhState.getBase64PubKey(), ecdhPub); |
| 87 | BOOST_CHECK_EQUAL(cert, *clientCert); |
| 88 | |
| 89 | block = NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::REVOKE, |
| 90 | ecdhState.getBase64PubKey(), |
| 91 | cert); |
| 92 | NewRenewRevokeEncoder::decodeApplicationParameters(block, RequestType::REVOKE, ecdhPub, clientCert); |
| 93 | BOOST_CHECK_EQUAL(ecdhState.getBase64PubKey(), ecdhPub); |
| 94 | BOOST_CHECK_EQUAL(cert, *clientCert); |
| 95 | |
| 96 | // NewRenewRevokeEncoder::encodeDataContent(ecdhState.getBase64PubKey(), "") |
| 97 | } |
| 98 | |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 99 | BOOST_AUTO_TEST_SUITE_END() // TestProtocolDetail |
| 100 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 101 | } // namespace tests |
| 102 | } // namespace ndncert |
| 103 | } // namespace ndn |