blob: ee1a2c5f2fca4ad499362487c1069bdc347e3c8e [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 Zhang062be6d2020-10-14 17:13:43 -070021#include "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 Zhang32d4b4e2020-10-28 22:10:49 -070028using namespace ca;
29
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070030BOOST_FIXTURE_TEST_SUITE(TestCaSqlite, DatabaseFixture)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070031
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070032BOOST_AUTO_TEST_CASE(RequestOperations)
33{
Zhiyi Zhangcf214572020-10-06 16:55:43 -070034 CaSqlite storage(Name(), dbDir.string() + "/TestCaSqlite_RequestOperations.db");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070035
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070036 auto identity1 = addIdentity(Name("/ndn/site1"));
37 auto key1 = identity1.getDefaultKey();
38 auto cert1 = key1.getDefaultCertificate();
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070039
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070040 // add operation
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070041 RequestId requestId = {1,2,3,4,5,6,7,8};
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070042 std::array<uint8_t, 16> aesKey1;
43 RequestState request1(Name("/ndn/site1"), requestId, RequestType::NEW, Status::BEFORE_CHALLENGE, cert1, std::move(aesKey1));
Zhiyi Zhangc74f1582020-10-06 16:51:51 -070044 storage.addRequest(request1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070045
46 // get operation
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070047 auto result = storage.getRequest(requestId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048 BOOST_CHECK_EQUAL(request1.m_cert, result.m_cert);
Zhiyi Zhang48f23782020-09-28 12:11:24 -070049 BOOST_CHECK(request1.m_status == result.m_status);
Suyong Won256c9062020-05-11 02:45:56 -070050 BOOST_CHECK_EQUAL(request1.m_caPrefix, result.m_caPrefix);
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070051 BOOST_CHECK_EQUAL_COLLECTIONS(request1.m_encryptionKey.begin(), request1.m_encryptionKey.end(),
52 result.m_encryptionKey.begin(), result.m_encryptionKey.end());
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070053
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070054 // update operation
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055 JsonSection json;
56 json.put("test", "4567");
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070057 std::array<uint8_t, 16> aesKey2;
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070058 RequestState request2(Name("/ndn/site1"), requestId, RequestType::NEW, Status::CHALLENGE, cert1,
Zhiyi Zhang222810b2020-10-16 21:50:35 -070059 "email", "test", time::system_clock::now(), 3, time::seconds(3600),
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070060 std::move(json), std::move(aesKey2), 0);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070061 storage.updateRequest(request2);
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070062 result = storage.getRequest(requestId);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070063 BOOST_CHECK_EQUAL(request2.m_cert, result.m_cert);
Zhiyi Zhang48f23782020-09-28 12:11:24 -070064 BOOST_CHECK(request2.m_status == result.m_status);
Suyong Won256c9062020-05-11 02:45:56 -070065 BOOST_CHECK_EQUAL(request2.m_caPrefix, result.m_caPrefix);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070066
67 auto identity2 = addIdentity(Name("/ndn/site2"));
68 auto key2 = identity2.getDefaultKey();
69 auto cert2 = key2.getDefaultCertificate();
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070070 RequestId requestId2 = {8,7,6,5,4,3,2,1};
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070071 std::array<uint8_t, 16> aesKey3;
72 RequestState request3(Name("/ndn/site2"), requestId2, RequestType::NEW,
73 Status::BEFORE_CHALLENGE, cert2, std::move(aesKey3));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070074 storage.addRequest(request3);
75
76 // list operation
77 auto allRequests = storage.listAllRequests();
78 BOOST_CHECK_EQUAL(allRequests.size(), 2);
79
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070080 storage.deleteRequest(requestId2);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070081 allRequests = storage.listAllRequests();
82 BOOST_CHECK_EQUAL(allRequests.size(), 1);
83
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070084 storage.deleteRequest(requestId);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070085 allRequests = storage.listAllRequests();
86 BOOST_CHECK_EQUAL(allRequests.size(), 0);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070087}
88
tylerliu63900d52020-09-30 03:01:18 -070089BOOST_AUTO_TEST_CASE(DuplicateAdd)
90{
Zhiyi Zhangcf214572020-10-06 16:55:43 -070091 CaSqlite storage(Name(), dbDir.string() + "/TestCaSqlite_DuplicateAdd.db");
tylerliu63900d52020-09-30 03:01:18 -070092
93 auto identity1 = addIdentity(Name("/ndn/site1"));
94 auto key1 = identity1.getDefaultKey();
95 auto cert1 = key1.getDefaultCertificate();
96
97 // add operation
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070098 RequestId requestId = {1,2,3,4,5,6,7,8};
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070099 std::array<uint8_t, 16> aesKey;
100 RequestState request1(Name("/ndn/site1"),requestId, RequestType::NEW,
101 Status::BEFORE_CHALLENGE, cert1, std::move(aesKey));
tylerliu63900d52020-09-30 03:01:18 -0700102 BOOST_CHECK_NO_THROW(storage.addRequest(request1));
103 // add again
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700104 BOOST_CHECK_THROW(storage.addRequest(request1), std::runtime_error);
tylerliu63900d52020-09-30 03:01:18 -0700105}
106
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700107BOOST_AUTO_TEST_SUITE_END() // TestCaModule
108
109} // namespace tests
110} // namespace ndncert
111} // namespace ndn