blob: 16dbfe8ff155fc8d84a5d7c160f3497508cf79cf [file] [log] [blame]
Zhiyi Zhang91c846b2017-04-12 14:16:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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 Zhang91c846b2017-04-12 14:16:31 -070021#include "ca-detail/ca-sqlite.hpp"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070022#include "database-fixture.hpp"
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070023#include <iostream>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070024
25namespace ndn {
26namespace ndncert {
27namespace tests {
28
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070029BOOST_FIXTURE_TEST_SUITE(TestCaSqlite, DatabaseFixture)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070030
31BOOST_AUTO_TEST_CASE(Initialization)
32{
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070033 BOOST_CHECK_NO_THROW(CaSqlite storage(dbDir.string()));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070034}
35
36BOOST_AUTO_TEST_CASE(CertificateOperations)
37{
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070038 CaSqlite storage(dbDir.string());
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070039
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070040 auto identity1 = addIdentity(Name("/ndn/site1"));
41 auto key1 = identity1.getDefaultKey();
42 auto cert1 = key1.getDefaultCertificate();
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070043
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070044 // 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);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070074}
75
76BOOST_AUTO_TEST_CASE(RequestOperations)
77{
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070078 CaSqlite storage(dbDir.string());
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070079
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070080 auto identity1 = addIdentity(Name("/ndn/site1"));
81 auto key1 = identity1.getDefaultKey();
82 auto cert1 = key1.getDefaultCertificate();
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070083
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070084 // add operation
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070085 CertificateRequest request1(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert1);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070086 BOOST_CHECK_NO_THROW(storage.addRequest(request1));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070087
88 // get operation
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070089 auto result = storage.getRequest("123");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070090 BOOST_CHECK_EQUAL(request1.m_cert, result.m_cert);
91 BOOST_CHECK_EQUAL(request1.m_status, result.m_status);
92 BOOST_CHECK_EQUAL(request1.m_caName, result.m_caName);
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");
97 CertificateRequest request2(Name("/ndn/site1"), "123", STATUS_CHALLENGE, CHALLENGE_STATUS_SUCCESS,
98 "Email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert1);
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);
102 BOOST_CHECK_EQUAL(request2.m_status, result.m_status);
103 BOOST_CHECK_EQUAL(request2.m_caName, result.m_caName);
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();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108 CertificateRequest request3(Name("/ndn/site2"), "456", STATUS_BEFORE_CHALLENGE, cert2);
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
124BOOST_AUTO_TEST_SUITE_END() // TestCaModule
125
126} // namespace tests
127} // namespace ndncert
128} // namespace ndn