blob: 1a1571320ed3d61fb9c4e8d8edee7877557abc42 [file] [log] [blame]
Zhiyi Zhangae123bf2017-04-14 12:24:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Davide Pesavento0d1d11c2022-04-11 22:11:34 -04003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhangae123bf2017-04-14 12:24:53 -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 Zhang062be6d2020-10-14 17:13:43 -070021#include "detail/ca-memory.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040022
Davide Pesavento829aff62022-05-15 20:30:34 -040023#include "tests/boost-test.hpp"
24#include "tests/key-chain-fixture.hpp"
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070025
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040026namespace ndncert::tests {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070027
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070028using namespace ca;
29
Davide Pesavento829aff62022-05-15 20:30:34 -040030BOOST_FIXTURE_TEST_SUITE(TestCaMemory, KeyChainFixture)
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070031
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070032BOOST_AUTO_TEST_CASE(RequestOperations)
33{
34 CaMemory storage;
35
Davide Pesavento829aff62022-05-15 20:30:34 -040036 auto identity1 = m_keyChain.createIdentity(Name("/ndn/site1"));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070037 auto key1 = identity1.getDefaultKey();
38 auto cert1 = key1.getDefaultCertificate();
39
40 // add operation
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080041 RequestId requestId = {{101}};
42 RequestState request1;
43 request1.caPrefix = Name("/ndn/site1");
44 request1.requestId = requestId;
45 request1.requestType = RequestType::NEW;
46 request1.cert = cert1;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070047 BOOST_CHECK_NO_THROW(storage.addRequest(request1));
48
49 // get operation
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070050 auto result = storage.getRequest(requestId);
tylerliu7b9185c2020-11-24 12:15:18 -080051 BOOST_CHECK_EQUAL(request1.cert, result.cert);
52 BOOST_CHECK(request1.status == result.status);
53 BOOST_CHECK_EQUAL(request1.caPrefix, result.caPrefix);
54 BOOST_CHECK_EQUAL_COLLECTIONS(request1.encryptionKey.begin(), request1.encryptionKey.end(),
55 result.encryptionKey.begin(), result.encryptionKey.end());
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070056
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070057 // update operation
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080058 RequestState request2;
59 request2.caPrefix = Name("/ndn/site1");
60 request2.requestId = requestId;
61 request2.requestType = RequestType::NEW;
62 request2.cert = cert1;
63 request2.challengeType = "email";
64 JsonSection secret;
65 secret.add("code", "1234");
66 request2.challengeState = ChallengeState("test", time::system_clock::now(), 3,
67 time::seconds(3600), std::move(secret));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070068 storage.updateRequest(request2);
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070069 result = storage.getRequest(requestId);
tylerliu7b9185c2020-11-24 12:15:18 -080070 BOOST_CHECK_EQUAL(request2.cert, result.cert);
71 BOOST_CHECK(request2.status == result.status);
72 BOOST_CHECK_EQUAL(request2.caPrefix, result.caPrefix);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070073
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080074 // another add operation
Davide Pesavento829aff62022-05-15 20:30:34 -040075 auto identity2 = m_keyChain.createIdentity(Name("/ndn/site2"));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070076 auto key2 = identity2.getDefaultKey();
77 auto cert2 = key2.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080078 RequestId requestId2 = {{102}};
79 RequestState request3;
80 request3.caPrefix = Name("/ndn/site2");
81 request3.requestId = requestId2;
82 request3.requestType = RequestType::NEW;
83 request3.cert = cert2;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070084 storage.addRequest(request3);
85
86 // list operation
87 auto allRequests = storage.listAllRequests();
88 BOOST_CHECK_EQUAL(allRequests.size(), 2);
89
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070090 storage.deleteRequest(requestId2);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070091 allRequests = storage.listAllRequests();
92 BOOST_CHECK_EQUAL(allRequests.size(), 1);
93}
94
Davide Pesavento0dc02012021-11-23 22:55:03 -050095BOOST_AUTO_TEST_SUITE_END() // TestCaMemory
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070096
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040097} // namespace ndncert::tests