blob: 87669078d2916fe12f315a5479dfc99ec553c138 [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 Pesaventoaf4e4e72024-01-26 14:30:11 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040030#include <ndn-cxx/security/validator-null.hpp>
Davide Pesaventocab86032020-12-10 20:30:12 -050031#include <ndn-cxx/util/dummy-client-face.hpp>
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040032
Davide Pesavento9c369b32023-09-17 15:25:05 -040033#include <boost/mp11/list.hpp>
34
Davide Pesavento5d2f1512023-08-11 14:50:51 -040035namespace ndn::nac::tests {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040036
Davide Pesaventoba3f6892020-12-08 22:18:35 -050037class DecryptorStaticDataEnvironment : public IoKeyChainFixture
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040038{
39public:
Alexander Afanasyevda366d82018-06-29 18:18:02 -040040 DecryptorStaticDataEnvironment()
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040041 {
42 StaticData data;
43 for (const auto& block : data.managerPackets) {
Davide Pesaventoaf4e4e72024-01-26 14:30:11 -050044 m_ims.insert(*std::make_shared<Data>(block));
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040045 }
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040046 for (const auto& block : data.encryptorPackets) {
Davide Pesaventoaf4e4e72024-01-26 14:30:11 -050047 m_ims.insert(*std::make_shared<Data>(block));
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040048 }
49
Davide Pesaventocab86032020-12-10 20:30:12 -050050 auto serveFromIms = [this] (const Name&, const Interest& interest) {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040051 auto data = m_ims.find(interest);
52 if (data != nullptr) {
Davide Pesaventocab86032020-12-10 20:30:12 -050053 m_imsFace.put(*data);
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040054 }
55 };
Davide Pesaventocab86032020-12-10 20:30:12 -050056 m_imsFace.setInterestFilter("/", serveFromIms, [] (auto...) {});
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040057 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 Pesaventoba3f6892020-12-08 22:18:35 -050063 m_keyChain.createIdentity("/not/authorized");
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040064 }
65
Davide Pesaventocab86032020-12-10 20:30:12 -050066protected:
Davide Pesavento5d2f1512023-08-11 14:50:51 -040067 DummyClientFace m_imsFace{m_io, m_keyChain, {false, true}};
Davide Pesaventocab86032020-12-10 20:30:12 -050068
69private:
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040070 InMemoryStoragePersistent m_ims;
71};
72
73template<class T>
Alexander Afanasyevda366d82018-06-29 18:18:02 -040074class DecryptorFixture : public DecryptorStaticDataEnvironment
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040075{
76public:
77 DecryptorFixture()
Davide Pesaventocab86032020-12-10 20:30:12 -050078 : face(m_io, m_keyChain, {false, true})
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040079 , decryptor(m_keyChain.getPib().getIdentity(T().identity).getDefaultKey(), validator, m_keyChain, face)
80 {
Davide Pesaventocab86032020-12-10 20:30:12 -050081 face.linkTo(m_imsFace);
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040082 advanceClocks(1_ms, 10);
83 }
84
85public:
Davide Pesavento5d2f1512023-08-11 14:50:51 -040086 DummyClientFace face;
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040087 security::ValidatorNull validator;
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040088 Decryptor decryptor;
89};
90
91BOOST_AUTO_TEST_SUITE(TestDecryptor)
92
93struct Valid
94{
95 std::string identity = "/first/user";
96 bool expectToSucceed = true;
97};
98
99struct Invalid
100{
101 std::string identity = "/not/authorized";
102 bool expectToSucceed = false;
103};
104
Davide Pesavento9c369b32023-09-17 15:25:05 -0400105using Identities = boost::mp11::mp_list<Valid, Invalid>;
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -0400106
107BOOST_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 Pesavento32d1dc22020-12-09 18:01:47 -0500117 std::string content(buffer->get<char>(), buffer->size());
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -0400118 BOOST_CHECK_EQUAL(content, "Data to encrypt");
119 },
Davide Pesavento32d1dc22020-12-09 18:01:47 -0500120 [&] (const ErrorCode&, const std::string& msg) {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -0400121 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
130BOOST_AUTO_TEST_SUITE_END()
131
Davide Pesavento5d2f1512023-08-11 14:50:51 -0400132} // namespace ndn::nac::tests