-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "../sdk/net/crypto/crypto.h" | ||
#include "unit_testutils.h" | ||
|
||
TEST_CASE_METHOD(iggy::testutil::SelfSignedCertificate, "certificate loading", UT_TAG) { | ||
auto tmpDir = std::filesystem::temp_directory_path(); | ||
iggy::crypto::CertificateStore* cs = new iggy::crypto::LocalCertificateStore(tmpDir); | ||
auto cert = cs->getCertificate(getCertificatePath().filename()); | ||
REQUIRE(cert.size() > 0); | ||
delete cs; | ||
} | ||
|
||
TEST_CASE_METHOD(iggy::testutil::SelfSignedCertificate, "private key loading", UT_TAG) { | ||
auto tmpDir = std::filesystem::temp_directory_path(); | ||
iggy::crypto::KeyStore* ks = new iggy::crypto::LocalKeyStore(tmpDir); | ||
auto pk = ks->getPrivateKey(getKeyPath().filename()); | ||
REQUIRE(pk.size() > 0); | ||
delete ks; | ||
} | ||
|
||
TEST_CASE("revocation methods", UT_TAG) { | ||
SECTION("default CRL configuration") { | ||
iggy::crypto::CRL crl; | ||
|
||
REQUIRE(!crl.getCrlPath().has_value()); | ||
REQUIRE(!crl.getCrlUrl().has_value()); | ||
} | ||
|
||
SECTION("default OCSP configuration") { | ||
iggy::crypto::OCSP ocsp; | ||
|
||
REQUIRE(!ocsp.getOcspOverrideUrl().has_value()); | ||
REQUIRE(ocsp.isStaplingEnabled()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "unit_testutils.h" | ||
#include <fmt/format.h> | ||
#include <iostream> | ||
#include <reproc++/drain.hpp> | ||
#include <reproc++/reproc.hpp> | ||
#include <stdexcept> | ||
#include <vector> | ||
|
||
iggy::testutil::SelfSignedCertificate::SelfSignedCertificate() { | ||
std::vector<std::string> arguments = {"openssl", "req", | ||
"-x509", "-newkey", | ||
"rsa:2048", "-nodes", | ||
"-keyout", this->keyPath.string(), | ||
"-out", this->certificatePath.string(), | ||
"-days", "365", | ||
"-subj", "/CN=localhost"}; | ||
reproc::options options; | ||
options.redirect.discard = true; | ||
reproc::process process; | ||
|
||
std::string output; | ||
reproc::sink::string sink(output); | ||
auto err = process.start(arguments, options); | ||
if (err) { | ||
throw std::runtime_error(fmt::format("Failed to run OpenSSL: {}", err.message())); | ||
} | ||
err = reproc::drain(process, sink, reproc::sink::null); | ||
if (err) { | ||
throw std::runtime_error(fmt::format("Failed to read OpenSSL output: {}", err.message())); | ||
} | ||
std::cout << output << std::flush; | ||
} | ||
|
||
iggy::testutil::SelfSignedCertificate::~SelfSignedCertificate() { | ||
std::filesystem::remove(this->certificatePath); | ||
std::filesystem::remove(this->keyPath); | ||
} | ||
|
||
std::filesystem::path iggy::testutil::SelfSignedCertificate::generateRandomTempPath(std::string baseName) { | ||
return std::filesystem::temp_directory_path() / (std::to_string(std::rand()) + baseName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,28 @@ | ||
#pragma once | ||
|
||
#include <catch.hpp> | ||
#include <filesystem> | ||
#include <string> | ||
|
||
const char UT_TAG[] = "[Unit Tests]"; | ||
|
||
namespace iggy { | ||
namespace testutil { | ||
/** | ||
* @brief A helper class for generating self-signed certificates for testing purposes using OpenSSL CLI. | ||
*/ | ||
class SelfSignedCertificate { | ||
private: | ||
std::filesystem::path certificatePath = generateRandomTempPath("cert.pem"); | ||
std::filesystem::path keyPath = generateRandomTempPath("key.pem"); | ||
|
||
static std::filesystem::path generateRandomTempPath(std::string baseName); | ||
|
||
public: | ||
SelfSignedCertificate(); | ||
~SelfSignedCertificate(); | ||
std::filesystem::path getCertificatePath() { return this->certificatePath; } | ||
std::filesystem::path getKeyPath() { return this->keyPath; } | ||
}; | ||
} // namespace testutil | ||
} // namespace iggy |