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 | /* |
| 3 | * Copyright (c) 2014-2020, 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 | |
| 22 | #include "encryptor.hpp" |
| 23 | #include "encrypted-content.hpp" |
| 24 | #include "access-manager.hpp" |
| 25 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 26 | #include "tests/boost-test.hpp" |
| 27 | #include "tests/dummy-forwarder.hpp" |
| 28 | #include "tests/io-key-chain-fixture.hpp" |
| 29 | #include "tests/unit/static-data.hpp" |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 30 | |
| 31 | #include <iostream> |
| 32 | #include <boost/mpl/vector.hpp> |
| 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 | : fw(m_io, m_keyChain) |
| 43 | , imsFace(static_cast<util::DummyClientFace&>(fw.addFace())) |
| 44 | { |
| 45 | StaticData data; |
| 46 | for (const auto& block : data.managerPackets) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 47 | m_ims.insert(*make_shared<Data>(block)); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | for (const auto& block : data.encryptorPackets) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 51 | m_ims.insert(*make_shared<Data>(block)); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | auto serveFromIms = [this] (const Name& prefix, const Interest& interest) { |
| 55 | auto data = m_ims.find(interest); |
| 56 | if (data != nullptr) { |
| 57 | imsFace.put(*data); |
| 58 | } |
| 59 | }; |
| 60 | imsFace.setInterestFilter("/", serveFromIms, [] (auto...) {}); |
| 61 | advanceClocks(1_ms, 10); |
| 62 | |
| 63 | // import "/first/user" identity |
| 64 | m_keyChain.importSafeBag(SafeBag(data.userIdentities.at(0)), "password", strlen("password")); |
| 65 | // credentialIdentity = m_keyChain.getPib().getIdentity("/first/user"); |
| 66 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 67 | m_keyChain.createIdentity("/not/authorized"); |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | public: |
| 71 | DummyForwarder fw; |
| 72 | util::DummyClientFace& imsFace; |
| 73 | InMemoryStoragePersistent m_ims; |
| 74 | }; |
| 75 | |
| 76 | template<class T> |
Alexander Afanasyev | da366d8 | 2018-06-29 18:18:02 -0400 | [diff] [blame] | 77 | class DecryptorFixture : public DecryptorStaticDataEnvironment |
Alexander Afanasyev | ff3ee9f | 2018-06-13 20:33:30 -0400 | [diff] [blame] | 78 | { |
| 79 | public: |
| 80 | DecryptorFixture() |
| 81 | : face(static_cast<util::DummyClientFace&>(fw.addFace())) |
| 82 | , decryptor(m_keyChain.getPib().getIdentity(T().identity).getDefaultKey(), validator, m_keyChain, face) |
| 83 | { |
| 84 | advanceClocks(1_ms, 10); |
| 85 | } |
| 86 | |
| 87 | public: |
| 88 | util::DummyClientFace& face; |
| 89 | ValidatorNull validator; |
| 90 | Decryptor decryptor; |
| 91 | }; |
| 92 | |
| 93 | BOOST_AUTO_TEST_SUITE(TestDecryptor) |
| 94 | |
| 95 | struct Valid |
| 96 | { |
| 97 | std::string identity = "/first/user"; |
| 98 | bool expectToSucceed = true; |
| 99 | }; |
| 100 | |
| 101 | struct Invalid |
| 102 | { |
| 103 | std::string identity = "/not/authorized"; |
| 104 | bool expectToSucceed = false; |
| 105 | }; |
| 106 | |
| 107 | using Identities = boost::mpl::vector<Valid, Invalid>; |
| 108 | |
| 109 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(DecryptSuccess, T, Identities, DecryptorFixture<T>) |
| 110 | { |
| 111 | StaticData data; |
| 112 | |
| 113 | size_t nSuccesses = 0; |
| 114 | size_t nFailures = 0; |
| 115 | this->decryptor.decrypt(data.encryptedBlobs.at(0), |
| 116 | [&] (ConstBufferPtr buffer) { |
| 117 | ++nSuccesses; |
| 118 | BOOST_CHECK_EQUAL(buffer->size(), 15); |
| 119 | std::string content(reinterpret_cast<const char*>(buffer->data()), buffer->size()); |
| 120 | BOOST_CHECK_EQUAL(content, "Data to encrypt"); |
| 121 | }, |
| 122 | [&] (const ErrorCode& code, const std::string& msg) { |
| 123 | BOOST_TEST_MESSAGE(msg); |
| 124 | ++nFailures; |
| 125 | }); |
| 126 | this->advanceClocks(2_s, 10); |
| 127 | |
| 128 | BOOST_CHECK_EQUAL(nSuccesses, T().expectToSucceed ? 1 : 0); |
| 129 | BOOST_CHECK_EQUAL(nFailures, T().expectToSucceed ? 0 : 1); |
| 130 | } |
| 131 | |
| 132 | BOOST_AUTO_TEST_SUITE_END() |
| 133 | |
| 134 | } // namespace tests |
| 135 | } // namespace nac |
| 136 | } // namespace ndn |