blob: 8e0be7e4927bde9a0190e6e66af56927004905a6 [file] [log] [blame]
Zhiyi Zhang91c846b2017-04-12 14:16:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento914d05f2019-07-13 16:20:19 -04002/*
tylerliu182bc532020-09-25 01:54:45 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang91c846b2017-04-12 14:16:31 -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-sqlite.hpp"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070022#include "test-common.hpp"
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070023
24namespace ndn {
25namespace ndncert {
26namespace tests {
27
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070028BOOST_FIXTURE_TEST_SUITE(TestCaSqlite, DatabaseFixture)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070029
30BOOST_AUTO_TEST_CASE(Initialization)
31{
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070032 BOOST_CHECK_NO_THROW(CaSqlite storage(dbDir.string()));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070033}
34
35BOOST_AUTO_TEST_CASE(CertificateOperations)
36{
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070037 CaSqlite storage(dbDir.string());
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070038
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070039 auto identity1 = addIdentity(Name("/ndn/site1"));
40 auto key1 = identity1.getDefaultKey();
41 auto cert1 = key1.getDefaultCertificate();
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070042
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070043 // add operation
44 BOOST_CHECK_NO_THROW(storage.addCertificate("123", cert1));
45
46 // get operation
47 BOOST_CHECK_EQUAL(storage.getCertificate("123"), cert1);
48
49 // list operation
50 auto allCerts = storage.listAllIssuedCertificates();
51 BOOST_CHECK_EQUAL(allCerts.size(), 1);
52
53 auto identity2 = addIdentity(Name("/ndn/site2"));
54 auto key2 = identity2.getDefaultKey();
55 auto cert2 = key2.getDefaultCertificate();
56 storage.addCertificate("456", cert2);
57
58 allCerts = storage.listAllIssuedCertificates();
59 BOOST_CHECK_EQUAL(allCerts.size(), 2);
60
61 BOOST_CHECK_NO_THROW(storage.deleteCertificate("123"));
62
63 allCerts = storage.listAllIssuedCertificates();
64 BOOST_CHECK_EQUAL(allCerts.size(), 1);
65
66 auto identity3 = addIdentity(Name("/ndn/site3"));
67 auto key3 = identity3.getDefaultKey();
68 auto cert3 = key3.getDefaultCertificate();
69
70 // update operation
71 BOOST_CHECK_NO_THROW(storage.updateCertificate("456", cert3));
72 BOOST_CHECK_EQUAL(storage.getCertificate("456"), cert3);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070073}
74
75BOOST_AUTO_TEST_CASE(RequestOperations)
76{
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070077 CaSqlite storage(dbDir.string());
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070078
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070079 auto identity1 = addIdentity(Name("/ndn/site1"));
80 auto key1 = identity1.getDefaultKey();
81 auto cert1 = key1.getDefaultCertificate();
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070082
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070083 // add operation
tylerliu8e170d62020-09-30 01:31:53 -070084 RequestState request1(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert1, makeStringBlock(tlv::ContentType_Key, "PretendItIsAKey"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070085 BOOST_CHECK_NO_THROW(storage.addRequest(request1));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070086
87 // get operation
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070088 auto result = storage.getRequest("123");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070089 BOOST_CHECK_EQUAL(request1.m_cert, result.m_cert);
Zhiyi Zhang48f23782020-09-28 12:11:24 -070090 BOOST_CHECK(request1.m_status == result.m_status);
Suyong Won256c9062020-05-11 02:45:56 -070091 BOOST_CHECK_EQUAL(request1.m_caPrefix, result.m_caPrefix);
tylerliu8e170d62020-09-30 01:31:53 -070092 BOOST_CHECK_EQUAL(request1.m_encryptionKey, result.m_encryptionKey);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070093
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070094 // update operation
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070095 JsonSection json;
96 json.put("test", "4567");
Zhiyi Zhange232a742020-09-29 17:34:17 -070097 RequestState request2(Name("/ndn/site1"), "123", RequestType::NEW, Status::CHALLENGE, cert1,
tylerliu8e170d62020-09-30 01:31:53 -070098 "email", "test", time::system_clock::now(), 3, time::seconds(3600), std::move(json), makeEmptyBlock(tlv::ContentType_Key));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070099 storage.updateRequest(request2);
100 result = storage.getRequest("123");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700101 BOOST_CHECK_EQUAL(request2.m_cert, result.m_cert);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700102 BOOST_CHECK(request2.m_status == result.m_status);
Suyong Won256c9062020-05-11 02:45:56 -0700103 BOOST_CHECK_EQUAL(request2.m_caPrefix, result.m_caPrefix);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700104
105 auto identity2 = addIdentity(Name("/ndn/site2"));
106 auto key2 = identity2.getDefaultKey();
107 auto cert2 = key2.getDefaultCertificate();
tylerliu8e170d62020-09-30 01:31:53 -0700108 RequestState request3(Name("/ndn/site2"), "456", RequestType::NEW, Status::BEFORE_CHALLENGE, cert2, makeStringBlock(tlv::ContentType_Key, "PretendItIsAKey"));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700109 storage.addRequest(request3);
110
111 // list operation
112 auto allRequests = storage.listAllRequests();
113 BOOST_CHECK_EQUAL(allRequests.size(), 2);
114
115 storage.deleteRequest("456");
116 allRequests = storage.listAllRequests();
117 BOOST_CHECK_EQUAL(allRequests.size(), 1);
118
119 storage.deleteRequest("123");
120 allRequests = storage.listAllRequests();
121 BOOST_CHECK_EQUAL(allRequests.size(), 0);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700122}
123
tylerliu63900d52020-09-30 03:01:18 -0700124BOOST_AUTO_TEST_CASE(DuplicateAdd)
125{
126 CaSqlite storage(dbDir.string());
127
128 auto identity1 = addIdentity(Name("/ndn/site1"));
129 auto key1 = identity1.getDefaultKey();
130 auto cert1 = key1.getDefaultCertificate();
131
132 // add operation
133 RequestState request1(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert1, makeEmptyBlock(tlv::ContentType_Key));
134 BOOST_CHECK_NO_THROW(storage.addRequest(request1));
135 // add again
136 BOOST_CHECK_THROW(storage.addRequest(request1), std::exception);
137}
138
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700139BOOST_AUTO_TEST_SUITE_END() // TestCaModule
140
141} // namespace tests
142} // namespace ndncert
143} // namespace ndn