blob: 78077db5068e2dd74fd947727deb69710cfa1bc1 [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 Zhang1f5e86e2020-12-04 15:07:57 -080041 RequestId requestId = {{101}};
42 RequestState request1;
43 request1.caPrefix = Name("/ndn/site1");
44 request1.requestId = requestId;
45 request1.requestType = RequestType::NEW;
46 request1.cert = cert1;
Zhiyi Zhang48f546f2020-12-24 17:31:02 -080047 request1.encryptionKey = {{102}};
48 request1.decryptionIv.assign({1,2,3,4,5,6,7,8,9,10,11,12});
49 request1.decryptionIv.assign({2,3,4,5,6,7,8,9,10,11,12,13});
Zhiyi Zhangc74f1582020-10-06 16:51:51 -070050 storage.addRequest(request1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070051
52 // get operation
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070053 auto result = storage.getRequest(requestId);
tylerliu7b9185c2020-11-24 12:15:18 -080054 BOOST_CHECK_EQUAL(request1.cert, result.cert);
55 BOOST_CHECK(request1.status == result.status);
56 BOOST_CHECK_EQUAL(request1.caPrefix, result.caPrefix);
57 BOOST_CHECK_EQUAL_COLLECTIONS(request1.encryptionKey.begin(), request1.encryptionKey.end(),
58 result.encryptionKey.begin(), result.encryptionKey.end());
Zhiyi Zhang48f546f2020-12-24 17:31:02 -080059 BOOST_CHECK_EQUAL_COLLECTIONS(request1.encryptionIv.begin(), request1.encryptionIv.end(),
60 result.encryptionIv.begin(), result.encryptionIv.end());
61 BOOST_CHECK_EQUAL_COLLECTIONS(request1.decryptionIv.begin(), request1.decryptionIv.end(),
62 result.decryptionIv.begin(), result.decryptionIv.end());
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070063
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070064 // update operation
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080065 RequestState request2;
66 request2.caPrefix = Name("/ndn/site1");
67 request2.requestId = requestId;
68 request2.requestType = RequestType::NEW;
69 request2.cert = cert1;
70 request2.challengeType = "email";
71 JsonSection secret;
72 secret.add("code", "1234");
73 request2.challengeState = ChallengeState("test", time::system_clock::now(), 3,
74 time::seconds(3600), std::move(secret));
Zhiyi Zhang48f546f2020-12-24 17:31:02 -080075 request2.decryptionIv.assign({1,2,3,4,5,6,7,8,9,10,11,14});
76 request2.decryptionIv.assign({2,3,4,5,6,7,8,9,10,11,12,15});
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070077 storage.updateRequest(request2);
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070078 result = storage.getRequest(requestId);
tylerliu7b9185c2020-11-24 12:15:18 -080079 BOOST_CHECK_EQUAL(request2.cert, result.cert);
80 BOOST_CHECK(request2.status == result.status);
81 BOOST_CHECK_EQUAL(request2.caPrefix, result.caPrefix);
Zhiyi Zhang48f546f2020-12-24 17:31:02 -080082 BOOST_CHECK_EQUAL_COLLECTIONS(request2.encryptionIv.begin(), request2.encryptionIv.end(),
83 result.encryptionIv.begin(), result.encryptionIv.end());
84 BOOST_CHECK_EQUAL_COLLECTIONS(request2.decryptionIv.begin(), request2.decryptionIv.end(),
85 result.decryptionIv.begin(), result.decryptionIv.end());
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070086
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080087 // another add operation
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070088 auto identity2 = addIdentity(Name("/ndn/site2"));
89 auto key2 = identity2.getDefaultKey();
90 auto cert2 = key2.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080091 RequestId requestId2 = {{102}};
92 RequestState request3;
93 request3.caPrefix = Name("/ndn/site2");
94 request3.requestId = requestId2;
95 request3.requestType = RequestType::NEW;
96 request3.cert = cert2;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070097 storage.addRequest(request3);
98
99 // list operation
100 auto allRequests = storage.listAllRequests();
101 BOOST_CHECK_EQUAL(allRequests.size(), 2);
102
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700103 storage.deleteRequest(requestId2);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700104 allRequests = storage.listAllRequests();
105 BOOST_CHECK_EQUAL(allRequests.size(), 1);
106
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700107 storage.deleteRequest(requestId);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700108 allRequests = storage.listAllRequests();
109 BOOST_CHECK_EQUAL(allRequests.size(), 0);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700110}
111
tylerliu63900d52020-09-30 03:01:18 -0700112BOOST_AUTO_TEST_CASE(DuplicateAdd)
113{
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800114 CaSqlite storage(Name(), dbDir.string() + "/TestCaSqlite_DuplicateAdd.db");
tylerliu63900d52020-09-30 03:01:18 -0700115
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800116 auto identity1 = addIdentity(Name("/ndn/site1"));
117 auto key1 = identity1.getDefaultKey();
118 auto cert1 = key1.getDefaultCertificate();
tylerliu63900d52020-09-30 03:01:18 -0700119
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800120 // add operation
121 RequestId requestId = {{101}};
122 RequestState request1;
123 request1.caPrefix = Name("/ndn/site1");
124 request1.requestId = requestId;
125 request1.requestType = RequestType::NEW;
126 request1.cert = cert1;
127 BOOST_CHECK_NO_THROW(storage.addRequest(request1));
128
129 // add again
130 BOOST_CHECK_THROW(storage.addRequest(request1), std::runtime_error);
tylerliu63900d52020-09-30 03:01:18 -0700131}
132
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700133BOOST_AUTO_TEST_SUITE_END() // TestCaModule
134
135} // namespace tests
136} // namespace ndncert
137} // namespace ndn