bson_ext 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,169 @@
1
+ /* testjson.c */
2
+
3
+ #include "test.h"
4
+ #include <stdio.h>
5
+ #include <string.h>
6
+
7
+ #include "mongo.h"
8
+ #include "json/json.h"
9
+ #include "md5.h"
10
+
11
+ void json_to_bson_append_element( bson_buffer * bb , const char * k , struct json_object * v );
12
+
13
+ /**
14
+ should already have called start_array
15
+ this will not call start/finish
16
+ */
17
+ void json_to_bson_append_array( bson_buffer * bb , struct json_object * a ){
18
+ int i;
19
+ char buf[10];
20
+ for ( i=0; i<json_object_array_length( a ); i++){
21
+ sprintf( buf , "%d" , i );
22
+ json_to_bson_append_element( bb , buf , json_object_array_get_idx( a , i ) );
23
+ }
24
+ }
25
+
26
+ void json_to_bson_append( bson_buffer * bb , struct json_object * o ){
27
+ json_object_object_foreach(o,k,v){
28
+ json_to_bson_append_element( bb , k , v );
29
+ }
30
+ }
31
+
32
+ void json_to_bson_append_element( bson_buffer * bb , const char * k , struct json_object * v ){
33
+ if ( ! v ){
34
+ bson_append_null( bb , k );
35
+ return;
36
+ }
37
+
38
+ switch ( json_object_get_type( v ) ){
39
+ case json_type_int:
40
+ bson_append_int( bb , k , json_object_get_int( v ) );
41
+ break;
42
+ case json_type_boolean:
43
+ bson_append_bool( bb , k , json_object_get_boolean( v ) );
44
+ break;
45
+ case json_type_double:
46
+ bson_append_double( bb , k , json_object_get_double( v ) );
47
+ break;
48
+ case json_type_string:
49
+ bson_append_string( bb , k , json_object_get_string( v ) );
50
+ break;
51
+ case json_type_object:
52
+ bson_append_start_object( bb , k );
53
+ json_to_bson_append( bb , v );
54
+ bson_append_finish_object( bb );
55
+ break;
56
+ case json_type_array:
57
+ bson_append_start_array( bb , k );
58
+ json_to_bson_append_array( bb , v );
59
+ bson_append_finish_object( bb );
60
+ break;
61
+ default:
62
+ fprintf( stderr , "can't handle type for : %s\n" , json_object_to_json_string(v) );
63
+ }
64
+ }
65
+
66
+
67
+ char * json_to_bson( char * js ){
68
+ struct json_object * o = json_tokener_parse(js);
69
+ bson_buffer bb;
70
+
71
+ if ( is_error( o ) ){
72
+ fprintf( stderr , "\t ERROR PARSING\n" );
73
+ return 0;
74
+ }
75
+
76
+ if ( ! json_object_is_type( o , json_type_object ) ){
77
+ fprintf( stderr , "json_to_bson needs a JSON object, not type\n" );
78
+ return 0;
79
+ }
80
+
81
+ bson_buffer_init( &bb );
82
+ json_to_bson_append( &bb , o );
83
+ return bson_buffer_finish( &bb );
84
+ }
85
+
86
+ int json_to_bson_test( char * js , int size , const char * hash ){
87
+ bson b;
88
+ mongo_md5_state_t st;
89
+ mongo_md5_byte_t digest[16];
90
+ char myhash[33];
91
+ int i;
92
+
93
+ fprintf( stderr , "----\n%s\n" , js );
94
+
95
+
96
+ bson_init( &b , json_to_bson( js ) , 1 );
97
+
98
+ if ( b.data == 0 ){
99
+ if ( size == 0 )
100
+ return 1;
101
+ fprintf( stderr , "failed when wasn't supposed to: %s\n" , js );
102
+ return 0;
103
+
104
+ }
105
+
106
+ if ( size != bson_size( &b ) ){
107
+ fprintf( stderr , "sizes don't match [%s] want != got %d != %d\n" , js , size , bson_size(&b) );
108
+ bson_destroy( &b );
109
+ return 0;
110
+ }
111
+
112
+ mongo_md5_init(&st);
113
+ mongo_md5_append( &st , (const mongo_md5_byte_t*)b.data , bson_size( &b ) );
114
+ mongo_md5_finish(&st, digest);
115
+
116
+ for ( i=0; i<16; i++ )
117
+ sprintf( myhash + ( i * 2 ) , "%.2x" , digest[i] );
118
+ myhash[32] = 0;
119
+
120
+ if ( strlen( hash ) != 32 ){
121
+ printf( "\tinvalid hash given got %s\n" , myhash );
122
+ bson_destroy( &b );
123
+ return 0;
124
+ }
125
+ else if ( strstr( myhash , hash ) != myhash ){
126
+ printf( " hashes don't match\n");
127
+ printf( " JSON: %s\n\t%s\n", hash, js );
128
+ printf( " BSON: %s\n", myhash);
129
+ bson_print( &b );
130
+ bson_destroy( &b );
131
+ return 0;
132
+ }
133
+
134
+ bson_destroy( &b );
135
+ return 1;
136
+ }
137
+
138
+ int total = 0;
139
+ int fails = 0;
140
+
141
+ int run_json_to_bson_test( char * js , int size , const char * hash ){
142
+ total++;
143
+ if ( ! json_to_bson_test( js , size , hash ) )
144
+ fails++;
145
+
146
+ return fails;
147
+ }
148
+
149
+ #define JSONBSONTEST run_json_to_bson_test
150
+
151
+ int main(){
152
+
153
+ run_json_to_bson_test( "1" , 0 , 0 );
154
+
155
+ JSONBSONTEST( "{ 'x' : true }" , 9 , "6fe24623e4efc5cf07f027f9c66b5456" );
156
+ JSONBSONTEST( "{ 'x' : null }" , 8 , "12d43430ff6729af501faf0638e68888" );
157
+ JSONBSONTEST( "{ 'x' : 5.2 }" , 16 , "aaeeac4a58e9c30eec6b0b0319d0dff2" );
158
+ JSONBSONTEST( "{ 'x' : 'eliot' }" , 18 , "331a3b8b7cbbe0706c80acdb45d4ebbe" );
159
+ JSONBSONTEST( "{ 'x' : 5.2 , 'y' : 'truth' , 'z' : 1.1 }" , 40 , "7c77b3a6e63e2f988ede92624409da58" );
160
+ JSONBSONTEST( "{ 'x' : 4 }" , 12 , "d1ed8dbf79b78fa215e2ded74548d89d" );
161
+ JSONBSONTEST( "{ 'x' : 5.2 , 'y' : 'truth' , 'z' : 1 }" , 36 , "8993953de080e9d4ef449d18211ef88a" );
162
+ JSONBSONTEST( "{ 'x' : 'eliot' , 'y' : true , 'z' : 1 }" , 29 , "24e79c12e6c746966b123310cb1a3290" );
163
+ JSONBSONTEST( "{ 'a' : { 'b' : 1.1 } }" , 24 , "31887a4b9d55cd9f17752d6a8a45d51f" );
164
+ JSONBSONTEST( "{ 'x' : 5.2 , 'y' : { 'a' : 'eliot' , 'b' : true } , 'z' : null }" , 44 , "b3de8a0739ab329e7aea138d87235205" );
165
+ JSONBSONTEST( "{ 'x' : 5.2 , 'y' : [ 'a' , 'eliot' , 'b' , true ] , 'z' : null }" , 62 , "cb7bad5697714ba0cbf51d113b6a0ee8" );
166
+
167
+ fprintf( stderr, "----\ntotal: %d\nfails : %d\n" , total , fails );
168
+ return fails;
169
+ }
@@ -0,0 +1,41 @@
1
+ #include "test.h"
2
+ #include "mongo.h"
3
+ #include <stdio.h>
4
+ #include <string.h>
5
+ #include <stdlib.h>
6
+
7
+ static mongo_connection conn[1];
8
+ static mongo_connection_options left;
9
+ static mongo_connection_options right;
10
+
11
+ int main(){
12
+
13
+ INIT_SOCKETS_FOR_WINDOWS;
14
+
15
+ strncpy(left.host, TEST_SERVER, 255);
16
+ left.host[254] = '\0';
17
+ left.port = 27017;
18
+
19
+ strncpy(right.host, "0.0.0.0", 255);
20
+ right.port = 12345;
21
+
22
+ ASSERT(mongo_connect_pair(conn, &left, &right) == mongo_conn_success);
23
+ ASSERT(conn->left_opts->port == 27017);
24
+ ASSERT(conn->right_opts->port == 12345);
25
+ ASSERT(mongo_cmd_ismaster(conn, NULL));
26
+
27
+ mongo_destroy(conn);
28
+
29
+ ASSERT(mongo_connect_pair(conn, &right, &left) == mongo_conn_success);
30
+ ASSERT(conn->left_opts->port == 27017); /* should have swapped left and right */
31
+ ASSERT(conn->right_opts->port == 12345);
32
+ ASSERT(mongo_cmd_ismaster(conn, NULL));
33
+
34
+ ASSERT(mongo_reconnect(conn) == mongo_conn_success);
35
+ ASSERT(conn->left_opts->port == 27017); /* should have swapped left and right */
36
+ ASSERT(conn->right_opts->port == 12345);
37
+ ASSERT(mongo_cmd_ismaster(conn, NULL));
38
+
39
+ mongo_destroy(conn);
40
+ return 0;
41
+ }
@@ -0,0 +1,81 @@
1
+ /* test.c */
2
+
3
+ #include "test.h"
4
+ #include "mongo.h"
5
+ #include <stdio.h>
6
+ #include <string.h>
7
+ #include <stdlib.h>
8
+ #include <unistd.h>
9
+
10
+ int test_connect( const char* set_name ) {
11
+
12
+ mongo_connection conn[1];
13
+ int res;
14
+
15
+ INIT_SOCKETS_FOR_WINDOWS;
16
+
17
+ mongo_replset_init_conn( conn, set_name );
18
+ mongo_replset_add_seed( conn, TEST_SERVER, 30000 );
19
+ mongo_replset_add_seed( conn, TEST_SERVER, 30001 );
20
+
21
+ if( res = mongo_replset_connect( conn ) ) {
22
+ mongo_destroy( conn );
23
+ return res;
24
+ }
25
+ else {
26
+ mongo_disconnect( conn );
27
+ return mongo_reconnect( conn );
28
+ }
29
+ }
30
+
31
+ int test_reconnect( const char* set_name ) {
32
+
33
+ mongo_connection conn[1];
34
+ int res;
35
+ int e = 0;
36
+ bson b;
37
+
38
+ INIT_SOCKETS_FOR_WINDOWS;
39
+
40
+ mongo_replset_init_conn( conn, set_name );
41
+ mongo_replset_add_seed( conn, TEST_SERVER, 30000 );
42
+ mongo_replset_add_seed( conn, TEST_SERVER, 30001 );
43
+
44
+ if( res = mongo_replset_connect( conn ) ) {
45
+ mongo_destroy( conn );
46
+ return res;
47
+ }
48
+ else {
49
+ fprintf( stderr, "Disconnect now:\n");
50
+ sleep( 10 );
51
+ do {
52
+ MONGO_TRY {
53
+ e = 1;
54
+ res = mongo_find_one( conn, "foo.bar", bson_empty(&b), bson_empty(&b), NULL);
55
+ e = 0;
56
+ } MONGO_CATCH {
57
+ sleep( 2 );
58
+ if( e++ < 30) {
59
+ fprintf( stderr, "Attempting reconnect %d.\n", e);
60
+ mongo_reconnect( conn );
61
+ } else {
62
+ fprintf( stderr, "Fail.\n");
63
+ return -1;
64
+ }
65
+ }
66
+ } while(e);
67
+ }
68
+
69
+ return 0;
70
+ }
71
+
72
+ int main() {
73
+ ASSERT( test_connect( "test-rs" ) == 0 );
74
+ ASSERT( test_connect( "test-foobar" ) == mongo_conn_bad_set_name );
75
+
76
+ /* Run this for testing failover.
77
+ ASSERT( test_reconnect( "test-rs" ) == 0 );
78
+ */
79
+
80
+ return 0;
81
+ }
@@ -0,0 +1,35 @@
1
+ /* resize.c */
2
+
3
+ #include "test.h"
4
+ #include "bson.h"
5
+ #include <string.h>
6
+
7
+ /* 64 Xs */
8
+ const char* bigstring = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
9
+
10
+ int main(){
11
+ bson_buffer bb;
12
+ bson b;
13
+
14
+ bson_buffer_init(&bb);
15
+ bson_append_string(&bb, "a", bigstring);
16
+ bson_append_start_object(&bb, "sub");
17
+ bson_append_string(&bb,"a", bigstring);
18
+ bson_append_start_object(&bb, "sub");
19
+ bson_append_string(&bb,"a", bigstring);
20
+ bson_append_start_object(&bb, "sub");
21
+ bson_append_string(&bb,"a", bigstring);
22
+ bson_append_string(&bb,"b", bigstring);
23
+ bson_append_string(&bb,"c", bigstring);
24
+ bson_append_string(&bb,"d", bigstring);
25
+ bson_append_string(&bb,"e", bigstring);
26
+ bson_append_string(&bb,"f", bigstring);
27
+ bson_append_finish_object(&bb);
28
+ bson_append_finish_object(&bb);
29
+ bson_append_finish_object(&bb);
30
+ bson_from_buffer(&b, &bb);
31
+
32
+ /* bson_print(&b); */
33
+ bson_destroy(&b);
34
+ return 0;
35
+ }
@@ -0,0 +1,110 @@
1
+ /* test.c */
2
+
3
+ #include "test.h"
4
+ #include "mongo.h"
5
+ #include <stdio.h>
6
+ #include <string.h>
7
+ #include <stdlib.h>
8
+
9
+ int main(){
10
+ mongo_connection conn[1];
11
+ bson_buffer bb;
12
+ bson b;
13
+ mongo_cursor * cursor;
14
+ int i;
15
+ char hex_oid[25];
16
+ bson_timestamp_t ts = { 1, 2 };
17
+
18
+ const char * col = "c.simple";
19
+ const char * ns = "test.c.simple";
20
+
21
+ INIT_SOCKETS_FOR_WINDOWS;
22
+
23
+ if (mongo_connect( conn , TEST_SERVER, 27017 )){
24
+ printf("failed to connect\n");
25
+ exit(1);
26
+ }
27
+
28
+ /* if the collection doesn't exist dropping it will fail */
29
+ if (!mongo_cmd_drop_collection(conn, "test", col, NULL)
30
+ && mongo_find_one(conn, ns, bson_empty(&b), bson_empty(&b), NULL)){
31
+ printf("failed to drop collection\n");
32
+ exit(1);
33
+ }
34
+
35
+ for(i=0; i< 5; i++){
36
+ bson_buffer_init( & bb );
37
+
38
+ bson_append_new_oid( &bb, "_id" );
39
+ bson_append_timestamp( &bb, "ts", &ts );
40
+ bson_append_double( &bb , "a" , 17 );
41
+ bson_append_int( &bb , "b" , 17 );
42
+ bson_append_string( &bb , "c" , "17" );
43
+
44
+ {
45
+ bson_buffer * sub = bson_append_start_object( &bb , "d" );
46
+ bson_append_int( sub, "i", 71 );
47
+ bson_append_finish_object(sub);
48
+ }
49
+ {
50
+ bson_buffer * arr = bson_append_start_array( &bb , "e" );
51
+ bson_append_int( arr, "0", 71 );
52
+ bson_append_string( arr, "1", "71" );
53
+ bson_append_finish_object(arr);
54
+ }
55
+
56
+ bson_from_buffer(&b, &bb);
57
+ mongo_insert( conn , ns , &b );
58
+ bson_destroy(&b);
59
+ }
60
+
61
+ cursor = mongo_find( conn , ns , bson_empty(&b) , 0 , 0 , 0 , 0 );
62
+
63
+ while (mongo_cursor_next(cursor)){
64
+ bson_iterator it;
65
+ bson_iterator_init(&it, cursor->current.data);
66
+ while(bson_iterator_next(&it)){
67
+ fprintf(stderr, " %s: ", bson_iterator_key(&it));
68
+
69
+ switch(bson_iterator_type(&it)){
70
+ case bson_double:
71
+ fprintf(stderr, "(double) %e\n", bson_iterator_double(&it));
72
+ break;
73
+ case bson_int:
74
+ fprintf(stderr, "(int) %d\n", bson_iterator_int(&it));
75
+ break;
76
+ case bson_string:
77
+ fprintf(stderr, "(string) \"%s\"\n", bson_iterator_string(&it));
78
+ break;
79
+ case bson_oid:
80
+ bson_oid_to_string(bson_iterator_oid(&it), hex_oid);
81
+ fprintf(stderr, "(oid) \"%s\"\n", hex_oid);
82
+ break;
83
+ case bson_object:
84
+ fprintf(stderr, "(subobject) {...}\n");
85
+ break;
86
+ case bson_array:
87
+ fprintf(stderr, "(array) [...]\n");
88
+ break;
89
+ case bson_timestamp:
90
+ fprintf(stderr, "(timestamp) [...]\n");
91
+ break;
92
+ default:
93
+ fprintf(stderr, "(type %d)\n", bson_iterator_type(&it));
94
+ break;
95
+ }
96
+ }
97
+ fprintf(stderr, "\n");
98
+ }
99
+
100
+ mongo_cursor_destroy(cursor);
101
+ mongo_cmd_drop_db(conn, "test");
102
+ mongo_disconnect( conn );
103
+
104
+ mongo_reconnect( conn );
105
+
106
+ ASSERT( mongo_simple_int_command( conn, "admin", "ping", 1, NULL ) );
107
+
108
+ mongo_destroy( conn );
109
+ return 0;
110
+ }
@@ -0,0 +1,22 @@
1
+ /* sizes.c */
2
+
3
+ #include "test.h"
4
+ #include "mongo.h"
5
+ #include <stdio.h>
6
+
7
+ int main(){
8
+ mongo_reply mr;
9
+
10
+ ASSERT(sizeof(int) == 4);
11
+ ASSERT(sizeof(int64_t) == 8);
12
+ ASSERT(sizeof(double) == 8);
13
+ ASSERT(sizeof(bson_oid_t) == 12);
14
+
15
+ ASSERT(sizeof(mongo_header) == 4+4+4+4);
16
+ ASSERT(sizeof(mongo_reply_fields) == 4+8+4+4);
17
+
18
+ /* field offset of obj in mongo_reply */
19
+ ASSERT((&mr.objs - (char*)&mr) == (4+4+4+4 + 4+8+4+4));
20
+
21
+ return 0;
22
+ }