@@ -212,6 +212,48 @@ test_framework_get_host (void)
212
212
return host ? host : bson_strdup ("localhost" );
213
213
}
214
214
215
+ /*
216
+ *--------------------------------------------------------------------------
217
+ *
218
+ * test_framework_get_admin_user --
219
+ *
220
+ * Get the username of an admin user on the test MongoDB server.
221
+ *
222
+ * Returns:
223
+ * A string you must bson_free, or NULL.
224
+ *
225
+ * Side effects:
226
+ * None.
227
+ *
228
+ *--------------------------------------------------------------------------
229
+ */
230
+ char *
231
+ test_framework_get_admin_user (void )
232
+ {
233
+ return test_framework_getenv ("MONGOC_TEST_USER" );
234
+ }
235
+
236
+ /*
237
+ *--------------------------------------------------------------------------
238
+ *
239
+ * test_framework_get_admin_password --
240
+ *
241
+ * Get the password of an admin user on the test MongoDB server.
242
+ *
243
+ * Returns:
244
+ * A string you must bson_free, or NULL.
245
+ *
246
+ * Side effects:
247
+ * None.
248
+ *
249
+ *--------------------------------------------------------------------------
250
+ */
251
+ char *
252
+ test_framework_get_admin_password (void )
253
+ {
254
+ return test_framework_getenv ("MONGOC_TEST_PASSWORD" );
255
+ }
256
+
215
257
/*
216
258
*--------------------------------------------------------------------------
217
259
*
@@ -271,7 +313,7 @@ uri_has_options (const mongoc_uri_t *uri)
271
313
*
272
314
* Get the connection string of the test MongoDB server. Pass NULL
273
315
* to get the default connection string, or pass a string in to have
274
- * "ssl=true" added if appropriate.
316
+ * username, password, and "ssl=true" added if appropriate.
275
317
*
276
318
* Returns:
277
319
* A string you must bson_free.
@@ -285,15 +327,24 @@ char *
285
327
test_framework_get_uri_str (const char * uri_str )
286
328
{
287
329
char * host = test_framework_get_host ();
330
+ char * user = test_framework_get_admin_user ();
331
+ char * password = test_framework_get_admin_password ();
288
332
char * test_uri_str_base = uri_str ?
289
333
bson_strdup (uri_str ) :
290
334
bson_strdup_printf ("mongodb://%s/" , host );
291
335
292
336
mongoc_uri_t * uri_parsed = mongoc_uri_new (test_uri_str_base );
293
337
char * test_uri_str ;
338
+ char * test_uri_str_auth ;
294
339
295
340
assert (uri_parsed );
296
341
342
+ if ((user && !password ) || (!user && password )) {
343
+ fprintf (stderr , "Specify neither MONGOC_TEST_USER nor"
344
+ " MONGOC_TEST_PASSWORD, or both\n" );
345
+ abort ();
346
+ }
347
+
297
348
/* add "ssl=true" if needed */
298
349
if (test_framework_get_ssl () && !mongoc_uri_get_ssl (uri_parsed )) {
299
350
test_uri_str = bson_strdup_printf (
@@ -304,10 +355,27 @@ test_framework_get_uri_str (const char *uri_str)
304
355
test_uri_str = bson_strdup (test_uri_str_base );
305
356
}
306
357
358
+ /* must we add "user:password"? */
359
+ mongoc_uri_destroy (uri_parsed );
360
+ uri_parsed = mongoc_uri_new (test_uri_str );
361
+ assert (uri_parsed );
362
+
363
+ if (user && password && !mongoc_uri_get_username (uri_parsed )) {
364
+ /* TODO: uri-escape username and password */
365
+ test_uri_str_auth = bson_strdup_printf (
366
+ "mongodb://%s:%s@%s" ,
367
+ user , password , test_uri_str + strlen ("mongodb://" ));
368
+ } else {
369
+ test_uri_str_auth = bson_strdup (test_uri_str );
370
+ }
371
+
307
372
mongoc_uri_destroy (uri_parsed );
308
373
bson_free (host );
309
374
bson_free (test_uri_str_base );
310
- return test_uri_str ;
375
+ bson_free (test_uri_str );
376
+ bson_free (user );
377
+ bson_free (password );
378
+ return test_uri_str_auth ;
311
379
}
312
380
313
381
/*
0 commit comments