Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 4 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 11 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 15 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 18 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 19 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "tools/pib/pib-validator.hpp" |
| 23 | #include "tools/pib/encoding/update-param.hpp" |
| 24 | #include "tools/pib/encoding/delete-param.hpp" |
| 25 | #include <ndn-cxx/security/key-chain.hpp> |
| 26 | |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 27 | #include "../identity-management-time-fixture.hpp" |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 28 | #include <boost/filesystem.hpp> |
| 29 | #include "tests/test-common.hpp" |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace pib { |
| 33 | namespace tests { |
| 34 | |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 35 | class PibValidatorFixture : public ndn::tests::IdentityManagementTimeFixture |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 36 | { |
| 37 | public: |
| 38 | PibValidatorFixture() |
| 39 | : tmpPath(boost::filesystem::path(TMP_TESTS_PATH) / "DbTest") |
| 40 | , db(tmpPath.c_str()) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | ~PibValidatorFixture() |
| 45 | { |
| 46 | boost::filesystem::remove_all(tmpPath); |
| 47 | } |
| 48 | |
| 49 | boost::asio::io_service io; |
| 50 | boost::filesystem::path tmpPath; |
| 51 | PibDb db; |
| 52 | bool isProcessed; |
| 53 | }; |
| 54 | |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 55 | BOOST_FIXTURE_TEST_SUITE(PibPibValidator, PibValidatorFixture) |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 56 | |
| 57 | BOOST_AUTO_TEST_CASE(Basic) |
| 58 | { |
| 59 | PibValidator validator(db); |
| 60 | |
| 61 | Name testUser("/localhost/pib/test/mgmt"); |
| 62 | BOOST_REQUIRE(addIdentity(testUser, RsaKeyParams())); |
| 63 | Name testUserCertName = m_keyChain.getDefaultCertificateNameForIdentity(testUser); |
| 64 | shared_ptr<IdentityCertificate> testUserCert = m_keyChain.getCertificate(testUserCertName); |
| 65 | |
| 66 | advanceClocks(io, time::milliseconds(100)); |
| 67 | Name testUser2("/localhost/pib/test2/mgmt"); |
| 68 | BOOST_REQUIRE(addIdentity(testUser2, RsaKeyParams())); |
| 69 | |
| 70 | db.updateMgmtCertificate(*testUserCert); |
| 71 | |
| 72 | advanceClocks(io, time::milliseconds(100)); |
| 73 | Name normalId("/normal/id"); |
| 74 | BOOST_REQUIRE(addIdentity(normalId, RsaKeyParams())); |
| 75 | Name normalIdCertName = m_keyChain.getDefaultCertificateNameForIdentity(normalId); |
| 76 | shared_ptr<IdentityCertificate> normalIdCert = m_keyChain.getCertificate(normalIdCertName); |
| 77 | |
| 78 | db.addIdentity(normalId); |
| 79 | db.addKey(normalIdCert->getPublicKeyName(), normalIdCert->getPublicKeyInfo()); |
| 80 | db.addCertificate(*normalIdCert); |
| 81 | |
| 82 | Name command1("/localhost/pib/test/verb/param"); |
| 83 | shared_ptr<Interest> interest1 = make_shared<Interest>(command1); |
| 84 | m_keyChain.signByIdentity(*interest1, testUser); |
| 85 | // "test" user is trusted for any command about itself, OK. |
| 86 | isProcessed = false; |
| 87 | validator.validate(*interest1, |
| 88 | [this] (const shared_ptr<const Interest>&) { |
| 89 | isProcessed = true; |
| 90 | BOOST_CHECK(true); |
| 91 | }, |
| 92 | [this] (const shared_ptr<const Interest>&, const std::string&) { |
| 93 | isProcessed = true; |
| 94 | BOOST_CHECK(false); |
| 95 | }); |
| 96 | BOOST_CHECK(isProcessed); |
| 97 | |
| 98 | Name command2("/localhost/pib/test/verb/param"); |
| 99 | shared_ptr<Interest> interest2 = make_shared<Interest>(command2); |
| 100 | m_keyChain.signByIdentity(*interest2, testUser2); |
| 101 | // "test2" user is NOT trusted for any command about other user, MUST fail |
| 102 | isProcessed = false; |
| 103 | validator.validate(*interest2, |
| 104 | [this] (const shared_ptr<const Interest>&) { |
| 105 | isProcessed = true; |
| 106 | BOOST_CHECK(false); |
| 107 | }, |
| 108 | [this] (const shared_ptr<const Interest>&, const std::string&) { |
| 109 | isProcessed = true; |
| 110 | BOOST_CHECK(true); |
| 111 | }); |
| 112 | BOOST_CHECK(isProcessed); |
| 113 | |
| 114 | Name command3("/localhost/pib/test/verb/param"); |
| 115 | shared_ptr<Interest> interest3 = make_shared<Interest>(command3); |
| 116 | m_keyChain.signByIdentity(*interest3, normalId); |
| 117 | // "normalId" is in "test" pib, can be trusted for some commands about "test". |
| 118 | // Detail checking is needed, but it is not the job of Validator, OK. |
| 119 | isProcessed = false; |
| 120 | validator.validate(*interest3, |
| 121 | [this] (const shared_ptr<const Interest>&) { |
| 122 | isProcessed = true; |
| 123 | BOOST_CHECK(true); |
| 124 | }, |
| 125 | [this] (const shared_ptr<const Interest>&, const std::string&) { |
| 126 | isProcessed = true; |
| 127 | BOOST_CHECK(false); |
| 128 | }); |
| 129 | BOOST_CHECK(isProcessed); |
| 130 | |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() |
| 134 | |
| 135 | } // namespace tests |
| 136 | } // namespace pib |
| 137 | } // namespace ndn |