blob: ee93eddec331adf7878db9377cbba412c3ce40cf [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
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070030BOOST_AUTO_TEST_CASE(RequestOperations)
31{
Zhiyi Zhangc74f1582020-10-06 16:51:51 -070032 CaSqlite storage(Name(), dbDir.string() + "TestCaSqlite_RequestOperations.db");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070033
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070034 auto identity1 = addIdentity(Name("/ndn/site1"));
35 auto key1 = identity1.getDefaultKey();
36 auto cert1 = key1.getDefaultCertificate();
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070037
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070038 // add operation
tylerliu8704d032020-06-23 10:18:15 -070039 CaState request1(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert1, makeStringBlock(tlv::ContentType_Key, "PretendItIsAKey"));
Zhiyi Zhangc74f1582020-10-06 16:51:51 -070040 storage.addRequest(request1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070041
42 // get operation
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070043 auto result = storage.getRequest("123");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044 BOOST_CHECK_EQUAL(request1.m_cert, result.m_cert);
Zhiyi Zhang48f23782020-09-28 12:11:24 -070045 BOOST_CHECK(request1.m_status == result.m_status);
Suyong Won256c9062020-05-11 02:45:56 -070046 BOOST_CHECK_EQUAL(request1.m_caPrefix, result.m_caPrefix);
tylerliu8e170d62020-09-30 01:31:53 -070047 BOOST_CHECK_EQUAL(request1.m_encryptionKey, result.m_encryptionKey);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070048
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070049 // update operation
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070050 JsonSection json;
51 json.put("test", "4567");
tylerliu8704d032020-06-23 10:18:15 -070052 CaState request2(Name("/ndn/site1"), "123", RequestType::NEW, Status::CHALLENGE, cert1,
53 "email", "test", time::system_clock::now(), 3, time::seconds(3600), std::move(json), makeEmptyBlock(tlv::ContentType_Key));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070054 storage.updateRequest(request2);
55 result = storage.getRequest("123");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070056 BOOST_CHECK_EQUAL(request2.m_cert, result.m_cert);
Zhiyi Zhang48f23782020-09-28 12:11:24 -070057 BOOST_CHECK(request2.m_status == result.m_status);
Suyong Won256c9062020-05-11 02:45:56 -070058 BOOST_CHECK_EQUAL(request2.m_caPrefix, result.m_caPrefix);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070059
60 auto identity2 = addIdentity(Name("/ndn/site2"));
61 auto key2 = identity2.getDefaultKey();
62 auto cert2 = key2.getDefaultCertificate();
tylerliu8704d032020-06-23 10:18:15 -070063 CaState request3(Name("/ndn/site2"), "456", RequestType::NEW, Status::BEFORE_CHALLENGE, cert2, makeStringBlock(tlv::ContentType_Key, "PretendItIsAKey"));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070064 storage.addRequest(request3);
65
66 // list operation
67 auto allRequests = storage.listAllRequests();
68 BOOST_CHECK_EQUAL(allRequests.size(), 2);
69
70 storage.deleteRequest("456");
71 allRequests = storage.listAllRequests();
72 BOOST_CHECK_EQUAL(allRequests.size(), 1);
73
74 storage.deleteRequest("123");
75 allRequests = storage.listAllRequests();
76 BOOST_CHECK_EQUAL(allRequests.size(), 0);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070077}
78
tylerliu63900d52020-09-30 03:01:18 -070079BOOST_AUTO_TEST_CASE(DuplicateAdd)
80{
Zhiyi Zhangc74f1582020-10-06 16:51:51 -070081 CaSqlite storage(Name(), dbDir.string() + "TestCaSqlite_DuplicateAdd.db");
tylerliu63900d52020-09-30 03:01:18 -070082
83 auto identity1 = addIdentity(Name("/ndn/site1"));
84 auto key1 = identity1.getDefaultKey();
85 auto cert1 = key1.getDefaultCertificate();
86
87 // add operation
tylerliu8704d032020-06-23 10:18:15 -070088 CaState request1(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert1, makeEmptyBlock(tlv::ContentType_Key));
tylerliu63900d52020-09-30 03:01:18 -070089 BOOST_CHECK_NO_THROW(storage.addRequest(request1));
90 // add again
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070091 BOOST_CHECK_THROW(storage.addRequest(request1), std::runtime_error);
tylerliu63900d52020-09-30 03:01:18 -070092}
93
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070094BOOST_AUTO_TEST_SUITE_END() // TestCaModule
95
96} // namespace tests
97} // namespace ndncert
98} // namespace ndn