Example #1
0
static void
test_bool (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_bool (&expected, "foo", -1, 1);

   BCON_APPEND (&bcon, "foo", BCON_BOOL (1));

   bson_eq_bson (&bcon, &expected);

   bson_reinit (&bcon);
   bson_reinit (&expected);

   bson_append_bool (&expected, "foo", -1, 0);

   BCON_APPEND (&bcon, "foo", BCON_BOOL (0));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #2
0
static void
test_concat (void)
{
   bson_t bcon, expected, child, child2;

   bson_init (&bcon);
   bson_init (&expected);
   bson_init (&child);
   bson_init (&child2);

   bson_append_utf8 (&child, "hello", -1, "world", -1);
   bson_append_document (&expected, "foo", -1, &child);

   BCON_APPEND (&bcon, "foo", "{", BCON(&child), "}");

   bson_eq_bson (&bcon, &expected);

   bson_reinit (&bcon);
   bson_reinit (&expected);
   bson_reinit (&child);

   bson_append_utf8 (&child, "0", -1, "bar", -1);
   bson_append_utf8 (&child, "1", -1, "baz", -1);
   bson_append_array (&expected, "foo", -1, &child);

   bson_append_utf8 (&child2, "0", -1, "baz", -1);
   BCON_APPEND (&bcon, "foo", "[", "bar", BCON(&child2), "]");

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&child);
   bson_destroy (&child2);
   bson_destroy (&expected);
}
Example #3
0
int
main (int argc, char *argv[])
{
   int i;
   int n;
   int bcon;
   bson_t bson, foo, bar, baz;
   bson_init (&bson);

   if (argc != 3) {
      fprintf (stderr,
               "usage: bcon-speed NUM_ITERATIONS [y|n]\n"
               "\n"
               "  y = perform speed tests with bcon\n"
               "  n = perform speed tests with bson_append\n"
               "\n");
      return EXIT_FAILURE;
   }

   assert (argc == 3);

   n = atoi (argv[1]);
   bcon = (argv[2][0] == 'y') ? 1 : 0;

   for (i = 0; i < n; i++) {
      if (bcon) {
         BCON_APPEND (&bson,
                      "foo",
                      "{",
                      "bar",
                      "{",
                      "baz",
                      "[",
                      BCON_INT32 (1),
                      BCON_INT32 (2),
                      BCON_INT32 (3),
                      "]",
                      "}",
                      "}");
      } else {
         bson_append_document_begin (&bson, "foo", -1, &foo);
         bson_append_document_begin (&foo, "bar", -1, &bar);
         bson_append_array_begin (&bar, "baz", -1, &baz);
         bson_append_int32 (&baz, "0", -1, 1);
         bson_append_int32 (&baz, "1", -1, 2);
         bson_append_int32 (&baz, "2", -1, 3);
         bson_append_array_end (&bar, &baz);
         bson_append_document_end (&foo, &bar);
         bson_append_document_end (&bson, &foo);
      }

      bson_reinit (&bson);
   }

   bson_destroy (&bson);

   return 0;
}
Example #4
0
static void
test_double (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_double (&expected, "foo", -1, 1.1);
   BCON_APPEND(&bcon, "foo", BCON_DOUBLE(1.1));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #5
0
static void
test_regex (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   /* option flags are sorted */
   bson_append_regex (&expected, "foo", -1, "^foo|bar$", "mis");
   BCON_APPEND (&bcon, "foo", BCON_REGEX ("^foo|bar$", "msi"));
   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #6
0
static void
test_minkey (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_minkey (&expected, "foo", -1);
   BCON_APPEND (&bcon, "foo", BCON_MINKEY);

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #7
0
static void
test_utf8 (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_utf8 (&expected, "hello", -1, "world", -1);
   BCON_APPEND (&bcon, "hello", "world");

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #8
0
static void
test_regex (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_regex (&expected, "foo", -1, "^foo|bar$", "i");

   BCON_APPEND (&bcon, "foo", BCON_REGEX ("^foo|bar$", "i"));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #9
0
static void
test_null (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_null (&expected, "foo", -1);

   BCON_APPEND (&bcon, "foo", BCON_NULL);

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #10
0
static void
test_date_time (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_date_time (&expected, "foo", -1, 10000);

   BCON_APPEND (&bcon, "foo", BCON_DATE_TIME (10000));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #11
0
static void
test_code (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_code (&expected, "foo", -1, "var a = {};");

   BCON_APPEND (&bcon, "foo", BCON_CODE ("var a = {};"));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #12
0
static void
test_symbol (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_symbol (&expected, "foo", -1, "deadbeef", -1);

   BCON_APPEND (&bcon, "foo", BCON_SYMBOL ("deadbeef"));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #13
0
static void
test_undefined (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_undefined (&expected, "foo", -1);

   BCON_APPEND (&bcon, "foo", BCON_UNDEFINED);

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #14
0
static void
test_int64 (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_int64 (&expected, "foo", -1, 100);

   BCON_APPEND (&bcon, "foo", BCON_INT64 (100));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #15
0
static void
test_timestamp (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_timestamp (&expected, "foo", -1, 100, 1000);

   BCON_APPEND (&bcon, "foo", BCON_TIMESTAMP (100, 1000));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #16
0
static void
test_decimal128 (void)
{
   bson_t bcon, expected;
   bson_decimal128_t dec;
   bson_decimal128_from_string("120E20", &dec);

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_decimal128 (&expected, "foo", -1, &dec);
   BCON_APPEND(&bcon, "foo", BCON_DECIMAL128(&dec));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #17
0
static void
test_iter (void)
{
   bson_t bcon, expected;
   bson_iter_t iter;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_int32 (&expected, "foo", -1, 100);
   bson_iter_init_find (&iter, &expected, "foo");

   BCON_APPEND (&bcon, "foo", BCON_ITER(&iter));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #18
0
static void
test_oid (void)
{
   bson_t bcon, expected;
   bson_oid_t oid;

   bson_init (&bcon);
   bson_init (&expected);

   bson_oid_init (&oid, NULL);

   bson_append_oid (&expected, "foo", -1, &oid);
   BCON_APPEND (&bcon, "foo", BCON_OID (&oid));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #19
0
static void
test_inline_doc (void)
{
   bson_t bcon, expected, child;

   bson_init (&bcon);
   bson_init (&expected);
   bson_init (&child);

   bson_append_utf8 (&child, "bar", -1, "baz", -1);
   bson_append_document (&expected, "foo", -1, &child);

   BCON_APPEND (&bcon, "foo", "{", "bar", "baz", "}");

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
   bson_destroy (&child);
}
Example #20
0
static void
test_inline_array (void)
{
   bson_t bcon, expected, child;

   bson_init (&bcon);
   bson_init (&expected);
   bson_init (&child);

   bson_append_utf8 (&child, "0", -1, "baz", -1);
   bson_append_array (&expected, "foo", -1, &child);

   BCON_APPEND (&bcon, "foo", "[", "baz", "]");

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&child);
   bson_destroy (&expected);
}
Example #21
0
static void
test_dbpointer (void)
{
   bson_t bcon, expected;
   bson_oid_t oid;

   bson_init (&bcon);
   bson_init (&expected);

   bson_oid_init (&oid, NULL);

   bson_append_dbpointer (&expected, "foo", -1, "collection", &oid);

   BCON_APPEND (&bcon, "foo", BCON_DBPOINTER ("collection", &oid));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #22
0
static void
test_binary (void)
{
   bson_t bcon, expected;

   bson_init (&bcon);
   bson_init (&expected);

   bson_append_binary (
      &expected, "foo", -1, BSON_SUBTYPE_BINARY, (uint8_t *) "deadbeef", 8);

   BCON_APPEND (&bcon,
                "foo",
                BCON_BIN (BSON_SUBTYPE_BINARY, (const uint8_t *) "deadbeef", 8),
                NULL);

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
}
Example #23
0
static void
test_codewscope (void)
{
   bson_t bcon, expected, scope;

   bson_init (&bcon);
   bson_init (&expected);
   bson_init (&scope);

   bson_append_int32(&scope, "b", -1, 10);

   bson_append_code_with_scope (&expected, "foo", -1, "var a = b;", &scope);

   BCON_APPEND (&bcon, "foo", BCON_CODEWSCOPE ("var a = b;", &scope));

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
   bson_destroy (&scope);
}
Example #24
0
static void
test_inline_nested (void)
{
   bson_t bcon, expected, foo, bar, third;

   bson_init (&bcon);
   bson_init (&expected);
   bson_init (&foo);
   bson_init (&bar);
   bson_init (&third);

   bson_append_utf8 (&third, "hello", -1, "world", -1);
   bson_append_int32 (&bar, "0", -1, 1);
   bson_append_int32 (&bar, "1", -1, 2);
   bson_append_document (&bar, "2", -1, &third);
   bson_append_array (&foo, "bar", -1, &bar);
   bson_append_document (&expected, "foo", -1, &foo);

   BCON_APPEND (&bcon,
                "foo",
                "{",
                "bar",
                "[",
                BCON_INT32 (1),
                BCON_INT32 (2),
                "{",
                "hello",
                "world",
                "}",
                "]",
                "}");

   bson_eq_bson (&bcon, &expected);

   bson_destroy (&bcon);
   bson_destroy (&expected);
   bson_destroy (&foo);
   bson_destroy (&bar);
   bson_destroy (&third);
}
Example #25
0
int
main (int argc, char *argv[])
{
   mongoc_client_t *client = NULL;
   mongoc_database_t *database = NULL;
   mongoc_collection_t *collection = NULL;
   mongoc_cursor_t *cursor = NULL;
   bson_error_t error;
   const char *uristr = "mongodb://127.0.0.1/";
   const char *authuristr;
   bson_t roles;
   bson_t query;
   const bson_t *doc;

   if (argc != 2) {
      printf ("%s - [implicit|scram]\n", argv[0]);
      return 1;
   }

   if (strcmp (argv[1], "implicit") == 0) {
      authuristr = "mongodb://user,=:[email protected]/test?appname=scram-example";
   } else if (strcmp (argv[1], "scram") == 0) {
      authuristr = "mongodb://user,=:[email protected]/"
                   "test?appname=scram-example&authMechanism=SCRAM-SHA-1";
   } else {
      printf ("%s - [implicit|scram]\n", argv[0]);
      return 1;
   }

   mongoc_init ();

   client = mongoc_client_new (uristr);

   if (!client) {
      fprintf (stderr, "Failed to parse URI.\n");
      return EXIT_FAILURE;
   }

   mongoc_client_set_error_api (client, 2);

   database = mongoc_client_get_database (client, "test");

   bson_init (&roles);
   bson_init (&query);

   BCON_APPEND (&roles, "0", "{", "role", "root", "db", "admin", "}");

   mongoc_database_add_user (database, "user,=", "pass", &roles, NULL, &error);

   mongoc_database_destroy (database);

   mongoc_client_destroy (client);

   client = mongoc_client_new (authuristr);

   if (!client) {
      fprintf (stderr, "failed to parse SCRAM uri\n");
      goto CLEANUP;
   }

   mongoc_client_set_error_api (client, 2);

   collection = mongoc_client_get_collection (client, "test", "test");

   cursor = mongoc_collection_find_with_opts (collection, &query, NULL, NULL);

   mongoc_cursor_next (cursor, &doc);

   if (mongoc_cursor_error (cursor, &error)) {
      fprintf (stderr, "Auth error: %s\n", error.message);
      goto CLEANUP;
   }

CLEANUP:

   bson_destroy (&roles);
   bson_destroy (&query);

   if (collection) {
      mongoc_collection_destroy (collection);
   }

   if (client) {
      mongoc_client_destroy (client);
   }

   if (cursor) {
      mongoc_cursor_destroy (cursor);
   }

   mongoc_cleanup ();

   return EXIT_SUCCESS;
}