Skip to content

Commit 32a07c2

Browse files
committed
Add unit test for cass_ssl_set_default_verify_paths
Ensure certificate validation fails prior to calling said function, and succeeds afterwards. The used certificate is specified to openssl via environment variables.
1 parent adce846 commit 32a07c2

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/src/unit/tests/test_connection.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "request_callback.hpp"
2424
#include "ssl.hpp"
2525

26+
#include <cstdio>
27+
#include <fstream>
28+
2629
#ifdef WIN32
2730
#undef STATUS_TIMEOUT
2831
#endif
@@ -190,6 +193,48 @@ TEST_F(ConnectionUnitTest, Ssl) {
190193
EXPECT_EQ(state.status, STATUS_SUCCESS);
191194
}
192195

196+
TEST_F(ConnectionUnitTest, SslDefaultVerifyPaths) {
197+
const String host = "127.0.0.1";
198+
const int verification_flags = CASS_SSL_VERIFY_PEER_CERT | CASS_SSL_VERIFY_PEER_IDENTITY;
199+
const char* cert_path = "cassandra-unit-test.cert";
200+
201+
mockssandra::SimpleCluster cluster(simple());
202+
const String cert = cluster.use_ssl(host);
203+
EXPECT_FALSE(cert.empty()) << "Unable to enable SSL";
204+
ConnectionSettings settings;
205+
settings.socket_settings.ssl_context = SslContextFactory::create();
206+
settings.socket_settings.ssl_context->set_verify_flags(verification_flags);
207+
ASSERT_EQ(cluster.start_all(), 0);
208+
209+
// Test that cert verification fails prior to calling set_default_verify_paths
210+
Connector::ConnectionError connect_rc = Connector::CONNECTION_OK;
211+
Connector::Ptr connector0(new Connector(Host::Ptr(new Host(Address(host, PORT))),
212+
PROTOCOL_VERSION,
213+
bind_callback(on_connection_error_code, &connect_rc)));
214+
connector0->with_settings(settings)->connect(loop());
215+
uv_run(loop(), UV_RUN_DEFAULT);
216+
EXPECT_EQ(connect_rc, Connector::CONNECTION_ERROR_SSL_VERIFY)
217+
<< "Verification succeeded without certificate.";
218+
219+
// Generate certificate as file (which is used by our mock cluster) and import it
220+
std::ofstream cert_buffer(cert_path);
221+
cert_buffer << cert;
222+
cert_buffer.close();
223+
ASSERT_EQ(uv_os_setenv("SSL_CERT_FILE", cert_path), 0) << "Failed to prepare openssl environment";
224+
ASSERT_EQ(settings.socket_settings.ssl_context->set_default_verify_paths(), CASS_OK)
225+
<< "Failed to import default / system SSL certificates.";
226+
ASSERT_EQ(std::remove(cert_path), 0) << "Failed to cleanup temporary certificate file.";
227+
228+
// Ensure verification succeeds with this certificate.
229+
State state;
230+
Connector::Ptr connector1(new Connector(Host::Ptr(new Host(Address(host, PORT))),
231+
PROTOCOL_VERSION,
232+
bind_callback(on_connection_connected, &state)));
233+
connector1->with_settings(settings)->connect(loop());
234+
uv_run(loop(), UV_RUN_DEFAULT);
235+
EXPECT_EQ(state.status, STATUS_SUCCESS);
236+
}
237+
193238
TEST_F(ConnectionUnitTest, Refused) {
194239
// Don't start cluster
195240

0 commit comments

Comments
 (0)