blob: 7b798554d39be5bd0a8ffb746b50a4aa376b8e3a [file] [log] [blame]
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento9062a502020-01-04 17:14:04 -05002/*
Davide Pesavento2e5b7b12022-09-19 23:30:44 -04003 * Copyright (c) 2014-2022, Regents of the University of California
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -04004 *
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 Afanasyevff3ee9f2018-06-13 20:33:30 -040022#include "access-manager.hpp"
Davide Pesaventocab86032020-12-10 20:30:12 -050023#include "encrypted-content.hpp"
24#include "encryptor.hpp"
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040025
Davide Pesaventoba3f6892020-12-08 22:18:35 -050026#include "tests/boost-test.hpp"
Davide Pesaventoba3f6892020-12-08 22:18:35 -050027#include "tests/io-key-chain-fixture.hpp"
28#include "tests/unit/static-data.hpp"
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040029
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040030#include <boost/mpl/vector.hpp>
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040031#include <ndn-cxx/security/validator-null.hpp>
Davide Pesaventocab86032020-12-10 20:30:12 -050032#include <ndn-cxx/util/dummy-client-face.hpp>
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040033
34namespace ndn {
35namespace nac {
36namespace tests {
37
Davide Pesaventoba3f6892020-12-08 22:18:35 -050038class DecryptorStaticDataEnvironment : public IoKeyChainFixture
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040039{
40public:
Alexander Afanasyevda366d82018-06-29 18:18:02 -040041 DecryptorStaticDataEnvironment()
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040042 {
43 StaticData data;
44 for (const auto& block : data.managerPackets) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -050045 m_ims.insert(*make_shared<Data>(block));
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040046 }
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040047 for (const auto& block : data.encryptorPackets) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -050048 m_ims.insert(*make_shared<Data>(block));
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040049 }
50
Davide Pesaventocab86032020-12-10 20:30:12 -050051 auto serveFromIms = [this] (const Name&, const Interest& interest) {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040052 auto data = m_ims.find(interest);
53 if (data != nullptr) {
Davide Pesaventocab86032020-12-10 20:30:12 -050054 m_imsFace.put(*data);
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040055 }
56 };
Davide Pesaventocab86032020-12-10 20:30:12 -050057 m_imsFace.setInterestFilter("/", serveFromIms, [] (auto...) {});
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040058 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 Pesaventoba3f6892020-12-08 22:18:35 -050064 m_keyChain.createIdentity("/not/authorized");
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040065 }
66
Davide Pesaventocab86032020-12-10 20:30:12 -050067protected:
68 util::DummyClientFace m_imsFace{m_io, m_keyChain, {false, true}};
69
70private:
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040071 InMemoryStoragePersistent m_ims;
72};
73
74template<class T>
Alexander Afanasyevda366d82018-06-29 18:18:02 -040075class DecryptorFixture : public DecryptorStaticDataEnvironment
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040076{
77public:
78 DecryptorFixture()
Davide Pesaventocab86032020-12-10 20:30:12 -050079 : face(m_io, m_keyChain, {false, true})
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040080 , decryptor(m_keyChain.getPib().getIdentity(T().identity).getDefaultKey(), validator, m_keyChain, face)
81 {
Davide Pesaventocab86032020-12-10 20:30:12 -050082 face.linkTo(m_imsFace);
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040083 advanceClocks(1_ms, 10);
84 }
85
86public:
Davide Pesaventocab86032020-12-10 20:30:12 -050087 util::DummyClientFace face;
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040088 security::ValidatorNull validator;
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040089 Decryptor decryptor;
90};
91
92BOOST_AUTO_TEST_SUITE(TestDecryptor)
93
94struct Valid
95{
96 std::string identity = "/first/user";
97 bool expectToSucceed = true;
98};
99
100struct Invalid
101{
102 std::string identity = "/not/authorized";
103 bool expectToSucceed = false;
104};
105
106using Identities = boost::mpl::vector<Valid, Invalid>;
107
108BOOST_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 Pesavento32d1dc22020-12-09 18:01:47 -0500118 std::string content(buffer->get<char>(), buffer->size());
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -0400119 BOOST_CHECK_EQUAL(content, "Data to encrypt");
120 },
Davide Pesavento32d1dc22020-12-09 18:01:47 -0500121 [&] (const ErrorCode&, const std::string& msg) {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -0400122 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
131BOOST_AUTO_TEST_SUITE_END()
132
133} // namespace tests
134} // namespace nac
135} // namespace ndn