|
| 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