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 | dbd9d43 | 2020-10-07 15:56:27 -0700 | [diff] [blame] | 21 | #include "ca-storage/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 | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 28 | BOOST_FIXTURE_TEST_SUITE(TestCaSqlite, DatabaseFixture) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 29 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 30 | BOOST_AUTO_TEST_CASE(RequestOperations) |
| 31 | { |
Zhiyi Zhang | cf21457 | 2020-10-06 16:55:43 -0700 | [diff] [blame] | 32 | CaSqlite storage(Name(), dbDir.string() + "/TestCaSqlite_RequestOperations.db"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 33 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 34 | auto identity1 = addIdentity(Name("/ndn/site1")); |
| 35 | auto key1 = identity1.getDefaultKey(); |
| 36 | auto cert1 = key1.getDefaultCertificate(); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 37 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 38 | // add operation |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 39 | CaState request1(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert1, makeStringBlock(tlv::ContentType_Key, "PretendItIsAKey")); |
Zhiyi Zhang | c74f158 | 2020-10-06 16:51:51 -0700 | [diff] [blame] | 40 | storage.addRequest(request1); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 41 | |
| 42 | // get operation |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 43 | auto result = storage.getRequest("123"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 44 | BOOST_CHECK_EQUAL(request1.m_cert, result.m_cert); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 45 | BOOST_CHECK(request1.m_status == result.m_status); |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(request1.m_caPrefix, result.m_caPrefix); |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(request1.m_encryptionKey, result.m_encryptionKey); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 48 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 49 | // update operation |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 50 | JsonSection json; |
| 51 | json.put("test", "4567"); |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 52 | 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 Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 54 | storage.updateRequest(request2); |
| 55 | result = storage.getRequest("123"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(request2.m_cert, result.m_cert); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 57 | BOOST_CHECK(request2.m_status == result.m_status); |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(request2.m_caPrefix, result.m_caPrefix); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 59 | |
| 60 | auto identity2 = addIdentity(Name("/ndn/site2")); |
| 61 | auto key2 = identity2.getDefaultKey(); |
| 62 | auto cert2 = key2.getDefaultCertificate(); |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 63 | CaState request3(Name("/ndn/site2"), "456", RequestType::NEW, Status::BEFORE_CHALLENGE, cert2, makeStringBlock(tlv::ContentType_Key, "PretendItIsAKey")); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 64 | 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 Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 77 | } |
| 78 | |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 79 | BOOST_AUTO_TEST_CASE(DuplicateAdd) |
| 80 | { |
Zhiyi Zhang | cf21457 | 2020-10-06 16:55:43 -0700 | [diff] [blame] | 81 | CaSqlite storage(Name(), dbDir.string() + "/TestCaSqlite_DuplicateAdd.db"); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 82 | |
| 83 | auto identity1 = addIdentity(Name("/ndn/site1")); |
| 84 | auto key1 = identity1.getDefaultKey(); |
| 85 | auto cert1 = key1.getDefaultCertificate(); |
| 86 | |
| 87 | // add operation |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 88 | CaState request1(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert1, makeEmptyBlock(tlv::ContentType_Key)); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 89 | BOOST_CHECK_NO_THROW(storage.addRequest(request1)); |
| 90 | // add again |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 91 | BOOST_CHECK_THROW(storage.addRequest(request1), std::runtime_error); |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 94 | BOOST_AUTO_TEST_SUITE_END() // TestCaModule |
| 95 | |
| 96 | } // namespace tests |
| 97 | } // namespace ndncert |
| 98 | } // namespace ndn |