static void test_mongoc_client_authenticate (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; mongoc_cursor_t *cursor; const bson_t *doc; bson_error_t error; char *username; char *uri; bool r; bson_t q; username = gen_test_user (); uri = gen_good_uri (username); /* * Add a user to the test database. */ client = mongoc_client_new(gTestUri); database = mongoc_client_get_database(client, "test"); mongoc_database_remove_user (database, username, &error); r = mongoc_database_add_user(database, username, "testpass", NULL, NULL, &error); ASSERT_CMPINT(r, ==, 1); mongoc_database_destroy(database); mongoc_client_destroy(client); /* * Try authenticating with that user. */ bson_init(&q); client = mongoc_client_new(uri); collection = mongoc_client_get_collection(client, "test", "test"); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, &q, NULL, NULL); r = mongoc_cursor_next(cursor, &doc); if (!r) { r = mongoc_cursor_error(cursor, &error); if (r) { MONGOC_ERROR("Authentication failure: \"%s\"", error.message); } assert(!r); } mongoc_cursor_destroy(cursor); /* * Remove all test users. */ database = mongoc_client_get_database (client, "test"); r = mongoc_database_remove_all_users (database, &error); assert (r); mongoc_database_destroy (database); mongoc_collection_destroy(collection); mongoc_client_destroy(client); bson_free (username); bson_free (uri); }
&q, NULL, NULL); r = mongoc_cursor_next(cursor, &doc); if (!r) { r = mongoc_cursor_error(cursor, &error); if (r) { MONGOC_ERROR("Authentication failure: \"%s\"", error.message); } ASSERT_CMPINT(r, ==, false); } mongoc_cursor_destroy(cursor); /* * Remove all test users. */ database = mongoc_client_get_database (client, "test"); r = mongoc_database_remove_all_users (database, &error); ASSERT_CMPINT(r, ==, true); mongoc_database_destroy (database); mongoc_collection_destroy(collection); mongoc_client_destroy(client); bson_free (username); bson_free (uri); } static void test_mongoc_client_authenticate_failure (void) { mongoc_collection_t *collection;