Skip to content

Commit b50a9df

Browse files
committed
CDRIVER-547 show that concurrent sasl fails
(cherry picked from commit 817fe03)
1 parent 5368256 commit b50a9df

File tree

6 files changed

+167
-1
lines changed

6 files changed

+167
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ set(test-libmongoc-sources
225225
${SOURCE_DIR}/tests/test-mongoc-stream.c
226226
${SOURCE_DIR}/tests/test-mongoc-uri.c
227227
${SOURCE_DIR}/tests/test-mongoc-write-concern.c
228+
${SOURCE_DIR}/tests/test-sasl.c
228229
${SOURCE_DIR}/tests/test-write-commands.c
229230
${SOURCE_DIR}/tests/TestSuite.c
230231
)

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,15 @@ SSL with default options. Configure SSL options with paths
131131
`MONGOC_TEST_SSL_CRL_FILE`. Set the `MONGOC_TEST_SSL_WEAK_CERT_VALIDATION`
132132
environment variable `on` to relax server certificate validation.
133133

134+
The SASL / GSSAPI / Kerberos tests are skipped by default. To run them, set up a
135+
separate `mongod` with Kerberos and set its host and Kerberos principal name
136+
as environment variables:
137+
138+
* `MONGOC_TEST_GSSAPI_HOST`
139+
* `MONGOC_TEST_GSSAPI_USER`
140+
141+
URI-escape the username, for example write "user@realm" as "user%40realm".
142+
The user must be authorized to query `test.collection`.
143+
134144
All tests should pass before submitting a patch.
135145

tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ test_libmongoc_SOURCES = \
9494
tests/test-mongoc-uri.c \
9595
tests/test-mongoc-write-concern.c \
9696
tests/test-libmongoc.h \
97+
tests/test-sasl.c \
9798
tests/test-write-commands.c \
9899
tests/TestSuite.c \
99100
tests/TestSuite.h

tests/test-libmongoc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extern void test_matcher_install (TestSuite *suite);
4040
extern void test_queue_install (TestSuite *suite);
4141
extern void test_read_prefs_install (TestSuite *suite);
4242
extern void test_rpc_install (TestSuite *suite);
43+
extern void test_sasl_install (TestSuite *suite);
4344
extern void test_socket_install (TestSuite *suite);
4445
extern void test_stream_install (TestSuite *suite);
4546
extern void test_uri_install (TestSuite *suite);
@@ -127,7 +128,7 @@ gen_collection_name (const char *str)
127128
*
128129
*--------------------------------------------------------------------------
129130
*/
130-
static char *
131+
char *
131132
test_framework_getenv (const char *name)
132133
{
133134
#ifdef _MSC_VER
@@ -506,6 +507,7 @@ main (int argc,
506507
test_queue_install (&suite);
507508
test_read_prefs_install (&suite);
508509
test_rpc_install (&suite);
510+
test_sasl_install (&suite);
509511
test_socket_install (&suite);
510512
test_stream_install (&suite);
511513
test_uri_install (&suite);

tests/test-libmongoc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void usleep (int64_t usec);
2626

2727
char *gen_collection_name (const char *prefix);
2828
void suppress_one_message (void);
29+
char *test_framework_getenv (const char *name);
2930
char *test_framework_get_host (void);
3031
bool test_framework_get_ssl (void);
3132
char *test_framework_get_uri_str (const char *uri_str);

tests/test-sasl.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright 2015 MongoDB, 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 <mongoc.h>
18+
#include <mongoc-thread-private.h>
19+
20+
#include "TestSuite.h"
21+
#include "test-libmongoc.h"
22+
23+
24+
static const char *GSSAPI_HOST = "MONGOC_TEST_GSSAPI_HOST";
25+
static const char *GSSAPI_USER = "MONGOC_TEST_GSSAPI_USER";
26+
27+
#define NTHREADS 10
28+
#define NLOOPS 10
29+
30+
31+
int
32+
should_run_gssapi_kerberos (void)
33+
{
34+
char *host = test_framework_getenv (GSSAPI_HOST);
35+
char *user = test_framework_getenv (GSSAPI_USER);
36+
int ret = (host && user);
37+
38+
bson_free (host);
39+
bson_free (user);
40+
41+
return ret;
42+
}
43+
44+
45+
struct closure_t
46+
{
47+
mongoc_client_pool_t *pool;
48+
int finished;
49+
mongoc_mutex_t mutex;
50+
};
51+
52+
53+
static void *
54+
gssapi_kerberos_worker (void *data)
55+
{
56+
struct closure_t *closure = (struct closure_t *)data;
57+
mongoc_client_pool_t *pool = closure->pool;
58+
mongoc_client_t *client;
59+
mongoc_collection_t *collection;
60+
mongoc_cursor_t *cursor;
61+
bson_error_t error;
62+
const bson_t *doc;
63+
bson_t query = BSON_INITIALIZER;
64+
int i;
65+
66+
for (i = 0; i < NLOOPS; i++) {
67+
client = mongoc_client_pool_pop (pool);
68+
collection = mongoc_client_get_collection (client, "test", "collection");
69+
cursor = mongoc_collection_find (collection,
70+
MONGOC_QUERY_NONE,
71+
0,
72+
0,
73+
0,
74+
&query,
75+
NULL,
76+
NULL);
77+
78+
if (!mongoc_cursor_next (cursor, &doc) &&
79+
mongoc_cursor_error (cursor, &error)) {
80+
fprintf (stderr, "Cursor Failure: %s\n", error.message);
81+
abort ();
82+
}
83+
84+
mongoc_cursor_destroy (cursor);
85+
mongoc_collection_destroy (collection);
86+
mongoc_client_pool_push (pool, client);
87+
}
88+
89+
bson_destroy (&query);
90+
91+
mongoc_mutex_lock (&closure->mutex);
92+
closure->finished++;
93+
mongoc_mutex_unlock (&closure->mutex);
94+
95+
return NULL;
96+
}
97+
98+
99+
static void
100+
test_gssapi_kerberos ()
101+
{
102+
char *host = test_framework_getenv (GSSAPI_HOST);
103+
char *user = test_framework_getenv (GSSAPI_USER);
104+
char *uri_str;
105+
mongoc_uri_t *uri;
106+
struct closure_t closure = { 0 };
107+
int i;
108+
mongoc_thread_t threads[NTHREADS];
109+
110+
assert (host && user);
111+
112+
mongoc_mutex_init (&closure.mutex);
113+
114+
uri_str = bson_strdup_printf (
115+
"mongodb://%s@%s/?authMechanism=GSSAPI&serverselectiontimeoutms=1000",
116+
user, host);
117+
118+
uri = mongoc_uri_new (uri_str);
119+
closure.pool = mongoc_client_pool_new (uri);
120+
121+
for (i = 0; i < NTHREADS; i++) {
122+
mongoc_thread_create (&threads[i],
123+
gssapi_kerberos_worker,
124+
(void *)&closure);
125+
}
126+
127+
for (i = 0; i < NTHREADS; i++) {
128+
mongoc_thread_join (threads[i]);
129+
}
130+
131+
mongoc_mutex_lock (&closure.mutex);
132+
ASSERT_CMPINT (NTHREADS, ==, closure.finished);
133+
mongoc_mutex_unlock (&closure.mutex);
134+
135+
mongoc_client_pool_destroy (closure.pool);
136+
mongoc_mutex_destroy (&closure.mutex);
137+
mongoc_uri_destroy (uri);
138+
bson_free (uri_str);
139+
bson_free (host);
140+
bson_free (user);
141+
}
142+
143+
144+
void
145+
test_sasl_install (TestSuite *suite)
146+
{
147+
TestSuite_AddFull (suite,
148+
"/SASL/gssapi_kerberos",
149+
test_gssapi_kerberos,
150+
should_run_gssapi_kerberos);
151+
}

0 commit comments

Comments
 (0)