blob: e3aa143902498c05e4fd8b637883586f1a1a3eb7 [file] [log] [blame]
Zhiyi Zhange537dd52020-10-01 18:02:24 -07001/* -*- 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 Zhang062be6d2020-10-14 17:13:43 -070021#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 Zhange537dd52020-10-01 18:02:24 -070025#include "test-common.hpp"
26
27namespace ndn {
28namespace ndncert {
29namespace tests {
30
31BOOST_FIXTURE_TEST_SUITE(TestProtocolDetail, IdentityManagementTimeFixture)
32
33BOOST_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 Zhang8f1ade32020-10-14 16:42:57 -070040 auto encoded = InfoEncoder::encodeDataContent(config.m_caItem, cert);
41 auto decoded = InfoEncoder::decodeDataContent(encoded);
Zhiyi Zhange537dd52020-10-01 18:02:24 -070042 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 Zhange537dd52020-10-01 18:02:24 -070045 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
50BOOST_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 Zhang8f1ade32020-10-14 16:42:57 -070054 auto appParametersTlv = ProbeEncoder::encodeApplicationParameters(std::move(parameters));
55 auto decodedParameters = ProbeEncoder::decodeApplicationParameters(appParametersTlv);
Zhiyi Zhange537dd52020-10-01 18:02:24 -070056 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 Zhang8f1ade32020-10-14 16:42:57 -070064 auto contentTlv = ProbeEncoder::encodeDataContent(ids, 2, config.m_redirection);
tylerliub47dad72020-10-08 21:36:55 -070065 std::vector<Name> decodedRedirectionItems;
66 std::vector<std::pair<Name, int>> decodedIds;
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070067 ProbeEncoder::decodeDataContent(contentTlv, decodedIds, decodedRedirectionItems);
tylerliub47dad72020-10-08 21:36:55 -070068 BOOST_CHECK_EQUAL(decodedIds[0].first, Name("/example"));
69 BOOST_CHECK_EQUAL(decodedIds[0].second, 2);
Zhiyi Zhange537dd52020-10-01 18:02:24 -070070 BOOST_CHECK_EQUAL(decodedRedirectionItems[0], config.m_redirection->at(0)->getFullName());
71}
72
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070073BOOST_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 Zhange537dd52020-10-01 18:02:24 -070099BOOST_AUTO_TEST_SUITE_END() // TestProtocolDetail
100
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700101} // namespace tests
102} // namespace ndncert
103} // namespace ndn