Ejemplo n.º 1
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.º 2
0
int 
main() {
    int i, ntests;

    ntests = sizeof(tests) / sizeof(*tests);
    for (i = 0; i < ntests; i++) {
        const struct test t = tests[i];

        {
            dt_t got = dt_from_nth_weekday_in_year(t.y, t.nth);
            dt_t exp = dt_from_yd(t.ey, t.ed);
            cmp_ok(got, "==", exp, "dt_from_nth_weekday_in_year(%d, %d)", t.y, t.nth);
        }

        {
            dt_t src = dt_from_yd(t.y, 1);
            dt_t got = dt_nth_weekday_in_year(src, t.nth);
            dt_t exp = dt_from_yd(t.ey, t.ed);
            cmp_ok(got, "==", exp, "dt_nth_weekday_in_year(%d, %d)", src, t.nth);
        }

        {
            dt_t src = dt_from_nth_weekday_in_year(t.y, t.nth);
            int  got = dt_weekday_in_year(src, t.nth < 0);
            cmp_ok(got, "==", t.nth, "dt_weekday_in_year(%d, %s)", src, (t.nth < 0 ? "true" : "false"));
        }

    }
    done_testing();
}
Ejemplo n.º 3
0
int 
main() {
    int i, ntests;

    ntests = sizeof(tests) / sizeof(*tests);
    for (i = 0; i < ntests; i++) {
        const struct test t = tests[i];

        {
            dt_t src = dt_from_ywd(t.y, t.w, t.d);
            dt_t got = dt_end_of_week(src, t.dow);
            dt_t exp = dt_from_ywd(t.ey, t.ew, t.ed);
            cmp_ok(got, "==", exp, "dt_end_of_week(%d, %d)", src, t.dow);
        }

        {
            dt_t src = dt_from_ywd(t.y, t.w, t.d);
            dt_t dt1 = dt_end_of_week(src, t.dow);
            dt_t dt2 = dt_start_of_week(src, t.dow) + 6;
            cmp_ok(dt1, "==", dt2, "dt_end_of_week(%d, %d) == dt_start_of_week(%d, %d) + 6", 
              src, t.dow, src, t.dow);
        }

    }
    done_testing();
}
void
test_bson_utc_datetime (void)
{
  bson *b;

  b = bson_new ();
  ok (bson_append_utc_datetime (b, "date", 1294860709000),
      "bson_append_utc_datetime() works");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 19, "BSON UTC datetime element size check");
  ok (memcmp (bson_data (b),
              "\023\000\000\000\011\144\141\164\145\000\210\154\266\173\055"
              "\001\000\000\000",
              bson_size (b)) == 0,
      "BSON UTC datetime element contents check");

  bson_free (b);

  b = bson_new ();
  ok (bson_append_utc_datetime (b, NULL, 1294860709000) == FALSE,
      "bson_append_utc_datetime() with a NULL key should fail");
  ok (bson_append_utc_datetime (NULL, "date", 1294860709000) == FALSE,
      "bson_append_utc_datetime() without a BSON object should fail");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
          "BSON object should be empty");

  ok (bson_append_utc_datetime (b, "date", 1294860709000) == FALSE,
      "Appending to a finished element should fail");

  bson_free (b);
}
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.º 6
0
void
test_bson_null (void)
{
  bson *b;

  b = bson_new ();
  ok (bson_append_null (b, "null"),
      "bson_append_null() works");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 11, "BSON NULL element size check");
  ok (memcmp (bson_data (b),
	      "\013\000\000\000\012\156\165\154\154\000\000",
	      bson_size (b)) == 0,
      "BSON NULL element contents check");

  bson_free (b);

  b = bson_new ();
  ok (bson_append_null (b, NULL) == FALSE,
      "bson_append_null() should fail without a key name");
  ok (bson_append_null (NULL, "null") == FALSE,
      "bson_append_null() should fail without a BSON object");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
	  "BSON object should be empty");

  ok (bson_append_null (b, "null") == FALSE,
      "Appending to a finished element should fail");

  bson_free (b);
}
Ejemplo n.º 7
0
MMDB_entry_data_list_s *test_languages_value(MMDB_entry_data_list_s
                                             *entry_data_list)
{
    MMDB_entry_data_list_s *languages = entry_data_list = entry_data_list->next;

    cmp_ok(languages->entry_data.type, "==", MMDB_DATA_TYPE_ARRAY,
           "'languages' key's value is an array");
    cmp_ok(languages->entry_data.data_size, "==", 2,
           "'languages' key's value has 2 elements");

    MMDB_entry_data_list_s *idx0 = entry_data_list = entry_data_list->next;
    cmp_ok(idx0->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
           "first array entry is a UTF8_STRING");
    const char *lang0 = dup_entry_string_or_bail(idx0->entry_data);
    is(lang0, "en", "first language is en");
    free((void *)lang0);

    MMDB_entry_data_list_s *idx1 = entry_data_list = entry_data_list->next;
    cmp_ok(idx1->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
           "second array entry is a UTF8_STRING");
    const char *lang1 = dup_entry_string_or_bail(idx1->entry_data);
    is(lang1, "zh", "second language is zh");
    free((void *)lang1);

    return entry_data_list;
}
void
test_bson_cursor_get_int32 (void)
{
  bson *b;
  bson_cursor *c;
  gint d = 12345;

  ok (bson_cursor_get_int32 (NULL, &d) == FALSE,
      "bson_cursor_get_int32() with a NULL cursor fails");

  b = test_bson_generate_full ();
  c = bson_cursor_new (b);

  ok (bson_cursor_get_int32 (c, NULL) == FALSE,
      "bson_cursor_get_int32() with a NULL destination fails");
  ok (bson_cursor_get_int32 (c, &d) == FALSE,
      "bson_cursor_get_int32() at the initial position fails");
  cmp_ok (d, "==", 12345,
          "destination remains unchanged after failed cursor operations");
  bson_cursor_free (c);

  c = bson_find (b, "int32");
  ok (bson_cursor_get_int32 (c, &d),
      "bson_cursor_get_int32() works");
  cmp_ok (d, "==", 32,
          "bson_cursor_get_int32() returns the correct result");

  bson_cursor_next (c);
  ok (bson_cursor_get_int32 (c, &d) == FALSE,
      "bson_cursor_get_int32() should fail when the cursor points to "
      "non-int32 data");

  bson_cursor_free (c);
  bson_free (b);
}
Ejemplo n.º 9
0
void test_nopolicy (flux_t *h)
{
    flux_reduce_t *r;
    int i, errors;

    clear_counts ();

    ok ((r = flux_reduce_create (h, reduce_ops, 0., NULL, 0)) != NULL,
        "nopolicy: flux_reduce_create works");

    errors = 0;
    for (i = 0; i < 100; i++) {
        if (flux_reduce_append (r, xstrdup ("hi"), 0) < 0)
            errors++;
    }
    ok (errors == 0,
        "nopolicy: flux_reduce_append added 100 items in batch 0");
    cmp_ok (forward_calls, "==", 0,
        "nopolicy: op.forward not called as we are rank 0");
    cmp_ok (reduce_calls, "==", 0,
        "nopolicy: op.reduce not called as we have no flush policy");
    cmp_ok (sink_calls, "==", 100,
        "nopolicy: op.sink called 100 times");
    cmp_ok (sink_items, "==", 100,
        "nopolicy: op.sink processed 100 items");

    flux_reduce_destroy (r);
}
Ejemplo n.º 10
0
int 
main() {
    int i, ntests;

    ntests = sizeof(tests) / sizeof(*tests);
    for (i = 0; i < ntests; i++) {
        const struct test t = tests[i];

        {
            dt_t src = dt_from_ymd(t.y, t.m, t.d);
            dt_t got = dt_start_of_month(src, t.delta);
            dt_t exp = dt_from_ymd(t.ey, t.em, t.ed);
            cmp_ok(got, "==", exp, "dt_start_of_month(%d (%.4d-%.2d-%.2d), %d)", 
              src, t.y, t.m, t.d, t.delta);
        }

        {
            dt_t src = dt_from_ymd(t.y, t.m, t.d);
            dt_t dt1 = dt_start_of_month(src, t.delta);
            dt_t dt2 = dt_end_of_month(src, t.delta - 1) + 1;
            cmp_ok(dt1, "==", dt2, "dt_start_of_month(%d, %d) == dt_end_of_month(%d, %d - 1) + 1",
              src, t.y, src, t.y);
        }

        {
            dt_t dt1 = dt_from_ymd(t.y, t.m, t.d);
            dt_t dt2 = dt_start_of_month(dt1, t.delta);
            int got = dt_delta_months(dt1, dt2, 0);
            cmp_ok(got, "==", t.delta, "dt_delta_months(%d, %d, false)", dt1, dt2);
        }
    }
    done_testing();
}
Ejemplo n.º 11
0
int
main() {
    int i, ntests;

    ntests = sizeof(tests) / sizeof(*tests);
    for (i = 0; i < ntests; i++) {
        const struct test t = tests[i];

        {
            dt_t src = dt_from_yd(t.y, t.d);
            dt_t got = dt_add_years(src, t.delta, t.adjust);
            dt_t exp = dt_from_yd(t.ey, t.ed);
            cmp_ok(got, "==", exp, "dt_add_years(%d (%.4d-%.3d), %d, %d)",
              src, t.y, t.d, t.delta, t.adjust);
        }

        if (t.adjust != DT_EXCESS) {
            dt_t dt1 = dt_from_yd(t.y, t.d);
            dt_t dt2 = dt_add_years(dt1, t.delta, t.adjust);
            int got = dt_delta_years(dt1, dt2, 0);
            cmp_ok(got, "==", t.delta, "dt_delta_years(%d, %d, false)", dt1, dt2);
        }
    }
    done_testing();
}
void
test_bson_timestamp (void)
{
  bson *b;
  gint64 l = 9876543210;

  b = bson_new ();
  ok (bson_append_timestamp (b, "ts", l), "bson_append_timestamp() works");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 17, "BSON timestamp element size check");
  ok (memcmp (bson_data (b),
              "\021\000\000\000\021\164\163\000\352\026\260\114\002\000\000"
              "\000\000",
              bson_size (b)) == 0,
      "BSON timestamp element contents check");

  bson_free (b);

  b = bson_new ();
  ok (bson_append_timestamp (b, NULL, l) == FALSE,
      "bson_append_timestamp() with a NULL key should fail");
  ok (bson_append_timestamp (NULL, "ts", l) == FALSE,
      "bson_append_timestamp() without a BSON object should fail");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
          "BSON object should be empty");

  ok (bson_append_timestamp (b, "ts", l) == FALSE,
      "Appending to a finished element should fail");

  bson_free (b);
}
Ejemplo n.º 13
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.º 14
0
void
test_bson_document (void)
{
  bson *b, *e1, *e2;

  e1 = bson_new ();
  bson_append_int32 (e1, "i32", 1984);
  bson_append_string (e1, "str", "hello world", -1);
  bson_finish (e1);

  e2 = bson_new ();
  bson_append_string (e2, "foo", "bar", -1);
  ok (bson_append_document (e2, "subd", e1),
      "bson_append_document() works");
  bson_finish (e2);
  bson_free (e1);

  b = bson_new ();
  ok (bson_append_document (b, "doc", e2),
      "bson_append_document() works still");
  bson_finish (b);
  bson_free (e2);

  cmp_ok (bson_size (b), "==", 69, "BSON document element size check");
  ok (memcmp (bson_data (b),
	      "\105\000\000\000\003\144\157\143\000\073\000\000\000\002\146"
	      "\157\157\000\004\000\000\000\142\141\162\000\003\163\165\142"
	      "\144\000\043\000\000\000\020\151\063\062\000\300\007\000\000"
	      "\002\163\164\162\000\014\000\000\000\150\145\154\154\157\040"
	      "\167\157\162\154\144\000\000\000\000",
	      bson_size (b)) == 0,
      "BSON document element contents check");

  bson_free (b);

  e1 = bson_new ();
  bson_append_int32 (e1, "foo", 42);
  b = bson_new ();

  ok (bson_append_document (b, "doc", e1) == FALSE,
      "bson_append_document() with an unfinished document should fail");
  bson_finish (e1);
  ok (bson_append_document (b, NULL, e1) == FALSE,
      "bson_append_document() with a NULL key should fail");
  ok (bson_append_document (b, "doc", NULL) == FALSE,
      "bson_append_document() with a NULL document should fail");
  ok (bson_append_document (NULL, "doc", e1) == FALSE,
      "bson_append_document() without a BSON object should fail");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 5,
	  "BSON object should be empty");

  ok (bson_append_document (b, "doc", e1) == FALSE,
      "Appending to a finished element should fail");

  bson_free (e1);
  bson_free (b);
}
Ejemplo n.º 15
0
void
test_mongo_wire_cmd_custom (void)
{
  bson *cmd;
  mongo_packet *p;

  mongo_packet_header hdr;
  const guint8 *data;
  gint32 data_size;

  bson_cursor *c;
  gint32 pos;

  cmd = bson_new ();
  bson_append_int32 (cmd, "getnonce", 1);

  ok (mongo_wire_cmd_custom (1, "test", 0, NULL) == NULL,
      "mongo_wire_cmd_custom() fails with a NULL command");
  ok (mongo_wire_cmd_custom (1, "test", 0, cmd) == NULL,
      "mongo_wire_cmd_custom() fails with an unfinished command");
  bson_finish (cmd);
  ok (mongo_wire_cmd_custom (1, NULL, 0, cmd) == NULL,
      "mongo_wire_cmd_custom() fails with a NULL db");

  ok ((p = mongo_wire_cmd_custom (1, "test", 0, cmd)) != NULL,
      "mongo_wire_cmd_custom() works");
  bson_free (cmd);

  /* Verify the header */
  mongo_wire_packet_get_header (p, &hdr);
  cmp_ok ((data_size = mongo_wire_packet_get_data (p, &data)), "!=", -1,
	  "Packet data size looks fine");
  cmp_ok (hdr.length, "==", sizeof (mongo_packet_header) + data_size,
	  "Packet header length is OK");
  cmp_ok (hdr.id, "==", 1, "Packet request ID is ok");
  cmp_ok (hdr.resp_to, "==", 0, "Packet reply ID is ok");

  /*
   * Test the created request
   */

  /* pos = zero + collection_name + NULL + skip + ret */
  pos = sizeof (gint32) + strlen ("test.$cmd") + 1 + sizeof (gint32) * 2;
  ok ((cmd = bson_new_from_data (data + pos,
				 _DOC_SIZE (data, pos) - 1)) != NULL,
      "Packet contains a BSON document");
  bson_finish (cmd);

  ok ((c = bson_find (cmd, "getnonce")) != NULL,
      "BSON object contains a 'getnonce' key");
  cmp_ok (bson_cursor_type (c), "==", BSON_TYPE_INT32,
	  "'getnonce' key has the correct type");
  ok (bson_cursor_next (c) == FALSE,
      "'getnonce' key is the last in the object");

  bson_cursor_free (c);
  bson_free (cmd);
  mongo_wire_packet_free (p);
}
Ejemplo n.º 16
0
void
test_bson_array (void)
{
  bson *b, *e1, *e2;

  e1 = bson_new ();
  bson_append_int32 (e1, "0", 1984);
  bson_append_string (e1, "1", "hello world", -1);
  bson_finish (e1);

  e2 = bson_new ();
  bson_append_string (e2, "0", "bar", -1);
  ok (bson_append_array (e2, "1", e1),
      "bson_append_array() works");
  bson_finish (e2);
  bson_free (e1);

  b = bson_new ();
  ok (bson_append_array (b, "0", e2),
      "bson_append_array() works still");
  bson_finish (b);
  bson_free (e2);

  cmp_ok (bson_size (b), "==", 58, "BSON array element size check");
  ok (memcmp (bson_data (b),
	      "\072\000\000\000\004\060\000\062\000\000\000\002\060\000\004"
	      "\000\000\000\142\141\162\000\004\061\000\037\000\000\000\020"
	      "\060\000\300\007\000\000\002\061\000\014\000\000\000\150\145"
	      "\154\154\157\040\167\157\162\154\144\000\000\000\000",
	      bson_size (b)) == 0,
      "BSON array element contents check");

  bson_free (b);

  e1 = bson_new ();
  bson_append_int32 (e1, "0", 1984);
  b = bson_new ();

  ok (bson_append_array (b, "array", e1) == FALSE,
      "bson_append_array() with an unfinished array should fail");
  bson_finish (e1);
  ok (bson_append_array (b, NULL, e1) == FALSE,
      "bson_append_array() with a NULL name should fail");
  ok (bson_append_array (b, "foo", NULL) == FALSE,
      "bson_append_array() with a NULL array should fail");
  ok (bson_append_array (NULL, "foo", e1) == FALSE,
      "bson_append_array() with a NULL BSON should fail");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
	  "BSON object should be empty");

  ok (bson_append_array (b, "array", e1) == FALSE,
      "Appending to a finished element should fail");

  bson_free (e1);
  bson_free (b);
}
Ejemplo n.º 17
0
void test_array_0_result(int status, MMDB_entry_data_s entry_data,
                         char *function)
{
    cmp_ok(status, "==", MMDB_SUCCESS,
           "status for %s() is MMDB_SUCCESS - array[0]", function);
    ok(entry_data.has_data, "found a value for array[0]");
    cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
           "returned entry type is uint32 - array[0]");
    cmp_ok(entry_data.uint32, "==", 1, "entry value is 1 - array[0]");
}
Ejemplo n.º 18
0
void
test_bson_string (void)
{
  bson *b;

  /* Test #1: A single string element, with default size. */
  b = bson_new ();
  ok (bson_append_string (b, "hello", "world", -1),
      "bson_append_string() works");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 22, "BSON string element size check");
  ok (memcmp (bson_data (b),
              "\026\000\000\000\002\150\145\154\154\157\000\006\000\000\000"
              "\167\157\162\154\144\000\000",
              bson_size (b)) == 0,
      "BSON string element contents check");
  bson_free (b);

  /* Test #2: A single string element, with explicit length. */
  b = bson_new ();
  ok (bson_append_string (b, "goodbye",
                          "cruel world, this garbage is gone.",
                          strlen ("cruel world")),
      "bson_append_string() with explicit length works");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 30, "BSON string element size check, #2");
  ok (memcmp (bson_data (b),
              "\036\000\000\000\002\147\157\157\144\142\171\145\000\014\000"
              "\000\000\143\162\165\145\154\040\167\157\162\154\144\000\000",
              bson_size (b)) == 0,
      "BSON string element contents check, #2");
  bson_free (b);

  /* Test #3: Negative test, passing invalid arguments. */
  b = bson_new ();
  ok (bson_append_string (b, "hello", "world", -42) == FALSE,
      "bson_append_string() should fail with invalid length");
  ok (bson_append_string (b, "hello", NULL, -1) == FALSE,
      "bson_append_string() should fail without a string");
  ok (bson_append_string (b, NULL, "world", -1) == FALSE,
      "bson_append_string() should fail without a key name");
  ok (bson_append_string (NULL, "hello", "world", -1) == FALSE,
      "bson_append_string() should fail without a BSON object");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
          "BSON object should be empty");

  ok (bson_append_string (b, "hello", "world", -1) == FALSE,
      "Appending to a finished element should fail");

  bson_free (b);
}
void
test_bson_cursor_get_binary (void)
{
  bson *b;
  bson_cursor *c;
  const guint8 *d = (guint8 *)"deadbeef";
  bson_binary_subtype t = 0xff;
  gint32 s = -1;

  ok (bson_cursor_get_binary (NULL, &t, &d, &s) == FALSE,
      "bson_cursor_get_binary() with a NULL cursor fails");

  b = test_bson_generate_full ();
  c = bson_cursor_new (b);

  ok (bson_cursor_get_binary (c, NULL, NULL, NULL) == FALSE,
      "bson_cursor_get_binary() with NULL destinations fails");
  ok (bson_cursor_get_binary (c, NULL, &d, &s) == FALSE,
      "bson_cursor_get_binary() with a NULL subtype destination fails");
  ok (bson_cursor_get_binary (c, &t, NULL, &s) == FALSE,
      "bson_cursor_get_binary() with a NULL binary destination fails");
  ok (bson_cursor_get_binary (c, &t, &d, NULL) == FALSE,
      "bson_cursor_get_binary() with a NULL size destination fails");
  ok (bson_cursor_get_binary (c, &t, &d, &s) == FALSE,
      "bson_cursor_get_binary() at the initial position fails");
  ok (memcmp (d, "deadbeef", sizeof ("deadbeef")) == 0,
      "binary destination remains unchanged after failed cursor operations");
  cmp_ok (t, "==", 0xff,
          "subtype destination remains unchanged after failed cursor "
          "operations");
  cmp_ok (s, "==", -1,
          "size destination remains unchanged after failed cursor operations");
  bson_cursor_free (c);

  c = bson_find (b, "binary0");
  ok (bson_cursor_get_binary (c, &t, &d, &s),
      "bson_cursor_get_binary() works");
  cmp_ok (s, "==", 7,
          "bson_cursor_get_binary() returns the correct result");
  ok (memcmp (d, "foo\0bar", s) == 0,
      "bson_cursor_get_binary() returns the correct result");
  cmp_ok (t, "==", BSON_BINARY_SUBTYPE_GENERIC,
      "bson_cursor_get_binary() returns the correct result");

  bson_cursor_next (c);
  ok (bson_cursor_get_binary (c, &t, &d, &s) == FALSE,
      "bson_cursor_get_binary() should fail when the cursor points to "
      "non-binary data");

  bson_cursor_free (c);
  bson_free (b);
}
Ejemplo n.º 20
0
void test_complex_map_a_result(int status, MMDB_entry_data_s entry_data,
                               char *function)
{
    cmp_ok(status, "==", MMDB_SUCCESS,
           "status for %s() is MMDB_SUCCESS - map1{map2}{array}[0]{map3}{a}",
           function);
    ok(entry_data.has_data,
       "found a value for map1{map2}{array}[0]{map3}{a}");
    cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UINT32,
           "returned entry type is uint32 - map1{map2}{array}[0]{map3}{a}");
    cmp_ok(entry_data.uint32, "==", 1,
           "entry value is 1 - map1{map2}{array}[0]{map3}{a}");
}
Ejemplo n.º 21
0
void test_entry_data(MMDB_s *mmdb, MMDB_entry_s *entry, uint32_t node_number,
                     char * node_record)
{
    MMDB_entry_data_s entry_data;
    int status =
        MMDB_get_value(entry, &entry_data, "ip",
                       NULL);
    cmp_ok(status, "==", MMDB_SUCCESS,
           "successful data lookup for node");
    cmp_ok(
        entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
        "returned entry type is UTF8_STRING for %s record of node %i",
        node_record, node_number);
}
Ejemplo n.º 22
0
void test_metadata(MMDB_s *mmdb, const char *mode_desc)
{
    cmp_ok(mmdb->metadata.node_count, "==", 37, "node_count is 37 - %s",
           mode_desc);
    cmp_ok(mmdb->metadata.record_size, "==", 24, "record_size is 24 - %s",
           mode_desc);
    cmp_ok(mmdb->metadata.ip_version, "==", 4, "ip_version is 4 - %s",
           mode_desc);
    is(mmdb->metadata.database_type, "Test", "database_type is Test - %s",
       mode_desc);
    // 2013-07-01T00:00:00Z
    uint64_t expect_epoch = 1372636800;
    int is_ok =
        cmp_ok(mmdb->metadata.build_epoch, ">=", expect_epoch,
               "build_epoch > %lli", expect_epoch);
    if (!is_ok) {
        diag("  epoch is %lli", mmdb->metadata.build_epoch);
    }

    cmp_ok(mmdb->metadata.binary_format_major_version, "==", 2,
           "binary_format_major_version is 2 - %s", mode_desc);
    cmp_ok(mmdb->metadata.binary_format_minor_version, "==", 0,
           "binary_format_minor_version is 0 - %s", mode_desc);

    cmp_ok(mmdb->metadata.languages.count, "==", 2, "found 2 languages - %s",
           mode_desc);
    is(mmdb->metadata.languages.names[0], "en", "first language is en - %s",
       mode_desc);
    is(mmdb->metadata.languages.names[1], "zh", "second language is zh - %s",
       mode_desc);

    cmp_ok(mmdb->metadata.description.count, "==", 2,
           "found 2 descriptions - %s", mode_desc);
    for (uint16_t i = 0; i < mmdb->metadata.description.count; i++) {
        const char *language =
            mmdb->metadata.description.descriptions[i]->language;
        const char *description =
            mmdb->metadata.description.descriptions[i]->description;
        if (strncmp(language, "en", 2) == 0) {
            ok(1, "found en description");
            is(description, "Test Database", "en description");
        } else if (strncmp(language, "zh", 2) == 0) {
            ok(1, "found zh description");
            is(description, "Test Database Chinese", "zh description");
        } else {
            ok(0, "found unknown description in unexpected language - %s",
               language);
        }
    }

    cmp_ok(mmdb->full_record_byte_size, "==", 6,
           "full_record_byte_size is 6 - %s", mode_desc);
}
Ejemplo n.º 23
0
void
test_bson_binary (void)
{
  bson *b;

  b = bson_new ();
  ok (bson_append_binary (b, "binary0", BSON_BINARY_SUBTYPE_GENERIC,
                          (guint8 *)"foo\0bar", 7),
      "bson_append_binary(), type 0 works");
  ok (bson_append_binary (b, "binary2", BSON_BINARY_SUBTYPE_BINARY,
                          (guint8 *)"\0\0\0\7foo\0bar", 11),
      "bson_append_binary(), type 2 works");
  bson_finish (b);

  cmp_ok (bson_size (b), "==", 51, "BSON binary element size check");
  ok (memcmp (bson_data (b),
              "\063\000\000\000\005\142\151\156\141\162\171\060\000\007\000"
              "\000\000\000\146\157\157\000\142\141\162\005\142\151\156\141"
              "\162\171\062\000\013\000\000\000\002\000\000\000\007\146\157"
              "\157\000\142\141\162\000",
              bson_size (b)) == 0,
      "BSON binary element contents check");

  bson_free (b);

  b = bson_new ();
  ok (bson_append_binary (b, NULL, BSON_BINARY_SUBTYPE_GENERIC,
                          (guint8 *)"foo\0bar", 7) == FALSE,
      "bson_append_binary() without a key name should fail");
  ok (bson_append_binary (b, "binary1", BSON_BINARY_SUBTYPE_GENERIC,
                          NULL, 10) == FALSE,
      "bson_append_binary () without binary data should fail");
  ok (bson_append_binary (b, "binary3", BSON_BINARY_SUBTYPE_GENERIC,
                          (guint8 *)"foo\0bar", -1) == FALSE,
      "bson_append_binary () with an invalid length should fail");
  ok (bson_append_binary (NULL, "binary1", BSON_BINARY_SUBTYPE_GENERIC,
                          (guint8 *)"foo\0bar", 7) == FALSE,
      "bson_append_binary () without a BSON object should fail");
  bson_finish (b);
  cmp_ok (bson_size (b), "==", 5,
          "BSON object should be empty");

  ok (bson_append_binary (b, "binary", BSON_BINARY_SUBTYPE_GENERIC,
                          (guint8 *)"foo\0bar", 7) == FALSE,
      "Appending to a finished element should fail");

  bson_free (b);
}
Ejemplo n.º 24
0
MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result, uint32_t expect_type,
                          const char *description, ...)
{
    va_list keys;
    va_start(keys, description);

    MMDB_entry_data_s data;
    int status = MMDB_vget_value(&result->entry, &data, keys);

    va_end(keys);

    if (cmp_ok(status, "==", MMDB_SUCCESS,
               "no error from call to MMDB_vget_value - %s", description)) {

        if (!ok(data.type == expect_type, "got the expected data type - %s",
                description)) {

            diag("  data type value is %i but expected %i", data.type,
                 expect_type);
        }
    } else {
        diag("  error from MMDB_vget_value - %s", MMDB_strerror(status));
    }

    return data;
}
void
test_bson_cursor_get_timestamp (void)
{
  bson *b;
  bson_cursor *c;
  gint64 d = (gint64)987654;

  ok (bson_cursor_get_timestamp (NULL, &d) == FALSE,
      "bson_cursor_get_timestamp() with a NULL cursor fails");

  b = test_bson_generate_full ();
  c = bson_cursor_new (b);

  ok (bson_cursor_get_timestamp (c, NULL) == FALSE,
      "bson_cursor_get_timestamp() with a NULL destination fails");
  ok (bson_cursor_get_timestamp (c, &d) == FALSE,
      "bson_cursor_get_timestamp() at the initial position fails");
  cmp_ok (d, "==", 987654,
          "destination remains unchanged after failed cursor operations");
  bson_cursor_free (c);

  c = bson_find (b, "ts");
  ok (bson_cursor_get_timestamp (c, &d),
      "bson_cursor_get_timestamp() works");
  ok (d == 1294860709000,
      "bson_cursor_get_timestamp() returns the correct result");

  bson_cursor_next (c);
  ok (bson_cursor_get_timestamp (c, &d) == FALSE,
      "bson_cursor_get_timestamp() should fail when the cursor points to "
      "non-timestamp data");

  bson_cursor_free (c);
  bson_free (b);
}
Ejemplo n.º 26
0
int main(void) {
  plan(2);

  const char *downloaded = "__remote_readme__.md";
  unlink(downloaded);

  const char *url = "https://raw.github.com/thlorenz/cre-downloader/master/README.md";
  FILE *fp;

  char *remote = 0;
  int remote_len, status;

  // Download
  fp = fopen(downloaded, "wb");
  status = download_url_file(url, fp);
  fclose(fp);

  cmp_ok(status, "==", 200, "returns status 200");

  // Read downloaded README
  fp = fopen(downloaded, "r");

  remote_len = fs_fsize(fp);
  remote = malloc(remote_len);
  fread(remote, 1, remote_len, fp);
  fclose(fp);

  unlink(downloaded);

  like(remote, "cre-downloader", "downloaded README has same content as local README");

  return 0;
}
Ejemplo n.º 27
0
int 
main() {
    int i, ntests;

    ntests = sizeof(good) / sizeof(*good);
    for (i = 0; i < ntests; i++) {
        const struct good_t t = good[i];

        {
            int offset = 0;
            size_t glen;

            glen = dt_parse_iso_zone_lenient(t.str, strlen(t.str), &offset);
            ok(glen == t.elen, "dt_parse_iso_zone_lenient(%s) size_t: %d", t.str, (int)glen);
            cmp_ok(offset, "==", t.offset, "dt_parse_iso_zone_lenient(%s)", t.str);
        }
    }

    ntests = sizeof(bad) / sizeof(*bad);
    for (i = 0; i < ntests; i++) {
        const struct bad_t t = bad[i];

        {
            int offset = 0;
            size_t glen;

            glen = dt_parse_iso_zone_lenient(t.str, strlen(t.str), &offset);
            ok(glen == 0, "dt_parse_iso_zone_lenient(%s) size_t: %d", t.str, (int)glen);
        }
    }
    done_testing();
}
void
test_mongo_sync_gridfs_get_set_chunk_size (void)
{
  mongo_sync_gridfs *gfs;

  ok (mongo_sync_gridfs_get_chunk_size (NULL) == -1,
      "mongo_sync_gridfs_get_chunk_size() fails with a NULL gfs");
  ok (mongo_sync_gridfs_set_chunk_size (NULL, 16 * 1024) == FALSE,
      "mongo_sync_gridfs_set_chunk_size() fails with a NULL gfs");

  begin_network_tests (3);

  gfs = mongo_sync_gridfs_new (mongo_sync_connect (config.primary_host,
                                                   config.primary_port,
                                                   FALSE),
                               config.gfs_prefix);

  ok (mongo_sync_gridfs_set_chunk_size (gfs, -1) == FALSE,
      "mongo_sync_gridfs_set_chunk_size() fails if the size is invalid");
  ok (mongo_sync_gridfs_set_chunk_size (gfs, 12345),
      "mongo_sync_gridfs_set_chunk_size() works");
  cmp_ok (mongo_sync_gridfs_get_chunk_size (gfs), "==", 12345,
          "mongo_sync_gridfs_get_chunk_size() works");

  mongo_sync_gridfs_free (gfs, TRUE);

  end_network_tests ();
}
Ejemplo n.º 29
0
int
main() {
    int ntests, nholidays;
    int i;
    dt_t holidays[20];

    nholidays = sizeof(days) / sizeof(*days);
    for (i = 0; i < nholidays; i++) {
        const struct ymd date = days[i];
        holidays[i] = dt_from_ymd(date.y, date.m, date.d);
    }

    ntests = sizeof(tests) / sizeof(*tests);
    for (i = 0; i < ntests; i++) {
        const struct test t = tests[i];

        {
            dt_t src = dt_from_ymd(t.y, t.m, t.d);
            dt_t got = dt_add_workdays(src, t.delta, holidays, nholidays);
            dt_t exp = dt_from_ymd(t.ey, t.em, t.ed);
            cmp_ok(got, "==", exp, "dt_add_workdays(%.4d-%.2d-%.2d, %d)", 
              src, t.y, t.m, t.d, t.delta);
        }
    }
    done_testing();
}
Ejemplo n.º 30
0
void
test_bson_cursor_get_document (void)
{
  bson *b, *d = NULL;
  bson_cursor *c;

  ok (bson_cursor_get_document (NULL, &d) == FALSE,
      "bson_cursor_get_document() with a NULL cursor fails");

  b = test_bson_generate_full ();
  c = bson_cursor_new (b);

  ok (bson_cursor_get_document (c, NULL) == FALSE,
      "bson_cursor_get_document() with a NULL destination fails");
  ok (bson_cursor_get_document (c, &d) == FALSE,
      "bson_cursor_get_document() at the initial position fails");
  ok (d == NULL,
      "destination remains unchanged after failed cursor operations");
  bson_cursor_free (c);

  c = bson_find (b, "doc");
  ok (bson_cursor_get_document (c, &d),
      "bson_cursor_get_document() works");
  cmp_ok (bson_size (d), ">", 0,
	  "the returned document is finished");
  bson_free (d);

  bson_cursor_next (c);
  ok (bson_cursor_get_document (c, &d) == FALSE,
      "bson_cursor_get_document() fails if the cursor points to "
      "non-document data");

  bson_cursor_free (c);
  bson_free (b);
}