Skip to content

Commit

Permalink
... with local cert and SSL_CERT_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
m8mble committed Mar 13, 2021
1 parent 0e9b95a commit b6c59d7
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tests/src/unit/tests/test_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@ using namespace datastax::internal::core;

namespace {

static void setenv(const std::string& name, const std::string& value) {
void setenv(const std::string& name, const std::string& value) {
#ifdef _WIN32
_putenv(const_cast<char*>(std::string(name + "=" + value).c_str()));
#else
::setenv(name.c_str(), value.c_str(), 1);
#endif
}

String current_dir()
{
char buffer[256];
#ifdef _WIN32
_getcwd(buffer, 256);
#else
getcwd(buffer, 256);
#endif
return buffer;
}

}

class ConnectionUnitTest : public LoopTest {
Expand Down Expand Up @@ -208,7 +219,7 @@ TEST_F(ConnectionUnitTest, Ssl) {
TEST_F(ConnectionUnitTest, SslDefaultVerifyPaths) {
const String host = "127.0.0.1";
const int verification_flags = CASS_SSL_VERIFY_PEER_CERT | CASS_SSL_VERIFY_PEER_IDENTITY;
const String cert_path = std::tmpnam(nullptr);
const String cert_path = "tmp.cassandra.unit-test.cert";

mockssandra::SimpleCluster cluster(simple());
const String cert = cluster.use_ssl(host);
Expand All @@ -228,11 +239,14 @@ TEST_F(ConnectionUnitTest, SslDefaultVerifyPaths) {
EXPECT_EQ(connect_rc, Connector::CONNECTION_ERROR_SSL_VERIFY)
<< "Verification succeeded without certificate.";

const String cwd = current_dir();

// Generate certificate as file (which is used by our mock cluster) and import it
std::ofstream cert_buffer(cert_path.c_str());
cert_buffer << cert;
cert_buffer.close();
setenv("SSL_CERT_FILE", cert_path.c_str());
setenv("SSL_CERT_DIR", cwd.c_str());
std::cout << "Debug SslDefaultVerifyPaths: SSL_CERT_FILE " << cert_path << " " << cert << std::endl;
for (const auto var: {"SSL_CERT_FILE", "SSL_CERT_DIR"}) {
const char* value = std::getenv(var);
Expand Down

0 comments on commit b6c59d7

Please sign in to comment.