blob: e5229f201830788e3ea969427c530dfd815cb679 [file] [log] [blame]
Zhiyi Zhangae123bf2017-04-14 12:24:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
tylerliu182bc532020-09-25 01:54:45 -07003 * Copyright (c) 2017-2020, 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 Zhang8bd8e5b2020-09-29 17:40:40 -070021#include "ca-storage-detail/ca-memory.hpp"
22#include "ca-storage-detail/ca-sqlite.hpp"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070023#include "test-common.hpp"
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070024
25namespace ndn {
26namespace ndncert {
27namespace tests {
28
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070029BOOST_FIXTURE_TEST_SUITE(TestCaMemory, IdentityManagementFixture)
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070030
31BOOST_AUTO_TEST_CASE(Initialization)
32{
33 BOOST_CHECK_NO_THROW(CaMemory storage);
34}
35
36BOOST_AUTO_TEST_CASE(CertificateOperations)
37{
38 CaMemory storage;
39
40 auto identity1 = addIdentity(Name("/ndn/site1"));
41 auto key1 = identity1.getDefaultKey();
42 auto cert1 = key1.getDefaultCertificate();
43
44 // add operation
45 BOOST_CHECK_NO_THROW(storage.addCertificate("123", cert1));
46
47 // get operation
48 BOOST_CHECK_EQUAL(storage.getCertificate("123"), cert1);
49
50 // list operation
51 auto allCerts = storage.listAllIssuedCertificates();
52 BOOST_CHECK_EQUAL(allCerts.size(), 1);
53
54 auto identity2 = addIdentity(Name("/ndn/site2"));
55 auto key2 = identity2.getDefaultKey();
56 auto cert2 = key2.getDefaultCertificate();
57 storage.addCertificate("456", cert2);
58
59 allCerts = storage.listAllIssuedCertificates();
60 BOOST_CHECK_EQUAL(allCerts.size(), 2);
61
62 BOOST_CHECK_NO_THROW(storage.deleteCertificate("123"));
63
64 allCerts = storage.listAllIssuedCertificates();
65 BOOST_CHECK_EQUAL(allCerts.size(), 1);
66
67 auto identity3 = addIdentity(Name("/ndn/site3"));
68 auto key3 = identity3.getDefaultKey();
69 auto cert3 = key3.getDefaultCertificate();
70
71 // update operation
72 BOOST_CHECK_NO_THROW(storage.updateCertificate("456", cert3));
73 BOOST_CHECK_EQUAL(storage.getCertificate("456"), cert3);
74}
75
76BOOST_AUTO_TEST_CASE(RequestOperations)
77{
78 CaMemory storage;
79
80 auto identity1 = addIdentity(Name("/ndn/site1"));
81 auto key1 = identity1.getDefaultKey();
82 auto cert1 = key1.getDefaultCertificate();
83
84 // add operation
tylerliu8704d032020-06-23 10:18:15 -070085 CaState request1(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert1, makeStringBlock(tlv::ContentType_Key, "PretendItIsAKey"));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070086 BOOST_CHECK_NO_THROW(storage.addRequest(request1));
87
88 // get operation
89 auto result = storage.getRequest("123");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070090 BOOST_CHECK_EQUAL(request1.m_cert, result.m_cert);
Zhiyi Zhang48f23782020-09-28 12:11:24 -070091 BOOST_CHECK(request1.m_status == result.m_status);
Suyong Won256c9062020-05-11 02:45:56 -070092 BOOST_CHECK_EQUAL(request1.m_caPrefix, result.m_caPrefix);
tylerliu8e170d62020-09-30 01:31:53 -070093 BOOST_CHECK_EQUAL(request1.m_encryptionKey, result.m_encryptionKey);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070094
95 JsonSection json;
96 json.put("code", "1234");
97
98 // update operation
tylerliu8704d032020-06-23 10:18:15 -070099 CaState request2(Name("/ndn/site1"), "123", RequestType::NEW, Status::CHALLENGE, cert1,
100 "email", "test", time::system_clock::now(), 3, time::seconds(3600), std::move(json), makeStringBlock(tlv::ContentType_Key, "PretendItIsAKey"));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700101 storage.updateRequest(request2);
102 result = storage.getRequest("123");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700103 BOOST_CHECK_EQUAL(request2.m_cert, result.m_cert);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700104 BOOST_CHECK(request2.m_status == result.m_status);
Suyong Won256c9062020-05-11 02:45:56 -0700105 BOOST_CHECK_EQUAL(request2.m_caPrefix, result.m_caPrefix);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700106
107 auto identity2 = addIdentity(Name("/ndn/site2"));
108 auto key2 = identity2.getDefaultKey();
109 auto cert2 = key2.getDefaultCertificate();
tylerliu8704d032020-06-23 10:18:15 -0700110 CaState request3(Name("/ndn/site2"), "456", RequestType::NEW, Status::BEFORE_CHALLENGE, cert2, makeStringBlock(tlv::ContentType_Key, "PretendItIsAKey"));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700111 storage.addRequest(request3);
112
113 // list operation
114 auto allRequests = storage.listAllRequests();
115 BOOST_CHECK_EQUAL(allRequests.size(), 2);
116
117 storage.deleteRequest("456");
118 allRequests = storage.listAllRequests();
119 BOOST_CHECK_EQUAL(allRequests.size(), 1);
120}
121
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700122BOOST_AUTO_TEST_SUITE_END() // TestCaModule
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700123
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700124} // namespace tests
125} // namespace ndncert
126} // namespace ndn