Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 9062a50 | 2020-01-04 17:14:04 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2020, Regents of the University of California |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 4 | * |
| 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 Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 22 | #include "tests/boost-test.hpp" |
| 23 | #include "tests/dummy-forwarder.hpp" |
| 24 | #include "tests/io-key-chain-fixture.hpp" |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 25 | |
| 26 | #include <iostream> |
| 27 | #include <ndn-cxx/util/string-helper.hpp> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace nac { |
| 31 | namespace tests { |
| 32 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 33 | class AccessManagerFixture : public IoKeyChainFixture |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 34 | { |
| 35 | public: |
| 36 | AccessManagerFixture() |
| 37 | : fw(m_io, m_keyChain) |
| 38 | , face(static_cast<util::DummyClientFace&>(fw.addFace())) |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 39 | , 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 Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 44 | , 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 | |
| 53 | public: |
| 54 | DummyForwarder fw; |
| 55 | util::DummyClientFace& face; |
| 56 | Identity accessIdentity; |
| 57 | Identity nacIdentity; |
| 58 | std::vector<Identity> userIdentities; |
| 59 | AccessManager manager; |
| 60 | }; |
| 61 | |
| 62 | BOOST_FIXTURE_TEST_SUITE(TestAccessManager, AccessManagerFixture) |
| 63 | |
| 64 | BOOST_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 | |
| 74 | BOOST_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 | |
| 93 | BOOST_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 | |
| 111 | BOOST_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 Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 117 | std::cerr << "const Block nacIdentity = \""; |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 118 | 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 Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 123 | 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 Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 127 | "password", strlen("password"))->wireEncode(); |
| 128 | printHex(std::cerr, block.wire(), block.size(), true); |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 129 | std::cerr << "\"_block,\n"; |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 130 | } |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 131 | std::cerr << "};\n\n"; |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 132 | |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 133 | std::cerr << "const std::vector<Block> managerPackets = {\n"; |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 134 | for (const auto& data : manager) { |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 135 | std::cerr << " \""; |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 136 | printHex(std::cerr, data.wireEncode().wire(), data.wireEncode().size(), true); |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 137 | std::cerr << "\"_block,\n"; |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 138 | } |
Davide Pesavento | ba3f689 | 2020-12-08 22:18:35 -0500 | [diff] [blame^] | 139 | std::cerr << "};\n\n"; |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_SUITE_END() |
| 143 | |
| 144 | } // namespace tests |
| 145 | } // namespace nac |
| 146 | } // namespace ndn |