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 | /* |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, 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" |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 22 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 23 | #include "tests/boost-test.hpp" |
| 24 | #include "tests/key-chain-fixture.hpp" |
| 25 | |
| 26 | #include <boost/filesystem/operations.hpp> |
| 27 | #include <boost/filesystem/path.hpp> |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 29 | namespace ndncert::tests { |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 30 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 31 | using namespace ca; |
| 32 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 33 | class DatabaseFixture : public KeyChainFixture |
| 34 | { |
| 35 | public: |
| 36 | DatabaseFixture() |
| 37 | { |
| 38 | boost::filesystem::path parentDir{UNIT_TESTS_TMPDIR}; |
| 39 | dbDir = parentDir / "test-home" / ".ndncert"; |
| 40 | if (!boost::filesystem::exists(dbDir)) { |
| 41 | boost::filesystem::create_directory(dbDir); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | ~DatabaseFixture() |
| 46 | { |
| 47 | boost::filesystem::remove_all(dbDir); |
| 48 | } |
| 49 | |
| 50 | protected: |
| 51 | boost::filesystem::path dbDir; |
| 52 | }; |
| 53 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 54 | BOOST_FIXTURE_TEST_SUITE(TestCaSqlite, DatabaseFixture) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 55 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 56 | BOOST_AUTO_TEST_CASE(RequestOperations) |
| 57 | { |
Zhiyi Zhang | cf21457 | 2020-10-06 16:55:43 -0700 | [diff] [blame] | 58 | CaSqlite storage(Name(), dbDir.string() + "/TestCaSqlite_RequestOperations.db"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 59 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 60 | auto identity1 = m_keyChain.createIdentity(Name("/ndn/site1")); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 61 | auto key1 = identity1.getDefaultKey(); |
| 62 | auto cert1 = key1.getDefaultCertificate(); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 63 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 64 | // add operation |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 65 | RequestId requestId = {{101}}; |
| 66 | RequestState request1; |
| 67 | request1.caPrefix = Name("/ndn/site1"); |
| 68 | request1.requestId = requestId; |
| 69 | request1.requestType = RequestType::NEW; |
| 70 | request1.cert = cert1; |
Zhiyi Zhang | 48f546f | 2020-12-24 17:31:02 -0800 | [diff] [blame] | 71 | request1.encryptionKey = {{102}}; |
| 72 | request1.decryptionIv.assign({1,2,3,4,5,6,7,8,9,10,11,12}); |
| 73 | request1.decryptionIv.assign({2,3,4,5,6,7,8,9,10,11,12,13}); |
Zhiyi Zhang | c74f158 | 2020-10-06 16:51:51 -0700 | [diff] [blame] | 74 | storage.addRequest(request1); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 75 | |
| 76 | // get operation |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 77 | auto result = storage.getRequest(requestId); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(request1.cert, result.cert); |
| 79 | BOOST_CHECK(request1.status == result.status); |
| 80 | BOOST_CHECK_EQUAL(request1.caPrefix, result.caPrefix); |
| 81 | BOOST_CHECK_EQUAL_COLLECTIONS(request1.encryptionKey.begin(), request1.encryptionKey.end(), |
| 82 | result.encryptionKey.begin(), result.encryptionKey.end()); |
Zhiyi Zhang | 48f546f | 2020-12-24 17:31:02 -0800 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL_COLLECTIONS(request1.encryptionIv.begin(), request1.encryptionIv.end(), |
| 84 | result.encryptionIv.begin(), result.encryptionIv.end()); |
| 85 | BOOST_CHECK_EQUAL_COLLECTIONS(request1.decryptionIv.begin(), request1.decryptionIv.end(), |
| 86 | result.decryptionIv.begin(), result.decryptionIv.end()); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 87 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 88 | // update operation |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 89 | RequestState request2; |
| 90 | request2.caPrefix = Name("/ndn/site1"); |
| 91 | request2.requestId = requestId; |
| 92 | request2.requestType = RequestType::NEW; |
| 93 | request2.cert = cert1; |
| 94 | request2.challengeType = "email"; |
| 95 | JsonSection secret; |
| 96 | secret.add("code", "1234"); |
| 97 | request2.challengeState = ChallengeState("test", time::system_clock::now(), 3, |
| 98 | time::seconds(3600), std::move(secret)); |
Zhiyi Zhang | 48f546f | 2020-12-24 17:31:02 -0800 | [diff] [blame] | 99 | request2.decryptionIv.assign({1,2,3,4,5,6,7,8,9,10,11,14}); |
| 100 | request2.decryptionIv.assign({2,3,4,5,6,7,8,9,10,11,12,15}); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 101 | storage.updateRequest(request2); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 102 | result = storage.getRequest(requestId); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(request2.cert, result.cert); |
| 104 | BOOST_CHECK(request2.status == result.status); |
| 105 | BOOST_CHECK_EQUAL(request2.caPrefix, result.caPrefix); |
Zhiyi Zhang | 48f546f | 2020-12-24 17:31:02 -0800 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL_COLLECTIONS(request2.encryptionIv.begin(), request2.encryptionIv.end(), |
| 107 | result.encryptionIv.begin(), result.encryptionIv.end()); |
| 108 | BOOST_CHECK_EQUAL_COLLECTIONS(request2.decryptionIv.begin(), request2.decryptionIv.end(), |
| 109 | result.decryptionIv.begin(), result.decryptionIv.end()); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 110 | |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 111 | // another add operation |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 112 | auto identity2 = m_keyChain.createIdentity(Name("/ndn/site2")); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 113 | auto key2 = identity2.getDefaultKey(); |
| 114 | auto cert2 = key2.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 115 | RequestId requestId2 = {{102}}; |
| 116 | RequestState request3; |
| 117 | request3.caPrefix = Name("/ndn/site2"); |
| 118 | request3.requestId = requestId2; |
| 119 | request3.requestType = RequestType::NEW; |
| 120 | request3.cert = cert2; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 121 | storage.addRequest(request3); |
| 122 | |
| 123 | // list operation |
| 124 | auto allRequests = storage.listAllRequests(); |
| 125 | BOOST_CHECK_EQUAL(allRequests.size(), 2); |
| 126 | |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 127 | storage.deleteRequest(requestId2); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 128 | allRequests = storage.listAllRequests(); |
| 129 | BOOST_CHECK_EQUAL(allRequests.size(), 1); |
| 130 | |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 131 | storage.deleteRequest(requestId); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 132 | allRequests = storage.listAllRequests(); |
| 133 | BOOST_CHECK_EQUAL(allRequests.size(), 0); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 134 | } |
| 135 | |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 136 | BOOST_AUTO_TEST_CASE(DuplicateAdd) |
| 137 | { |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 138 | CaSqlite storage(Name(), dbDir.string() + "/TestCaSqlite_DuplicateAdd.db"); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 139 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 140 | auto identity1 = m_keyChain.createIdentity(Name("/ndn/site1")); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 141 | auto key1 = identity1.getDefaultKey(); |
| 142 | auto cert1 = key1.getDefaultCertificate(); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 143 | |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 144 | // add operation |
| 145 | RequestId requestId = {{101}}; |
| 146 | RequestState request1; |
| 147 | request1.caPrefix = Name("/ndn/site1"); |
| 148 | request1.requestId = requestId; |
| 149 | request1.requestType = RequestType::NEW; |
| 150 | request1.cert = cert1; |
| 151 | BOOST_CHECK_NO_THROW(storage.addRequest(request1)); |
| 152 | |
| 153 | // add again |
| 154 | BOOST_CHECK_THROW(storage.addRequest(request1), std::runtime_error); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 157 | BOOST_AUTO_TEST_SUITE_END() // TestCaSqlite |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 158 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 159 | } // namespace ndncert::tests |