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 | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, 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 | |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 34 | namespace ndn::nac::tests { |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 35 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 36 | class DecryptorStaticDataEnvironment : public IoKeyChainFixture |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 37 | { |
| 38 | public: |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 39 | DecryptorStaticDataEnvironment() |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 40 | { |
| 41 | StaticData data; |
| 42 | for (const auto& block : data.managerPackets) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 43 | m_ims.insert(*make_shared<Data>(block)); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 44 | } |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 45 | for (const auto& block : data.encryptorPackets) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 46 | m_ims.insert(*make_shared<Data>(block)); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 49 | auto serveFromIms = [this] (const Name&, const Interest& interest) { |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 50 | auto data = m_ims.find(interest); |
| 51 | if (data != nullptr) { |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 52 | m_imsFace.put(*data); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 53 | } |
| 54 | }; |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 55 | m_imsFace.setInterestFilter("/", serveFromIms, [] (auto...) {}); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 56 | advanceClocks(1_ms, 10); |
| 57 | |
| 58 | // import "/first/user" identity |
| 59 | m_keyChain.importSafeBag(SafeBag(data.userIdentities.at(0)), "password", strlen("password")); |
| 60 | // credentialIdentity = m_keyChain.getPib().getIdentity("/first/user"); |
| 61 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame] | 62 | m_keyChain.createIdentity("/not/authorized"); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 65 | protected: |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 66 | DummyClientFace m_imsFace{m_io, m_keyChain, {false, true}}; |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 67 | |
| 68 | private: |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 69 | InMemoryStoragePersistent m_ims; |
| 70 | }; |
| 71 | |
| 72 | template<class T> |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 73 | class DecryptorFixture : public DecryptorStaticDataEnvironment |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 74 | { |
| 75 | public: |
| 76 | DecryptorFixture() |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 77 | : face(m_io, m_keyChain, {false, true}) |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 78 | , decryptor(m_keyChain.getPib().getIdentity(T().identity).getDefaultKey(), validator, m_keyChain, face) |
| 79 | { |
Davide Pesavento | cab8603 | 2020-12-10 20:30:12 -0500 | [diff] [blame] | 80 | face.linkTo(m_imsFace); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 81 | advanceClocks(1_ms, 10); |
| 82 | } |
| 83 | |
| 84 | public: |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 85 | DummyClientFace face; |
Davide Pesavento | 2e5b7b1 | 2022-09-19 23:30:44 -0400 | [diff] [blame] | 86 | security::ValidatorNull validator; |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 87 | Decryptor decryptor; |
| 88 | }; |
| 89 | |
| 90 | BOOST_AUTO_TEST_SUITE(TestDecryptor) |
| 91 | |
| 92 | struct Valid |
| 93 | { |
| 94 | std::string identity = "/first/user"; |
| 95 | bool expectToSucceed = true; |
| 96 | }; |
| 97 | |
| 98 | struct Invalid |
| 99 | { |
| 100 | std::string identity = "/not/authorized"; |
| 101 | bool expectToSucceed = false; |
| 102 | }; |
| 103 | |
| 104 | using Identities = boost::mpl::vector<Valid, Invalid>; |
| 105 | |
| 106 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(DecryptSuccess, T, Identities, DecryptorFixture<T>) |
| 107 | { |
| 108 | StaticData data; |
| 109 | |
| 110 | size_t nSuccesses = 0; |
| 111 | size_t nFailures = 0; |
| 112 | this->decryptor.decrypt(data.encryptedBlobs.at(0), |
| 113 | [&] (ConstBufferPtr buffer) { |
| 114 | ++nSuccesses; |
| 115 | BOOST_CHECK_EQUAL(buffer->size(), 15); |
Davide Pesavento | 32d1dc2 | 2020-12-09 18:01:47 -0500 | [diff] [blame] | 116 | std::string content(buffer->get<char>(), buffer->size()); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(content, "Data to encrypt"); |
| 118 | }, |
Davide Pesavento | 32d1dc2 | 2020-12-09 18:01:47 -0500 | [diff] [blame] | 119 | [&] (const ErrorCode&, const std::string& msg) { |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 120 | BOOST_TEST_MESSAGE(msg); |
| 121 | ++nFailures; |
| 122 | }); |
| 123 | this->advanceClocks(2_s, 10); |
| 124 | |
| 125 | BOOST_CHECK_EQUAL(nSuccesses, T().expectToSucceed ? 1 : 0); |
| 126 | BOOST_CHECK_EQUAL(nFailures, T().expectToSucceed ? 0 : 1); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_SUITE_END() |
| 130 | |
Davide Pesavento | 5d2f151 | 2023-08-11 14:50:51 -0400 | [diff] [blame^] | 131 | } // namespace ndn::nac::tests |