blob: 63821943d01d1d40fc319ef32b2a2865bf21b7c6 [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"
tylerliud59f2cf2020-10-30 00:00:10 -070026#include "test-common.hpp"
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070027#include <configuration.hpp>
28#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");
43 auto& cert = caCache.m_caItems.front().m_cert;
tylerliud59f2cf2020-10-30 00:00:10 -070044
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070045 auto b = InfoEncoder::encodeDataContent(config.m_caItem, *cert);
46 auto item = InfoEncoder::decodeDataContent(b);
tylerliud59f2cf2020-10-30 00:00:10 -070047
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070048 BOOST_CHECK_EQUAL(*item.m_cert, *cert);
49 BOOST_CHECK_EQUAL(item.m_caInfo, config.m_caItem.m_caInfo);
50 BOOST_CHECK_EQUAL(item.m_caPrefix, config.m_caItem.m_caPrefix);
tylerliu2670ba92020-10-30 10:25:03 -070051 BOOST_CHECK_EQUAL_COLLECTIONS(item.m_probeParameterKeys.begin(), item.m_probeParameterKeys.end(),
52 config.m_caItem.m_probeParameterKeys.begin(), config.m_caItem.m_probeParameterKeys.end())
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070053 BOOST_CHECK_EQUAL(item.m_maxValidityPeriod, config.m_caItem.m_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";
59 auto b = ErrorEncoder::encodeDataContent(ErrorCode::NAME_NOT_ALLOWED, msg);
60 auto item = ErrorEncoder::decodefromDataContent(b);
61 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{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070067 std::vector<std::tuple<std::string, std::string>> parameters;
68 parameters.emplace_back("key1", "value1");
69 parameters.emplace_back("key2", "value2");
70 auto appParam = ProbeEncoder::encodeApplicationParameters(std::move(parameters));
71 auto param1 = ProbeEncoder::decodeApplicationParameters(appParam);
72 BOOST_CHECK_EQUAL(parameters.size(), param1.size());
73 BOOST_CHECK_EQUAL(std::get<0>(parameters[0]), std::get<0>(param1[0]));
74 BOOST_CHECK_EQUAL(std::get<1>(parameters[0]), std::get<1>(param1[0]));
75 BOOST_CHECK_EQUAL(std::get<0>(parameters[1]), std::get<0>(param1[1]));
76 BOOST_CHECK_EQUAL(std::get<1>(parameters[1]), std::get<1>(param1[1]));
tylerliud59f2cf2020-10-30 00:00:10 -070077}
78
79BOOST_AUTO_TEST_CASE(ProbeEncodingData)
80{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -070081 ca::CaConfig config;
82 config.load("tests/unit-tests/config-files/config-ca-5");
83 std::vector<Name> names;
84 names.emplace_back("/ndn/1");
85 names.emplace_back("/ndn/2");
86 auto b = ProbeEncoder::encodeDataContent(names, 2, config.m_redirection);
87 std::vector<std::pair<Name, int>> retNames;
88 std::vector<Name> redirection;
89 ProbeEncoder::decodeDataContent(b, retNames, redirection);
90 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 }
97 BOOST_CHECK_EQUAL(redirection.size(), config.m_redirection->size());
98 auto it3 = redirection.begin();
99 auto it4 = config.m_redirection->begin();
100 for (; it3 != redirection.end() && it4 != config.m_redirection->end(); it3++, it4++) {
101 BOOST_CHECK_EQUAL(*it3, (*it4)->getFullName());
102 }
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");
109 auto& certRequest = caCache.m_caItems.front().m_cert;
110 std::vector<uint8_t> pub = ECDHState().getSelfPubKey();
111 auto b = NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::REVOKE, pub, *certRequest);
112 std::vector<uint8_t> returnedPub;
113 std::shared_ptr<security::Certificate> returnedCert;
114 NewRenewRevokeEncoder::decodeApplicationParameters(b, RequestType::REVOKE, returnedPub, returnedCert);
tylerliud59f2cf2020-10-30 00:00:10 -0700115
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700116 BOOST_CHECK_EQUAL(returnedPub.size(), pub.size());
117 for (auto it1 = returnedPub.begin(), it2 = pub.begin();
118 it1 != returnedPub.end() && it2 != pub.end(); it1++, it2++) {
119 BOOST_CHECK_EQUAL(*it1, *it2);
120 }
121 BOOST_CHECK_EQUAL(*returnedCert, *certRequest);
tylerliud59f2cf2020-10-30 00:00:10 -0700122}
123
124BOOST_AUTO_TEST_CASE(NewRevokeEncodingData)
125{
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700126 std::vector<uint8_t> pub = ECDHState().getSelfPubKey();
127 std::array<uint8_t, 32> salt = {101};
128 RequestId id = {102};
129 std::list<std::string> list;
130 list.emplace_back("abc");
131 list.emplace_back("def");
132 auto b = NewRenewRevokeEncoder::encodeDataContent(pub, salt, id, Status::BEFORE_CHALLENGE, list);
133 std::vector<uint8_t> returnedPub;
134 std::array<uint8_t, 32> returnedSalt;
135 RequestId returnedId;
136 Status s;
137 auto retlist = NewRenewRevokeEncoder::decodeDataContent(b, returnedPub, returnedSalt, returnedId, s);
tylerliu2670ba92020-10-30 10:25:03 -0700138 BOOST_CHECK_EQUAL_COLLECTIONS(returnedPub.begin(), returnedPub.end(), pub.begin(), pub.end());
139 BOOST_CHECK_EQUAL_COLLECTIONS(returnedSalt.begin(), returnedSalt.end(), salt.begin(), salt.end());
140 BOOST_CHECK_EQUAL_COLLECTIONS(returnedId.begin(), returnedId.end(), id.begin(), id.end());
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700141 BOOST_CHECK_EQUAL(static_cast<size_t>(s), static_cast<size_t>(Status::BEFORE_CHALLENGE));
tylerliud59f2cf2020-10-30 00:00:10 -0700142}
143
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700144BOOST_AUTO_TEST_CASE(ChallengeEncoding)
145{
146 const uint8_t key[] = {0x23, 0x70, 0xe3, 0x20, 0xd4, 0x34, 0x42, 0x08,
147 0xe0, 0xff, 0x56, 0x83, 0xf2, 0x43, 0xb2, 0x13};
148 time::system_clock::TimePoint t = time::system_clock::now();
149 requester::ProfileStorage caCache;
150 caCache.load("tests/unit-tests/config-files/config-client-1");
151 security::Certificate certRequest = *caCache.m_caItems.front().m_cert;
152 RequestId id = {102};
153 std::array<uint8_t, 16> aesKey;
154 std::memcpy(aesKey.data(), key, sizeof(key));
155 ca::RequestState state(Name("/ndn/ucla"), id, RequestType::NEW, Status::PENDING,
156 certRequest, "pin", "test", t, 3, time::seconds(321), JsonSection(),
157 std::move(aesKey), 0);
158 auto b = ChallengeEncoder::encodeDataContent(state, Name("/ndn/ucla/a/b/c"));
tylerliud59f2cf2020-10-30 00:00:10 -0700159
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700160 requester::RequestContext context(m_keyChain, caCache.m_caItems.front(), RequestType::NEW);
161 std::memcpy(context.m_aesKey.data(), key, sizeof(key));
162 ChallengeEncoder::decodeDataContent(b, context);
tylerliud59f2cf2020-10-30 00:00:10 -0700163
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700164 BOOST_CHECK_EQUAL(static_cast<size_t>(context.m_status), static_cast<size_t>(Status::PENDING));
165 BOOST_CHECK_EQUAL(context.m_challengeStatus, "test");
166 BOOST_CHECK_EQUAL(context.m_remainingTries, 3);
167 BOOST_ASSERT(context.m_freshBefore > time::system_clock::now() + time::seconds(321) - time::milliseconds(100));
168 BOOST_ASSERT(context.m_freshBefore < time::system_clock::now() + time::seconds(321) + time::milliseconds(100));
169 BOOST_CHECK_EQUAL(context.m_issuedCertName, "/ndn/ucla/a/b/c");
170}
tylerliud59f2cf2020-10-30 00:00:10 -0700171
172BOOST_AUTO_TEST_SUITE_END()
173
Zhiyi Zhang4c259db2020-10-30 09:36:01 -0700174} // namespace tests
175} // namespace ndncert
176} // namespace ndn