summaryrefslogtreecommitdiff
path: root/src/mongo/client/embedded/libmongodbcapi_test.cpp
blob: 69be2750540baedfad11434c41ad5e836d491a41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/**
 *    Copyright (C) 2017 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */


#include "mongo/client/embedded/libmongodbcapi.h"

#include <set>

#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/json.h"
#include "mongo/db/server_options.h"
#include "mongo/stdx/memory.h"
#include "mongo/unittest/temp_dir.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/net/message.h"
#include "mongo/util/net/op_msg.h"
#include "mongo/util/options_parser/environment.h"
#include "mongo/util/options_parser/option_section.h"
#include "mongo/util/options_parser/options_parser.h"
#include "mongo/util/quick_exit.h"
#include "mongo/util/shared_buffer.h"
#include "mongo/util/signal_handlers_synchronous.h"

namespace moe = mongo::optionenvironment;

namespace {

std::unique_ptr<mongo::unittest::TempDir> globalTempDir;

struct MongodCapiCleaner {
    void operator()(libmongodbcapi_client* const p) {
        if (p) {
            libmongodbcapi_db_client_destroy(p);
        }
    }
};

using MongoDBCAPIClientPtr = std::unique_ptr<libmongodbcapi_client, MongodCapiCleaner>;

class MongodbCAPITest : public mongo::unittest::Test {
protected:
    void setUp() {
        if (!globalTempDir) {
            globalTempDir = mongo::stdx::make_unique<mongo::unittest::TempDir>("embedded_mongo");
        }
        const char* argv[] = {
            "mongo_embedded_capi_test", "--port", "0", "--dbpath", globalTempDir->path().c_str()};
        db = libmongodbcapi_db_new(5, argv, nullptr);
        ASSERT(db != nullptr);
    }

    void tearDown() {
        libmongodbcapi_db_destroy(db);
        ASSERT_EQUALS(libmongodbcapi_get_last_error(), LIBMONGODB_CAPI_ERROR_SUCCESS);
    }

    libmongodbcapi_db* getDB() {
        return db;
    }

    MongoDBCAPIClientPtr createClient() {
        MongoDBCAPIClientPtr client(libmongodbcapi_db_client_new(db));
        ASSERT(client != nullptr);
        ASSERT_EQUALS(libmongodbcapi_get_last_error(), LIBMONGODB_CAPI_ERROR_SUCCESS);
        return client;
    }

    mongo::Message messageFromBuffer(void* data, size_t dataLen) {
        auto sb = mongo::SharedBuffer::allocate(dataLen);
        memcpy(sb.get(), data, dataLen);
        mongo::Message msg(std::move(sb));
        return msg;
    }

private:
    libmongodbcapi_db* db;
};

TEST_F(MongodbCAPITest, CreateAndDestroyDB) {
    // Test the setUp() and tearDown() test fixtures
}

TEST_F(MongodbCAPITest, CreateAndDestroyDBAndClient) {
    auto client = createClient();
}

// This test is to make sure that destroying the db will destroy all of its clients
// This test will only fail under ASAN
TEST_F(MongodbCAPITest, DoNotDestroyClient) {
    createClient().release();
}

TEST_F(MongodbCAPITest, CreateMultipleClients) {
    const int numClients = 10;
    std::set<MongoDBCAPIClientPtr> clients;
    for (int i = 0; i < numClients; i++) {
        clients.insert(createClient());
    }

    // ensure that each client is unique by making sure that the set size equals the number of
    // clients instantiated
    ASSERT_EQUALS(static_cast<int>(clients.size()), numClients);
}

TEST_F(MongodbCAPITest, DBPump) {
    libmongodbcapi_db* db = getDB();
    int err = libmongodbcapi_db_pump(db);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);
}

TEST_F(MongodbCAPITest, IsMaster) {
    // create the client object
    auto client = createClient();

    // craft the isMaster message
    mongo::BSONObj inputObj = mongo::fromjson("{isMaster: 1}");
    auto inputOpMsg = mongo::OpMsgRequest::fromDBAndBody("admin", inputObj);
    auto inputMessage = inputOpMsg.serialize();


    // declare the output size and pointer
    void* output;
    size_t outputSize;

    // call the wire protocol
    int err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), inputMessage.buf(), inputMessage.size(), &output, &outputSize);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    // convert the shared buffer to a mongo::message and ensure that it is valid
    auto outputMessage = messageFromBuffer(output, outputSize);
    ASSERT(outputMessage.size() > 0);
    ASSERT(outputMessage.operation() == inputMessage.operation());

    // convert the message into an OpMessage to examine its BSON
    auto outputOpMsg = mongo::OpMsg::parseOwned(outputMessage);
    ASSERT(outputOpMsg.body.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputOpMsg.body.getBoolField("ismaster"));
}


TEST_F(MongodbCAPITest, InsertDocument) {
    auto client = createClient();

    mongo::BSONObj insertObj = mongo::fromjson(
        "{insert: 'collection_name', documents: [{firstName: 'Mongo', lastName: 'DB', age: 10}]}");
    auto insertOpMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", insertObj);
    mongo::Message insertMessage = insertOpMsg.serialize();

    void* output;
    size_t outputSize;
    int err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), insertMessage.buf(), insertMessage.size(), &output, &outputSize);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage = messageFromBuffer(output, outputSize);
    ASSERT(outputMessage.size() > 0);
    ASSERT(outputMessage.operation() == insertMessage.operation());

    auto outputOpMsg = mongo::OpMsg::parseOwned(outputMessage);
    auto outputBSON = outputOpMsg.body;

    ASSERT(outputBSON.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON.hasField("n"));
    ASSERT(outputBSON.getIntField("n") == 1);
    ASSERT(outputBSON.hasField("ok"));
    ASSERT(outputBSON.getField("ok").numberDouble() == 1.0);
}

TEST_F(MongodbCAPITest, InsertMultipleDocuments) {
    auto client = createClient();

    mongo::BSONObj insertObj = mongo::fromjson(
        "{insert: 'collection_name', documents: [{firstName: 'doc1FirstName', lastName: "
        "'doc1LastName', age: 30}, {firstName: 'doc2FirstName', lastName: 'doc2LastName', age: "
        "20}]}");
    auto insertOpMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", insertObj);
    mongo::Message insertMessage = insertOpMsg.serialize();

    void* output;
    size_t outputSize;
    int err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), insertMessage.buf(), insertMessage.size(), &output, &outputSize);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage = messageFromBuffer(output, outputSize);
    ASSERT(outputMessage.size() > 0);
    ASSERT(outputMessage.operation() == insertMessage.operation());

    auto outputOpMsg = mongo::OpMsg::parseOwned(outputMessage);
    auto outputBSON = outputOpMsg.body;

    ASSERT(outputBSON.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON.hasField("n"));
    ASSERT(outputBSON.getIntField("n") == 2);
    ASSERT(outputBSON.hasField("ok"));
    ASSERT(outputBSON.getField("ok").numberDouble() == 1.0);
}

TEST_F(MongodbCAPITest, ReadDB) {
    auto client = createClient();

    mongo::BSONObj findObj = mongo::fromjson("{find: 'collection_name', limit: 2}");
    auto findMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", findObj);
    mongo::Message findMessage = findMsg.serialize();

    void* output;
    size_t outputSize;
    int err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), findMessage.buf(), findMessage.size(), &output, &outputSize);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage = messageFromBuffer(output, outputSize);
    ASSERT(outputMessage.size() > 0);

    auto outputOpMsg = mongo::OpMsg::parseOwned(outputMessage);
    mongo::BSONObj outputBSON = outputOpMsg.body;

    ASSERT(outputBSON.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON.hasField("cursor"));
    ASSERT(outputBSON.getField("cursor").embeddedObject().hasField("firstBatch"));
    mongo::BSONObj arrObj =
        outputBSON.getField("cursor").embeddedObject().getField("firstBatch").embeddedObject();
    ASSERT(arrObj.couldBeArray());

    mongo::BSONObjIterator i(arrObj);
    int index = 0;
    while (i.moreWithEOO()) {
        mongo::BSONElement e = i.next();
        if (e.eoo())
            break;
        index++;
    }
    ASSERT(index == 2);
}

TEST_F(MongodbCAPITest, InsertAndRead) {
    auto client = createClient();

    mongo::BSONObj insertObj = mongo::fromjson(
        "{insert: 'collection_name', documents: [{firstName: 'Mongo', lastName: 'DB', age: 10}]}");
    auto insertOpMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", insertObj);
    mongo::Message insertMessage = insertOpMsg.serialize();

    void* output;
    size_t outputSize;
    int err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), insertMessage.buf(), insertMessage.size(), &output, &outputSize);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage1 = messageFromBuffer(output, outputSize);
    ASSERT(outputMessage1.size() > 0);
    ASSERT(outputMessage1.operation() == insertMessage.operation());

    auto outputOpMsg1 = mongo::OpMsg::parseOwned(outputMessage1);
    mongo::BSONObj outputBSON1 = outputOpMsg1.body;
    ASSERT(outputBSON1.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON1.hasField("n"));
    ASSERT(outputBSON1.getIntField("n") == 1);
    ASSERT(outputBSON1.hasField("ok"));
    ASSERT(outputBSON1.getField("ok").numberDouble() == 1.0);

    mongo::BSONObj findObj = mongo::fromjson("{find: 'collection_name', limit: 1}");
    auto findMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", findObj);
    mongo::Message findMessage = findMsg.serialize();

    void* output2;
    size_t outputSize2;
    err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), findMessage.buf(), findMessage.size(), &output2, &outputSize2);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage2 = messageFromBuffer(output2, outputSize2);
    ASSERT(outputMessage2.size() > 0);

    auto outputOpMsg2 = mongo::OpMsg::parseOwned(outputMessage2);
    mongo::BSONObj outputBSON2 = outputOpMsg2.body;

    ASSERT(outputBSON2.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON2.hasField("cursor"));
    ASSERT(outputBSON2.getField("cursor").embeddedObject().hasField("firstBatch"));
    mongo::BSONObj arrObj =
        outputBSON2.getField("cursor").embeddedObject().getField("firstBatch").embeddedObject();
    ASSERT(arrObj.couldBeArray());

    mongo::BSONObjIterator i(arrObj);
    int index = 0;
    while (i.moreWithEOO()) {
        mongo::BSONElement e = i.next();
        if (e.eoo())
            break;
        index++;
    }
    ASSERT(index == 1);
}

TEST_F(MongodbCAPITest, InsertAndReadDifferentClients) {
    auto client1 = createClient();
    auto client2 = createClient();

    mongo::BSONObj insertObj = mongo::fromjson(
        "{insert: 'collection_name', documents: [{firstName: 'Mongo', lastName: 'DB', age: 10}]}");
    auto insertOpMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", insertObj);
    mongo::Message insertMessage = insertOpMsg.serialize();

    void* output;
    size_t outputSize;
    int err = libmongodbcapi_db_client_wire_protocol_rpc(
        client1.get(), insertMessage.buf(), insertMessage.size(), &output, &outputSize);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage1 = messageFromBuffer(output, outputSize);
    ASSERT(outputMessage1.size() > 0);
    ASSERT(outputMessage1.operation() == insertMessage.operation());

    auto outputOpMsg1 = mongo::OpMsg::parseOwned(outputMessage1);
    mongo::BSONObj outputBSON1 = outputOpMsg1.body;
    ASSERT(outputBSON1.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON1.hasField("n"));
    ASSERT(outputBSON1.getIntField("n") == 1);
    ASSERT(outputBSON1.hasField("ok"));
    ASSERT(outputBSON1.getField("ok").numberDouble() == 1.0);

    mongo::BSONObj findObj = mongo::fromjson("{find: 'collection_name', limit: 1}");
    auto findMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", findObj);
    mongo::Message findMessage = findMsg.serialize();

    void* output2;
    size_t outputSize2;
    err = libmongodbcapi_db_client_wire_protocol_rpc(
        client2.get(), findMessage.buf(), findMessage.size(), &output2, &outputSize2);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage2 = messageFromBuffer(output2, outputSize2);
    ASSERT(outputMessage2.size() > 0);

    auto outputOpMsg2 = mongo::OpMsg::parseOwned(outputMessage2);
    mongo::BSONObj outputBSON2 = outputOpMsg2.body;

    ASSERT(outputBSON2.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON2.hasField("cursor"));
    ASSERT(outputBSON2.getField("cursor").embeddedObject().hasField("firstBatch"));
    mongo::BSONObj arrObj =
        outputBSON2.getField("cursor").embeddedObject().getField("firstBatch").embeddedObject();
    ASSERT(arrObj.couldBeArray());

    mongo::BSONObjIterator i(arrObj);
    int index = 0;
    while (i.moreWithEOO()) {
        mongo::BSONElement e = i.next();
        if (e.eoo())
            break;
        index++;
    }
    ASSERT(index == 1);
}

TEST_F(MongodbCAPITest, InsertAndDelete) {
    auto client = createClient();
    mongo::BSONObj insertObj = mongo::fromjson(
        "{insert: 'collection_name', documents: [{firstName: 'toDelete', lastName: 'notImportant', "
        "age: 10}]}");
    auto insertOpMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", insertObj);
    mongo::Message insertMessage = insertOpMsg.serialize();

    void* output;
    size_t outputSize;
    int err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), insertMessage.buf(), insertMessage.size(), &output, &outputSize);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage1 = messageFromBuffer(output, outputSize);
    ASSERT(outputMessage1.size() > 0);
    ASSERT(outputMessage1.operation() == insertMessage.operation());

    auto outputOpMsg1 = mongo::OpMsg::parseOwned(outputMessage1);
    mongo::BSONObj outputBSON1 = outputOpMsg1.body;
    ASSERT(outputBSON1.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON1.hasField("n"));
    ASSERT(outputBSON1.getIntField("n") == 1);
    ASSERT(outputBSON1.hasField("ok"));
    ASSERT(outputBSON1.getField("ok").numberDouble() == 1.0);


    // Delete
    mongo::BSONObj deleteObj = mongo::fromjson(
        "{delete: 'collection_name', deletes:   [{q: {firstName: 'toDelete', age: 10}, limit: "
        "1}]}");
    auto deleteOpMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", deleteObj);
    mongo::Message deleteMessage = deleteOpMsg.serialize();

    void* output2;
    size_t outputSize2;
    err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), deleteMessage.buf(), deleteMessage.size(), &output2, &outputSize2);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage2 = messageFromBuffer(output2, outputSize2);
    ASSERT(outputMessage2.size() > 0);
    ASSERT(outputMessage2.operation() == deleteMessage.operation());

    auto outputOpMsg2 = mongo::OpMsg::parseOwned(outputMessage2);
    mongo::BSONObj outputBSON2 = outputOpMsg2.body;
    ASSERT(outputBSON2.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON2.hasField("n"));
    ASSERT(outputBSON2.getIntField("n") == 1);
    ASSERT(outputBSON2.hasField("ok"));
    ASSERT(outputBSON2.getField("ok").numberDouble() == 1.0);
}


TEST_F(MongodbCAPITest, InsertAndUpdate) {
    auto client = createClient();

    mongo::BSONObj insertObj = mongo::fromjson(
        "{insert: 'collection_name', documents: [{firstName: 'toUpdate', lastName: 'notImportant', "
        "age: 10}]}");
    auto insertOpMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", insertObj);
    mongo::Message insertMessage = insertOpMsg.serialize();

    void* output;
    size_t outputSize;
    int err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), insertMessage.buf(), insertMessage.size(), &output, &outputSize);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage1 = messageFromBuffer(output, outputSize);
    ASSERT(outputMessage1.size() > 0);
    ASSERT(outputMessage1.operation() == insertMessage.operation());

    auto outputOpMsg1 = mongo::OpMsg::parseOwned(outputMessage1);
    mongo::BSONObj outputBSON1 = outputOpMsg1.body;
    ASSERT(outputBSON1.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON1.hasField("n"));
    ASSERT(outputBSON1.getIntField("n") == 1);
    ASSERT(outputBSON1.hasField("ok"));
    ASSERT(outputBSON1.getField("ok").numberDouble() == 1.0);


    // Update
    mongo::BSONObj updateObj = mongo::fromjson(
        "{update: 'collection_name', updates: [ {q: {firstName: 'toUpdate', age: 10}, u: {'$inc': "
        "{age: 5}}}]}");
    auto updateOpMsg = mongo::OpMsgRequest::fromDBAndBody("db_name", updateObj);
    mongo::Message updateMessage = updateOpMsg.serialize();

    void* output2;
    size_t outputSize2;
    err = libmongodbcapi_db_client_wire_protocol_rpc(
        client.get(), updateMessage.buf(), updateMessage.size(), &output2, &outputSize2);
    ASSERT_EQUALS(err, LIBMONGODB_CAPI_ERROR_SUCCESS);

    auto outputMessage2 = messageFromBuffer(output2, outputSize2);
    ASSERT(outputMessage2.size() > 0);
    ASSERT(outputMessage2.operation() == updateMessage.operation());

    auto outputOpMsg2 = mongo::OpMsg::parseOwned(outputMessage2);
    mongo::BSONObj outputBSON2 = outputOpMsg2.body;
    ASSERT(outputBSON2.valid(mongo::BSONVersion::kLatest));
    ASSERT(outputBSON2.hasField("ok"));
    ASSERT(outputBSON2.getField("ok").numberDouble() == 1.0);
    ASSERT(outputBSON2.hasField("nModified"));
    ASSERT(outputBSON2.getIntField("nModified") == 1);
}

// This test is temporary to make sure that only one database can be created
// This restriction may be relaxed at a later time
TEST_F(MongodbCAPITest, CreateMultipleDBs) {
    libmongodbcapi_db* db2 = libmongodbcapi_db_new(0, nullptr, nullptr);
    ASSERT(db2 == nullptr);
    ASSERT_EQUALS(libmongodbcapi_get_last_error(), LIBMONGODB_CAPI_ERROR_UNKNOWN);
}
}  // namespace

// Define main function as an entry to these tests.
// These test functions cannot use the main() defined for unittests because they
// call runGlobalInitializers(). The embedded C API calls mongoDbMain() which
// calls runGlobalInitializers().
int main(int argc, char** argv, char** envp) {
    moe::OptionsParser parser;
    moe::Environment environment;
    moe::OptionSection options;
    std::map<std::string, std::string> env;

    options.addOptionChaining(
        "tempPath", "tempPath", moe::String, "directory to place mongo::TempDir subdirectories");
    std::vector<std::string> argVector(argv, argv + argc);
    mongo::Status ret = parser.run(options, argVector, env, &environment);
    if (!ret.isOK()) {
        std::cerr << options.helpString();
        return EXIT_FAILURE;
    }
    if (environment.count("tempPath")) {
        ::mongo::unittest::TempDir::setTempPath(environment["tempPath"].as<std::string>());
    }

    ::mongo::clearSignalMask();
    ::mongo::setupSynchronousSignalHandlers();
    ::mongo::serverGlobalParams.noUnixSocket = true;
    ::mongo::unittest::setupTestLogger();
    auto result = ::mongo::unittest::Suite::run(std::vector<std::string>(), "", 1);
    globalTempDir.reset();
    mongo::quickExit(result);
}