blob: 4d7e6c89b5d74cf4c68b4162eb36f5cfe3592b1e [file] [log] [blame]
tylerliud59f2cf2020-10-30 00:00:10 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, Regents of the University of California.
tylerliud59f2cf2020-10-30 00:00:10 -07004 *
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 Zhang4c259db2020-10-30 09:36:01 -070021#include "detail/challenge-encoder.hpp"
22#include "detail/error-encoder.hpp"
23#include "detail/info-encoder.hpp"
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070024#include "detail/probe-encoder.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040025#include "detail/request-encoder.hpp"
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080026#include "detail/ca-configuration.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040027
Davide Pesavento829aff62022-05-15 20:30:34 -040028#include "tests/boost-test.hpp"
29#include "tests/clock-fixture.hpp"
30#include "tests/key-chain-fixture.hpp"
tylerliud59f2cf2020-10-30 00:00:10 -070031
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040032namespace ndncert::tests {
tylerliud59f2cf2020-10-30 00:00:10 -070033
Davide Pesavento829aff62022-05-15 20:30:34 -040034BOOST_AUTO_TEST_SUITE(TestProtocolEncoding)
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070035
tylerliud59f2cf2020-10-30 00:00:10 -070036BOOST_AUTO_TEST_CASE(InfoEncoding)
37{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070038 ca::CaConfig config;
39 config.load("tests/unit-tests/config-files/config-ca-1");
tylerliud59f2cf2020-10-30 00:00:10 -070040
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070041 requester::ProfileStorage caCache;
42 caCache.load("tests/unit-tests/config-files/config-client-1");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080043 auto& cert = caCache.getKnownProfiles().front().cert;
tylerliud59f2cf2020-10-30 00:00:10 -070044
tylerliuf2e6bb52020-12-13 13:23:05 -080045 auto b = infotlv::encodeDataContent(config.caProfile, *cert);
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080046 auto item = infotlv::decodeDataContent(b);
tylerliud59f2cf2020-10-30 00:00:10 -070047
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080048 BOOST_CHECK_EQUAL(*item.cert, *cert);
49 BOOST_CHECK_EQUAL(item.caInfo, config.caProfile.caInfo);
50 BOOST_CHECK_EQUAL(item.caPrefix, config.caProfile.caPrefix);
51 BOOST_CHECK_EQUAL_COLLECTIONS(item.probeParameterKeys.begin(), item.probeParameterKeys.end(),
52 config.caProfile.probeParameterKeys.begin(), config.caProfile.probeParameterKeys.end());
53 BOOST_CHECK_EQUAL(item.maxValidityPeriod, config.caProfile.maxValidityPeriod);
tylerliud59f2cf2020-10-30 00:00:10 -070054}
55
56BOOST_AUTO_TEST_CASE(ErrorEncoding)
57{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070058 std::string msg = "Just to test";
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080059 auto b = errortlv::encodeDataContent(ErrorCode::NAME_NOT_ALLOWED, msg);
60 auto item = errortlv::decodefromDataContent(b);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070061 BOOST_CHECK_EQUAL(std::get<0>(item), ErrorCode::NAME_NOT_ALLOWED);
62 BOOST_CHECK_EQUAL(std::get<1>(item), msg);
tylerliud59f2cf2020-10-30 00:00:10 -070063}
64
65BOOST_AUTO_TEST_CASE(ProbeEncodingAppParam)
66{
tylerliu40226332020-11-11 15:37:16 -080067 std::multimap<std::string, std::string> parameters;
68 parameters.emplace("key1", "value1");
69 parameters.emplace("key2", "value2");
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080070 auto appParam = probetlv::encodeApplicationParameters(std::move(parameters));
71 auto param1 = probetlv::decodeApplicationParameters(appParam);
tylerliu40226332020-11-11 15:37:16 -080072 BOOST_CHECK_EQUAL(param1.size(), 2);
73 BOOST_CHECK_EQUAL(param1.find("key1")->second, "value1");
74 BOOST_CHECK_EQUAL(param1.find("key2")->second, "value2");
tylerliud59f2cf2020-10-30 00:00:10 -070075}
76
77BOOST_AUTO_TEST_CASE(ProbeEncodingData)
78{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070079 ca::CaConfig config;
80 config.load("tests/unit-tests/config-files/config-ca-5");
81 std::vector<Name> names;
82 names.emplace_back("/ndn/1");
83 names.emplace_back("/ndn/2");
Tianyuan Yu13aac732022-03-03 20:59:54 -080084 std::vector<Name> redirectionNames;
85 for (const auto& i : config.redirection) redirectionNames.push_back(i.first->getFullName());
86 auto b = probetlv::encodeDataContent(names, 2, redirectionNames);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070087 std::vector<std::pair<Name, int>> retNames;
88 std::vector<Name> redirection;
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080089 probetlv::decodeDataContent(b, retNames, redirection);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070090 BOOST_CHECK_EQUAL(retNames.size(), names.size());
91 auto it1 = retNames.begin();
92 auto it2 = names.begin();
93 for (; it1 != retNames.end() && it2 != names.end(); it1++, it2++) {
94 BOOST_CHECK_EQUAL(it1->first, *it2);
95 BOOST_CHECK_EQUAL(it1->second, 2);
96 }
tylerliuf2e6bb52020-12-13 13:23:05 -080097 BOOST_CHECK_EQUAL(redirection.size(), config.redirection.size());
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070098 auto it3 = redirection.begin();
tylerliuf2e6bb52020-12-13 13:23:05 -080099 auto it4 = config.redirection.begin();
100 for (; it3 != redirection.end() && it4 != config.redirection.end(); it3++, it4++) {
Tianyuan Yu13aac732022-03-03 20:59:54 -0800101 BOOST_CHECK_EQUAL(*it3, it4->first->getFullName());
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700102 }
tylerliud59f2cf2020-10-30 00:00:10 -0700103}
104
105BOOST_AUTO_TEST_CASE(NewRevokeEncodingParam)
106{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700107 requester::ProfileStorage caCache;
108 caCache.load("tests/unit-tests/config-files/config-client-1");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800109 auto& certRequest = caCache.getKnownProfiles().front().cert;
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700110 std::vector<uint8_t> pub = ECDHState().getSelfPubKey();
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800111 auto b = requesttlv::encodeApplicationParameters(RequestType::REVOKE, pub, *certRequest);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700112 std::vector<uint8_t> returnedPub;
Davide Pesavento0dc02012021-11-23 22:55:03 -0500113 std::shared_ptr<Certificate> returnedCert;
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800114 requesttlv::decodeApplicationParameters(b, RequestType::REVOKE, returnedPub, returnedCert);
tylerliud59f2cf2020-10-30 00:00:10 -0700115
Davide Pesavento829aff62022-05-15 20:30:34 -0400116 BOOST_TEST(returnedPub == pub, boost::test_tools::per_element());
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700117 BOOST_CHECK_EQUAL(*returnedCert, *certRequest);
tylerliud59f2cf2020-10-30 00:00:10 -0700118}
119
120BOOST_AUTO_TEST_CASE(NewRevokeEncodingData)
121{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700122 std::vector<uint8_t> pub = ECDHState().getSelfPubKey();
Zhiyi Zhang80593022020-11-17 10:55:48 -0800123 std::array<uint8_t, 32> salt = {{101}};
124 RequestId id = {{102}};
tylerliuf2e6bb52020-12-13 13:23:05 -0800125 std::vector<std::string> list;
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700126 list.emplace_back("abc");
127 list.emplace_back("def");
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800128 auto b = requesttlv::encodeDataContent(pub, salt, id, list);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700129 std::vector<uint8_t> returnedPub;
130 std::array<uint8_t, 32> returnedSalt;
131 RequestId returnedId;
Zhiyi Zhang1b101b32021-02-17 14:36:03 -0800132 auto retlist = requesttlv::decodeDataContent(b, returnedPub, returnedSalt, returnedId);
tylerliu2670ba92020-10-30 10:25:03 -0700133 BOOST_CHECK_EQUAL_COLLECTIONS(returnedPub.begin(), returnedPub.end(), pub.begin(), pub.end());
134 BOOST_CHECK_EQUAL_COLLECTIONS(returnedSalt.begin(), returnedSalt.end(), salt.begin(), salt.end());
135 BOOST_CHECK_EQUAL_COLLECTIONS(returnedId.begin(), returnedId.end(), id.begin(), id.end());
tylerliud59f2cf2020-10-30 00:00:10 -0700136}
137
Davide Pesavento829aff62022-05-15 20:30:34 -0400138class ChallengeEncodingFixture : public ClockFixture, public KeyChainFixture
139{
140};
141
142BOOST_FIXTURE_TEST_CASE(ChallengeEncoding, ChallengeEncodingFixture)
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700143{
144 const uint8_t key[] = {0x23, 0x70, 0xe3, 0x20, 0xd4, 0x34, 0x42, 0x08,
145 0xe0, 0xff, 0x56, 0x83, 0xf2, 0x43, 0xb2, 0x13};
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700146 requester::ProfileStorage caCache;
147 caCache.load("tests/unit-tests/config-files/config-client-1");
Davide Pesavento0dc02012021-11-23 22:55:03 -0500148 auto certRequest = *caCache.getKnownProfiles().front().cert;
Zhiyi Zhang80593022020-11-17 10:55:48 -0800149 RequestId id = {{102}};
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800150 ca::RequestState state;
151 state.caPrefix = Name("/ndn/ucla");
152 state.requestId = id;
153 state.requestType = RequestType::NEW;
154 state.status = Status::PENDING;
155 state.cert = certRequest;
156 std::memcpy(state.encryptionKey.data(), key, sizeof(key));
157 state.challengeType = "pin";
Zhiyi Zhang01efccb2020-12-07 11:01:00 -0800158 auto tp = time::system_clock::now();
159 state.challengeState = ca::ChallengeState("test", tp, 3, time::seconds(3600), JsonSection());
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800160 auto contentBlock = challengetlv::encodeDataContent(state, Name("/ndn/ucla/a/b/c"));
tylerliud59f2cf2020-10-30 00:00:10 -0700161
tylerliu4140fe82021-01-27 15:45:44 -0800162 requester::Request context(m_keyChain, caCache.getKnownProfiles().front(), RequestType::NEW);
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800163 context.m_requestId = id;
164 std::memcpy(context.m_aesKey.data(), key, sizeof(key));
Zhiyi Zhang01efccb2020-12-07 11:01:00 -0800165 advanceClocks(time::seconds(10));
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800166 challengetlv::decodeDataContent(contentBlock, context);
tylerliud59f2cf2020-10-30 00:00:10 -0700167
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800168 BOOST_CHECK_EQUAL(static_cast<size_t>(context.m_status), static_cast<size_t>(Status::PENDING));
169 BOOST_CHECK_EQUAL(context.m_challengeStatus, "test");
170 BOOST_CHECK_EQUAL(context.m_remainingTries, 3);
171 BOOST_CHECK_EQUAL(context.m_freshBefore, tp + time::seconds(3600) + time::seconds(10));
172 BOOST_CHECK_EQUAL(context.m_issuedCertName, "/ndn/ucla/a/b/c");
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700173}
tylerliud59f2cf2020-10-30 00:00:10 -0700174
Davide Pesavento0dc02012021-11-23 22:55:03 -0500175BOOST_AUTO_TEST_SUITE_END() // TestProtocolEncoding
tylerliud59f2cf2020-10-30 00:00:10 -0700176
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400177} // namespace ndncert::tests