blob: 7ca5150ae29c7f663a070a707b3c28fce77e798f [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>
Chandler May1ccd81b2016-02-11 08:25:25 -050039#include <thrift/c_glib/transport/thrift_framed_transport.h>
Roger Meier213a6642010-10-27 12:30:11 +000040
41#define TEST_BOOL TRUE
42#define TEST_BYTE 123
43#define TEST_I16 12345
44#define TEST_I32 1234567890
Simon Southbf8f7b42015-12-23 20:29:29 -050045#define TEST_I64 G_GINT64_CONSTANT (123456789012345)
Roger Meier213a6642010-10-27 12:30:11 +000046#define TEST_DOUBLE 1234567890.123
47#define TEST_STRING "this is a test string 1234567890!@#$%^&*()"
48#define TEST_PORT 51199
49
50static int transport_read_count = 0;
51static int transport_read_error = 0;
52static int transport_read_error_at = -1;
53gint32
Chandler May1ccd81b2016-02-11 08:25:25 -050054my_thrift_transport_read_all (ThriftTransport *transport, gpointer buf,
55 guint32 len, GError **error)
Roger Meier213a6642010-10-27 12:30:11 +000056{
57 if (transport_read_count != transport_read_error_at
58 && transport_read_error == 0)
59 {
60 transport_read_count++;
Chandler May1ccd81b2016-02-11 08:25:25 -050061 return thrift_transport_read_all (transport, buf, len, error);
Roger Meier213a6642010-10-27 12:30:11 +000062 }
63 return -1;
64}
65
66static int transport_write_count = 0;
67static int transport_write_error = 0;
68static int transport_write_error_at = -1;
69gboolean
70my_thrift_transport_write (ThriftTransport *transport, const gpointer buf,
71 const guint32 len, GError **error)
72{
73 if (transport_write_count != transport_write_error_at
74 && transport_write_error == 0)
75 {
76 transport_write_count++;
77 return thrift_transport_write (transport, buf, len, error);
78 }
79 return FALSE;
80}
81
Chandler May1ccd81b2016-02-11 08:25:25 -050082#define thrift_transport_read_all my_thrift_transport_read_all
Roger Meier213a6642010-10-27 12:30:11 +000083#define thrift_transport_write my_thrift_transport_write
Roger Meiere3da7682013-01-11 11:41:53 +010084#include "../src/thrift/c_glib/protocol/thrift_binary_protocol.c"
Chandler May1ccd81b2016-02-11 08:25:25 -050085#undef thrift_transport_read_all
Roger Meier213a6642010-10-27 12:30:11 +000086#undef thrift_transport_write
87
88static void thrift_server_primitives (const int port);
89static void thrift_server_complex_types (const int port);
Chandler May1ccd81b2016-02-11 08:25:25 -050090static void thrift_server_many_frames (const int port);
Roger Meier213a6642010-10-27 12:30:11 +000091
92static void
93test_create_and_destroy(void)
94{
95 GObject *object = NULL;
96
97 /* create an object and then destroy it */
98 object = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, NULL);
99 assert (object != NULL);
100 g_object_unref (object);
101}
102
103static void
104test_initialize(void)
105{
106 ThriftSocket *tsocket = NULL;
107 ThriftBinaryProtocol *protocol = NULL;
108 ThriftSocket *temp = NULL;
109
110 /* create a ThriftTransport */
111 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
112 "port", 51188, NULL);
113 assert (tsocket != NULL);
114 /* create a ThriftBinaryProtocol using the Transport */
115 protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
116 tsocket, NULL);
117 assert (protocol != NULL);
118 /* fetch the properties */
119 g_object_get (G_OBJECT(protocol), "transport", &temp, NULL);
120 g_object_unref (temp);
121
122 /* clean up memory */
123 g_object_unref (protocol);
124 g_object_unref (tsocket);
125}
126
127static void
128test_read_and_write_primitives(void)
129{
130 int status;
131 pid_t pid;
132 ThriftSocket *tsocket = NULL;
133 ThriftTransport *transport = NULL;
134 ThriftBinaryProtocol *tb = NULL;
135 ThriftProtocol *protocol = NULL;
136 gpointer binary = (gpointer *) TEST_STRING;
137 guint32 len = strlen (TEST_STRING);
138 int port = TEST_PORT;
139
140 /* fork a server from the client */
141 pid = fork ();
142 assert (pid >= 0);
143
144 if (pid == 0)
145 {
146 /* child listens */
147 thrift_server_primitives (port);
148 exit (0);
149 } else {
150 /* parent. wait a bit for the socket to be created. */
151 sleep (1);
152
153 /* create a ThriftSocket */
154 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
155 "port", port, NULL);
156 transport = THRIFT_TRANSPORT (tsocket);
157 thrift_transport_open (transport, NULL);
158 assert (thrift_transport_is_open (transport));
159
160 /* create a ThriftBinaryTransport */
161 tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
162 tsocket, NULL);
163 protocol = THRIFT_PROTOCOL (tb);
164 assert (protocol != NULL);
165
166 /* write a bunch of primitives */
167 assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
168 assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
169 assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
170 assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
171 assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
172 assert (thrift_binary_protocol_write_double (protocol,
173 TEST_DOUBLE, NULL) > 0);
174 assert (thrift_binary_protocol_write_string (protocol,
175 TEST_STRING, NULL) > 0);
176 assert (thrift_binary_protocol_write_binary (protocol, binary,
177 len, NULL) > 0);
178 assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
179 assert (thrift_binary_protocol_write_binary (protocol, binary,
180 len, NULL) > 0);
181
182 /* test write errors */
183 transport_write_error = 1;
184 assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE,
185 NULL) == -1);
186 assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) == -1);
187 assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) == -1);
188 assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) == -1);
189 assert (thrift_binary_protocol_write_double (protocol, TEST_DOUBLE,
190 NULL) == -1);
191 assert (thrift_binary_protocol_write_binary (protocol, binary, len,
192 NULL) == -1);
193 transport_write_error = 0;
194
195 /* test binary partial failure */
196 transport_write_count = 0;
197 transport_write_error_at = 1;
198 assert (thrift_binary_protocol_write_binary (protocol, binary,
199 len, NULL) == -1);
200 transport_write_error_at = -1;
201
202 /* clean up */
203 thrift_transport_close (transport, NULL);
204 g_object_unref (tsocket);
205 g_object_unref (protocol);
206 assert (wait (&status) == pid);
207 assert (status == 0);
208 }
209}
210
211static void
212test_read_and_write_complex_types (void)
213{
214 int status;
215 pid_t pid;
216 ThriftSocket *tsocket = NULL;
217 ThriftTransport *transport = NULL;
218 ThriftBinaryProtocol *tb = NULL;
219 ThriftProtocol *protocol = NULL;
220 int port = TEST_PORT;
221
222 /* fork a server from the client */
223 pid = fork ();
224 assert (pid >= 0);
225
226 if (pid == 0)
227 {
228 /* child listens */
229 thrift_server_complex_types (port);
230 exit (0);
231 } else {
232 /* parent. wait a bit for the socket to be created. */
233 sleep (1);
234
235 /* create a ThriftSocket */
236 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
237 "port", port, NULL);
238 transport = THRIFT_TRANSPORT (tsocket);
239 thrift_transport_open (transport, NULL);
240 assert (thrift_transport_is_open (transport));
241
242 /* create a ThriftBinaryTransport */
243 tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
244 tsocket, NULL);
245 protocol = THRIFT_PROTOCOL (tb);
246 assert (protocol != NULL);
247
248 /* test structures */
249 assert (thrift_binary_protocol_write_struct_begin (protocol,
250 NULL, NULL) == 0);
251 assert (thrift_binary_protocol_write_struct_end (protocol, NULL) == 0);
252
253 assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
254 1, NULL) > 0);
255 assert (thrift_binary_protocol_write_field_end (protocol, NULL) == 0);
256
257 /* test write error */
258 transport_write_error = 1;
259 assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
260 1, NULL) == -1);
261 transport_write_error = 0;
262
263 /* test 2nd write error */
264 transport_write_count = 0;
265 transport_write_error_at = 1;
266 assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
267 1, NULL) == -1);
268 transport_write_error_at = -1;
269
270 /* test 2nd read failure on a field */
271 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
272
273 /* test write_field_stop */
274 assert (thrift_binary_protocol_write_field_stop (protocol, NULL) > 0);
275
276 /* write a map */
277 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
278 1, NULL) > 0);
279 assert (thrift_binary_protocol_write_map_end (protocol, NULL) == 0);
280
281 /* test 2nd read failure on a map */
282 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
283
284 /* test 3rd read failure on a map */
285 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
286 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
287
288 /* test 1st write failure on a map */
289 transport_write_error = 1;
290 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
291 1, NULL) == -1);
292 transport_write_error = 0;
293
294 /* test 2nd write failure on a map */
295 transport_write_count = 0;
296 transport_write_error_at = 1;
297 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
298 1, NULL) == -1);
299 transport_write_error_at = -1;
300
301 /* test 3rd write failure on a map */
302 transport_write_count = 0;
303 transport_write_error_at = 2;
304 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
305 1, NULL) == -1);
306 transport_write_error_at = -1;
307
308 /* test negative map size */
309 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
310 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
311 thrift_binary_protocol_write_i32 (protocol, -10, NULL);
312
313 /* test list operations */
314 assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
315 1, NULL) > 0);
316 assert (thrift_binary_protocol_write_list_end (protocol, NULL) == 0);
317
318 /* test 2nd read failure on a list */
319 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
320
321 /* test negative list size */
322 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
323 thrift_binary_protocol_write_i32 (protocol, -10, NULL);
324
325 /* test first write error on a list */
326 transport_write_error = 1;
327 assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
328 1, NULL) == -1);
329 transport_write_error = 0;
330
331 /* test 2nd write error on a list */
332 transport_write_count = 0;
333 transport_write_error_at = 1;
334 assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
335 1, NULL) == -1);
336 transport_write_error_at = -1;
337
338 /* test set operation s*/
339 assert (thrift_binary_protocol_write_set_begin (protocol, T_VOID,
340 1, NULL) > 0);
341 assert (thrift_binary_protocol_write_set_end (protocol, NULL) == 0);
342
343 /* invalid version */
344 assert (thrift_binary_protocol_write_i32 (protocol, -1, NULL) > 0);
345
346 /* sz > 0 for a message */
347 assert (thrift_binary_protocol_write_i32 (protocol, 1, NULL) > 0);
348
349 /* send a valid message */
350 thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
351 thrift_binary_protocol_write_string (protocol, "test", NULL);
352 thrift_binary_protocol_write_i32 (protocol, 1, NULL);
353
354 /* broken 2nd read */
355 thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
356
357 /* send a broken 3rd read */
358 thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
359 thrift_binary_protocol_write_string (protocol, "test", NULL);
360
361 /* send a valid message */
362 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
363 T_CALL, 1, NULL) > 0);
364
365 assert (thrift_binary_protocol_write_message_end (protocol, NULL) == 0);
366
367 /* send broken writes */
368 transport_write_error = 1;
369 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
370 T_CALL, 1, NULL) == -1);
371 transport_write_error = 0;
372
373 transport_write_count = 0;
374 transport_write_error_at = 2;
375 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
376 T_CALL, 1, NULL) == -1);
377 transport_write_error_at = -1;
378
379 transport_write_count = 0;
380 transport_write_error_at = 3;
381 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
382 T_CALL, 1, NULL) == -1);
383 transport_write_error_at = -1;
384
385 /* clean up */
386 thrift_transport_close (transport, NULL);
387 g_object_unref (tsocket);
388 g_object_unref (protocol);
389 assert (wait (&status) == pid);
390 assert (status == 0);
391 }
392}
393
Chandler May1ccd81b2016-02-11 08:25:25 -0500394static void
395test_read_and_write_many_frames (void)
396{
397 int status;
398 pid_t pid;
399 ThriftSocket *tsocket = NULL;
400 ThriftTransport *transport = NULL;
401 ThriftFramedTransport *ft = NULL;
402 ThriftBinaryProtocol *tb = NULL;
403 ThriftProtocol *protocol = NULL;
404 gpointer binary = (gpointer *) TEST_STRING;
405 const guint32 len = strlen (TEST_STRING);
406 int port = TEST_PORT;
407
408 /* fork a server from the client */
409 pid = fork ();
410 assert (pid >= 0);
411
412 if (pid == 0)
413 {
414 /* child listens */
415 thrift_server_many_frames (port);
416 exit (0);
417 } else {
418 /* parent. wait a bit for the socket to be created. */
419 sleep (1);
420
421 /* create a ThriftSocket */
422 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
423 "port", port, NULL);
424 assert (tsocket != NULL);
425 transport = THRIFT_TRANSPORT (tsocket);
426
427 /* wrap in a framed transport */
428 ft = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport", transport,
429 "w_buf_size", 1, NULL);
430 assert (ft != NULL);
431 transport = THRIFT_TRANSPORT (ft);
432
433 thrift_transport_open (transport, NULL);
434 assert (thrift_transport_is_open (transport));
435
436 /* create a binary protocol */
437 tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
438 transport, NULL);
439 protocol = THRIFT_PROTOCOL (tb);
440 assert (protocol != NULL);
441
442 /* write a bunch of primitives */
443 assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
444 thrift_transport_flush (transport, NULL);
445 assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
446 thrift_transport_flush (transport, NULL);
447 assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
448 thrift_transport_flush (transport, NULL);
449 assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
450 thrift_transport_flush (transport, NULL);
451 assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
452 thrift_transport_flush (transport, NULL);
453 assert (thrift_binary_protocol_write_double (protocol,
454 TEST_DOUBLE, NULL) > 0);
455 thrift_transport_flush (transport, NULL);
456 assert (thrift_binary_protocol_write_string (protocol,
457 TEST_STRING, NULL) > 0);
458 thrift_transport_flush (transport, NULL);
459 assert (thrift_binary_protocol_write_binary (protocol, binary,
460 len, NULL) > 0);
461 thrift_transport_flush (transport, NULL);
462 assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
463 thrift_transport_flush (transport, NULL);
464 assert (thrift_binary_protocol_write_binary (protocol, binary,
465 len, NULL) > 0);
466 thrift_transport_flush (transport, NULL);
467
468 /* clean up */
469 thrift_transport_write_end (transport, NULL);
470 thrift_transport_close (transport, NULL);
471 g_object_unref (ft);
472 g_object_unref (tsocket);
473 g_object_unref (tb);
474 assert (wait (&status) == pid);
475 assert (status == 0);
476 }
477}
478
Roger Meier213a6642010-10-27 12:30:11 +0000479
480static void
481thrift_server_primitives (const int port)
482{
483 ThriftServerTransport *transport = NULL;
484 ThriftTransport *client = NULL;
485 ThriftBinaryProtocol *tbp = NULL;
486 ThriftProtocol *protocol = NULL;
487 gboolean value_boolean = FALSE;
488 gint8 value_byte = 0;
489 gint16 value_16 = 0;
490 gint32 value_32 = 0;
491 gint64 value_64 = 0;
492 gdouble value_double = 0;
493 gchar *string = NULL;
494 gpointer binary = NULL;
495 guint32 len = 0;
496 void *comparator = (void *) TEST_STRING;
497
498 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
499 "port", port, NULL);
500 transport = THRIFT_SERVER_TRANSPORT (tsocket);
501 thrift_server_transport_listen (transport, NULL);
502 client = thrift_server_transport_accept (transport, NULL);
503 assert (client != NULL);
504
505 tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
506 client, NULL);
507 protocol = THRIFT_PROTOCOL (tbp);
508
509 assert (thrift_binary_protocol_read_bool (protocol,
510 &value_boolean, NULL) > 0);
511 assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0);
512 assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0);
513 assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0);
514 assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0);
515 assert (thrift_binary_protocol_read_double (protocol,
516 &value_double, NULL) > 0);
517 assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0);
518 assert (thrift_binary_protocol_read_binary (protocol, &binary,
519 &len, NULL) > 0);
520
521 assert (value_boolean == TEST_BOOL);
Chandler May8b0fe282016-01-16 15:10:34 -0500522 assert (value_byte == TEST_BYTE);
523 assert (value_16 == TEST_I16);
524 assert (value_32 == TEST_I32);
525 assert (value_64 == TEST_I64);
526 assert (value_double == TEST_DOUBLE);
Roger Meier213a6642010-10-27 12:30:11 +0000527 assert (strcmp (TEST_STRING, string) == 0);
528 assert (memcmp (comparator, binary, len) == 0);
529
530 g_free (string);
531 g_free (binary);
532
533 thrift_binary_protocol_read_binary (protocol, &binary, &len, NULL);
534 g_free (binary);
535
536 transport_read_count = 0;
537 transport_read_error_at = 0;
538 assert (thrift_binary_protocol_read_binary (protocol, &binary,
539 &len, NULL) == -1);
540 transport_read_error_at = -1;
541
542 transport_read_count = 0;
543 transport_read_error_at = 1;
544 assert (thrift_binary_protocol_read_binary (protocol, &binary,
545 &len, NULL) == -1);
546 transport_read_error_at = -1;
547
548 transport_read_error = 1;
549 assert (thrift_binary_protocol_read_bool (protocol,
550 &value_boolean, NULL) == -1);
551 assert (thrift_binary_protocol_read_byte (protocol,
552 &value_byte, NULL) == -1);
553 assert (thrift_binary_protocol_read_i16 (protocol,
554 &value_16, NULL) == -1);
555 assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) == -1);
556 assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) == -1);
557 assert (thrift_binary_protocol_read_double (protocol,
558 &value_double, NULL) == -1);
559 transport_read_error = 0;
560
561 /* test partial write failure */
562 thrift_protocol_read_i32 (protocol, &value_32, NULL);
563
564 thrift_transport_read_end (client, NULL);
565 thrift_transport_close (client, NULL);
566
567 g_object_unref (tbp);
568 g_object_unref (client);
569 g_object_unref (tsocket);
570}
571
572static void
573thrift_server_complex_types (const int port)
574{
575 ThriftServerTransport *transport = NULL;
576 ThriftTransport *client = NULL;
577 ThriftBinaryProtocol *tbp = NULL;
578 ThriftProtocol *protocol = NULL;
579 gchar *struct_name = NULL;
580 gchar *field_name = NULL;
581 gchar *message_name = NULL;
582 ThriftType element_type, key_type, value_type, field_type;
583 ThriftMessageType message_type;
584 gint8 value = 0;
585 gint16 field_id = 0;
586 guint32 size = 0;
587 gint32 seqid = 0;
588 gint32 version = 0;
589
590 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
591 "port", port, NULL);
592 transport = THRIFT_SERVER_TRANSPORT (tsocket);
593 thrift_server_transport_listen (transport, NULL);
594 client = thrift_server_transport_accept (transport, NULL);
595 assert (client != NULL);
596
597 tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
598 client, NULL);
599 protocol = THRIFT_PROTOCOL (tbp);
600
601 thrift_binary_protocol_read_struct_begin (protocol, &struct_name, NULL);
602 thrift_binary_protocol_read_struct_end (protocol, NULL);
603
604 thrift_binary_protocol_read_field_begin (protocol, &field_name, &field_type,
605 &field_id, NULL);
606 thrift_binary_protocol_read_field_end (protocol, NULL);
607
608 /* test first read error on a field */
609 transport_read_error = 1;
610 assert (thrift_binary_protocol_read_field_begin (protocol,
611 &field_name, &field_type,
612 &field_id, NULL) == -1);
613 transport_read_error = 0;
614
615 /* test 2nd write failure */
616 thrift_binary_protocol_read_byte (protocol, &value, NULL);
617
618 /* test 2nd read failure on a field */
619 transport_read_count = 0;
620 transport_read_error_at = 1;
621 assert (thrift_binary_protocol_read_field_begin (protocol,
622 &field_name, &field_type,
623 &field_id, NULL) == -1);
624 transport_read_error_at = -1;
625
626 /* test field stop */
627 thrift_binary_protocol_read_field_begin (protocol, &field_name, &field_type,
628 &field_id, NULL);
629
630 thrift_binary_protocol_read_map_begin (protocol, &key_type, &value_type,
631 &size, NULL);
632 thrift_binary_protocol_read_map_end (protocol, NULL);
633
634 /* test read failure on a map */
635 transport_read_count = 0;
636 transport_read_error_at = 0;
637 assert (thrift_binary_protocol_read_map_begin (protocol,
638 &key_type, &value_type,
639 &size, NULL) == -1);
640 transport_read_error_at = -1;
641
642 /* test 2nd read failure on a map */
643 transport_read_count = 0;
644 transport_read_error_at = 1;
645 assert (thrift_binary_protocol_read_map_begin (protocol,
646 &key_type, &value_type,
647 &size, NULL) == -1);
648 transport_read_error_at = -1;
649
650 /* test 3rd read failure on a map */
651 transport_read_count = 0;
652 transport_read_error_at = 2;
653 assert (thrift_binary_protocol_read_map_begin (protocol,
654 &key_type, &value_type,
655 &size, NULL) == -1);
656 transport_read_error_at = -1;
657
658 /* test 2nd write failure */
659 thrift_binary_protocol_read_byte (protocol, &value, NULL);
660
661 /* test 3rd write failure */
662 thrift_binary_protocol_read_byte (protocol, &value, NULL);
663 thrift_binary_protocol_read_byte (protocol, &value, NULL);
664
665 /* test negative map size */
666 assert (thrift_binary_protocol_read_map_begin (protocol,
667 &key_type, &value_type,
668 &size, NULL) == -1);
669
670 /* test list operations */
671 thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL);
672 thrift_binary_protocol_read_list_end (protocol, NULL);
673
674 /* test read failure */
675 transport_read_error = 1;
676 assert (thrift_binary_protocol_read_list_begin (protocol, &element_type,
677 &size, NULL) == -1);
678 transport_read_error = 0;
679
680 /* test 2nd read failure */
681 transport_read_count = 0;
682 transport_read_error_at = 1;
683 thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL);
684 transport_read_error_at = -1;
685
686 /* test negative list size failure */
687 thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL);
688
689 /* test 2nd write failure */
690 thrift_binary_protocol_read_byte (protocol, &value, NULL);
691
692 /* test set operations */
693 thrift_binary_protocol_read_set_begin (protocol, &element_type, &size, NULL);
694 thrift_binary_protocol_read_set_end (protocol, NULL);
695
696 /* broken read */
697 transport_read_error = 1;
698 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
699 &message_type, &seqid,
700 NULL) == -1);
701 transport_read_error = 0;
702
703 /* invalid protocol version */
704 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
705 &message_type, &seqid,
706 NULL) == -1);
707
708 /* sz > 0 */
709 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
710 &message_type, &seqid,
711 NULL) > 0);
712
713 /* read a valid message */
714 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
715 &message_type, &seqid,
716 NULL) > 0);
717 g_free (message_name);
718
719 /* broken 2nd read on a message */
720 transport_read_count = 0;
721 transport_read_error_at = 1;
722 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
723 &message_type, &seqid,
724 NULL) == -1);
725 transport_read_error_at = -1;
726
727 /* broken 3rd read on a message */
728 transport_read_count = 0;
729 transport_read_error_at = 3; /* read_string does two reads */
730 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
731 &message_type, &seqid,
732 NULL) == -1);
733 g_free (message_name);
734 transport_read_error_at = -1;
735
736 /* read a valid message */
737 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
738 &message_type, &seqid,
739 NULL) > 0);
740 g_free (message_name);
741
742 assert (thrift_binary_protocol_read_message_end (protocol, NULL) == 0);
743
744 /* handle 2nd write failure on a message */
745 thrift_binary_protocol_read_i32 (protocol, &version, NULL);
746
747 /* handle 2nd write failure on a message */
748 thrift_binary_protocol_read_i32 (protocol, &version, NULL);
749 thrift_binary_protocol_read_string (protocol, &message_name, NULL);
750
751 g_object_unref (client);
Simon South38e71552015-08-03 10:51:16 +0000752 /* TODO: investigate g_object_unref (tbp); */
Roger Meier213a6642010-10-27 12:30:11 +0000753 g_object_unref (tsocket);
754}
755
Chandler May1ccd81b2016-02-11 08:25:25 -0500756static void
757thrift_server_many_frames (const int port)
758{
759 ThriftServerTransport *transport = NULL;
760 ThriftTransport *client = NULL;
761 ThriftBinaryProtocol *tbp = NULL;
762 ThriftProtocol *protocol = NULL;
763 ThriftServerSocket *tsocket = NULL;
764 gboolean value_boolean = FALSE;
765 gint8 value_byte = 0;
766 gint16 value_16 = 0;
767 gint32 value_32 = 0;
768 gint64 value_64 = 0;
769 gdouble value_double = 0;
770 gchar *string = NULL;
771 gpointer binary = NULL;
772 guint32 len = 0;
773 void *comparator = (void *) TEST_STRING;
774
775 tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", port, NULL);
776 transport = THRIFT_SERVER_TRANSPORT (tsocket);
777 thrift_server_transport_listen (transport, NULL);
778
779 /* wrap the client in a framed transport */
780 client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
781 thrift_server_transport_accept (transport, NULL),
782 "r_buf_size", 1, NULL);
783 assert (client != NULL);
784
785 tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
786 client, NULL);
787 protocol = THRIFT_PROTOCOL (tbp);
788
789 assert (thrift_binary_protocol_read_bool (protocol,
790 &value_boolean, NULL) > 0);
791 assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0);
792 assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0);
793 assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0);
794 assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0);
795 assert (thrift_binary_protocol_read_double (protocol,
796 &value_double, NULL) > 0);
797 assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0);
798 assert (thrift_binary_protocol_read_binary (protocol, &binary,
799 &len, NULL) > 0);
800
801 assert (value_boolean == TEST_BOOL);
802 assert (value_byte == TEST_BYTE);
803 assert (value_16 == TEST_I16);
804 assert (value_32 == TEST_I32);
805 assert (value_64 == TEST_I64);
806 assert (value_double == TEST_DOUBLE);
807 assert (strcmp (TEST_STRING, string) == 0);
808 assert (memcmp (comparator, binary, len) == 0);
809
810 g_free (string);
811 g_free (binary);
812
813 thrift_transport_read_end (client, NULL);
814 thrift_transport_close (client, NULL);
815
816 g_object_unref (tbp);
817 g_object_unref (client);
818 g_object_unref (tsocket);
819}
820
Roger Meier213a6642010-10-27 12:30:11 +0000821int
Roger Meierc1010922010-11-26 10:17:48 +0000822main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000823{
Jens Geyer1c190272015-07-28 23:15:18 +0200824#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meierc1010922010-11-26 10:17:48 +0000825 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200826#endif
827
Roger Meierc1010922010-11-26 10:17:48 +0000828 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000829
Roger Meierc2cc61a2010-11-30 19:53:29 +0000830 g_test_add_func ("/testbinaryprotocol/CreateAndDestroy", test_create_and_destroy);
831 g_test_add_func ("/testbinaryprotocol/Initialize", test_initialize);
832 g_test_add_func ("/testbinaryprotocol/ReadAndWritePrimitives", test_read_and_write_primitives);
833 g_test_add_func ("/testbinaryprotocol/ReadAndWriteComplexTypes", test_read_and_write_complex_types);
Chandler May1ccd81b2016-02-11 08:25:25 -0500834 g_test_add_func ("/testbinaryprotocol/ReadAndWriteManyFrames",
835 test_read_and_write_many_frames);
Roger Meierc1010922010-11-26 10:17:48 +0000836
837 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000838}