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
|
#include <string.h>
#include "ha-test.h"
#include "mongoc-client-private.h"
#include "mongoc-tests.h"
#include "mongoc-write-concern-private.h"
#include "TestSuite.h"
#define TRUST_DIR "tests/trust_dir"
#define CAFILE TRUST_DIR "/verify/mongo_root.pem"
#define PEMFILE_LOCALHOST TRUST_DIR "/keys/127.0.0.1.pem"
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "test"
static char * gTestCAFile;
static char * gTestPEMFileLocalhost;
static bool use_pool;
static void
test_replica_set_ssl_client(void)
{
mongoc_collection_t *collection;
mongoc_client_t *client;
mongoc_client_pool_t *pool = NULL;
ha_replica_set_t *replica_set;
bson_error_t error;
bson_t b;
mongoc_ssl_opt_t sopt = { 0 };
sopt.pem_file = gTestPEMFileLocalhost;
sopt.ca_file = gTestCAFile;
replica_set = ha_replica_set_new("repltest1");
ha_replica_set_ssl(replica_set, &sopt);
ha_replica_set_add_replica(replica_set, "replica1");
ha_replica_set_add_replica(replica_set, "replica2");
ha_replica_set_add_replica(replica_set, "replica3");
ha_replica_set_start(replica_set);
ha_replica_set_wait_for_healthy(replica_set);
if (use_pool) {
pool = ha_replica_set_create_client_pool(replica_set);
client = mongoc_client_pool_pop (pool);
} else {
client = ha_replica_set_create_client(replica_set);
}
assert(client);
collection = mongoc_client_get_collection(client, "test", "test");
assert(collection);
bson_init(&b);
bson_append_utf8(&b, "hello", -1, "world", -1);
ASSERT_OR_PRINT (mongoc_collection_insert(collection, MONGOC_INSERT_NONE,
&b, NULL, &error), error);
mongoc_collection_destroy(collection);
if (use_pool) {
mongoc_client_pool_push (pool, client);
mongoc_client_pool_destroy (pool);
} else {
mongoc_client_destroy(client);
}
bson_destroy(&b);
ha_replica_set_shutdown(replica_set);
ha_replica_set_destroy(replica_set);
}
static void
log_handler (mongoc_log_level_t log_level,
const char *domain,
const char *message,
void *user_data)
{
/* Do Nothing */
}
int
main (int argc, /* IN */
char *argv[]) /* IN */
{
char *cwd;
char buf[1024];
if (argc <= 1 || !!strcmp (argv[1], "-v")) {
mongoc_log_set_handler (log_handler, NULL);
}
mongoc_init ();
cwd = getcwd(buf, sizeof(buf));
assert(cwd);
gTestCAFile = bson_strdup_printf("%s/" CAFILE, cwd);
gTestPEMFileLocalhost = bson_strdup_printf("%s/" PEMFILE_LOCALHOST, cwd);
use_pool = false;
run_test("/ReplicaSet/single/ssl/client", &test_replica_set_ssl_client);
use_pool = true;
run_test("/ReplicaSet/pool/ssl/client", &test_replica_set_ssl_client);
bson_free(gTestCAFile);
bson_free(gTestPEMFileLocalhost);
mongoc_cleanup();
return 0;
}
|