Skip to content

Commit e563c5d

Browse files
committed
Fix code review issues #1
1 parent 725dab6 commit e563c5d

20 files changed

+305
-112
lines changed

cpp-driver/gtests/src/unit/mockssandra.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright (c) DataStax, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
#include "mockssandra.hpp"
218

319
#include <assert.h>
@@ -47,12 +63,12 @@ String Ssl::generate_cert(const String& key, const String& cn) {
4763
X509_set_version(x509, 2);
4864
ASN1_INTEGER_set(X509_get_serialNumber(x509), 0);
4965
X509_gmtime_adj(X509_get_notBefore(x509), 0);
50-
X509_gmtime_adj(X509_get_notAfter(x509), (long)60*60*24*365);
66+
X509_gmtime_adj(X509_get_notAfter(x509), static_cast<long>(60 * 60 * 24 * 365));
5167
X509_set_pubkey(x509, pkey);
5268

5369
X509_NAME* name = X509_get_subject_name(x509);
54-
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (const unsigned char*)"US", -1, -1, 0);
55-
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (const unsigned char*)cn.c_str(), -1, -1, 0);
70+
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, reinterpret_cast<const unsigned char*>("US"), -1, -1, 0);
71+
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const unsigned char*>(cn.c_str()), -1, -1, 0);
5672
X509_set_issuer_name(x509, name);
5773
X509_sign(x509, pkey, EVP_md5());
5874

@@ -75,9 +91,10 @@ String Ssl::generate_cert(const String& key, const String& cn) {
7591
namespace internal {
7692

7793
static void print_ssl_error() {
78-
unsigned long n = ERR_get_error();
79-
char buf[1024];
80-
fprintf(stderr, "%s\n", ERR_error_string(n, buf));
94+
unsigned long err = ERR_get_error();
95+
char buf[256];
96+
ERR_error_string_n(err, buf, sizeof(buf));
97+
fprintf(stderr, "%s\n", buf);
8198
}
8299

83100
struct WriteReq {

cpp-driver/gtests/src/unit/mockssandra.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright (c) DataStax, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
#ifndef MOCKSSANDRA_HPP
218
#define MOCKSSANDRA_HPP
319

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright (c) DataStax, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#include "mockssandra_test.hpp"
18+
19+
#include "logger.hpp"
20+
#include "ssl.hpp"
21+
22+
namespace mockssandra {
23+
24+
SimpleClusterTest::SimpleClusterTest(size_t num_nodes,
25+
const RequestHandler* handler)
26+
: cluster_(handler != NULL ? handler
27+
: mockssandra::SimpleRequestHandlerBuilder().build(),
28+
num_nodes)
29+
, saved_log_level_(CASS_LOG_DISABLED) { }
30+
31+
void SimpleClusterTest::SetUp() {
32+
saved_log_level_ = cass::Logger::log_level();
33+
set_log_level(CASS_LOG_DISABLED);
34+
}
35+
36+
void SimpleClusterTest::TearDown() {
37+
stop_all();
38+
cass::Logger::set_log_level(saved_log_level_);
39+
}
40+
41+
void SimpleClusterTest::set_log_level(CassLogLevel log_level) {
42+
cass::Logger::set_log_level(log_level);
43+
}
44+
45+
cass::ConnectionSettings SimpleClusterTest::use_ssl() {
46+
cass::SslContext::Ptr ssl_context(cass::SslContextFactory::create());
47+
48+
String cert = cluster_.use_ssl();
49+
EXPECT_FALSE(cert.empty()) << "Unable to enable SSL";
50+
EXPECT_EQ(ssl_context->add_trusted_cert(cert.data(), cert.size()), CASS_OK);
51+
52+
cass::ConnectionSettings settings;
53+
settings.socket_settings.ssl_context = ssl_context;
54+
settings.socket_settings.hostname_resolution_enabled = true;
55+
56+
return settings;
57+
}
58+
59+
void SimpleClusterTest::start(size_t node) {
60+
if (cluster_.start(node) != 0) {
61+
cluster_.stop_all();
62+
ASSERT_TRUE(false) << "Unable to start node " << node;
63+
}
64+
}
65+
66+
void SimpleClusterTest::stop(size_t node) {
67+
cluster_.stop(node);
68+
}
69+
70+
void SimpleClusterTest::start_all() {
71+
ASSERT_EQ(cluster_.start_all(), 0);
72+
}
73+
74+
void SimpleClusterTest::stop_all() {
75+
cluster_.stop_all();
76+
}
77+
78+
void SimpleClusterTest::use_close_immediately() {
79+
cluster_.use_close_immediately();
80+
}
81+
82+
} // namespace mockssandra
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Copyright (c) DataStax, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#include "mockssandra.hpp"
18+
19+
#include "cassandra.h"
20+
#include "connector.hpp"
21+
22+
#include <gtest/gtest.h>
23+
24+
#ifndef MOCKSSANDRA_TEST_HPP
25+
#define MOCKSSANDRA_TEST_HPP
26+
27+
namespace mockssandra {
28+
29+
/**
30+
* A test that create a simple mockssandra cluster.
31+
*/
32+
class SimpleClusterTest : public testing::Test {
33+
public:
34+
/**
35+
* Construct a cluster with a specified number of nodes and request handler.
36+
*
37+
* @param num_nodes The number of node in the cluster.
38+
* @param handler The request handler.
39+
*/
40+
explicit SimpleClusterTest(size_t num_nodes = 1,
41+
const mockssandra::RequestHandler* handler = NULL);
42+
43+
/**
44+
* Test setup method. This remembers the current state of log level.
45+
*/
46+
virtual void SetUp();
47+
48+
/**
49+
* Test tear down method. This restores the previous log level state and stops
50+
* all cluster nodes.
51+
*/
52+
virtual void TearDown();
53+
54+
/**
55+
* Set the log level for the test. The log level will be restored
56+
* to its previous state at the end of each test.
57+
*
58+
* @param log_level The log level.
59+
*/
60+
void set_log_level(CassLogLevel log_level);
61+
62+
/**
63+
* Setup the cluster to use SSL and return a connection settings object with
64+
* a SSL context, a SSL certificate, and hostname resolution enabled.
65+
*
66+
* @return A connection settings object setup to use SSL.
67+
*/
68+
cass::ConnectionSettings use_ssl();
69+
70+
/**
71+
* Start a specific node.
72+
*
73+
* @param node The node to start.
74+
*/
75+
void start(size_t node);
76+
77+
/**
78+
* Stop a specific node.
79+
*
80+
* @param node The node to stop.
81+
*/
82+
void stop(size_t node);
83+
84+
/**
85+
* Start all nodes in the cluster.
86+
*/
87+
void start_all();
88+
89+
/**
90+
* Stop all nodes in the cluster.
91+
*/
92+
void stop_all();
93+
94+
/**
95+
* Setup the cluster so that connections to the cluster close immediately
96+
* after connection.
97+
*/
98+
void use_close_immediately();
99+
100+
protected:
101+
mockssandra::SimpleCluster cluster_;
102+
CassLogLevel saved_log_level_;
103+
};
104+
105+
} // namespace mockssandra
106+
107+
#endif // MOCKSSANDRA_TEST_HPP

0 commit comments

Comments
 (0)