blob: 6cb52f673931428a2dd7ded24901d3c3278c86d6 [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 Pesavento5d2f1512023-08-11 14:50:51 -04003 * Copyright (c) 2014-2023, 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
Davide Pesavento5d2f1512023-08-11 14:50:51 -040034namespace ndn::nac::tests {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040035
Davide Pesaventoba3f6892020-12-08 22:18:35 -050036class DecryptorStaticDataEnvironment : public IoKeyChainFixture
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040037{
38public:
Alexander Afanasyevda366d82018-06-29 18:18:02 -040039 DecryptorStaticDataEnvironment()
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040040 {
41 StaticData data;
42 for (const auto& block : data.managerPackets) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -050043 m_ims.insert(*make_shared<Data>(block));
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040044 }
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040045 for (const auto& block : data.encryptorPackets) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -050046 m_ims.insert(*make_shared<Data>(block));
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040047 }
48
Davide Pesaventocab86032020-12-10 20:30:12 -050049 auto serveFromIms = [this] (const Name&, const Interest& interest) {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040050 auto data = m_ims.find(interest);
51 if (data != nullptr) {
Davide Pesaventocab86032020-12-10 20:30:12 -050052 m_imsFace.put(*data);
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040053 }
54 };
Davide Pesaventocab86032020-12-10 20:30:12 -050055 m_imsFace.setInterestFilter("/", serveFromIms, [] (auto...) {});
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040056 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 Pesaventoba3f6892020-12-08 22:18:35 -050062 m_keyChain.createIdentity("/not/authorized");
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040063 }
64
Davide Pesaventocab86032020-12-10 20:30:12 -050065protected:
Davide Pesavento5d2f1512023-08-11 14:50:51 -040066 DummyClientFace m_imsFace{m_io, m_keyChain, {false, true}};
Davide Pesaventocab86032020-12-10 20:30:12 -050067
68private:
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040069 InMemoryStoragePersistent m_ims;
70};
71
72template<class T>
Alexander Afanasyevda366d82018-06-29 18:18:02 -040073class DecryptorFixture : public DecryptorStaticDataEnvironment
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040074{
75public:
76 DecryptorFixture()
Davide Pesaventocab86032020-12-10 20:30:12 -050077 : face(m_io, m_keyChain, {false, true})
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040078 , decryptor(m_keyChain.getPib().getIdentity(T().identity).getDefaultKey(), validator, m_keyChain, face)
79 {
Davide Pesaventocab86032020-12-10 20:30:12 -050080 face.linkTo(m_imsFace);
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040081 advanceClocks(1_ms, 10);
82 }
83
84public:
Davide Pesavento5d2f1512023-08-11 14:50:51 -040085 DummyClientFace face;
Davide Pesavento2e5b7b12022-09-19 23:30:44 -040086 security::ValidatorNull validator;
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040087 Decryptor decryptor;
88};
89
90BOOST_AUTO_TEST_SUITE(TestDecryptor)
91
92struct Valid
93{
94 std::string identity = "/first/user";
95 bool expectToSucceed = true;
96};
97
98struct Invalid
99{
100 std::string identity = "/not/authorized";
101 bool expectToSucceed = false;
102};
103
104using Identities = boost::mpl::vector<Valid, Invalid>;
105
106BOOST_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 Pesavento32d1dc22020-12-09 18:01:47 -0500116 std::string content(buffer->get<char>(), buffer->size());
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -0400117 BOOST_CHECK_EQUAL(content, "Data to encrypt");
118 },
Davide Pesavento32d1dc22020-12-09 18:01:47 -0500119 [&] (const ErrorCode&, const std::string& msg) {
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -0400120 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
129BOOST_AUTO_TEST_SUITE_END()
130
Davide Pesavento5d2f1512023-08-11 14:50:51 -0400131} // namespace ndn::nac::tests