blob: b522b065bcaf34463bfa771db28ce2cbc10a4dab [file] [log] [blame]
tylerliud59f2cf2020-10-30 00:00:10 -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 Zhang4c259db2020-10-30 09:36:01 -070021#include "detail/challenge-encoder.hpp"
22#include "detail/error-encoder.hpp"
23#include "detail/info-encoder.hpp"
24#include "detail/new-renew-revoke-encoder.hpp"
25#include "detail/probe-encoder.hpp"
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080026#include "detail/ca-configuration.hpp"
tylerliud59f2cf2020-10-30 00:00:10 -070027#include "test-common.hpp"
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080028#include "identity-management-fixture.hpp"
tylerliud59f2cf2020-10-30 00:00:10 -070029
30namespace ndn {
31namespace ndncert {
32namespace tests {
33
34BOOST_FIXTURE_TEST_SUITE(TestProtocolEncoding, IdentityManagementTimeFixture)
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");
tylerliuf2e6bb52020-12-13 13:23:05 -080084 auto b = probetlv::encodeDataContent(names, 2, config.redirection);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070085 std::vector<std::pair<Name, int>> retNames;
86 std::vector<Name> redirection;
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080087 probetlv::decodeDataContent(b, retNames, redirection);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070088 BOOST_CHECK_EQUAL(retNames.size(), names.size());
89 auto it1 = retNames.begin();
90 auto it2 = names.begin();
91 for (; it1 != retNames.end() && it2 != names.end(); it1++, it2++) {
92 BOOST_CHECK_EQUAL(it1->first, *it2);
93 BOOST_CHECK_EQUAL(it1->second, 2);
94 }
tylerliuf2e6bb52020-12-13 13:23:05 -080095 BOOST_CHECK_EQUAL(redirection.size(), config.redirection.size());
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070096 auto it3 = redirection.begin();
tylerliuf2e6bb52020-12-13 13:23:05 -080097 auto it4 = config.redirection.begin();
98 for (; it3 != redirection.end() && it4 != config.redirection.end(); it3++, it4++) {
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070099 BOOST_CHECK_EQUAL(*it3, (*it4)->getFullName());
100 }
tylerliud59f2cf2020-10-30 00:00:10 -0700101}
102
103BOOST_AUTO_TEST_CASE(NewRevokeEncodingParam)
104{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700105 requester::ProfileStorage caCache;
106 caCache.load("tests/unit-tests/config-files/config-client-1");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800107 auto& certRequest = caCache.getKnownProfiles().front().cert;
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700108 std::vector<uint8_t> pub = ECDHState().getSelfPubKey();
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800109 auto b = requesttlv::encodeApplicationParameters(RequestType::REVOKE, pub, *certRequest);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700110 std::vector<uint8_t> returnedPub;
111 std::shared_ptr<security::Certificate> returnedCert;
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800112 requesttlv::decodeApplicationParameters(b, RequestType::REVOKE, returnedPub, returnedCert);
tylerliud59f2cf2020-10-30 00:00:10 -0700113
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700114 BOOST_CHECK_EQUAL(returnedPub.size(), pub.size());
115 for (auto it1 = returnedPub.begin(), it2 = pub.begin();
116 it1 != returnedPub.end() && it2 != pub.end(); it1++, it2++) {
117 BOOST_CHECK_EQUAL(*it1, *it2);
118 }
119 BOOST_CHECK_EQUAL(*returnedCert, *certRequest);
tylerliud59f2cf2020-10-30 00:00:10 -0700120}
121
122BOOST_AUTO_TEST_CASE(NewRevokeEncodingData)
123{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700124 std::vector<uint8_t> pub = ECDHState().getSelfPubKey();
Zhiyi Zhang80593022020-11-17 10:55:48 -0800125 std::array<uint8_t, 32> salt = {{101}};
126 RequestId id = {{102}};
tylerliuf2e6bb52020-12-13 13:23:05 -0800127 std::vector<std::string> list;
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700128 list.emplace_back("abc");
129 list.emplace_back("def");
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800130 auto b = requesttlv::encodeDataContent(pub, salt, id, Status::BEFORE_CHALLENGE, list);
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700131 std::vector<uint8_t> returnedPub;
132 std::array<uint8_t, 32> returnedSalt;
133 RequestId returnedId;
134 Status s;
Zhiyi Zhangf22ae242020-11-17 10:51:15 -0800135 auto retlist = requesttlv::decodeDataContent(b, returnedPub, returnedSalt, returnedId, s);
tylerliu2670ba92020-10-30 10:25:03 -0700136 BOOST_CHECK_EQUAL_COLLECTIONS(returnedPub.begin(), returnedPub.end(), pub.begin(), pub.end());
137 BOOST_CHECK_EQUAL_COLLECTIONS(returnedSalt.begin(), returnedSalt.end(), salt.begin(), salt.end());
138 BOOST_CHECK_EQUAL_COLLECTIONS(returnedId.begin(), returnedId.end(), id.begin(), id.end());
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700139 BOOST_CHECK_EQUAL(static_cast<size_t>(s), static_cast<size_t>(Status::BEFORE_CHALLENGE));
tylerliud59f2cf2020-10-30 00:00:10 -0700140}
141
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700142BOOST_AUTO_TEST_CASE(ChallengeEncoding)
143{
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");
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800148 security::Certificate 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
Zhiyi Zhang84e11842020-11-19 20:03:23 -0800162 requester::RequestState context(m_keyChain, caCache.getKnownProfiles().front(), RequestType::NEW);
tylerliuf2e6bb52020-12-13 13:23:05 -0800163 context.requestId = id;
164 std::memcpy(context.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
tylerliuf2e6bb52020-12-13 13:23:05 -0800168 BOOST_CHECK_EQUAL(static_cast<size_t>(context.status), static_cast<size_t>(Status::PENDING));
169 BOOST_CHECK_EQUAL(context.challengeStatus, "test");
170 BOOST_CHECK_EQUAL(context.remainingTries, 3);
171 BOOST_CHECK_EQUAL(context.freshBefore, tp + time::seconds(3600) + time::seconds(10));
172 BOOST_CHECK_EQUAL(context.issuedCertName, "/ndn/ucla/a/b/c");
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700173}
tylerliud59f2cf2020-10-30 00:00:10 -0700174
175BOOST_AUTO_TEST_SUITE_END()
176
tylerliu6563f932020-10-30 11:13:38 -0700177} // namespace tests
178} // namespace ndncert
179} // namespace ndn