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