Ejemplo n.º 1
0
void
test_func_mongo_sync_ssl_connect (void)
{
  char trusted_fp[64];
  int fd;

  fd = open("./ssl/3party/server.sign", O_RDONLY);  
  read(fd, trusted_fp, 64);
  close(fd);

  mongo_sync_connection *conn = NULL;
  // 1. Trusted Fingerprints Test (for 3party)
  GList *trusted_fps = NULL; 
  trusted_fps = g_list_append (trusted_fps, "SHA1:00:DE:AD:BE:EF"); // invalid fingerprint

  mongo_ssl_set_trusted_fingerprints (config.ssl_settings, trusted_fps);

  conn = mongo_sync_ssl_connect (config.primary_host, config.primary_port, TRUE, config.ssl_settings);

  ok (conn == NULL && (mongo_ssl_get_last_verify_result (config.ssl_settings) == MONGO_SSL_V_ERR_UNTRUSTED_FP), 
     "SSL connection fails with untrusted fingerprint");

  g_list_append (trusted_fps, trusted_fp); // 3party/server.pem

  conn = mongo_sync_ssl_connect (config.primary_host, config.primary_port, TRUE, config.ssl_settings);
  
  ok (((conn != NULL) && (mongo_ssl_get_last_verify_result (config.ssl_settings) == MONGO_SSL_V_OK_TRUSTED_FP)), 
     "SSL connection works with trusted fingerprint");
  
  mongo_sync_disconnect (conn);

  // 2. Trusted DN Test (for 3party) 
  mongo_ssl_set_trusted_fingerprints (config.ssl_settings, NULL);
  GList *trusted_DNs = NULL;
  trusted_DNs = g_list_append (trusted_DNs, "*, O=Example Inc, ST=Some-State, C=*");

  mongo_ssl_set_trusted_DNs (config.ssl_settings, trusted_DNs);
  
  conn = mongo_sync_ssl_connect (config.primary_host, config.primary_port, TRUE, config.ssl_settings);

  ok ((conn == NULL) && (mongo_ssl_get_last_verify_result (config.ssl_settings) == MONGO_SSL_V_ERR_UNTRUSTED_DN), 
      "SSL connection fails with untrusted DN");
  
  g_list_append (trusted_DNs, 
                 "CN=127.0.0.1, ST=hu, C=hu, [email protected], O=libmongo_client_test, OU=test_server");

  conn = mongo_sync_ssl_connect (config.primary_host, config.primary_port, TRUE, config.ssl_settings);

  ok ((conn != NULL) && (mongo_ssl_get_last_verify_result (config.ssl_settings) == MONGO_SSL_V_OK_ALL), 
      "SSL connection works with trusted DN");

  mongo_sync_disconnect (conn);
}
void
test_mongo_sync_get_set_auto_reconnect (void)
{
  mongo_sync_connection *c;

  c = test_make_fake_sync_conn (-1, FALSE);

  errno = 0;
  ok (mongo_sync_conn_get_auto_reconnect (NULL) == FALSE,
      "mongo_sync_conn_get_auto_reconnect() returns FALSE with a "
      "NULL connection");
  cmp_ok (errno, "==", ENOTCONN,
	  "errno is now set to ENOTCONN");

  ok (mongo_sync_conn_get_auto_reconnect (c) == FALSE,
      "mongo_sync_get_auto_reconnect() works");
  cmp_ok (errno, "==", 0,
	  "errno is now cleared");

  errno = 0;
  mongo_sync_conn_set_auto_reconnect (NULL, TRUE);
  cmp_ok (errno, "==", ENOTCONN,
	  "errno is set to ENOTCONN after "
	  "mongo_sync_conn_set_auto_reconnect(NULL)");

  ok (mongo_sync_conn_set_auto_reconnect (c, TRUE),
      "mongo_sync_auto_reconnect() works");
  ok (mongo_sync_conn_get_auto_reconnect (c) == TRUE,
      "mongo_sync_set_auto_reconnect() worked");

  mongo_sync_disconnect (c);
}
Ejemplo n.º 3
0
void
test_func_mongo_sync_max_insert_size (void)
{
  mongo_sync_connection *conn;
  const bson *docs[10];
  bson *b1, *b2, *b3;

  b1 = bson_new ();
  bson_append_string (b1, "func_mongo_sync_max_insert_size", "works", -1);

  bson_finish (b1);
  b2 = bson_new ();
  bson_append_int32 (b2, "int32", 1984);
  bson_finish (b2);
  b3 = bson_new ();
  bson_finish (b3);

  conn = mongo_sync_connect (config.primary_host, config.primary_port,
			     FALSE);

  /*
   * cmd_insert_n()
   */
  mongo_sync_conn_set_max_insert_size (conn, bson_size (b1) +
				       bson_size (b3) + 1);

  docs[0] = b1;
  docs[1] = b2;
  docs[2] = b3;

  ok (mongo_sync_cmd_insert_n (conn, config.ns, 3, docs) == TRUE,
      "mongo_sync_cmd_insert_n() works with a small max_insert_size");

  mongo_sync_conn_set_max_insert_size (conn, 1);
  errno = 0;
  ok (mongo_sync_cmd_insert_n (conn, config.ns, 3, docs) == FALSE,
      "mongo_sync_cmd_insert_n() should fail if any one document is too big");
  cmp_ok (errno, "==", EMSGSIZE,
	  "errno is set to EMSGSIZE");

  /*
   * cmd_insert()
   */
  mongo_sync_conn_set_max_insert_size (conn, bson_size (b1) +
				       bson_size (b3) + 1);
  ok (mongo_sync_cmd_insert (conn, config.ns, b1, b2, b3, NULL) == TRUE,
      "mongo_sync_cmd_insert() works with a small max_insert_size");

  mongo_sync_conn_set_max_insert_size (conn, 1);
  errno = 0;
  ok (mongo_sync_cmd_insert (conn, config.ns, b1, b2, b3, NULL) == FALSE,
      "mongo_sync_cmd_insert() should fail if any one document is too big");
  cmp_ok (errno, "==", EMSGSIZE,
	  "errno is set to EMSGSIZE");

  mongo_sync_disconnect (conn);
  bson_free (b1);
  bson_free (b2);
  bson_free (b3);
}
Ejemplo n.º 4
0
void
test_mongo_sync_cmd_custom (void)
{
  mongo_sync_connection *c;
  bson *cmd;

  c = test_make_fake_sync_conn (-1, FALSE);
  cmd = bson_new ();
  bson_append_int32 (cmd, "getnonce", 1);
  bson_finish (cmd);

  ok (mongo_sync_cmd_custom (NULL, "test", cmd) == NULL,
      "mongo_sync_cmd_custom() fails with a NULL connection");
  ok (mongo_sync_cmd_custom (c, NULL, cmd) == NULL,
      "mongo_sync_cmd_custom() fails with a NULL namespace");

  ok (mongo_sync_cmd_custom (c, "test", cmd) == NULL,
      "mongo_sync_cmd_custom() fails with a bogus FD");
  mongo_sync_conn_set_slaveok (c, TRUE);
  ok (mongo_sync_cmd_custom (c, "test", cmd) == NULL,
      "mongo_sync_cmd_custom() fails with a bogus FD");

  bson_free (cmd);
  mongo_sync_disconnect (c);

  test_mongo_sync_cmd_custom_net ();
}
Ejemplo n.º 5
0
void
test_mongo_sync_cmd_custom_net_secondary (void)
{
  mongo_sync_connection *conn;
  bson *cmd;
  mongo_packet *p;

  skip (!config.secondary_host, 1,
	"Secondary server not configured");

  conn = mongo_sync_connect (config.secondary_host, config.secondary_port,
			     TRUE);
  cmd = bson_build (BSON_TYPE_INT32, "getnonce", 1,
		    BSON_TYPE_NONE);
  bson_finish (cmd);

  p = mongo_sync_cmd_custom (conn, config.db, cmd);
  ok (p != NULL,
      "mongo_sync_cmd_custom() works on the secondary too");
  mongo_wire_packet_free (p);

  bson_free (cmd);
  mongo_sync_disconnect (conn);

  endskip;
}
int
main (void)
{
  mongo_sync_connection *conn;

  conn = mongo_sync_connect ("localhost", 27017, FALSE);
  if (!conn)
    {
      fprintf (stderr, "Connection failed: %s\n", strerror (errno));
      return 1;
    }

  mongo_sync_cmd_create (conn, "lmc", "cmd_create", MONGO_COLLECTION_DEFAULTS);
  print_coll_info (mongo_sync_cmd_exists (conn, "lmc", "cmd_create"));

  mongo_sync_cmd_create (conn, "lmc", "cmd_create_capped",
                         MONGO_COLLECTION_CAPPED, 655360);
  print_coll_info (mongo_sync_cmd_exists (conn, "lmc", "cmd_create_capped"));

  mongo_sync_cmd_create (conn, "lmc", "cmd_create_capped_max",
                         MONGO_COLLECTION_CAPPED | MONGO_COLLECTION_CAPPED_MAX,
                         655360, 100);
  print_coll_info (mongo_sync_cmd_exists (conn, "lmc",
                                          "cmd_create_capped_max"));

  mongo_sync_cmd_create (conn, "lmc", "cmd_create_sized",
                         MONGO_COLLECTION_SIZED, 655360);
  print_coll_info (mongo_sync_cmd_exists (conn, "lmc", "cmd_create_sized"));

  mongo_sync_disconnect (conn);

  return 0;
}
Ejemplo n.º 7
0
void
tut_sync_connect_replica (void)
{
  mongo_sync_connection *conn;

  conn = mongo_sync_connect ("mongo-master", 27017, TRUE);
  if (!conn)
    {
      perror ("mongo_sync_connect()");
      return;
    }

  if (!mongo_sync_conn_set_auto_reconnect (conn, TRUE))
    {
      perror ("mongo_sync_conn_set_auto_reconnect()");
      return;
    }

  if (!mongo_sync_conn_seed_add (conn, "mongo-replica", 27017))
    {
      perror ("mongo_sync_conn_seed_add()");
      return;
    }
  if (!mongo_sync_conn_seed_add (conn, "mongo-replica-2", 27017))
    {
      perror ("mongo_sync_conn_seed_add()");
      return;
    }

  mongo_sync_disconnect (conn);
}
Ejemplo n.º 8
0
void
test_mongo_sync_cmd_kill_cursors_net (void)
{
  mongo_packet *p;
  mongo_sync_connection *conn;
  bson *b;
  gint i;
  mongo_reply_packet_header rh;
  gint64 cid;

  begin_network_tests (3);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  mongo_sync_conn_set_auto_reconnect (conn, TRUE);

  b = bson_new ();
  for (i = 0; i < 40; i++)
    {
      bson_reset (b);
      bson_append_string (b, "test-name", __FILE__, -1);
      bson_append_int32 (b, "seq", i);
      bson_finish (b);

      mongo_sync_cmd_insert (conn, config.ns, b, NULL);
    }
  bson_free (b);

  b = bson_new ();
  bson_append_string (b, "test-name", __FILE__, -1);
  bson_finish (b);

  p = mongo_sync_cmd_query (conn, config.ns,
			    MONGO_WIRE_FLAG_QUERY_NO_CURSOR_TIMEOUT,
			    0, 2, b, NULL);
  mongo_wire_reply_packet_get_header (p, &rh);
  cid = rh.cursor_id;
  mongo_wire_packet_free (p);

  ok (mongo_sync_cmd_kill_cursors (conn, 1, cid) == TRUE,
      "mongo_sync_kill_cursors() works");

  p = mongo_sync_cmd_query (conn, config.ns,
			    MONGO_WIRE_FLAG_QUERY_NO_CURSOR_TIMEOUT,
			    0, 2, b, NULL);
  bson_free (b);
  mongo_wire_reply_packet_get_header (p, &rh);
  cid = rh.cursor_id;
  mongo_wire_packet_free (p);
  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (3);

  ok (mongo_sync_cmd_kill_cursors (conn, 1, cid) == TRUE,
      "mongo_sync_cmd_kill_cursors() automatically reconnects");

  mongo_sync_disconnect (conn);

  test_mongo_sync_cmd_kill_cursors_net_secondary ();

  end_network_tests ();
}
Ejemplo n.º 9
0
void
test_mongo_sync_cmd_kill_cursors_net_secondary (void)
{
  mongo_packet *p;
  mongo_sync_connection *conn;
  bson *b;

  mongo_reply_packet_header rh;
  gint64 cid;

  skip (!config.secondary_host, 1,
	"Secondary server not configured");

  conn = mongo_sync_connect (config.secondary_host, config.secondary_port,
			     TRUE);
  b = bson_new ();
  bson_append_string (b, "test-name", __FILE__, -1);
  bson_finish (b);

  p = mongo_sync_cmd_query (conn, config.ns,
			    MONGO_WIRE_FLAG_QUERY_NO_CURSOR_TIMEOUT,
			    0, 2, b, NULL);
  bson_free (b);
  mongo_wire_reply_packet_get_header (p, &rh);
  cid = rh.cursor_id;
  mongo_wire_packet_free (p);

  ok (mongo_sync_cmd_kill_cursors (conn, 1, cid) == TRUE,
      "mongo_sync_cmd_kill_cursors() works on secondary too");

  mongo_sync_disconnect (conn);

  endskip;
}
Ejemplo n.º 10
0
void
test_mongo_sync_cmd_get_last_error (void)
{
  mongo_sync_connection *c;
  gchar *error;

  test_env_setup ();

  c = test_make_fake_sync_conn (-1, FALSE);

  errno = 0;
  ok (mongo_sync_cmd_get_last_error (NULL, config.db, &error) == FALSE,
      "mongo_sync_cmd_get_last_error() returns FALSE with a NULL connection");
  cmp_ok (errno, "==", ENOTCONN,
	  "errno is set to ENOTCONN");

  ok (mongo_sync_cmd_get_last_error (c, NULL, &error) == FALSE,
      "mongo_sync_cmd_get_last_error() fails with a NULL db");

  errno = 0;
  ok (mongo_sync_cmd_get_last_error (c, config.db, NULL) == FALSE,
      "mongo_sync_cmd_get_last_error() fails with a NULL error destination");
  cmp_ok (errno, "==", EINVAL,
	  "errno is set to EINVAL");

  mongo_sync_disconnect (c);
  test_env_free ();
}
Ejemplo n.º 11
0
void
test_mongo_sync_cmd_user_add_net (void)
{
  mongo_sync_connection *c;

  begin_network_tests (3);

  c = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  mongo_sync_conn_set_auto_reconnect (c, TRUE);

  ok (mongo_sync_cmd_user_add (c, config.db, "test", "s3kr1+") == TRUE,
      "mongo_sync_cmd_user_add() works");

  shutdown (c->super.fd, SHUT_RDWR);
  sleep (3);

  ok (mongo_sync_cmd_user_add (c, config.db, "test", "s3kr1+") == TRUE,
      "mongo_sync_cmd_user_add() automatically reconnects");

  mongo_sync_disconnect (c);

  test_mongo_sync_cmd_user_add_net_secondary ();

  end_network_tests ();
}
Ejemplo n.º 12
0
void
test_mongo_sync_gridfs_new (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  gchar *f, *c;

  conn = test_make_fake_sync_conn (4, TRUE);

  ok (mongo_sync_gridfs_new (NULL, "test.fs") == NULL,
      "mongo_sync_gridfs_new() should fail with a NULL connection");

  ok (mongo_sync_gridfs_new (conn, "test.fs") == NULL,
      "mongo_sync_gridfs_new() should fail with a bogus connection");

  ok (mongo_sync_gridfs_new (conn, NULL) == NULL,
      "mongo_sync_gridfs_new() should fail with a NULL ns prefix");

  ok (mongo_sync_gridfs_new (conn, "bogus") == NULL,
      "mongo_sync_gridfs_new() should fail with a bogus ns prefix");

  mongo_sync_disconnect (conn);

  begin_network_tests (4);

  f = g_strconcat (config.gfs_prefix, ".files", NULL);
  c = g_strconcat (config.gfs_prefix, ".chunks", NULL);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);

  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
  ok (gfs != NULL,
      "mongo_sync_gridfs_new() works");
  is (gfs->ns.prefix, config.gfs_prefix,
      "The namespace prefix is as specified");
  is (gfs->ns.files, f,
      "The files namespace is correct");
  is (gfs->ns.chunks, c,
      "The chunks namespace is correct");
  mongo_sync_gridfs_free (gfs, FALSE);

  mongo_sync_disconnect (conn);

  g_free (f);
  g_free (c);
  end_network_tests ();
}
Ejemplo n.º 13
0
void
tut_sync_query_simple (void)
{
  mongo_sync_connection *conn;
  mongo_packet *p;
  mongo_sync_cursor *cursor;
  bson *query;
  gint i = 0;

  conn = mongo_sync_connect ("localhost", 27017, FALSE);
  if (!conn)
    {
      perror ("mongo_sync_connect()");
      exit (1);
    }

  query = bson_new ();
  bson_finish (query);

  p = mongo_sync_cmd_query (conn, "tutorial.docs", 0,
			    0, 10, query, NULL);
  if (!p)
    {
      perror ("mongo_sync_cmd_query()");
      exit (1);
    }
  bson_free (query);

  cursor = mongo_sync_cursor_new (conn, "tutorial.docs", p);
  if (!cursor)
    {
      perror ("mongo_sync_cursor_new()");
      exit (1);
    }

  while (mongo_sync_cursor_next (cursor))
    {
      bson *result = mongo_sync_cursor_get_data (cursor);
      bson_cursor *c;

      if (!result)
	{
	  perror ("mongo_sync_cursor_get_data()");
	  exit (1);
	}

      printf ("Keys in document #%d:\n", i);
      c = bson_cursor_new (result);
      while (bson_cursor_next (c))
	printf ("\t%s\n", bson_cursor_key (c));

      i++;
      bson_cursor_free (c);
      bson_free (result);
    }

  mongo_sync_cursor_free (cursor);
  mongo_sync_disconnect (conn);
}
Ejemplo n.º 14
0
static void
afmongodb_dd_disconnect(LogThrDestDriver *s)
{
  MongoDBDestDriver *self = (MongoDBDestDriver *)s;

  mongo_sync_disconnect(self->conn);
  self->conn = NULL;
}
Ejemplo n.º 15
0
void
tut_sync_connect (void)
{
  mongo_sync_connection *conn;

  conn = mongo_sync_connect ("localhost", 27017, TRUE);
  if (!conn)
    {
      perror ("mongo_sync_connect()");
      exit (1);
    }
  mongo_sync_disconnect (conn);
}
Ejemplo n.º 16
0
void
test_func_mongo_sync_auto_reconnect (void)
{
  mongo_sync_connection *conn;
  bson *b;
  mongo_packet *p;

  b = bson_new ();
  bson_append_int32 (b, "f_sync_auto_reconnect", 1);
  bson_finish (b);

  conn = mongo_sync_connect (config.primary_host, config.primary_port,
			     TRUE);
  ok (mongo_sync_cmd_insert (conn, config.ns, b, NULL) == TRUE);

  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (1);

  ok (mongo_sync_cmd_insert (conn, config.ns, b, NULL) == FALSE,
      "Inserting fails with auto-reconnect turned off, and a broken "
      "connection");

  mongo_sync_conn_set_auto_reconnect (conn, TRUE);

  ok (mongo_sync_cmd_insert (conn, config.ns, b, NULL) == TRUE,
      "Inserting works with auto-reconnect turned on, and a broken "
      "connection");

  mongo_sync_conn_set_auto_reconnect (conn, FALSE);

  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (1);

  ok (mongo_sync_cmd_insert (conn, config.ns, b, NULL) == FALSE,
      "Turning off auto-reconnect works");

  shutdown (conn->super.fd, SHUT_RDWR);
  sleep (1);

  p = mongo_sync_cmd_query (conn, config.ns, 0, 0, 1, b, NULL);
  ok (p == NULL,
      "Query fails with auto-reconnect turned off");

  mongo_sync_conn_set_auto_reconnect (conn, TRUE);
  p = mongo_sync_cmd_query (conn, config.ns, 0, 0, 1, b, NULL);
  ok (p != NULL,
      "Query does reconnect with auto-reconnect turned on");
  mongo_wire_packet_free (p);

  mongo_sync_disconnect (conn);
}
Ejemplo n.º 17
0
void
tut_sync_insert (void)
{
  mongo_sync_connection *conn;
  bson *doc1, *doc2, *doc3;

  conn = mongo_sync_connect ("localhost", 27017, FALSE);
  if (!conn)
    {
      perror ("mongo_sync_connect()");
      exit (1);
    }

  doc1 = bson_build (BSON_TYPE_STRING, "hello", "world", -1,
		     BSON_TYPE_INT32, "the_final_answer", 42,
		     BSON_TYPE_BOOLEAN, "yes?", FALSE,
		     BSON_TYPE_INT32, "n", 1,
		     BSON_TYPE_NONE);
  bson_finish (doc1);

  if (!mongo_sync_cmd_insert (conn, "tutorial.docs", doc1, NULL))
    {
      perror ("mongo_sync_cmd_insert()");
      exit (1);
    }

  doc2 = bson_build (BSON_TYPE_INT32, "n", 2,
		     BSON_TYPE_BOOLEAN, "yes?", FALSE,
		     BSON_TYPE_STRING, "hello", "dolly", -1,
		     BSON_TYPE_NONE);
  bson_finish (doc2);

  doc3 = bson_build (BSON_TYPE_INT32, "n", 3,
		     BSON_TYPE_STRING, "hello", "nurse", -1,
		     BSON_TYPE_BOOLEAN, "yes?", TRUE,
		     BSON_TYPE_NONE);
  bson_finish (doc3);

  if (!mongo_sync_cmd_insert (conn, "tutorial.docs", doc2, doc3, NULL))
    {
      perror ("mongo_sync_cmd_insert()");
      exit (1);
    }

  bson_free (doc3);
  bson_free (doc2);
  bson_free (doc1);

  mongo_sync_disconnect (conn);
}
Ejemplo n.º 18
0
gpointer
ssl_ping_success_thread (gpointer _c)
{
  mongo_ssl_ctx *c = (mongo_ssl_ctx*) _c;
  sleep ( rand () % 5 );
  mongo_ssl_lock (c);
  mongo_ssl_set_crl (c, NULL);
  mongo_sync_connection *conn = mongo_sync_ssl_connect (config.primary_host, config.primary_port, TRUE, c);
  ok (conn != NULL, "connection should succeed on a thread that does not initiate CRL check");
  ok (mongo_sync_cmd_ping (conn), "ping should succeed on a thread that does not initiate CRL check");
  mongo_ssl_unlock (c);
  mongo_sync_disconnect (conn);

  return NULL;
}
Ejemplo n.º 19
0
void
mongo_sync_pool_free (mongo_sync_pool *pool)
{
  GList *l;

  if (!pool)
    return;

  l = pool->masters;
  while (l)
    {
      mongo_sync_disconnect ((mongo_sync_connection *)l->data);
      l = g_list_delete_link (l, l);
    }

  l = pool->slaves;
  while (l)
    {
      mongo_sync_disconnect ((mongo_sync_connection *)l->data);
      l = g_list_delete_link (l, l);
    }

  g_free (pool);
}
Ejemplo n.º 20
0
void
test_mongo_sync_cmd_is_master_net (void)
{
  mongo_sync_connection *conn;

  begin_network_tests (2);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, TRUE);
  ok (mongo_sync_cmd_is_master (conn) == TRUE,
      "mongo_sync_cmd_is_master() works");
  mongo_sync_disconnect (conn);

  test_mongo_sync_cmd_is_master_net_secondary ();

  end_network_tests ();
}
Ejemplo n.º 21
0
gpointer
ssl_insert_thread (gpointer _c)
{
  mongo_ssl_ctx *c = (mongo_ssl_ctx*) _c;
  static int insert_count = 0;
  insert_count++;
  mongo_sync_connection *conn = mongo_sync_ssl_connect (config.primary_host, config.primary_port, TRUE, c);
  bson *test_doc = bson_new ();
  gchar *test_string =  g_strdup_printf ("%s:%d", "ssl_insert_thread", insert_count);
  bson_append_string (test_doc, test_string, "ok", -1);
  bson_finish (test_doc);
  ok (mongo_sync_cmd_insert (conn, config.ns, test_doc, NULL), "mongo_sync_cmd_insert () works in a multi-threaded scenario over SSL");
  mongo_sync_disconnect (conn);
  bson_free (test_doc);
  g_free (test_string);
}
Ejemplo n.º 22
0
void
test_mongo_sync_cmd_create_net (void)
{
  mongo_sync_connection *conn;
  gchar *cc;

  begin_network_tests (5);

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);

  cc = g_strconcat (config.coll, ".capped", NULL);

  mongo_sync_cmd_drop (conn, config.db, config.coll);
  mongo_sync_cmd_drop (conn, config.db, cc);

  ok (mongo_sync_cmd_create (conn, config.db, config.coll,
			     MONGO_COLLECTION_DEFAULTS) == TRUE,
      "mongo_sync_cmd_create() can create normal collections");
  mongo_sync_cmd_drop (conn, config.db, config.coll);

  ok (mongo_sync_cmd_create (conn, config.db, config.coll,
			     MONGO_COLLECTION_SIZED,
			     (gint64) 64 * 1024 * 10) == TRUE,
      "mongo_sync_cmd_create() can create pre-allocated collections");

  ok (mongo_sync_cmd_create (conn, config.db, cc,
			     MONGO_COLLECTION_CAPPED, (gint64) -1) == FALSE,
      "mongo_sync_cmd_create() fails when trying to create a capped "
      "collection with an invalid size");
  ok (mongo_sync_cmd_create (conn, config.db, cc,
			     MONGO_COLLECTION_CAPPED_MAX,
			     (gint64) (64 * 1024 * 10), (gint64) -1) == FALSE,
      "mongo_sync_cmd_create() fails when trying to create a capped "
      "collection with invalid max.");
  ok (mongo_sync_cmd_create (conn, config.db, cc,
			     MONGO_COLLECTION_CAPPED_MAX |
			     MONGO_COLLECTION_AUTO_INDEX_ID,
			     (gint64)(64 * 1024 * 10), (gint64) 10) == TRUE,
      "mongo_sync_cmd_create() can create capped collections");

  mongo_sync_cmd_drop (conn, config.db, cc);

  g_free (cc);
  mongo_sync_disconnect (conn);

  end_network_tests ();
}
Ejemplo n.º 23
0
void
test_mongo_sync_connect (void)
{
  mongo_sync_connection *c;

  ok (mongo_sync_connect (NULL, 27017, FALSE) == NULL,
      "mongo_sync_connect() fails with a NULL host");

  begin_network_tests (1);

  ok ((c = mongo_sync_connect (config.primary_host,
			       config.primary_port, FALSE)) != NULL,
      "mongo_sync_connect() works");
  mongo_sync_disconnect (c);

  end_network_tests ();
}
Ejemplo n.º 24
0
void
test_mongo_sync_cmd_is_master_net_secondary (void)
{
  mongo_sync_connection *conn;

  skip (!config.secondary_host, 1,
        "Secondary server not configured");

  errno = 0;
  conn = mongo_sync_connect (config.secondary_host, config.secondary_port,
                             TRUE);
  ok (mongo_sync_cmd_is_master (conn) == FALSE && errno == 0,
      "mongo_sync_cmd_is_master() works correctly on a secondary");
  mongo_sync_disconnect (conn);

  endskip;
}
Ejemplo n.º 25
0
int
main (void)
{
  mongo_sync_connection *conn;

  conn = mongo_sync_connect ("localhost", 27017, FALSE);
  if (!conn)
    {
      fprintf (stderr, "Connection failed: %s\n", strerror (errno));
      return 1;
    }

  do_inserts (conn);
  do_query (conn);

  mongo_sync_disconnect (conn);
  return 0;
}
Ejemplo n.º 26
0
void
test_mongo_sync_conn_seed_add (void)
{
  mongo_sync_connection *c;

  c = test_make_fake_sync_conn (42, TRUE);

  ok (mongo_sync_conn_seed_add (NULL, "localhost", 27017) == FALSE,
      "mongo_sync_conn_seed_add() should fail with a NULL connection");
  ok (mongo_sync_conn_seed_add (c, NULL, 27017) == FALSE,
      "mongo_sync_conn_seed_add() should fail with a NULL host");
  ok (mongo_sync_conn_seed_add (c, "localhost", -1) == FALSE,
      "mongo_sync_conn_seed_add() should fail with an invalid port");

  ok (mongo_sync_conn_seed_add (c, "localhost", 27017),
      "mongo_sync_conn_seed_add() works");

  mongo_sync_disconnect (c);
}
Ejemplo n.º 27
0
void
test_mongo_sync_cmd_kill_cursors (void)
{
  mongo_sync_connection *c;

  c = test_make_fake_sync_conn (-1, FALSE);

  ok (mongo_sync_cmd_kill_cursors (NULL, 1, (gint64)1234) == FALSE,
      "mongo_sync_cmd_kill_cursors() fails with a NULL connection");
  ok (mongo_sync_cmd_kill_cursors (c, 0, (gint64)1234) == FALSE,
      "mongo_sync_cmd_kill_cursors() fails with a negative number of cursors");

  ok (mongo_sync_cmd_kill_cursors (c, 1, (gint64)1234) == FALSE,
      "mongo_sync_cmd_kill_cursors() fails with a bogus FD");

  mongo_sync_disconnect (c);

  test_mongo_sync_cmd_kill_cursors_net ();
}
Ejemplo n.º 28
0
void
test_func_mongo_sync_conn_seed_add (void)
{
  mongo_sync_connection *conn;
  GList *l;

  conn = mongo_sync_connect (config.primary_host, config.primary_port,
			     FALSE);
  close (conn->super.fd);
  l = conn->rs.hosts;
  while (l)
    {
      g_free (l->data);
      l = g_list_delete_link (l, l);
    }
  conn->rs.hosts = NULL;

  conn = mongo_sync_reconnect (conn, TRUE);
  ok (conn == NULL,
      "mongo_sync_reconnect() fails without seeds or discovery");

  conn = mongo_sync_connect (config.primary_host, config.primary_port,
			     FALSE);
  close (conn->super.fd);
  l = conn->rs.hosts;
  while (l)
    {
      g_free (l->data);
      l = g_list_delete_link (l, l);
    }
  conn->rs.hosts = NULL;

  ok (mongo_sync_conn_seed_add (conn, config.primary_host,
				config.primary_port),
      "mongo_sync_conn_seed_add() works");

  conn = mongo_sync_reconnect (conn, TRUE);
  ok (conn != NULL,
      "mongo_sync_reconnect() works when properly seeded");

  mongo_sync_disconnect (conn);
}
Ejemplo n.º 29
0
void mmg_destroy(mmg_conn *db)
{
    if (!db) return;

    if (db->c) {
        mongo_sync_cursor_free(db->c);
        db->c = NULL;
    }
    /* TODO packet */
    if (db->docs) {
        bson_free(db->docs);
        db->docs = NULL;
    }
    if (db->docq) {
        bson_free(db->docq);
        db->docq = NULL;
    }

    mongo_sync_disconnect(db->con);
}
Ejemplo n.º 30
0
void
test_mongo_sync_cmd_user_add_net_secondary (void)
{
  mongo_sync_connection *c;
  gboolean ret;

  skip (!config.secondary_host, 1,
	"Secondary server not configured");

  c = mongo_sync_connect (config.secondary_host, config.secondary_port, TRUE);
  mongo_sync_conn_set_auto_reconnect (c, TRUE);

  ret = mongo_sync_cmd_user_add (c, config.db, "test", "s3kr1+");
  ok (ret && mongo_sync_cmd_is_master (c),
      "mongo_sync_cmd_user_add() automatically reconnects to master");

  mongo_sync_disconnect (c);

  endskip;
}