icinga2/test/remote-certificate-fixture.hpp
Johannes Schmidt e11aad842a Discover Boost test cases automatically after build
This adds a global fixture that can parse an additional argument to
the test executables (`--generate_ctest_config`). When run by
CMake during build, this generates a CTest script containing all
the tests and their properties.

An additional decorator, that defines CTest properties for a test case
or suite that will be added to the tests during config generation.

This version needs no hacks, no huge CMake scripts, just a bit of
additional C++ code that iterates over all test-cases and collects
the information CTest needs.

One caveat is still that this does not work with cross-compilation,
which probably isn't an issue to begin with, but there are also ways
to fix that if necessary.
2025-08-19 09:24:42 +02:00

70 lines
1.7 KiB
C++

/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
#pragma once
#include <BoostTestTargetConfig.h>
#include "remote/apilistener.hpp"
#include "remote/pkiutility.hpp"
#include "test/base-configuration-fixture.hpp"
namespace icinga {
struct CertificateFixture : ConfigurationDataDirFixture
{
CertificateFixture()
{
namespace fs = boost::filesystem;
m_CaDir = ApiListener::GetCaDir();
m_CertsDir = ApiListener::GetCertsDir();
m_CaCrtFile = m_CertsDir / "ca.crt";
fs::create_directories(m_PersistentCertsDir / "ca");
fs::create_directories(m_PersistentCertsDir / "certs");
if (fs::exists(m_DataDir / "ca")) {
fs::remove(m_DataDir / "ca");
}
if (fs::exists(m_DataDir / "certs")) {
fs::remove(m_DataDir / "certs");
}
fs::create_directory_symlink(m_PersistentCertsDir / "certs", m_DataDir / "certs");
fs::create_directory_symlink(m_PersistentCertsDir / "ca", m_DataDir / "ca");
if (!fs::exists(m_CaCrtFile)) {
PkiUtility::NewCa();
fs::copy_file(m_CaDir / "ca.crt", m_CaCrtFile);
}
}
auto EnsureCertFor(const std::string& name)
{
struct Cert
{
String crtFile;
String keyFile;
String csrFile;
};
Cert cert;
cert.crtFile = (m_CertsDir / (name + ".crt")).string();
cert.keyFile = (m_CertsDir / (name + ".key")).string();
cert.csrFile = (m_CertsDir / (name + ".csr")).string();
if (!Utility::PathExists(cert.crtFile)) {
PkiUtility::NewCert(name, cert.keyFile, cert.csrFile, cert.crtFile);
PkiUtility::SignCsr(cert.csrFile, cert.crtFile);
}
return cert;
}
boost::filesystem::path m_CaDir;
boost::filesystem::path m_CertsDir;
boost::filesystem::path m_CaCrtFile;
static const boost::filesystem::path m_PersistentCertsDir;
};
} // namespace icinga