blob: e02afaf0b2eb2900c6508329f24c5448acdd0cdd [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;
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070080 auto selfKey = ecdhState.getSelfPubKey();
81 auto block = NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::NEW, selfKey, cert);
82 std::vector<uint8_t> peerKey;
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070083 shared_ptr<security::Certificate> clientCert;
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070084 NewRenewRevokeEncoder::decodeApplicationParameters(block, RequestType::NEW, peerKey, clientCert);
85 BOOST_CHECK_EQUAL_COLLECTIONS(selfKey.begin(), selfKey.end(), peerKey.begin(), peerKey.end());
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070086 BOOST_CHECK_EQUAL(cert, *clientCert);
87
Zhiyi Zhangbed854c2020-10-20 18:25:35 -070088 block = NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::REVOKE, selfKey, cert);
89 NewRenewRevokeEncoder::decodeApplicationParameters(block, RequestType::REVOKE, peerKey, clientCert);
90 BOOST_CHECK_EQUAL_COLLECTIONS(selfKey.begin(), selfKey.end(), peerKey.begin(), peerKey.end());
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070091 BOOST_CHECK_EQUAL(cert, *clientCert);
92
93 // NewRenewRevokeEncoder::encodeDataContent(ecdhState.getBase64PubKey(), "")
94}
95
Zhiyi Zhange537dd52020-10-01 18:02:24 -070096BOOST_AUTO_TEST_SUITE_END() // TestProtocolDetail
97
Zhiyi Zhange4891b72020-10-10 15:11:57 -070098} // namespace tests
99} // namespace ndncert
100} // namespace ndn