@@ -212,6 +212,48 @@ test_framework_get_host (void)
212212 return host ? host : bson_strdup ("localhost" );
213213}
214214
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+
215257/*
216258 *--------------------------------------------------------------------------
217259 *
@@ -271,7 +313,7 @@ uri_has_options (const mongoc_uri_t *uri)
271313 *
272314 * Get the connection string of the test MongoDB server. Pass NULL
273315 * 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.
275317 *
276318 * Returns:
277319 * A string you must bson_free.
@@ -285,15 +327,24 @@ char *
285327test_framework_get_uri_str (const char * uri_str )
286328{
287329 char * host = test_framework_get_host ();
330+ char * user = test_framework_get_admin_user ();
331+ char * password = test_framework_get_admin_password ();
288332 char * test_uri_str_base = uri_str ?
289333 bson_strdup (uri_str ) :
290334 bson_strdup_printf ("mongodb://%s/" , host );
291335
292336 mongoc_uri_t * uri_parsed = mongoc_uri_new (test_uri_str_base );
293337 char * test_uri_str ;
338+ char * test_uri_str_auth ;
294339
295340 assert (uri_parsed );
296341
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+
297348 /* add "ssl=true" if needed */
298349 if (test_framework_get_ssl () && !mongoc_uri_get_ssl (uri_parsed )) {
299350 test_uri_str = bson_strdup_printf (
@@ -304,10 +355,27 @@ test_framework_get_uri_str (const char *uri_str)
304355 test_uri_str = bson_strdup (test_uri_str_base );
305356 }
306357
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+
307372 mongoc_uri_destroy (uri_parsed );
308373 bson_free (host );
309374 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 ;
311379}
312380
313381/*
0 commit comments