blob: f8b110b0db33dffd557a495cfcc53db1a05f397a [file] [log] [blame]
Alexander Afanasyeve96538a2018-06-13 20:32:53 -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 Afanasyeve96538a2018-06-13 20:32:53 -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 "access-manager.hpp"
21
Davide Pesaventoba3f6892020-12-08 22:18:35 -050022#include "tests/boost-test.hpp"
Davide Pesaventoba3f6892020-12-08 22:18:35 -050023#include "tests/io-key-chain-fixture.hpp"
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040024
25#include <iostream>
Davide Pesaventocab86032020-12-10 20:30:12 -050026#include <ndn-cxx/util/dummy-client-face.hpp>
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040027#include <ndn-cxx/util/string-helper.hpp>
28
Davide Pesavento5d2f1512023-08-11 14:50:51 -040029namespace ndn::nac::tests {
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040030
Davide Pesaventoba3f6892020-12-08 22:18:35 -050031class AccessManagerFixture : public IoKeyChainFixture
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040032{
33public:
34 AccessManagerFixture()
Davide Pesaventocab86032020-12-10 20:30:12 -050035 : face(m_io, m_keyChain, {true, true})
Davide Pesaventoba3f6892020-12-08 22:18:35 -050036 , accessIdentity(m_keyChain.createIdentity("/access/policy/identity"))
37 , nacIdentity(m_keyChain.createIdentity("/access/policy/identity/NAC/dataset", // hack to get access to KEK key-id
38 RsaKeyParams()))
39 , userIdentities{m_keyChain.createIdentity("/first/user", RsaKeyParams()),
40 m_keyChain.createIdentity("/second/user", RsaKeyParams())}
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040041 , manager(accessIdentity, Name("/dataset"), m_keyChain, face)
42 {
43 advanceClocks(1_ms, 10);
44
45 for (auto& user : userIdentities) {
46 manager.addMember(user.getDefaultKey().getDefaultCertificate());
47 }
48 }
49
50public:
Davide Pesavento5d2f1512023-08-11 14:50:51 -040051 DummyClientFace face;
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040052 Identity accessIdentity;
53 Identity nacIdentity;
54 std::vector<Identity> userIdentities;
55 AccessManager manager;
56};
57
58BOOST_FIXTURE_TEST_SUITE(TestAccessManager, AccessManagerFixture)
59
60BOOST_AUTO_TEST_CASE(PublishedKek)
61{
62 face.receive(Interest(Name("/access/policy/identity/NAC/dataset/KEK"))
63 .setCanBePrefix(true).setMustBeFresh(true));
64 advanceClocks(1_ms, 10);
65
66 BOOST_CHECK_EQUAL(face.sentData.at(0).getName().getPrefix(-1), "/access/policy/identity/NAC/dataset/KEK");
67 BOOST_CHECK_EQUAL(face.sentData.at(0).getName().get(-1), nacIdentity.getDefaultKey().getName().get(-1));
68}
69
70BOOST_AUTO_TEST_CASE(PublishedKdks)
71{
72 for (auto& user : userIdentities) {
73 Name kdk("/access/policy/identity/NAC/dataset/KDK");
74 kdk
75 .append(nacIdentity.getDefaultKey().getName().get(-1))
76 .append("ENCRYPTED-BY")
77 .append(user.getDefaultKey().getName());
78
Davide Pesavento32d1dc22020-12-09 18:01:47 -050079 face.receive(Interest(kdk).setCanBePrefix(true).setMustBeFresh(true));
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040080 advanceClocks(1_ms, 10);
81
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040082 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), kdk);
83 face.sentData.clear();
84 }
85}
86
87BOOST_AUTO_TEST_CASE(EnumerateDataFromIms)
88{
89 BOOST_CHECK_EQUAL(manager.size(), 3);
90 size_t nKek = 0;
91 size_t nKdk = 0;
92 for (const auto& data : manager) {
93 BOOST_TEST_MESSAGE(data.getName());
94 if (data.getName().at(5) == KEK) {
95 ++nKek;
96 }
97 else if (data.getName().at(5) == KDK) {
98 ++nKdk;
99 }
100 }
101 BOOST_CHECK_EQUAL(nKek, 1);
102 BOOST_CHECK_EQUAL(nKdk, 2);
103}
104
Davide Pesavento32d1dc22020-12-09 18:01:47 -0500105BOOST_AUTO_TEST_CASE(GenerateTestData,
106 * ut::description("regenerates the static test data used by other test cases")
107 * ut::disabled())
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400108{
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500109 std::cerr << "const Block nacIdentity = \"";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400110 auto block = m_keyChain.exportSafeBag(nacIdentity.getDefaultKey().getDefaultCertificate(),
111 "password", strlen("password"))->wireEncode();
Davide Pesavento714dba02022-03-17 20:46:28 -0400112 printHex(std::cerr, block, true);
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400113 std::cerr << "\"_block;\n\n";
114
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500115 std::cerr << "const std::vector<Block> userIdentities = {\n";
116 for (const auto& userId : userIdentities) {
117 std::cerr << " \"";
118 block = m_keyChain.exportSafeBag(userId.getDefaultKey().getDefaultCertificate(),
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400119 "password", strlen("password"))->wireEncode();
Davide Pesavento714dba02022-03-17 20:46:28 -0400120 printHex(std::cerr, block, true);
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500121 std::cerr << "\"_block,\n";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400122 }
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500123 std::cerr << "};\n\n";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400124
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500125 std::cerr << "const std::vector<Block> managerPackets = {\n";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400126 for (const auto& data : manager) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500127 std::cerr << " \"";
Davide Pesavento714dba02022-03-17 20:46:28 -0400128 printHex(std::cerr, data.wireEncode(), true);
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500129 std::cerr << "\"_block,\n";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400130 }
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500131 std::cerr << "};\n\n";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400132}
133
134BOOST_AUTO_TEST_SUITE_END()
135
Davide Pesavento5d2f1512023-08-11 14:50:51 -0400136} // namespace ndn::nac::tests