Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 9062a50 | 2020-01-04 17:14:04 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 4 | * |
| 5 | * NAC library is free software: you can redistribute it and/or modify it under the |
| 6 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 7 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 14 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 15 | * <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * See AUTHORS.md for complete list of NAC library authors and contributors. |
| 18 | */ |
| 19 | |
| 20 | #include "decryptor.hpp" |
| 21 | |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 22 | #include "access-manager.hpp" |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 23 | #include "encrypted-content.hpp" |
| 24 | #include "encryptor.hpp" |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 25 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 27 | #include "tests/io-key-chain-fixture.hpp" |
| 28 | #include "tests/unit/static-data.hpp" |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 29 | |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 30 | #include <boost/mpl/vector.hpp> |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame^] | 31 | #include <ndn-cxx/security/validator-null.hpp> |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
| 35 | namespace nac { |
| 36 | namespace tests { |
| 37 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 38 | class DecryptorStaticDataEnvironment : public IoKeyChainFixture |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 39 | { |
| 40 | public: |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 41 | DecryptorStaticDataEnvironment() |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 42 | { |
| 43 | StaticData data; |
| 44 | for (const auto& block : data.managerPackets) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 45 | m_ims.insert(*make_shared<Data>(block)); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 46 | } |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 47 | for (const auto& block : data.encryptorPackets) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 48 | m_ims.insert(*make_shared<Data>(block)); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 51 | auto serveFromIms = [this] (const Name&, const Interest& interest) { |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 52 | auto data = m_ims.find(interest); |
| 53 | if (data != nullptr) { |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 54 | m_imsFace.put(*data); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 55 | } |
| 56 | }; |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 57 | m_imsFace.setInterestFilter("/", serveFromIms, [] (auto...) {}); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 58 | advanceClocks(1_ms, 10); |
| 59 | |
| 60 | // import "/first/user" identity |
| 61 | m_keyChain.importSafeBag(SafeBag(data.userIdentities.at(0)), "password", strlen("password")); |
| 62 | // credentialIdentity = m_keyChain.getPib().getIdentity("/first/user"); |
| 63 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 64 | m_keyChain.createIdentity("/not/authorized"); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 67 | protected: |
| 68 | util::DummyClientFace m_imsFace{m_io, m_keyChain, {false, true}}; |
| 69 | |
| 70 | private: |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 71 | InMemoryStoragePersistent m_ims; |
| 72 | }; |
| 73 | |
| 74 | template<class T> |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 75 | class DecryptorFixture : public DecryptorStaticDataEnvironment |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 76 | { |
| 77 | public: |
| 78 | DecryptorFixture() |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 79 | : face(m_io, m_keyChain, {false, true}) |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 80 | , decryptor(m_keyChain.getPib().getIdentity(T().identity).getDefaultKey(), validator, m_keyChain, face) |
| 81 | { |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 82 | face.linkTo(m_imsFace); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 83 | advanceClocks(1_ms, 10); |
| 84 | } |
| 85 | |
| 86 | public: |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 87 | util::DummyClientFace face; |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame^] | 88 | security::ValidatorNull validator; |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 89 | Decryptor decryptor; |
| 90 | }; |
| 91 | |
| 92 | BOOST_AUTO_TEST_SUITE(TestDecryptor) |
| 93 | |
| 94 | struct Valid |
| 95 | { |
| 96 | std::string identity = "/first/user"; |
| 97 | bool expectToSucceed = true; |
| 98 | }; |
| 99 | |
| 100 | struct Invalid |
| 101 | { |
| 102 | std::string identity = "/not/authorized"; |
| 103 | bool expectToSucceed = false; |
| 104 | }; |
| 105 | |
| 106 | using Identities = boost::mpl::vector<Valid, Invalid>; |
| 107 | |
| 108 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(DecryptSuccess, T, Identities, DecryptorFixture<T>) |
| 109 | { |
| 110 | StaticData data; |
| 111 | |
| 112 | size_t nSuccesses = 0; |
| 113 | size_t nFailures = 0; |
| 114 | this->decryptor.decrypt(data.encryptedBlobs.at(0), |
| 115 | [&] (ConstBufferPtr buffer) { |
| 116 | ++nSuccesses; |
| 117 | BOOST_CHECK_EQUAL(buffer->size(), 15); |
Davide Pesavento | 32d1dc2 | 2020-12-09 18:01:47 -0500 | [diff] [blame] | 118 | std::string content(buffer->get<char>(), buffer->size()); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 119 | BOOST_CHECK_EQUAL(content, "Data to encrypt"); |
| 120 | }, |
Davide Pesavento | 32d1dc2 | 2020-12-09 18:01:47 -0500 | [diff] [blame] | 121 | [&] (const ErrorCode&, const std::string& msg) { |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 122 | BOOST_TEST_MESSAGE(msg); |
| 123 | ++nFailures; |
| 124 | }); |
| 125 | this->advanceClocks(2_s, 10); |
| 126 | |
| 127 | BOOST_CHECK_EQUAL(nSuccesses, T().expectToSucceed ? 1 : 0); |
| 128 | BOOST_CHECK_EQUAL(nFailures, T().expectToSucceed ? 0 : 1); |
| 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_SUITE_END() |
| 132 | |
| 133 | } // namespace tests |
| 134 | } // namespace nac |
| 135 | } // namespace ndn |