bson * test_bson_generate_full (void) { bson *b, *d, *a, *scope; guint8 oid[] = "1234567890ab"; a = bson_new (); bson_append_int32 (a, "0", 32); bson_append_int64 (a, "1", (gint64)-42); bson_finish (a); d = bson_new (); bson_append_string (d, "name", "sub-document", -1); bson_append_int32 (d, "answer", 42); bson_finish (d); scope = bson_new (); bson_append_string (scope, "v", "hello world", -1); bson_finish (scope); b = bson_new (); bson_append_double (b, "double", 3.14); bson_append_string (b, "str", "hello world", -1); bson_append_document (b, "doc", d); bson_append_array (b, "array", a); bson_append_binary (b, "binary0", BSON_BINARY_SUBTYPE_GENERIC, (guint8 *)"foo\0bar", 7); bson_append_oid (b, "_id", oid); bson_append_boolean (b, "TRUE", FALSE); bson_append_utc_datetime (b, "date", 1294860709000); bson_append_timestamp (b, "ts", 1294860709000); bson_append_null (b, "null"); bson_append_regex (b, "foobar", "s/foo.*bar/", "i"); bson_append_javascript (b, "alert", "alert (\"hello world!\");", -1); bson_append_symbol (b, "sex", "Marilyn Monroe", -1); bson_append_javascript_w_scope (b, "print", "alert (v);", -1, scope); bson_append_int32 (b, "int32", 32); bson_append_int64 (b, "int64", (gint64)-42); bson_finish (b); bson_free (d); bson_free (a); bson_free (scope); return b; }
void test_bson_js_code_w_scope (void) { bson *b, *scope; scope = bson_new (); bson_append_string (scope, "foo", "bar", -1); bson_finish (scope); /* Test #1: A single JS element, with default size. */ b = bson_new (); ok (bson_append_javascript_w_scope (b, "f", "alert ('hello');", -1, scope), "bson_append_javascript_w_scope() works"); bson_finish (b); cmp_ok (bson_size (b), "==", 51, "BSON javascript w/ element size check"); ok (memcmp (bson_data (b), "\063\000\000\000\017\146\000\053\000\000\000\021\000\000\000" "\141\154\145\162\164\040\050\047\150\145\154\154\157\047\051" "\073\000\022\000\000\000\002\146\157\157\000\004\000\000\000" "\142\141\162\000\000\000", bson_size (b)) == 0, "BSON javascript w/ scope element contents check"); bson_free (b); /* Test #2: A single javascript element, with explicit length. */ b = bson_new (); ok (bson_append_javascript_w_scope (b, "f", "alert ('hello'); garbage", strlen ("alert ('hello');"), scope), "bson_append_javascript_w_scope() with explicit length works"); bson_finish (b); cmp_ok (bson_size (b), "==", 51, "BSON javascript w/ element size check"); ok (memcmp (bson_data (b), "\063\000\000\000\017\146\000\053\000\000\000\021\000\000\000" "\141\154\145\162\164\040\050\047\150\145\154\154\157\047\051" "\073\000\022\000\000\000\002\146\157\157\000\004\000\000\000" "\142\141\162\000\000\000", bson_size (b)) == 0, "BSON javascript w/ scope element contents check"); bson_free (b); /* Test #3: Negative test, passing an invalid arguments. */ b = bson_new (); ok (bson_append_javascript_w_scope (b, "hello", "print();", -42, scope) == FALSE, "bson_append_javascript_w_scope() with an invalid length should fail"); ok (bson_append_javascript_w_scope (b, NULL, "print();", -1, scope) == FALSE, "bson_append_javascript_w_scope() should fail without a key name"); ok (bson_append_javascript_w_scope (b, "hello", NULL, -1, scope) == FALSE, "bson_append_javascript_w_scope() should fail without javascript code"); ok (bson_append_javascript_w_scope (NULL, "hello", "print();", -1, scope) == FALSE, "bson_append_javascript_w_scope() should fail without a BSON object"); ok (bson_append_javascript_w_scope (b, "hello", "print();", -1, NULL) == FALSE, "bson_append_javascript_w_scope() should fail without a scope object"); bson_finish (b); cmp_ok (bson_size (b), "==", 5, "BSON object should be empty"); ok (bson_append_javascript_w_scope (b, "js", "print();", -1, scope) == FALSE, "Appending to a finished element should fail"); bson_free (b); }