Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 2 | /* |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library 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 Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "security/validator-config.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 23 | #include "security/v2/certificate-fetcher-offline.hpp" |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 24 | #include "util/dummy-client-face.hpp" |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 25 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 26 | #include "boost-test.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 27 | #include "identity-management-fixture.hpp" |
| 28 | #include "v2/validator-config/common.hpp" |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 29 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 30 | namespace ndn { |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 31 | namespace security { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 32 | namespace tests { |
| 33 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 34 | using namespace ndn::tests; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 35 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 37 | BOOST_FIXTURE_TEST_SUITE(TestValidatorConfig, IdentityManagementFixture) |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 39 | // This test only for API, actual tests are in ValidationPolicyConfig and corresponding CertificateFetchers |
| 40 | |
| 41 | BOOST_AUTO_TEST_CASE(Construct) |
| 42 | { |
| 43 | util::DummyClientFace face; |
| 44 | |
| 45 | ValidatorConfig v1(face); |
| 46 | BOOST_CHECK_EQUAL(v1.m_policyConfig.m_isConfigured, false); |
| 47 | |
| 48 | ValidatorConfig v2(make_unique<v2::CertificateFetcherOffline>()); |
| 49 | BOOST_CHECK_EQUAL(v2.m_policyConfig.m_isConfigured, false); |
| 50 | } |
| 51 | |
| 52 | class ValidatorConfigFixture : public IdentityManagementFixture |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 53 | { |
| 54 | public: |
| 55 | ValidatorConfigFixture() |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 56 | : path(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "security" / "validator-config") |
| 57 | , validator(make_unique<v2::CertificateFetcherOffline>()) |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 58 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 59 | boost::filesystem::create_directories(path); |
| 60 | config = R"CONF( |
| 61 | trust-anchor |
| 62 | { |
| 63 | type any |
| 64 | } |
| 65 | )CONF"; |
| 66 | configFile = (this->path / "config.conf").string(); |
| 67 | std::ofstream f(configFile.c_str()); |
| 68 | f << config; |
| 69 | } |
| 70 | |
| 71 | ~ValidatorConfigFixture() |
| 72 | { |
| 73 | boost::filesystem::remove_all(path); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public: |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 77 | const boost::filesystem::path path; |
| 78 | std::string config; |
| 79 | std::string configFile; |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 80 | ValidatorConfig validator; |
| 81 | }; |
| 82 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 83 | BOOST_FIXTURE_TEST_SUITE(Loads, ValidatorConfigFixture) |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 84 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 85 | BOOST_AUTO_TEST_CASE(FromFile) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 86 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 87 | validator.load(configFile); |
| 88 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame^] | 89 | |
| 90 | // should reload policy |
| 91 | validator.load(configFile); |
| 92 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 95 | BOOST_AUTO_TEST_CASE(FromString) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 96 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 97 | validator.load(config, "config-file-from-string"); |
| 98 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame^] | 99 | |
| 100 | // should reload policy |
| 101 | validator.load(config, "config-file-from-string"); |
| 102 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(FromIstream) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 106 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 107 | std::istringstream is(config); |
| 108 | validator.load(is, "config-file-from-istream"); |
| 109 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame^] | 110 | |
| 111 | // should reload policy |
| 112 | std::istringstream is2(config); |
| 113 | validator.load(is2, "config-file-from-istream"); |
| 114 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 117 | BOOST_AUTO_TEST_CASE(FromSection) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 118 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 119 | validator.load(v2::validator_config::tests::makeSection(config), "config-file-from-section"); |
| 120 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame^] | 121 | |
| 122 | // should reload policy |
| 123 | validator.load(v2::validator_config::tests::makeSection(config), "config-file-from-section"); |
| 124 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 127 | BOOST_AUTO_TEST_SUITE_END() // Loads |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 128 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 129 | BOOST_AUTO_TEST_SUITE_END() // TestValidatorConfig |
| 130 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 131 | |
| 132 | } // namespace tests |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 133 | } // namespace security |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 134 | } // namespace ndn |