1 #include "tap.h"
2 #include "test.h"
3 #include "bson.h"
4 
5 #include <string.h>
6 
7 void
test_bson_int64(void)8 test_bson_js_code_w_scope (void)
9 {
10   bson *b, *scope;
11 
12   scope = bson_new ();
13   bson_append_string (scope, "foo", "bar", -1);
14   bson_finish (scope);
15 
16   /* Test #1: A single JS element, with default size. */
17   b = bson_new ();
18   ok (bson_append_javascript_w_scope (b, "f",
19                                       "alert ('hello');", -1,
20                                       scope),
21       "bson_append_javascript_w_scope() works");
22   bson_finish (b);
23 
24   cmp_ok (bson_size (b), "==", 51, "BSON javascript w/ element size check");
25   ok (memcmp (bson_data (b),
26               "\063\000\000\000\017\146\000\053\000\000\000\021\000\000\000"
27               "\141\154\145\162\164\040\050\047\150\145\154\154\157\047\051"
28               "\073\000\022\000\000\000\002\146\157\157\000\004\000\000\000"
29               "\142\141\162\000\000\000",
30               bson_size (b)) == 0,
31       "BSON javascript w/ scope element contents check");
32   bson_free (b);
33 
34   /* Test #2: A single javascript element, with explicit length. */
35   b = bson_new ();
36   ok (bson_append_javascript_w_scope (b, "f",
37                                       "alert ('hello'); garbage",
38                                       strlen ("alert ('hello');"),
39                                       scope),
40       "bson_append_javascript_w_scope() with explicit length works");
41   bson_finish (b);
42 
43   cmp_ok (bson_size (b), "==", 51, "BSON javascript w/ element size check");
44   ok (memcmp (bson_data (b),
45               "\063\000\000\000\017\146\000\053\000\000\000\021\000\000\000"
46               "\141\154\145\162\164\040\050\047\150\145\154\154\157\047\051"
47               "\073\000\022\000\000\000\002\146\157\157\000\004\000\000\000"
48               "\142\141\162\000\000\000",
49               bson_size (b)) == 0,
50       "BSON javascript w/ scope element contents check");
51   bson_free (b);
52 
53   /* Test #3: Negative test, passing an invalid arguments. */
54   b = bson_new ();
55 
56   ok (bson_append_javascript_w_scope (b, "hello", "print();",
57                                       -42, scope) == FALSE,
58       "bson_append_javascript_w_scope() with an invalid length should fail");
59   ok (bson_append_javascript_w_scope (b, NULL, "print();", -1, scope) == FALSE,
60       "bson_append_javascript_w_scope() should fail without a key name");
61   ok (bson_append_javascript_w_scope (b, "hello", NULL, -1, scope) == FALSE,
62       "bson_append_javascript_w_scope() should fail without javascript code");
63   ok (bson_append_javascript_w_scope (NULL, "hello", "print();",
64                                       -1, scope) == FALSE,
65       "bson_append_javascript_w_scope() should fail without a BSON object");
66   ok (bson_append_javascript_w_scope (b, "hello", "print();",
67                                       -1, NULL) == FALSE,
68       "bson_append_javascript_w_scope() should fail without a scope object");
69   bson_finish (b);
70   cmp_ok (bson_size (b), "==", 5,
71           "BSON object should be empty");
72 
73   ok (bson_append_javascript_w_scope (b, "js", "print();", -1, scope) == FALSE,
74       "Appending to a finished element should fail");
75 
76   bson_free (b);
77 }
78 
79 RUN_TEST (13, bson_js_code_w_scope);
80