Skip to content

Commit ad701c2

Browse files
committed
prepare to test more kinds of TLS errors
1 parent 22eaf49 commit ad701c2

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ if (OPENSSL_FOUND)
234234
${SOURCE_DIR}/tests/test-x509.c
235235
${SOURCE_DIR}/tests/ssl-test.c
236236
${SOURCE_DIR}/tests/test-mongoc-stream-tls.c
237-
${SOURCE_DIR}/tests/test-mongoc-stream-tls-hangup.c)
237+
${SOURCE_DIR}/tests/test-mongoc-stream-tls-error.c)
238238
mongoc_add_test(test-replica-set-ssl FALSE
239239
${SOURCE_DIR}/tests/test-replica-set-ssl.c
240240
${SOURCE_DIR}/tests/ha-test.c

tests/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ if ENABLE_SSL
101101
test_libmongoc_SOURCES += \
102102
tests/test-x509.c \
103103
tests/test-mongoc-stream-tls.c \
104-
tests/test-mongoc-stream-tls-hangup.c \
104+
tests/test-mongoc-stream-tls-error.c \
105105
tests/ssl-test.c \
106106
tests/ssl-test.h
107107
endif

tests/ssl-test.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
#include <mongoc-thread-private.h>
44

5+
typedef enum ssl_test_behavior {
6+
SSL_TEST_BEHAVIOR_NORMAL,
7+
SSL_TEST_BEHAVIOR_HANGUP_AFTER_HANDSHAKE,
8+
SSL_TEST_BEHAVIOR_STALL_BEFORE_HANDSHAKE,
9+
} ssl_test_behavior_t;
10+
511
typedef enum ssl_test_state {
612
SSL_TEST_CRASH,
713
SSL_TEST_SUCCESS,
@@ -19,14 +25,15 @@ typedef struct ssl_test_result {
1925

2026
typedef struct ssl_test_data
2127
{
22-
mongoc_ssl_opt_t *client;
23-
mongoc_ssl_opt_t *server;
24-
const char *host;
25-
unsigned short server_port;
26-
mongoc_cond_t cond;
27-
mongoc_mutex_t cond_mutex;
28-
ssl_test_result_t *client_result;
29-
ssl_test_result_t *server_result;
28+
mongoc_ssl_opt_t *client;
29+
mongoc_ssl_opt_t *server;
30+
ssl_test_behavior_t behavior;
31+
const char *host;
32+
unsigned short server_port;
33+
mongoc_cond_t cond;
34+
mongoc_mutex_t cond_mutex;
35+
ssl_test_result_t *client_result;
36+
ssl_test_result_t *server_result;
3037
} ssl_test_data_t;
3138

3239
void

tests/test-libmongoc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extern void test_write_concern_install (TestSuite *suite);
4848
#ifdef MONGOC_ENABLE_SSL
4949
extern void test_x509_install (TestSuite *suite);
5050
extern void test_stream_tls_install (TestSuite *suite);
51-
extern void test_stream_tls_hangup_install (TestSuite *suite);
51+
extern void test_stream_tls_error_install (TestSuite *suite);
5252
#endif
5353

5454

@@ -427,7 +427,7 @@ main (int argc,
427427
#ifdef MONGOC_ENABLE_SSL
428428
test_x509_install (&suite);
429429
test_stream_tls_install (&suite);
430-
test_stream_tls_hangup_install (&suite);
430+
test_stream_tls_error_install (&suite);
431431
#endif
432432

433433
ret = TestSuite_Run (&suite);

tests/test-mongoc-stream-tls-hangup.c renamed to tests/test-mongoc-stream-tls-error.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* 7. hangs up
1919
*/
2020
static void *
21-
ssl_hangup_server (void *ptr)
21+
ssl_error_server (void *ptr)
2222
{
2323
ssl_test_data_t *data = (ssl_test_data_t *)ptr;
2424

@@ -69,15 +69,22 @@ ssl_hangup_server (void *ptr)
6969
ssl_stream = mongoc_stream_tls_new (sock_stream, data->server, 0);
7070
assert (ssl_stream);
7171

72-
r = mongoc_stream_tls_do_handshake (ssl_stream, TIMEOUT);
73-
assert (r);
72+
if (data->behavior != SSL_TEST_BEHAVIOR_STALL_BEFORE_HANDSHAKE) {
73+
r = mongoc_stream_tls_do_handshake (ssl_stream, TIMEOUT);
74+
assert (r);
7475

75-
r = mongoc_stream_readv (ssl_stream, &iov, 1, 1, TIMEOUT);
76-
assert (r == 1);
76+
r = mongoc_stream_readv (ssl_stream, &iov, 1, 1, TIMEOUT);
77+
assert (r == 1);
7778

78-
mongoc_stream_close (ssl_stream);
79-
mongoc_stream_destroy (ssl_stream);
80-
mongoc_socket_destroy (listen_sock);
79+
/* this test function only implements two behaviors so far */
80+
ASSERT_CMPINT (data->behavior,
81+
==,
82+
SSL_TEST_BEHAVIOR_HANGUP_AFTER_HANDSHAKE);
83+
84+
mongoc_stream_close (ssl_stream);
85+
mongoc_stream_destroy (ssl_stream);
86+
mongoc_socket_destroy (listen_sock);
87+
}
8188

8289
data->server_result->result = SSL_TEST_SUCCESS;
8390

@@ -173,14 +180,15 @@ test_mongoc_tls_hangup (void)
173180

174181
data.server = &sopt;
175182
data.client = &copt;
183+
data.behavior = SSL_TEST_BEHAVIOR_HANGUP_AFTER_HANDSHAKE;
176184
data.server_result = &sr;
177185
data.client_result = &cr;
178186
data.host = "localhost";
179187

180188
mongoc_mutex_init (&data.cond_mutex);
181189
mongoc_cond_init (&data.cond);
182190

183-
r = mongoc_thread_create (threads, &ssl_hangup_server, &data);
191+
r = mongoc_thread_create (threads, &ssl_error_server, &data);
184192
assert (r == 0);
185193

186194
r = mongoc_thread_create (threads + 1, &ssl_hangup_client, &data);
@@ -199,7 +207,7 @@ test_mongoc_tls_hangup (void)
199207
}
200208

201209
void
202-
test_stream_tls_hangup_install (TestSuite *suite)
210+
test_stream_tls_error_install (TestSuite *suite)
203211
{
204212
TestSuite_Add (suite, "/TLS/hangup", test_mongoc_tls_hangup);
205213
}

0 commit comments

Comments
 (0)