Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, Regents of the University of California. |
| 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 | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 21 | #include "database-fixture.hpp" |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 22 | #include "ca-detail/ca-sqlite.hpp" |
| 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 | |
| 30 | BOOST_AUTO_TEST_CASE(Initialization) |
| 31 | { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 32 | BOOST_CHECK_NO_THROW(CaSqlite storage(dbDir.string())); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | BOOST_AUTO_TEST_CASE(CertificateOperations) |
| 36 | { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 37 | CaSqlite storage(dbDir.string()); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 38 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 39 | auto identity1 = addIdentity(Name("/ndn/site1")); |
| 40 | auto key1 = identity1.getDefaultKey(); |
| 41 | auto cert1 = key1.getDefaultCertificate(); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 42 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 43 | // add operation |
| 44 | BOOST_CHECK_NO_THROW(storage.addCertificate("123", cert1)); |
| 45 | |
| 46 | // get operation |
| 47 | BOOST_CHECK_EQUAL(storage.getCertificate("123"), cert1); |
| 48 | |
| 49 | // list operation |
| 50 | auto allCerts = storage.listAllIssuedCertificates(); |
| 51 | BOOST_CHECK_EQUAL(allCerts.size(), 1); |
| 52 | |
| 53 | auto identity2 = addIdentity(Name("/ndn/site2")); |
| 54 | auto key2 = identity2.getDefaultKey(); |
| 55 | auto cert2 = key2.getDefaultCertificate(); |
| 56 | storage.addCertificate("456", cert2); |
| 57 | |
| 58 | allCerts = storage.listAllIssuedCertificates(); |
| 59 | BOOST_CHECK_EQUAL(allCerts.size(), 2); |
| 60 | |
| 61 | BOOST_CHECK_NO_THROW(storage.deleteCertificate("123")); |
| 62 | |
| 63 | allCerts = storage.listAllIssuedCertificates(); |
| 64 | BOOST_CHECK_EQUAL(allCerts.size(), 1); |
| 65 | |
| 66 | auto identity3 = addIdentity(Name("/ndn/site3")); |
| 67 | auto key3 = identity3.getDefaultKey(); |
| 68 | auto cert3 = key3.getDefaultCertificate(); |
| 69 | |
| 70 | // update operation |
| 71 | BOOST_CHECK_NO_THROW(storage.updateCertificate("456", cert3)); |
| 72 | BOOST_CHECK_EQUAL(storage.getCertificate("456"), cert3); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | BOOST_AUTO_TEST_CASE(RequestOperations) |
| 76 | { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 77 | CaSqlite storage(dbDir.string()); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 78 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 79 | auto identity1 = addIdentity(Name("/ndn/site1")); |
| 80 | auto key1 = identity1.getDefaultKey(); |
| 81 | auto cert1 = key1.getDefaultCertificate(); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 82 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 83 | // add operation |
| 84 | CertificateRequest request1(Name("/ndn/site1"), "123", cert1); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 85 | BOOST_CHECK_NO_THROW(storage.addRequest(request1)); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 86 | |
| 87 | // get operation |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 88 | auto result = storage.getRequest("123"); |
| 89 | BOOST_CHECK_EQUAL(request1.getCert(), result.getCert()); |
| 90 | BOOST_CHECK_EQUAL(request1.getStatus(), result.getStatus()); |
| 91 | BOOST_CHECK_EQUAL(request1.getCaName(), result.getCaName()); |
| 92 | |
| 93 | JsonSection json; |
| 94 | json.put("code", "1234"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 95 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 96 | // update operation |
| 97 | CertificateRequest request2(Name("/ndn/site1"), "123", "need-verify", "EMAIL", |
| 98 | CaSqlite::convertJson2String(json), cert1); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 99 | storage.updateRequest(request2); |
| 100 | result = storage.getRequest("123"); |
| 101 | BOOST_CHECK_EQUAL(request2.getCert(), result.getCert()); |
| 102 | BOOST_CHECK_EQUAL(request2.getStatus(), result.getStatus()); |
| 103 | BOOST_CHECK_EQUAL(request2.getCaName(), result.getCaName()); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 104 | |
| 105 | auto identity2 = addIdentity(Name("/ndn/site2")); |
| 106 | auto key2 = identity2.getDefaultKey(); |
| 107 | auto cert2 = key2.getDefaultCertificate(); |
| 108 | CertificateRequest request3(Name("/ndn/site2"), "456", cert2); |
| 109 | storage.addRequest(request3); |
| 110 | |
| 111 | // list operation |
| 112 | auto allRequests = storage.listAllRequests(); |
| 113 | BOOST_CHECK_EQUAL(allRequests.size(), 2); |
| 114 | |
| 115 | storage.deleteRequest("456"); |
| 116 | allRequests = storage.listAllRequests(); |
| 117 | BOOST_CHECK_EQUAL(allRequests.size(), 1); |
| 118 | |
| 119 | storage.deleteRequest("123"); |
| 120 | allRequests = storage.listAllRequests(); |
| 121 | BOOST_CHECK_EQUAL(allRequests.size(), 0); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | BOOST_AUTO_TEST_SUITE_END() // TestCaModule |
| 125 | |
| 126 | } // namespace tests |
| 127 | } // namespace ndncert |
| 128 | } // namespace ndn |