blob: 96b201991d903afaadb49f7d20d36ff7af202fe2 [file] [log] [blame]
Roger Meierc1010922010-11-26 10:17:48 +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
Simon South38e71552015-08-03 10:51:16 +000020/* 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 () */
Simon South38e71552015-08-03 10:51:16 +000023#ifdef __GLIBC__
Simon South56cf7792015-12-29 12:02:18 -050024#include <features.h>
Simon South38e71552015-08-03 10:51:16 +000025#define __NO_STRING_INLINES 1
26#endif
27
Roger Meier213a6642010-10-27 12:30:11 +000028#include <unistd.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <assert.h>
32#include <netdb.h>
33#include <string.h>
Jens Geyer1c190272015-07-28 23:15:18 +020034#include <sys/wait.h>
Roger Meier213a6642010-10-27 12:30:11 +000035
Roger Meiere3da7682013-01-11 11:41:53 +010036#include <thrift/c_glib/protocol/thrift_protocol.h>
37#include <thrift/c_glib/transport/thrift_socket.h>
38#include <thrift/c_glib/transport/thrift_server_socket.h>
Roger Meier213a6642010-10-27 12:30:11 +000039
40#define TEST_BOOL TRUE
41#define TEST_BYTE 123
42#define TEST_I16 12345
43#define TEST_I32 1234567890
Simon Southbf8f7b42015-12-23 20:29:29 -050044#define TEST_I64 G_GINT64_CONSTANT (123456789012345)
Roger Meier213a6642010-10-27 12:30:11 +000045#define TEST_DOUBLE 1234567890.123
46#define TEST_STRING "this is a test string 1234567890!@#$%^&*()"
47#define TEST_PORT 51199
48
49static int transport_read_count = 0;
50static int transport_read_error = 0;
51static int transport_read_error_at = -1;
52gint32
53my_thrift_transport_read (ThriftTransport *transport, gpointer buf,
54 guint32 len, GError **error)
55{
56 if (transport_read_count != transport_read_error_at
57 && transport_read_error == 0)
58 {
59 transport_read_count++;
60 return thrift_transport_read (transport, buf, len, error);
61 }
62 return -1;
63}
64
65static int transport_write_count = 0;
66static int transport_write_error = 0;
67static int transport_write_error_at = -1;
68gboolean
69my_thrift_transport_write (ThriftTransport *transport, const gpointer buf,
70 const guint32 len, GError **error)
71{
72 if (transport_write_count != transport_write_error_at
73 && transport_write_error == 0)
74 {
75 transport_write_count++;
76 return thrift_transport_write (transport, buf, len, error);
77 }
78 return FALSE;
79}
80
81#define thrift_transport_read my_thrift_transport_read
82#define thrift_transport_write my_thrift_transport_write
Roger Meiere3da7682013-01-11 11:41:53 +010083#include "../src/thrift/c_glib/protocol/thrift_binary_protocol.c"
Roger Meier213a6642010-10-27 12:30:11 +000084#undef thrift_transport_read
85#undef thrift_transport_write
86
87static void thrift_server_primitives (const int port);
88static void thrift_server_complex_types (const int port);
89
90static void
91test_create_and_destroy(void)
92{
93 GObject *object = NULL;
94
95 /* create an object and then destroy it */
96 object = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, NULL);
97 assert (object != NULL);
98 g_object_unref (object);
99}
100
101static void
102test_initialize(void)
103{
104 ThriftSocket *tsocket = NULL;
105 ThriftBinaryProtocol *protocol = NULL;
106 ThriftSocket *temp = NULL;
107
108 /* create a ThriftTransport */
109 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
110 "port", 51188, NULL);
111 assert (tsocket != NULL);
112 /* create a ThriftBinaryProtocol using the Transport */
113 protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
114 tsocket, NULL);
115 assert (protocol != NULL);
116 /* fetch the properties */
117 g_object_get (G_OBJECT(protocol), "transport", &temp, NULL);
118 g_object_unref (temp);
119
120 /* clean up memory */
121 g_object_unref (protocol);
122 g_object_unref (tsocket);
123}
124
125static void
126test_read_and_write_primitives(void)
127{
128 int status;
129 pid_t pid;
130 ThriftSocket *tsocket = NULL;
131 ThriftTransport *transport = NULL;
132 ThriftBinaryProtocol *tb = NULL;
133 ThriftProtocol *protocol = NULL;
134 gpointer binary = (gpointer *) TEST_STRING;
135 guint32 len = strlen (TEST_STRING);
136 int port = TEST_PORT;
137
138 /* fork a server from the client */
139 pid = fork ();
140 assert (pid >= 0);
141
142 if (pid == 0)
143 {
144 /* child listens */
145 thrift_server_primitives (port);
146 exit (0);
147 } else {
148 /* parent. wait a bit for the socket to be created. */
149 sleep (1);
150
151 /* create a ThriftSocket */
152 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
153 "port", port, NULL);
154 transport = THRIFT_TRANSPORT (tsocket);
155 thrift_transport_open (transport, NULL);
156 assert (thrift_transport_is_open (transport));
157
158 /* create a ThriftBinaryTransport */
159 tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
160 tsocket, NULL);
161 protocol = THRIFT_PROTOCOL (tb);
162 assert (protocol != NULL);
163
164 /* write a bunch of primitives */
165 assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
166 assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
167 assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
168 assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
169 assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
170 assert (thrift_binary_protocol_write_double (protocol,
171 TEST_DOUBLE, NULL) > 0);
172 assert (thrift_binary_protocol_write_string (protocol,
173 TEST_STRING, NULL) > 0);
174 assert (thrift_binary_protocol_write_binary (protocol, binary,
175 len, NULL) > 0);
176 assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
177 assert (thrift_binary_protocol_write_binary (protocol, binary,
178 len, NULL) > 0);
179
180 /* test write errors */
181 transport_write_error = 1;
182 assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE,
183 NULL) == -1);
184 assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) == -1);
185 assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) == -1);
186 assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) == -1);
187 assert (thrift_binary_protocol_write_double (protocol, TEST_DOUBLE,
188 NULL) == -1);
189 assert (thrift_binary_protocol_write_binary (protocol, binary, len,
190 NULL) == -1);
191 transport_write_error = 0;
192
193 /* test binary partial failure */
194 transport_write_count = 0;
195 transport_write_error_at = 1;
196 assert (thrift_binary_protocol_write_binary (protocol, binary,
197 len, NULL) == -1);
198 transport_write_error_at = -1;
199
200 /* clean up */
201 thrift_transport_close (transport, NULL);
202 g_object_unref (tsocket);
203 g_object_unref (protocol);
204 assert (wait (&status) == pid);
205 assert (status == 0);
206 }
207}
208
209static void
210test_read_and_write_complex_types (void)
211{
212 int status;
213 pid_t pid;
214 ThriftSocket *tsocket = NULL;
215 ThriftTransport *transport = NULL;
216 ThriftBinaryProtocol *tb = NULL;
217 ThriftProtocol *protocol = NULL;
218 int port = TEST_PORT;
219
220 /* fork a server from the client */
221 pid = fork ();
222 assert (pid >= 0);
223
224 if (pid == 0)
225 {
226 /* child listens */
227 thrift_server_complex_types (port);
228 exit (0);
229 } else {
230 /* parent. wait a bit for the socket to be created. */
231 sleep (1);
232
233 /* create a ThriftSocket */
234 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
235 "port", port, NULL);
236 transport = THRIFT_TRANSPORT (tsocket);
237 thrift_transport_open (transport, NULL);
238 assert (thrift_transport_is_open (transport));
239
240 /* create a ThriftBinaryTransport */
241 tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
242 tsocket, NULL);
243 protocol = THRIFT_PROTOCOL (tb);
244 assert (protocol != NULL);
245
246 /* test structures */
247 assert (thrift_binary_protocol_write_struct_begin (protocol,
248 NULL, NULL) == 0);
249 assert (thrift_binary_protocol_write_struct_end (protocol, NULL) == 0);
250
251 assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
252 1, NULL) > 0);
253 assert (thrift_binary_protocol_write_field_end (protocol, NULL) == 0);
254
255 /* test write error */
256 transport_write_error = 1;
257 assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
258 1, NULL) == -1);
259 transport_write_error = 0;
260
261 /* test 2nd write error */
262 transport_write_count = 0;
263 transport_write_error_at = 1;
264 assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
265 1, NULL) == -1);
266 transport_write_error_at = -1;
267
268 /* test 2nd read failure on a field */
269 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
270
271 /* test write_field_stop */
272 assert (thrift_binary_protocol_write_field_stop (protocol, NULL) > 0);
273
274 /* write a map */
275 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
276 1, NULL) > 0);
277 assert (thrift_binary_protocol_write_map_end (protocol, NULL) == 0);
278
279 /* test 2nd read failure on a map */
280 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
281
282 /* test 3rd read failure on a map */
283 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
284 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
285
286 /* test 1st write failure on a map */
287 transport_write_error = 1;
288 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
289 1, NULL) == -1);
290 transport_write_error = 0;
291
292 /* test 2nd write failure on a map */
293 transport_write_count = 0;
294 transport_write_error_at = 1;
295 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
296 1, NULL) == -1);
297 transport_write_error_at = -1;
298
299 /* test 3rd write failure on a map */
300 transport_write_count = 0;
301 transport_write_error_at = 2;
302 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
303 1, NULL) == -1);
304 transport_write_error_at = -1;
305
306 /* test negative map size */
307 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
308 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
309 thrift_binary_protocol_write_i32 (protocol, -10, NULL);
310
311 /* test list operations */
312 assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
313 1, NULL) > 0);
314 assert (thrift_binary_protocol_write_list_end (protocol, NULL) == 0);
315
316 /* test 2nd read failure on a list */
317 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
318
319 /* test negative list size */
320 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
321 thrift_binary_protocol_write_i32 (protocol, -10, NULL);
322
323 /* test first write error on a list */
324 transport_write_error = 1;
325 assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
326 1, NULL) == -1);
327 transport_write_error = 0;
328
329 /* test 2nd write error on a list */
330 transport_write_count = 0;
331 transport_write_error_at = 1;
332 assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
333 1, NULL) == -1);
334 transport_write_error_at = -1;
335
336 /* test set operation s*/
337 assert (thrift_binary_protocol_write_set_begin (protocol, T_VOID,
338 1, NULL) > 0);
339 assert (thrift_binary_protocol_write_set_end (protocol, NULL) == 0);
340
341 /* invalid version */
342 assert (thrift_binary_protocol_write_i32 (protocol, -1, NULL) > 0);
343
344 /* sz > 0 for a message */
345 assert (thrift_binary_protocol_write_i32 (protocol, 1, NULL) > 0);
346
347 /* send a valid message */
348 thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
349 thrift_binary_protocol_write_string (protocol, "test", NULL);
350 thrift_binary_protocol_write_i32 (protocol, 1, NULL);
351
352 /* broken 2nd read */
353 thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
354
355 /* send a broken 3rd read */
356 thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
357 thrift_binary_protocol_write_string (protocol, "test", NULL);
358
359 /* send a valid message */
360 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
361 T_CALL, 1, NULL) > 0);
362
363 assert (thrift_binary_protocol_write_message_end (protocol, NULL) == 0);
364
365 /* send broken writes */
366 transport_write_error = 1;
367 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
368 T_CALL, 1, NULL) == -1);
369 transport_write_error = 0;
370
371 transport_write_count = 0;
372 transport_write_error_at = 2;
373 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
374 T_CALL, 1, NULL) == -1);
375 transport_write_error_at = -1;
376
377 transport_write_count = 0;
378 transport_write_error_at = 3;
379 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
380 T_CALL, 1, NULL) == -1);
381 transport_write_error_at = -1;
382
383 /* clean up */
384 thrift_transport_close (transport, NULL);
385 g_object_unref (tsocket);
386 g_object_unref (protocol);
387 assert (wait (&status) == pid);
388 assert (status == 0);
389 }
390}
391
392
393static void
394thrift_server_primitives (const int port)
395{
396 ThriftServerTransport *transport = NULL;
397 ThriftTransport *client = NULL;
398 ThriftBinaryProtocol *tbp = NULL;
399 ThriftProtocol *protocol = NULL;
400 gboolean value_boolean = FALSE;
401 gint8 value_byte = 0;
402 gint16 value_16 = 0;
403 gint32 value_32 = 0;
404 gint64 value_64 = 0;
405 gdouble value_double = 0;
406 gchar *string = NULL;
407 gpointer binary = NULL;
408 guint32 len = 0;
409 void *comparator = (void *) TEST_STRING;
410
411 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
412 "port", port, NULL);
413 transport = THRIFT_SERVER_TRANSPORT (tsocket);
414 thrift_server_transport_listen (transport, NULL);
415 client = thrift_server_transport_accept (transport, NULL);
416 assert (client != NULL);
417
418 tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
419 client, NULL);
420 protocol = THRIFT_PROTOCOL (tbp);
421
422 assert (thrift_binary_protocol_read_bool (protocol,
423 &value_boolean, NULL) > 0);
424 assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0);
425 assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0);
426 assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0);
427 assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0);
428 assert (thrift_binary_protocol_read_double (protocol,
429 &value_double, NULL) > 0);
430 assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0);
431 assert (thrift_binary_protocol_read_binary (protocol, &binary,
432 &len, NULL) > 0);
433
434 assert (value_boolean == TEST_BOOL);
435 assert (value_byte = TEST_BYTE);
436 assert (value_16 = TEST_I16);
437 assert (value_32 = TEST_I32);
438 assert (value_64 = TEST_I64);
439 assert (value_double = TEST_DOUBLE);
440 assert (strcmp (TEST_STRING, string) == 0);
441 assert (memcmp (comparator, binary, len) == 0);
442
443 g_free (string);
444 g_free (binary);
445
446 thrift_binary_protocol_read_binary (protocol, &binary, &len, NULL);
447 g_free (binary);
448
449 transport_read_count = 0;
450 transport_read_error_at = 0;
451 assert (thrift_binary_protocol_read_binary (protocol, &binary,
452 &len, NULL) == -1);
453 transport_read_error_at = -1;
454
455 transport_read_count = 0;
456 transport_read_error_at = 1;
457 assert (thrift_binary_protocol_read_binary (protocol, &binary,
458 &len, NULL) == -1);
459 transport_read_error_at = -1;
460
461 transport_read_error = 1;
462 assert (thrift_binary_protocol_read_bool (protocol,
463 &value_boolean, NULL) == -1);
464 assert (thrift_binary_protocol_read_byte (protocol,
465 &value_byte, NULL) == -1);
466 assert (thrift_binary_protocol_read_i16 (protocol,
467 &value_16, NULL) == -1);
468 assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) == -1);
469 assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) == -1);
470 assert (thrift_binary_protocol_read_double (protocol,
471 &value_double, NULL) == -1);
472 transport_read_error = 0;
473
474 /* test partial write failure */
475 thrift_protocol_read_i32 (protocol, &value_32, NULL);
476
477 thrift_transport_read_end (client, NULL);
478 thrift_transport_close (client, NULL);
479
480 g_object_unref (tbp);
481 g_object_unref (client);
482 g_object_unref (tsocket);
483}
484
485static void
486thrift_server_complex_types (const int port)
487{
488 ThriftServerTransport *transport = NULL;
489 ThriftTransport *client = NULL;
490 ThriftBinaryProtocol *tbp = NULL;
491 ThriftProtocol *protocol = NULL;
492 gchar *struct_name = NULL;
493 gchar *field_name = NULL;
494 gchar *message_name = NULL;
495 ThriftType element_type, key_type, value_type, field_type;
496 ThriftMessageType message_type;
497 gint8 value = 0;
498 gint16 field_id = 0;
499 guint32 size = 0;
500 gint32 seqid = 0;
501 gint32 version = 0;
502
503 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
504 "port", port, NULL);
505 transport = THRIFT_SERVER_TRANSPORT (tsocket);
506 thrift_server_transport_listen (transport, NULL);
507 client = thrift_server_transport_accept (transport, NULL);
508 assert (client != NULL);
509
510 tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
511 client, NULL);
512 protocol = THRIFT_PROTOCOL (tbp);
513
514 thrift_binary_protocol_read_struct_begin (protocol, &struct_name, NULL);
515 thrift_binary_protocol_read_struct_end (protocol, NULL);
516
517 thrift_binary_protocol_read_field_begin (protocol, &field_name, &field_type,
518 &field_id, NULL);
519 thrift_binary_protocol_read_field_end (protocol, NULL);
520
521 /* test first read error on a field */
522 transport_read_error = 1;
523 assert (thrift_binary_protocol_read_field_begin (protocol,
524 &field_name, &field_type,
525 &field_id, NULL) == -1);
526 transport_read_error = 0;
527
528 /* test 2nd write failure */
529 thrift_binary_protocol_read_byte (protocol, &value, NULL);
530
531 /* test 2nd read failure on a field */
532 transport_read_count = 0;
533 transport_read_error_at = 1;
534 assert (thrift_binary_protocol_read_field_begin (protocol,
535 &field_name, &field_type,
536 &field_id, NULL) == -1);
537 transport_read_error_at = -1;
538
539 /* test field stop */
540 thrift_binary_protocol_read_field_begin (protocol, &field_name, &field_type,
541 &field_id, NULL);
542
543 thrift_binary_protocol_read_map_begin (protocol, &key_type, &value_type,
544 &size, NULL);
545 thrift_binary_protocol_read_map_end (protocol, NULL);
546
547 /* test read failure on a map */
548 transport_read_count = 0;
549 transport_read_error_at = 0;
550 assert (thrift_binary_protocol_read_map_begin (protocol,
551 &key_type, &value_type,
552 &size, NULL) == -1);
553 transport_read_error_at = -1;
554
555 /* test 2nd read failure on a map */
556 transport_read_count = 0;
557 transport_read_error_at = 1;
558 assert (thrift_binary_protocol_read_map_begin (protocol,
559 &key_type, &value_type,
560 &size, NULL) == -1);
561 transport_read_error_at = -1;
562
563 /* test 3rd read failure on a map */
564 transport_read_count = 0;
565 transport_read_error_at = 2;
566 assert (thrift_binary_protocol_read_map_begin (protocol,
567 &key_type, &value_type,
568 &size, NULL) == -1);
569 transport_read_error_at = -1;
570
571 /* test 2nd write failure */
572 thrift_binary_protocol_read_byte (protocol, &value, NULL);
573
574 /* test 3rd write failure */
575 thrift_binary_protocol_read_byte (protocol, &value, NULL);
576 thrift_binary_protocol_read_byte (protocol, &value, NULL);
577
578 /* test negative map size */
579 assert (thrift_binary_protocol_read_map_begin (protocol,
580 &key_type, &value_type,
581 &size, NULL) == -1);
582
583 /* test list operations */
584 thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL);
585 thrift_binary_protocol_read_list_end (protocol, NULL);
586
587 /* test read failure */
588 transport_read_error = 1;
589 assert (thrift_binary_protocol_read_list_begin (protocol, &element_type,
590 &size, NULL) == -1);
591 transport_read_error = 0;
592
593 /* test 2nd read failure */
594 transport_read_count = 0;
595 transport_read_error_at = 1;
596 thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL);
597 transport_read_error_at = -1;
598
599 /* test negative list size failure */
600 thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL);
601
602 /* test 2nd write failure */
603 thrift_binary_protocol_read_byte (protocol, &value, NULL);
604
605 /* test set operations */
606 thrift_binary_protocol_read_set_begin (protocol, &element_type, &size, NULL);
607 thrift_binary_protocol_read_set_end (protocol, NULL);
608
609 /* broken read */
610 transport_read_error = 1;
611 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
612 &message_type, &seqid,
613 NULL) == -1);
614 transport_read_error = 0;
615
616 /* invalid protocol version */
617 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
618 &message_type, &seqid,
619 NULL) == -1);
620
621 /* sz > 0 */
622 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
623 &message_type, &seqid,
624 NULL) > 0);
625
626 /* read a valid message */
627 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
628 &message_type, &seqid,
629 NULL) > 0);
630 g_free (message_name);
631
632 /* broken 2nd read on a message */
633 transport_read_count = 0;
634 transport_read_error_at = 1;
635 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
636 &message_type, &seqid,
637 NULL) == -1);
638 transport_read_error_at = -1;
639
640 /* broken 3rd read on a message */
641 transport_read_count = 0;
642 transport_read_error_at = 3; /* read_string does two reads */
643 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
644 &message_type, &seqid,
645 NULL) == -1);
646 g_free (message_name);
647 transport_read_error_at = -1;
648
649 /* read a valid message */
650 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
651 &message_type, &seqid,
652 NULL) > 0);
653 g_free (message_name);
654
655 assert (thrift_binary_protocol_read_message_end (protocol, NULL) == 0);
656
657 /* handle 2nd write failure on a message */
658 thrift_binary_protocol_read_i32 (protocol, &version, NULL);
659
660 /* handle 2nd write failure on a message */
661 thrift_binary_protocol_read_i32 (protocol, &version, NULL);
662 thrift_binary_protocol_read_string (protocol, &message_name, NULL);
663
664 g_object_unref (client);
Simon South38e71552015-08-03 10:51:16 +0000665 /* TODO: investigate g_object_unref (tbp); */
Roger Meier213a6642010-10-27 12:30:11 +0000666 g_object_unref (tsocket);
667}
668
669int
Roger Meierc1010922010-11-26 10:17:48 +0000670main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000671{
Jens Geyer1c190272015-07-28 23:15:18 +0200672#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meierc1010922010-11-26 10:17:48 +0000673 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200674#endif
675
Roger Meierc1010922010-11-26 10:17:48 +0000676 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000677
Roger Meierc2cc61a2010-11-30 19:53:29 +0000678 g_test_add_func ("/testbinaryprotocol/CreateAndDestroy", test_create_and_destroy);
679 g_test_add_func ("/testbinaryprotocol/Initialize", test_initialize);
680 g_test_add_func ("/testbinaryprotocol/ReadAndWritePrimitives", test_read_and_write_primitives);
681 g_test_add_func ("/testbinaryprotocol/ReadAndWriteComplexTypes", test_read_and_write_complex_types);
Roger Meierc1010922010-11-26 10:17:48 +0000682
683 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000684}