blob: e2d41cd2a11c84adec39146db9913c24e109da2b [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California
*
* NAC library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of NAC library authors and contributors.
*/
#include "access-manager.hpp"
#include "tests/boost-test.hpp"
#include "tests/io-key-chain-fixture.hpp"
#include <ndn-cxx/util/dummy-client-face.hpp>
#include <ndn-cxx/util/string-helper.hpp>
#include <iostream>
namespace ndn::nac::tests {
class AccessManagerFixture : public IoKeyChainFixture
{
public:
AccessManagerFixture()
: face(m_io, m_keyChain, {true, true})
, accessIdentity(m_keyChain.createIdentity("/access/policy/identity"))
, nacIdentity(m_keyChain.createIdentity("/access/policy/identity/NAC/dataset", // hack to get access to KEK key-id
RsaKeyParams()))
, userIdentities{m_keyChain.createIdentity("/first/user", RsaKeyParams()),
m_keyChain.createIdentity("/second/user", RsaKeyParams())}
, manager(accessIdentity, Name("/dataset"), m_keyChain, face)
{
advanceClocks(1_ms, 10);
for (auto& user : userIdentities) {
manager.addMember(user.getDefaultKey().getDefaultCertificate());
}
}
public:
DummyClientFace face;
Identity accessIdentity;
Identity nacIdentity;
std::vector<Identity> userIdentities;
AccessManager manager;
};
BOOST_FIXTURE_TEST_SUITE(TestAccessManager, AccessManagerFixture)
BOOST_AUTO_TEST_CASE(PublishedKek)
{
face.receive(Interest(Name("/access/policy/identity/NAC/dataset/KEK"))
.setCanBePrefix(true).setMustBeFresh(true));
advanceClocks(1_ms, 10);
BOOST_CHECK_EQUAL(face.sentData.at(0).getName().getPrefix(-1), "/access/policy/identity/NAC/dataset/KEK");
BOOST_CHECK_EQUAL(face.sentData.at(0).getName().get(-1), nacIdentity.getDefaultKey().getName().get(-1));
}
BOOST_AUTO_TEST_CASE(PublishedKdks)
{
for (auto& user : userIdentities) {
Name kdk("/access/policy/identity/NAC/dataset/KDK");
kdk
.append(nacIdentity.getDefaultKey().getName().get(-1))
.append("ENCRYPTED-BY")
.append(user.getDefaultKey().getName());
face.receive(Interest(kdk).setCanBePrefix(true).setMustBeFresh(true));
advanceClocks(1_ms, 10);
BOOST_CHECK_EQUAL(face.sentData.at(0).getName(), kdk);
face.sentData.clear();
}
}
BOOST_AUTO_TEST_CASE(EnumerateDataFromIms)
{
BOOST_CHECK_EQUAL(manager.size(), 3);
size_t nKek = 0;
size_t nKdk = 0;
for (const auto& data : manager) {
BOOST_TEST_MESSAGE(data.getName());
if (data.getName().at(5) == KEK) {
++nKek;
}
else if (data.getName().at(5) == KDK) {
++nKdk;
}
}
BOOST_CHECK_EQUAL(nKek, 1);
BOOST_CHECK_EQUAL(nKdk, 2);
}
BOOST_AUTO_TEST_CASE(GenerateTestData,
* ut::description("regenerates the static test data used by other test cases")
* ut::disabled()
* ut::label("generator"))
{
std::cerr << "const Block nacIdentity = \"";
auto block = m_keyChain.exportSafeBag(nacIdentity.getDefaultKey().getDefaultCertificate(),
"password", strlen("password"))->wireEncode();
printHex(std::cerr, block, true);
std::cerr << "\"_block;\n";
std::cerr << "const std::vector<Block> userIdentities{\n";
for (const auto& userId : userIdentities) {
std::cerr << " \"";
block = m_keyChain.exportSafeBag(userId.getDefaultKey().getDefaultCertificate(),
"password", strlen("password"))->wireEncode();
printHex(std::cerr, block, true);
std::cerr << "\"_block,\n";
}
std::cerr << "};\n";
std::cerr << "const std::vector<Block> managerPackets{\n";
for (const auto& data : manager) {
std::cerr << " \"";
printHex(std::cerr, data.wireEncode(), true);
std::cerr << "\"_block,\n";
}
std::cerr << "};\n";
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace ndn::nac::tests