blob: f98561a7502f0f97d4d7f1b3e757bcc26c8005ca [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/*
3 * Copyright (c) 2014-2020, 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"
23#include "tests/dummy-forwarder.hpp"
24#include "tests/io-key-chain-fixture.hpp"
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040025
26#include <iostream>
27#include <ndn-cxx/util/string-helper.hpp>
28
29namespace ndn {
30namespace nac {
31namespace tests {
32
Davide Pesaventoba3f6892020-12-08 22:18:35 -050033class AccessManagerFixture : public IoKeyChainFixture
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040034{
35public:
36 AccessManagerFixture()
37 : fw(m_io, m_keyChain)
38 , face(static_cast<util::DummyClientFace&>(fw.addFace()))
Davide Pesaventoba3f6892020-12-08 22:18:35 -050039 , accessIdentity(m_keyChain.createIdentity("/access/policy/identity"))
40 , nacIdentity(m_keyChain.createIdentity("/access/policy/identity/NAC/dataset", // hack to get access to KEK key-id
41 RsaKeyParams()))
42 , userIdentities{m_keyChain.createIdentity("/first/user", RsaKeyParams()),
43 m_keyChain.createIdentity("/second/user", RsaKeyParams())}
Alexander Afanasyeve96538a2018-06-13 20:32:53 -040044 , manager(accessIdentity, Name("/dataset"), m_keyChain, face)
45 {
46 advanceClocks(1_ms, 10);
47
48 for (auto& user : userIdentities) {
49 manager.addMember(user.getDefaultKey().getDefaultCertificate());
50 }
51 }
52
53public:
54 DummyForwarder fw;
55 util::DummyClientFace& face;
56 Identity accessIdentity;
57 Identity nacIdentity;
58 std::vector<Identity> userIdentities;
59 AccessManager manager;
60};
61
62BOOST_FIXTURE_TEST_SUITE(TestAccessManager, AccessManagerFixture)
63
64BOOST_AUTO_TEST_CASE(PublishedKek)
65{
66 face.receive(Interest(Name("/access/policy/identity/NAC/dataset/KEK"))
67 .setCanBePrefix(true).setMustBeFresh(true));
68 advanceClocks(1_ms, 10);
69
70 BOOST_CHECK_EQUAL(face.sentData.at(0).getName().getPrefix(-1), "/access/policy/identity/NAC/dataset/KEK");
71 BOOST_CHECK_EQUAL(face.sentData.at(0).getName().get(-1), nacIdentity.getDefaultKey().getName().get(-1));
72}
73
74BOOST_AUTO_TEST_CASE(PublishedKdks)
75{
76 for (auto& user : userIdentities) {
77 Name kdk("/access/policy/identity/NAC/dataset/KDK");
78 kdk
79 .append(nacIdentity.getDefaultKey().getName().get(-1))
80 .append("ENCRYPTED-BY")
81 .append(user.getDefaultKey().getName());
82
83 face.receive(Interest(kdk)
84 .setCanBePrefix(true).setMustBeFresh(true));
85 advanceClocks(1_ms, 10);
86
87 BOOST_TEST_MESSAGE(kdk);
88 BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), kdk);
89 face.sentData.clear();
90 }
91}
92
93BOOST_AUTO_TEST_CASE(EnumerateDataFromIms)
94{
95 BOOST_CHECK_EQUAL(manager.size(), 3);
96 size_t nKek = 0;
97 size_t nKdk = 0;
98 for (const auto& data : manager) {
99 BOOST_TEST_MESSAGE(data.getName());
100 if (data.getName().at(5) == KEK) {
101 ++nKek;
102 }
103 else if (data.getName().at(5) == KDK) {
104 ++nKdk;
105 }
106 }
107 BOOST_CHECK_EQUAL(nKek, 1);
108 BOOST_CHECK_EQUAL(nKdk, 2);
109}
110
111BOOST_AUTO_TEST_CASE(DumpPackets) // use this to update content of other test cases
112{
113 if (std::getenv("NAC_DUMP_PACKETS") == nullptr) {
114 return;
115 }
116
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500117 std::cerr << "const Block nacIdentity = \"";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400118 auto block = m_keyChain.exportSafeBag(nacIdentity.getDefaultKey().getDefaultCertificate(),
119 "password", strlen("password"))->wireEncode();
120 printHex(std::cerr, block.wire(), block.size(), true);
121 std::cerr << "\"_block;\n\n";
122
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500123 std::cerr << "const std::vector<Block> userIdentities = {\n";
124 for (const auto& userId : userIdentities) {
125 std::cerr << " \"";
126 block = m_keyChain.exportSafeBag(userId.getDefaultKey().getDefaultCertificate(),
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400127 "password", strlen("password"))->wireEncode();
128 printHex(std::cerr, block.wire(), block.size(), 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
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500133 std::cerr << "const std::vector<Block> managerPackets = {\n";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400134 for (const auto& data : manager) {
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500135 std::cerr << " \"";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400136 printHex(std::cerr, data.wireEncode().wire(), data.wireEncode().size(), true);
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500137 std::cerr << "\"_block,\n";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400138 }
Davide Pesaventoba3f6892020-12-08 22:18:35 -0500139 std::cerr << "};\n\n";
Alexander Afanasyeve96538a2018-06-13 20:32:53 -0400140}
141
142BOOST_AUTO_TEST_SUITE_END()
143
144} // namespace tests
145} // namespace nac
146} // namespace ndn