Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 2 | /* |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 4 | * |
| 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 Zhang | 062be6d | 2020-10-14 17:13:43 -0700 | [diff] [blame] | 21 | #include "detail/ca-sqlite.hpp" |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 22 | #include "test-common.hpp" |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndncert { |
| 26 | namespace tests { |
| 27 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 28 | using namespace ca; |
| 29 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 30 | BOOST_FIXTURE_TEST_SUITE(TestCaSqlite, DatabaseFixture) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 31 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 32 | BOOST_AUTO_TEST_CASE(RequestOperations) |
| 33 | { |
Zhiyi Zhang | cf21457 | 2020-10-06 16:55:43 -0700 | [diff] [blame] | 34 | CaSqlite storage(Name(), dbDir.string() + "/TestCaSqlite_RequestOperations.db"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 35 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 36 | auto identity1 = addIdentity(Name("/ndn/site1")); |
| 37 | auto key1 = identity1.getDefaultKey(); |
| 38 | auto cert1 = key1.getDefaultCertificate(); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 39 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 40 | // add operation |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 41 | RequestId requestId = {1,2,3,4,5,6,7,8}; |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame^] | 42 | std::array<uint8_t, 16> aesKey1; |
| 43 | RequestState request1(Name("/ndn/site1"), requestId, RequestType::NEW, Status::BEFORE_CHALLENGE, cert1, std::move(aesKey1)); |
Zhiyi Zhang | c74f158 | 2020-10-06 16:51:51 -0700 | [diff] [blame] | 44 | storage.addRequest(request1); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 45 | |
| 46 | // get operation |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 47 | auto result = storage.getRequest(requestId); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(request1.m_cert, result.m_cert); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 49 | BOOST_CHECK(request1.m_status == result.m_status); |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(request1.m_caPrefix, result.m_caPrefix); |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame^] | 51 | BOOST_CHECK_EQUAL_COLLECTIONS(request1.m_encryptionKey.begin(), request1.m_encryptionKey.end(), |
| 52 | result.m_encryptionKey.begin(), result.m_encryptionKey.end()); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 53 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 54 | // update operation |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 55 | JsonSection json; |
| 56 | json.put("test", "4567"); |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame^] | 57 | std::array<uint8_t, 16> aesKey2; |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 58 | RequestState request2(Name("/ndn/site1"), requestId, RequestType::NEW, Status::CHALLENGE, cert1, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 59 | "email", "test", time::system_clock::now(), 3, time::seconds(3600), |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame^] | 60 | std::move(json), std::move(aesKey2), 0); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 61 | storage.updateRequest(request2); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 62 | result = storage.getRequest(requestId); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(request2.m_cert, result.m_cert); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 64 | BOOST_CHECK(request2.m_status == result.m_status); |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(request2.m_caPrefix, result.m_caPrefix); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 66 | |
| 67 | auto identity2 = addIdentity(Name("/ndn/site2")); |
| 68 | auto key2 = identity2.getDefaultKey(); |
| 69 | auto cert2 = key2.getDefaultCertificate(); |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 70 | RequestId requestId2 = {8,7,6,5,4,3,2,1}; |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame^] | 71 | std::array<uint8_t, 16> aesKey3; |
| 72 | RequestState request3(Name("/ndn/site2"), requestId2, RequestType::NEW, |
| 73 | Status::BEFORE_CHALLENGE, cert2, std::move(aesKey3)); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 74 | storage.addRequest(request3); |
| 75 | |
| 76 | // list operation |
| 77 | auto allRequests = storage.listAllRequests(); |
| 78 | BOOST_CHECK_EQUAL(allRequests.size(), 2); |
| 79 | |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 80 | storage.deleteRequest(requestId2); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 81 | allRequests = storage.listAllRequests(); |
| 82 | BOOST_CHECK_EQUAL(allRequests.size(), 1); |
| 83 | |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 84 | storage.deleteRequest(requestId); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 85 | allRequests = storage.listAllRequests(); |
| 86 | BOOST_CHECK_EQUAL(allRequests.size(), 0); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 87 | } |
| 88 | |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(DuplicateAdd) |
| 90 | { |
Zhiyi Zhang | cf21457 | 2020-10-06 16:55:43 -0700 | [diff] [blame] | 91 | CaSqlite storage(Name(), dbDir.string() + "/TestCaSqlite_DuplicateAdd.db"); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 92 | |
| 93 | auto identity1 = addIdentity(Name("/ndn/site1")); |
| 94 | auto key1 = identity1.getDefaultKey(); |
| 95 | auto cert1 = key1.getDefaultCertificate(); |
| 96 | |
| 97 | // add operation |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 98 | RequestId requestId = {1,2,3,4,5,6,7,8}; |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame^] | 99 | std::array<uint8_t, 16> aesKey; |
| 100 | RequestState request1(Name("/ndn/site1"),requestId, RequestType::NEW, |
| 101 | Status::BEFORE_CHALLENGE, cert1, std::move(aesKey)); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 102 | BOOST_CHECK_NO_THROW(storage.addRequest(request1)); |
| 103 | // add again |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 104 | BOOST_CHECK_THROW(storage.addRequest(request1), std::runtime_error); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 107 | BOOST_AUTO_TEST_SUITE_END() // TestCaModule |
| 108 | |
| 109 | } // namespace tests |
| 110 | } // namespace ndncert |
| 111 | } // namespace ndn |