blob: 3d16dc074707279329a47a5594b1a9ad1ff15445 [file] [log] [blame]
Chandler May6dde90b2016-01-10 06:01:10 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/* Disable string-function optimizations when glibc is used, as these produce
21 compiler warnings about string length when a string function is used inside
22 a call to assert () */
23#if !defined(__APPLE__) && !defined(__FreeBSD__) && \
24 !defined(__OpenBSD__) && !defined(__NetBSD__)
25#include <features.h>
26#endif
27
28#ifdef __GLIBC__
29#define __NO_STRING_INLINES 1
30#endif
31
32#include <unistd.h>
33#include <stdlib.h>
34#include <stdio.h>
35#include <assert.h>
36#include <netdb.h>
37#include <string.h>
38#include <sys/wait.h>
39
40#include <thrift/c_glib/protocol/thrift_protocol.h>
41#include <thrift/c_glib/transport/thrift_socket.h>
42#include <thrift/c_glib/transport/thrift_server_socket.h>
43
44#define TEST_BOOL TRUE
45#define TEST_BYTE 123
46#define TEST_I16 12345
47#define TEST_I32 1234567890
48#define TEST_I64 123456789012345
49#define TEST_NI16 (-12345)
50#define TEST_NI32 (-1234567890)
51#define TEST_NI64 (-123456789012345)
52#define TEST_DOUBLE 1234567890.123
53#define TEST_STRING "this is a test string 1234567890!@#$%^&*()"
54#define TEST_PORT 51199
55
56static int transport_read_count = 0;
57static int transport_read_error = 0;
58static int transport_read_error_at = -1;
59gint32
60my_thrift_transport_read (ThriftTransport *transport, gpointer buf,
61 guint32 len, GError **error)
62{
63 if (transport_read_count != transport_read_error_at
64 && transport_read_error == 0)
65 {
66 transport_read_count++;
67 return thrift_transport_read (transport, buf, len, error);
68 }
69 return -1;
70}
71
72static int transport_write_count = 0;
73static int transport_write_error = 0;
74static int transport_write_error_at = -1;
75gboolean
76my_thrift_transport_write (ThriftTransport *transport, const gpointer buf,
77 const guint32 len, GError **error)
78{
79 if (transport_write_count != transport_write_error_at
80 && transport_write_error == 0)
81 {
82 transport_write_count++;
83 return thrift_transport_write (transport, buf, len, error);
84 }
85 return FALSE;
86}
87
88#define thrift_transport_read my_thrift_transport_read
89#define thrift_transport_write my_thrift_transport_write
90#include "../src/thrift/c_glib/protocol/thrift_compact_protocol.c"
91#undef thrift_transport_read
92#undef thrift_transport_write
93
94static void thrift_server_primitives (const int port);
95static void thrift_server_complex_types (const int port);
96
97static void
98test_create_and_destroy (void)
99{
100 GObject *object = NULL;
101
102 /* create an object and then destroy it */
103 object = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, NULL);
104 assert (object != NULL);
105 g_object_unref (object);
106}
107
108static void
109test_initialize (void)
110{
111 ThriftSocket *tsocket = NULL;
112 ThriftCompactProtocol *protocol = NULL;
113 ThriftSocket *temp = NULL;
114
115 /* create a ThriftTransport */
116 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
117 "port", 51188, NULL);
118 assert (tsocket != NULL);
119 /* create a ThriftCompactProtocol using the Transport */
120 protocol = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
121 tsocket, NULL);
122 assert (protocol != NULL);
123 /* fetch the properties */
124 g_object_get (G_OBJECT (protocol), "transport", &temp, NULL);
125 g_object_unref (temp);
126
127 /* clean up memory */
128 g_object_unref (protocol);
129 g_object_unref (tsocket);
130}
131
132static void
133test_read_and_write_primitives (void)
134{
135 int status;
136 pid_t pid;
137 ThriftSocket *tsocket = NULL;
138 ThriftTransport *transport = NULL;
139 ThriftCompactProtocol *tc = NULL;
140 ThriftProtocol *protocol = NULL;
141 gpointer binary = (gpointer *) TEST_STRING;
142 guint32 len = strlen (TEST_STRING);
143 int port = TEST_PORT;
144
145 /* fork a server from the client */
146 pid = fork ();
147 assert (pid >= 0);
148
149 if (pid == 0)
150 {
151 /* child listens */
152 thrift_server_primitives (port);
153 exit (0);
154 } else {
155 /* parent. wait a bit for the socket to be created. */
156 sleep (1);
157
158 /* create a ThriftSocket */
159 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
160 "port", port, NULL);
161 transport = THRIFT_TRANSPORT (tsocket);
162 thrift_transport_open (transport, NULL);
163 assert (thrift_transport_is_open (transport));
164
165 /* create a ThriftCompactTransport */
166 tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
167 tsocket, NULL);
168 protocol = THRIFT_PROTOCOL (tc);
169 assert (protocol != NULL);
170
171 /* write a bunch of primitives */
172 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
173 assert (thrift_compact_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
174 assert (thrift_compact_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
175 assert (thrift_compact_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
176 assert (thrift_compact_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
177 assert (thrift_compact_protocol_write_i16 (protocol, TEST_NI16, NULL) > 0);
178 assert (thrift_compact_protocol_write_i32 (protocol, TEST_NI32, NULL) > 0);
179 assert (thrift_compact_protocol_write_i64 (protocol, TEST_NI64, NULL) > 0);
180 assert (thrift_compact_protocol_write_i16 (protocol, 2, NULL) > 0);
181 assert (thrift_compact_protocol_write_i32 (protocol, 2, NULL) > 0);
182 assert (thrift_compact_protocol_write_i64 (protocol, 2, NULL) > 0);
183 assert (thrift_compact_protocol_write_i16 (protocol, -2, NULL) > 0);
184 assert (thrift_compact_protocol_write_i32 (protocol, -2, NULL) > 0);
185 assert (thrift_compact_protocol_write_i64 (protocol, -2, NULL) > 0);
186 assert (thrift_compact_protocol_write_double (protocol,
187 TEST_DOUBLE, NULL) > 0);
188 assert (thrift_compact_protocol_write_string (protocol,
189 TEST_STRING, NULL) > 0);
190 assert (thrift_compact_protocol_write_binary (protocol, binary,
191 len, NULL) > 0);
192 assert (thrift_compact_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
193 assert (thrift_compact_protocol_write_binary (protocol, binary,
194 len, NULL) > 0);
195
196 /* test write errors */
197 transport_write_error = 1;
198 assert (thrift_compact_protocol_write_byte (protocol, TEST_BYTE,
199 NULL) == -1);
200 assert (thrift_compact_protocol_write_i16 (protocol, TEST_I16, NULL) == -1);
201 assert (thrift_compact_protocol_write_i32 (protocol, TEST_I32, NULL) == -1);
202 assert (thrift_compact_protocol_write_i64 (protocol, TEST_I64, NULL) == -1);
203 assert (thrift_compact_protocol_write_i16 (protocol, TEST_NI16,
204 NULL) == -1);
205 assert (thrift_compact_protocol_write_i32 (protocol, TEST_NI32,
206 NULL) == -1);
207 assert (thrift_compact_protocol_write_i64 (protocol, TEST_NI64,
208 NULL) == -1);
209 assert (thrift_compact_protocol_write_double (protocol, TEST_DOUBLE,
210 NULL) == -1);
211 assert (thrift_compact_protocol_write_binary (protocol, binary, len,
212 NULL) == -1);
213 transport_write_error = 0;
214
215 /* test binary partial failure */
216 transport_write_count = 0;
217 transport_write_error_at = 1;
218 assert (thrift_compact_protocol_write_binary (protocol, binary,
219 len, NULL) == -1);
220 transport_write_error_at = -1;
221
222 /* clean up */
223 thrift_transport_close (transport, NULL);
224 g_object_unref (tsocket);
225 g_object_unref (protocol);
226 assert (wait (&status) == pid);
227 assert (status == 0);
228 }
229}
230
231static void
232test_read_and_write_complex_types (void)
233{
234 int status;
235 pid_t pid;
236 ThriftSocket *tsocket = NULL;
237 ThriftTransport *transport = NULL;
238 ThriftCompactProtocol *tc = NULL;
239 ThriftProtocol *protocol = NULL;
240 int port = TEST_PORT;
241
242 /* fork a server from the client */
243 pid = fork ();
244 assert (pid >= 0);
245
246 if (pid == 0)
247 {
248 /* child listens */
249 thrift_server_complex_types (port);
250 exit (0);
251 } else {
252 /* parent. wait a bit for the socket to be created. */
253 sleep (1);
254
255 /* create a ThriftSocket */
256 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
257 "port", port, NULL);
258 transport = THRIFT_TRANSPORT (tsocket);
259 thrift_transport_open (transport, NULL);
260 assert (thrift_transport_is_open (transport));
261
262 /* create a ThriftCompactTransport */
263 tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
264 tsocket, NULL);
265 protocol = THRIFT_PROTOCOL (tc);
266 assert (protocol != NULL);
267
268 /* test structures */
269 assert (thrift_compact_protocol_write_struct_begin (protocol,
270 NULL, NULL) == 0);
271 assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0);
272
273 /* test field state w.r.t. deltas */
274
275 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
276 T_DOUBLE, 1, NULL) == 1);
277 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
278 T_DOUBLE,
279 16, NULL) == 1);
280 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
281 T_DOUBLE,
282 17, NULL) == 1);
283 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
284 T_DOUBLE,
285 15, NULL) > 1);
286 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
287 T_DOUBLE,
288 30, NULL) == 1);
289 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
290 T_DOUBLE,
291 46, NULL) > 1);
292 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
293 T_DOUBLE,
294 47, NULL) == 1);
295
296 /* test fields */
297 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
298 T_DOUBLE,
299 1, NULL) > 1);
300 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
301
302 /* test field state w.r.t. structs */
303
304 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
305 T_DOUBLE,
306 1, NULL) > 1);
307 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
308 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
309 T_DOUBLE,
310 16, NULL) == 1);
311 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
312
313 assert (thrift_compact_protocol_write_struct_begin (protocol,
314 NULL, NULL) == 0);
315 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
316 T_DOUBLE,
317 17, NULL) > 1);
318 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
319
320 assert (thrift_compact_protocol_write_struct_begin (protocol,
321 NULL, NULL) == 0);
322 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
323 T_DOUBLE,
324 18, NULL) > 1);
325 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
326 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
327 T_DOUBLE,
328 19, NULL) == 1);
329 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
330 assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0);
331
332 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
333 T_DOUBLE,
334 18, NULL) == 1);
335 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
336 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
337 T_DOUBLE,
338 25, NULL) == 1);
339 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
340 assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0);
341
342 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
343 T_DOUBLE,
344 17, NULL) == 1);
345 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
346
347 /* test field state w.r.t. bools */
348
349 /* deltas */
350 /* non-bool field -> bool field -> non-bool field */
351 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
352 T_DOUBLE,
353 18, NULL) == 1);
354 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
355 assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
356 19, NULL) == 0);
357 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL,
358 NULL) == 1);
359 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
360 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
361 T_DOUBLE,
362 20, NULL) == 1);
363 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
364 /* bool -> bool field -> bool */
365 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
366 assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
367 21, NULL) == 0);
368 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL,
369 NULL) == 1);
370 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
371 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
372
373 /* no deltas */
374 /* non-bool field -> bool field -> non-bool field */
375 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
376 T_DOUBLE,
377 1, NULL) > 1);
378 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
379 assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
380 1, NULL) == 0);
381 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 1);
382 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
383 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
384 T_DOUBLE,
385 1, NULL) > 1);
386 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
387 /* bool -> bool field -> bool */
388 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
389 assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
390 1, NULL) == 0);
391 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 1);
392 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
393 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
394
395 /* test write error */
396 transport_write_error = 1;
397 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
398 T_DOUBLE,
399 1, NULL) == -1);
400 transport_write_error = 0;
401
402 /* test 2nd write error */
403 transport_write_count = 0;
404 transport_write_error_at = 1;
405 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
406 T_DOUBLE,
407 1, NULL) == -1);
408 transport_write_error_at = -1;
409
410 /* test 2nd read failure on a field */
411 thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL);
412
413 /* test write_field_stop */
414 assert (thrift_compact_protocol_write_field_stop (protocol, NULL) > 0);
415
416 /* write a map */
417 assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE,
418 T_DOUBLE,
419 1, NULL) > 0);
420 assert (thrift_compact_protocol_write_map_end (protocol, NULL) == 0);
421
422 /* test 1st read failure on map---nothing to do on our side */
423
424 /* test 2nd read failure on a map */
425 thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL);
426
427 /* test 1st write failure on a map */
428 transport_write_error = 1;
429 assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE,
430 T_DOUBLE,
431 1, NULL) == -1);
432 transport_write_error = 0;
433
434 /* test 2nd write failure on a map */
435 transport_write_count = 0;
436 transport_write_error_at = 1;
437 assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE,
438 T_DOUBLE,
439 1, NULL) == -1);
440 transport_write_error_at = -1;
441
442 /* test negative map size */
443 thrift_compact_protocol_write_varint32 (tc, -10, NULL);
444 thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL);
445
446 /* test list operations */
447 assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
448 15, NULL) > 0);
449 assert (thrift_compact_protocol_write_list_end (protocol, NULL) == 0);
450
451 /* test 1st read failure on a small list---nothing to do on our end */
452
453 /* test 1st read failure on a big list---nothing to do on our end */
454
455 /* test 2nd read failure on a big list */
456 thrift_compact_protocol_write_byte (protocol, (gint8) 0xf0, NULL);
457
458 /* test negative list size */
459 thrift_compact_protocol_write_byte (protocol, (gint8) 0xf0, NULL);
460 thrift_compact_protocol_write_varint32 (tc, -10, NULL);
461
462 /* test first write error on a small list */
463 transport_write_error = 1;
464 assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
465 14, NULL) == -1);
466 transport_write_error = 0;
467
468 /* test first write error on a big list */
469 transport_write_error = 1;
470 assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
471 15, NULL) == -1);
472 transport_write_error = 0;
473
474 /* test 2nd write error on a big list */
475 transport_write_count = 0;
476 transport_write_error_at = 1;
477 assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
478 15, NULL) == -1);
479 transport_write_error_at = -1;
480
481 /* test set operation s*/
482 assert (thrift_compact_protocol_write_set_begin (protocol, T_DOUBLE,
483 1, NULL) > 0);
484 assert (thrift_compact_protocol_write_set_end (protocol, NULL) == 0);
485
486 /* invalid protocol */
487 assert (thrift_compact_protocol_write_byte (protocol, 0, NULL) > 0);
488
489 /* invalid version */
490 assert (thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u,
491 NULL) > 0);
492 assert (thrift_compact_protocol_write_byte (protocol, 0, NULL) > 0);
493
494 /* send a valid message */
495 assert (thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u,
496 NULL) > 0);
497 assert (thrift_compact_protocol_write_byte (protocol, 0x01u, NULL) > 0);
498 thrift_compact_protocol_write_varint32 (tc, 1, NULL);
499 thrift_compact_protocol_write_string (protocol, "test", NULL);
500
501 /* broken 2nd read */
502 thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL);
503
504 /* send a broken 3rd read */
505 thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL);
506 thrift_compact_protocol_write_byte (protocol, 0x01u, NULL);
507
508 /* send a broken 4th read */
509 thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL);
510 thrift_compact_protocol_write_byte (protocol, 0x01u, NULL);
511 thrift_compact_protocol_write_varint32 (tc, 1, NULL);
512
513 /* send a valid message */
514 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
515 T_CALL, 1, NULL) > 0);
516
517 assert (thrift_compact_protocol_write_message_end (protocol, NULL) == 0);
518
519 /* send broken writes */
520 transport_write_error = 1;
521 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
522 T_CALL, 1, NULL) == -1);
523 transport_write_error = 0;
524
525 transport_write_count = 0;
526 transport_write_error_at = 1;
527 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
528 T_CALL, 1, NULL) == -1);
529 transport_write_error_at = -1;
530
531 transport_write_count = 0;
532 transport_write_error_at = 2;
533 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
534 T_CALL, 1, NULL) == -1);
535 transport_write_error_at = -1;
536
537 transport_write_count = 0;
538 transport_write_error_at = 3;
539 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
540 T_CALL, 1, NULL) == -1);
541 transport_write_error_at = -1;
542
543 /* clean up */
544 thrift_transport_close (transport, NULL);
545 g_object_unref (tsocket);
546 g_object_unref (protocol);
547 assert (wait (&status) == pid);
548 assert (status == 0);
549 }
550}
551
552
553static void
554thrift_server_primitives (const int port)
555{
556 ThriftServerTransport *transport = NULL;
557 ThriftTransport *client = NULL;
558 ThriftCompactProtocol *tc = NULL;
559 ThriftProtocol *protocol = NULL;
560 gboolean value_boolean = FALSE;
561 gint8 value_byte = 0,
562 zigzag_p16 = 0, zigzag_p32 = 0, zigzag_p64 = 0,
563 zigzag_n16 = 0, zigzag_n32 = 0, zigzag_n64 = 0;
564 gint16 value_16 = 0;
565 gint32 value_32 = 0;
566 gint64 value_64 = 0;
567 gint16 value_n16 = 0;
568 gint32 value_n32 = 0;
569 gint64 value_n64 = 0;
570 gdouble value_double = 0;
571 gchar *string = NULL;
572 gpointer binary = NULL;
573 guint32 len = 0;
574 void *comparator = (void *) TEST_STRING;
575
576 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
577 "port", port, NULL);
578 transport = THRIFT_SERVER_TRANSPORT (tsocket);
579 thrift_server_transport_listen (transport, NULL);
580 client = thrift_server_transport_accept (transport, NULL);
581 assert (client != NULL);
582
583 tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
584 client, NULL);
585 protocol = THRIFT_PROTOCOL (tc);
586
587 assert (thrift_compact_protocol_read_bool (protocol,
588 &value_boolean, NULL) > 0);
589 assert (thrift_compact_protocol_read_byte (protocol, &value_byte, NULL) > 0);
590 assert (thrift_compact_protocol_read_i16 (protocol, &value_16, NULL) > 0);
591 assert (thrift_compact_protocol_read_i32 (protocol, &value_32, NULL) > 0);
592 assert (thrift_compact_protocol_read_i64 (protocol, &value_64, NULL) > 0);
593 assert (thrift_compact_protocol_read_i16 (protocol, &value_n16, NULL) > 0);
594 assert (thrift_compact_protocol_read_i32 (protocol, &value_n32, NULL) > 0);
595 assert (thrift_compact_protocol_read_i64 (protocol, &value_n64, NULL) > 0);
596 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p16, NULL) > 0);
597 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p32, NULL) > 0);
598 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p64, NULL) > 0);
599 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n16, NULL) > 0);
600 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n32, NULL) > 0);
601 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n64, NULL) > 0);
602 assert (thrift_compact_protocol_read_double (protocol,
603 &value_double, NULL) > 0);
604 assert (thrift_compact_protocol_read_string (protocol, &string, NULL) > 0);
605 assert (thrift_compact_protocol_read_binary (protocol, &binary,
606 &len, NULL) > 0);
607
608 assert (value_boolean == TEST_BOOL);
609 assert (value_byte == TEST_BYTE);
610 assert (value_16 == TEST_I16);
611 assert (value_32 == TEST_I32);
612 assert (value_64 == TEST_I64);
613 assert (value_n16 == TEST_NI16);
614 assert (value_n32 == TEST_NI32);
615 assert (value_n64 == TEST_NI64);
616 assert (zigzag_p16 == 4);
617 assert (zigzag_p32 == 4);
618 assert (zigzag_p64 == 4);
619 assert (zigzag_n16 == 3);
620 assert (zigzag_n32 == 3);
621 assert (zigzag_n64 == 3);
622 assert (value_double == TEST_DOUBLE);
623 assert (strcmp (TEST_STRING, string) == 0);
624 assert (memcmp (comparator, binary, len) == 0);
625
626 g_free (string);
627 g_free (binary);
628
629 thrift_compact_protocol_read_binary (protocol, &binary, &len, NULL);
630 g_free (binary);
631
632 transport_read_count = 0;
633 transport_read_error_at = 0;
634 assert (thrift_compact_protocol_read_binary (protocol, &binary,
635 &len, NULL) == -1);
636 transport_read_error_at = -1;
637
638 transport_read_count = 0;
639 transport_read_error_at = 1;
640 assert (thrift_compact_protocol_read_binary (protocol, &binary,
641 &len, NULL) == -1);
642 transport_read_error_at = -1;
643
644 transport_read_error = 1;
645 assert (thrift_compact_protocol_read_bool (protocol,
646 &value_boolean, NULL) == -1);
647 assert (thrift_compact_protocol_read_byte (protocol,
648 &value_byte, NULL) == -1);
649 assert (thrift_compact_protocol_read_i16 (protocol,
650 &value_16, NULL) == -1);
651 assert (thrift_compact_protocol_read_i32 (protocol, &value_32, NULL) == -1);
652 assert (thrift_compact_protocol_read_i64 (protocol, &value_64, NULL) == -1);
653 assert (thrift_compact_protocol_read_i16 (protocol,
654 &value_n16, NULL) == -1);
655 assert (thrift_compact_protocol_read_i32 (protocol, &value_n32, NULL) == -1);
656 assert (thrift_compact_protocol_read_i64 (protocol, &value_n64, NULL) == -1);
657 assert (thrift_compact_protocol_read_double (protocol,
658 &value_double, NULL) == -1);
659 transport_read_error = 0;
660
661 /* test partial write failure */
662 thrift_protocol_read_i32 (protocol, &value_32, NULL);
663
664 thrift_transport_read_end (client, NULL);
665 thrift_transport_close (client, NULL);
666
667 g_object_unref (tc);
668 g_object_unref (client);
669 g_object_unref (tsocket);
670}
671
672static void
673thrift_server_complex_types (const int port)
674{
675 ThriftServerTransport *transport = NULL;
676 ThriftTransport *client = NULL;
677 ThriftCompactProtocol *tc = NULL;
678 ThriftProtocol *protocol = NULL;
679 gchar *struct_name = NULL;
680 gchar *field_name = NULL;
681 gchar *message_name = NULL;
682 ThriftType element_type, key_type, value_type, field_type;
683 ThriftMessageType message_type;
684 gboolean value_boolean = ! TEST_BOOL;
685 gint8 value = 0;
686 gint16 field_id = 0;
687 guint32 size = 0;
688 gint32 seqid = 0;
689 gint8 version_and_type = 0;
690 gint8 protocol_id = 0;
691
692 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
693 "port", port, NULL);
694 transport = THRIFT_SERVER_TRANSPORT (tsocket);
695 thrift_server_transport_listen (transport, NULL);
696 client = thrift_server_transport_accept (transport, NULL);
697 assert (client != NULL);
698
699 tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
700 client, NULL);
701 protocol = THRIFT_PROTOCOL (tc);
702
703 /* test struct operations */
704
705 thrift_compact_protocol_read_struct_begin (protocol, &struct_name, NULL);
706 thrift_compact_protocol_read_struct_end (protocol, NULL);
707
708 /* test field state w.r.t. deltas */
709
710 field_id = 0;
711 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
712 &field_type,
713 &field_id, NULL) == 1);
714 assert (field_id == 1);
715 field_id = 0;
716 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
717 &field_type,
718 &field_id, NULL) == 1);
719 assert (field_id == 16);
720 field_id = 0;
721 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
722 &field_type,
723 &field_id, NULL) == 1);
724 assert (field_id == 17);
725 field_id = 0;
726 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
727 &field_type,
728 &field_id, NULL) > 1);
729 assert (field_id == 15);
730 field_id = 0;
731 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
732 &field_type,
733 &field_id, NULL) == 1);
734 assert (field_id == 30);
735 field_id = 0;
736 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
737 &field_type,
738 &field_id, NULL) > 1);
739 assert (field_id == 46);
740 field_id = 0;
741 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
742 &field_type,
743 &field_id, NULL) == 1);
744 assert (field_id == 47);
745 field_id = 0;
746
747 /* test field operations */
748
749 thrift_compact_protocol_read_field_begin (protocol, &field_name, &field_type,
750 &field_id, NULL);
751 thrift_compact_protocol_read_field_end (protocol, NULL);
752
753 /* test field state w.r.t. structs */
754
755 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
756 &field_type,
757 &field_id, NULL) > 1);
758 assert (field_id == 1);
759 field_id = 0;
760 thrift_compact_protocol_read_field_end (protocol, NULL);
761 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
762 &field_type,
763 &field_id, NULL) == 1);
764 assert (field_id == 16);
765 field_id = 0;
766 thrift_compact_protocol_read_field_end (protocol, NULL);
767
768 assert (thrift_compact_protocol_read_struct_begin (protocol,
769 &struct_name, NULL) == 0);
770 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
771 &field_type,
772 &field_id, NULL) > 1);
773 assert (field_id == 17);
774 field_id = 0;
775 thrift_compact_protocol_read_field_end (protocol, NULL);
776
777 assert (thrift_compact_protocol_read_struct_begin (protocol,
778 &struct_name, NULL) == 0);
779 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
780 &field_type,
781 &field_id, NULL) > 1);
782 assert (field_id == 18);
783 field_id = 0;
784 thrift_compact_protocol_read_field_end (protocol, NULL);
785 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
786 &field_type,
787 &field_id, NULL) == 1);
788 assert (field_id == 19);
789 field_id = 0;
790 thrift_compact_protocol_read_field_end (protocol, NULL);
791 assert (thrift_compact_protocol_read_struct_end (protocol, NULL) == 0);
792
793 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
794 &field_type,
795 &field_id, NULL) == 1);
796 assert (field_id == 18);
797 field_id = 0;
798 thrift_compact_protocol_read_field_end (protocol, NULL);
799 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
800 &field_type,
801 &field_id, NULL) == 1);
802 assert (field_id == 25);
803 field_id = 0;
804 thrift_compact_protocol_read_field_end (protocol, NULL);
805 assert (thrift_compact_protocol_read_struct_end (protocol, NULL) == 0);
806
807 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
808 &field_type,
809 &field_id, NULL) == 1);
810 assert (field_id == 17);
811 field_id = 0;
812 thrift_compact_protocol_read_field_end (protocol, NULL);
813
814 /* test field state w.r.t. bools */
815
816 /* deltas */
817 /* non-bool field -> bool field -> non-bool field */
818 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
819 &field_type,
820 &field_id, NULL) == 1);
821 thrift_compact_protocol_read_field_end (protocol, NULL);
822 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
823 &field_type,
824 &field_id, NULL) == 1);
825 assert (field_type == T_BOOL);
826 assert (thrift_compact_protocol_read_bool (protocol,
827 &value_boolean, NULL) == 0);
828 assert (value_boolean == TEST_BOOL);
829 value_boolean = ! TEST_BOOL;
830 thrift_compact_protocol_read_field_end (protocol, NULL);
831 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
832 &field_type,
833 &field_id, NULL) == 1);
834 thrift_compact_protocol_read_field_end (protocol, NULL);
835 /* bool -> bool field -> bool */
836 assert (thrift_compact_protocol_read_bool (protocol,
837 &value_boolean, NULL) > 0);
838 assert (value_boolean == TEST_BOOL);
839 value_boolean = ! TEST_BOOL;
840 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
841 &field_type,
842 &field_id, NULL) == 1);
843 assert (field_type == T_BOOL);
844 assert (thrift_compact_protocol_read_bool (protocol,
845 &value_boolean, NULL) == 0);
846 assert (value_boolean == TEST_BOOL);
847 value_boolean = ! TEST_BOOL;
848 thrift_compact_protocol_read_field_end (protocol, NULL);
849 assert (thrift_compact_protocol_read_bool (protocol,
850 &value_boolean, NULL) > 0);
851 assert (value_boolean == TEST_BOOL);
852 value_boolean = ! TEST_BOOL;
853
854 /* no deltas */
855 /* non-bool field -> bool field -> non-bool field */
856 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
857 &field_type,
858 &field_id, NULL) > 1);
859 thrift_compact_protocol_read_field_end (protocol, NULL);
860 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
861 &field_type,
862 &field_id, NULL) > 1);
863 assert (field_type == T_BOOL);
864 assert (thrift_compact_protocol_read_bool (protocol,
865 &value_boolean, NULL) == 0);
866 assert (value_boolean == TEST_BOOL);
867 value_boolean = ! TEST_BOOL;
868 thrift_compact_protocol_read_field_end (protocol, NULL);
869 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
870 &field_type,
871 &field_id, NULL) > 1);
872 thrift_compact_protocol_read_field_end (protocol, NULL);
873 /* bool -> bool field -> bool */
874 assert (thrift_compact_protocol_read_bool (protocol,
875 &value_boolean, NULL) > 0);
876 assert (value_boolean == TEST_BOOL);
877 value_boolean = ! TEST_BOOL;
878 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
879 &field_type,
880 &field_id, NULL) > 1);
881 assert (field_type == T_BOOL);
882 assert (thrift_compact_protocol_read_bool (protocol,
883 &value_boolean, NULL) == 0);
884 assert (value_boolean == TEST_BOOL);
885 value_boolean = ! TEST_BOOL;
886 thrift_compact_protocol_read_field_end (protocol, NULL);
887 assert (thrift_compact_protocol_read_bool (protocol,
888 &value_boolean, NULL) > 0);
889 assert (value_boolean == TEST_BOOL);
890 value_boolean = ! TEST_BOOL;
891
892 /* test first read error on a field */
893 transport_read_error = 1;
894 assert (thrift_compact_protocol_read_field_begin (protocol,
895 &field_name, &field_type,
896 &field_id, NULL) == -1);
897 transport_read_error = 0;
898
899 /* test 2nd write failure */
900 thrift_compact_protocol_read_byte (protocol, &value, NULL);
901
902 /* test 2nd read failure on a field */
903 transport_read_count = 0;
904 transport_read_error_at = 1;
905 assert (thrift_compact_protocol_read_field_begin (protocol,
906 &field_name, &field_type,
907 &field_id, NULL) == -1);
908 transport_read_error_at = -1;
909
910 /* test field stop */
911 thrift_compact_protocol_read_field_begin (protocol, &field_name, &field_type,
912 &field_id, NULL);
913
914 /* test map operations */
915
916 thrift_compact_protocol_read_map_begin (protocol, &key_type, &value_type,
917 &size, NULL);
918 thrift_compact_protocol_read_map_end (protocol, NULL);
919
920 /* test 1st read failure on a map */
921 transport_read_count = 0;
922 transport_read_error_at = 0;
923 assert (thrift_compact_protocol_read_map_begin (protocol,
924 &key_type, &value_type,
925 &size, NULL) == -1);
926 transport_read_error_at = -1;
927
928 /* test 2nd read failure on a map */
929 transport_read_count = 0;
930 transport_read_error_at = 1;
931 assert (thrift_compact_protocol_read_map_begin (protocol,
932 &key_type, &value_type,
933 &size, NULL) == -1);
934 transport_read_error_at = -1;
935
936 /* test 1st write failure on map---nothing to do on our side */
937
938 /* test 2nd write failure */
939 thrift_compact_protocol_read_byte (protocol, &value, NULL);
940
941 /* test negative map size */
942 assert (thrift_compact_protocol_read_map_begin (protocol,
943 &key_type, &value_type,
944 &size, NULL) == -1);
945
946 /* test list operations */
947 thrift_compact_protocol_read_list_begin (protocol, &element_type, &size,
948 NULL);
949 thrift_compact_protocol_read_list_end (protocol, NULL);
950
951 /* test small list 1st read failure */
952 transport_read_error = 1;
953 assert (thrift_compact_protocol_read_list_begin (protocol, &element_type,
954 &size, NULL) == -1);
955 transport_read_error = 0;
956
957 /* test big list 1st read failure */
958 transport_read_error = 1;
959 assert (thrift_compact_protocol_read_list_begin (protocol, &element_type,
960 &size, NULL) == -1);
961 transport_read_error = 0;
962
963 /* test big list 2nd read failure */
964 transport_read_count = 0;
965 transport_read_error_at = 1;
966 thrift_compact_protocol_read_list_begin (protocol, &element_type, &size,
967 NULL);
968 transport_read_error_at = -1;
969
970 /* test negative list size failure */
971 thrift_compact_protocol_read_list_begin (protocol, &element_type, &size,
972 NULL);
973
974 /* test small list 1st write failure---nothing to do on our end */
975
976 /* test big list 1st write failure---nothing to do on our end */
977
978 /* test big list 2nd write failure */
979 thrift_compact_protocol_read_byte (protocol, &value, NULL);
980
981 /* test set operations */
982 thrift_compact_protocol_read_set_begin (protocol, &element_type, &size, NULL);
983 thrift_compact_protocol_read_set_end (protocol, NULL);
984
985 /* broken read */
986 transport_read_error = 1;
987 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
988 &message_type, &seqid,
989 NULL) == -1);
990 transport_read_error = 0;
991
992 /* invalid protocol */
993 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
994 &message_type, &seqid,
995 NULL) == -1);
996
997 /* invalid version */
998 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
999 &message_type, &seqid,
1000 NULL) == -1);
1001
1002 /* read a valid message */
1003 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1004 &message_type, &seqid,
1005 NULL) > 0);
1006 g_free (message_name);
1007
1008 /* broken 2nd read on a message */
1009 transport_read_count = 0;
1010 transport_read_error_at = 1;
1011 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1012 &message_type, &seqid,
1013 NULL) == -1);
1014 transport_read_error_at = -1;
1015
1016 /* broken 3rd read on a message */
1017 transport_read_count = 0;
1018 transport_read_error_at = 2;
1019 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1020 &message_type, &seqid,
1021 NULL) == -1);
1022 transport_read_error_at = -1;
1023
1024 /* broken 4th read on a message */
1025 transport_read_count = 0;
1026 transport_read_error_at = 3;
1027 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1028 &message_type, &seqid,
1029 NULL) == -1);
1030 transport_read_error_at = -1;
1031
1032 /* read a valid message */
1033 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1034 &message_type, &seqid,
1035 NULL) > 0);
1036 g_free (message_name);
1037
1038 assert (thrift_compact_protocol_read_message_end (protocol, NULL) == 0);
1039
1040 /* handle 2nd write failure on a message */
1041 thrift_compact_protocol_read_byte (protocol, &protocol_id, NULL);
1042
1043 /* handle 3rd write failure on a message */
1044 thrift_compact_protocol_read_byte (protocol, &protocol_id, NULL);
1045 thrift_compact_protocol_read_byte (protocol, &version_and_type, NULL);
1046
1047 /* handle 4th write failure on a message */
1048 thrift_compact_protocol_read_byte (protocol, &protocol_id, NULL);
1049 thrift_compact_protocol_read_byte (protocol, &version_and_type, NULL);
1050 thrift_compact_protocol_read_varint32 (tc, &seqid, NULL);
1051
1052 g_object_unref (client);
1053 g_object_unref (tsocket);
1054}
1055
1056int
1057main (int argc, char *argv[])
1058{
1059#if (!GLIB_CHECK_VERSION (2, 36, 0))
1060 g_type_init ();
1061#endif
1062
1063 g_test_init (&argc, &argv, NULL);
1064
1065 g_test_add_func ("/testcompactprotocol/CreateAndDestroy",
1066 test_create_and_destroy);
1067 g_test_add_func ("/testcompactprotocol/Initialize", test_initialize);
1068 g_test_add_func ("/testcompactprotocol/ReadAndWritePrimitives",
1069 test_read_and_write_primitives);
1070 g_test_add_func ("/testcompactprotocol/ReadAndWriteComplexTypes",
1071 test_read_and_write_complex_types);
1072
1073 return g_test_run ();
1074}