blob: 940a9907bc7ff81c1a8ab880099964b950b03db0 [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/*
3 * Copyright (c) 2014-2020, 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
22#include "encryptor.hpp"
23#include "encrypted-content.hpp"
24#include "access-manager.hpp"
25
Davide Pesaventoba3f6892020-12-08 22:18:35 -050026#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 Afanasyevff3ee9f2018-06-13 20:33:30 -040030
31#include <iostream>
32#include <boost/mpl/vector.hpp>
33
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 : 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 Pesaventoba3f6892020-12-08 22:18:35 -050047 m_ims.insert(*make_shared<Data>(block));
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040048 }
49
50 for (const auto& block : data.encryptorPackets) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -050051 m_ims.insert(*make_shared<Data>(block));
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040052 }
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 Pesaventoba3f6892020-12-08 22:18:35 -050067 m_keyChain.createIdentity("/not/authorized");
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040068 }
69
70public:
71 DummyForwarder fw;
72 util::DummyClientFace& imsFace;
73 InMemoryStoragePersistent m_ims;
74};
75
76template<class T>
Alexander Afanasyevda366d82018-06-29 18:18:02 -040077class DecryptorFixture : public DecryptorStaticDataEnvironment
Alexander Afanasyevff3ee9f2018-06-13 20:33:30 -040078{
79public:
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
87public:
88 util::DummyClientFace& face;
89 ValidatorNull validator;
90 Decryptor decryptor;
91};
92
93BOOST_AUTO_TEST_SUITE(TestDecryptor)
94
95struct Valid
96{
97 std::string identity = "/first/user";
98 bool expectToSucceed = true;
99};
100
101struct Invalid
102{
103 std::string identity = "/not/authorized";
104 bool expectToSucceed = false;
105};
106
107using Identities = boost::mpl::vector<Valid, Invalid>;
108
109BOOST_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
132BOOST_AUTO_TEST_SUITE_END()
133
134} // namespace tests
135} // namespace nac
136} // namespace ndn