blob: 58f4df83be91575de035f9f0b08652d4d8ed5543 [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);
Simon South30a8b652016-12-22 06:29:17 -0500176 assert (thrift_binary_protocol_write_string (protocol, "", NULL) > 0);
Roger Meier213a6642010-10-27 12:30:11 +0000177 assert (thrift_binary_protocol_write_binary (protocol, binary,
178 len, NULL) > 0);
179 assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
180 assert (thrift_binary_protocol_write_binary (protocol, binary,
181 len, NULL) > 0);
182
183 /* test write errors */
184 transport_write_error = 1;
185 assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE,
186 NULL) == -1);
187 assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) == -1);
188 assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) == -1);
189 assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) == -1);
190 assert (thrift_binary_protocol_write_double (protocol, TEST_DOUBLE,
191 NULL) == -1);
192 assert (thrift_binary_protocol_write_binary (protocol, binary, len,
193 NULL) == -1);
194 transport_write_error = 0;
195
196 /* test binary partial failure */
197 transport_write_count = 0;
198 transport_write_error_at = 1;
199 assert (thrift_binary_protocol_write_binary (protocol, binary,
200 len, NULL) == -1);
201 transport_write_error_at = -1;
202
203 /* clean up */
204 thrift_transport_close (transport, NULL);
205 g_object_unref (tsocket);
206 g_object_unref (protocol);
207 assert (wait (&status) == pid);
208 assert (status == 0);
209 }
210}
211
212static void
213test_read_and_write_complex_types (void)
214{
215 int status;
216 pid_t pid;
217 ThriftSocket *tsocket = NULL;
218 ThriftTransport *transport = NULL;
219 ThriftBinaryProtocol *tb = NULL;
220 ThriftProtocol *protocol = NULL;
221 int port = TEST_PORT;
222
223 /* fork a server from the client */
224 pid = fork ();
225 assert (pid >= 0);
226
227 if (pid == 0)
228 {
229 /* child listens */
230 thrift_server_complex_types (port);
231 exit (0);
232 } else {
233 /* parent. wait a bit for the socket to be created. */
234 sleep (1);
235
236 /* create a ThriftSocket */
237 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
238 "port", port, NULL);
239 transport = THRIFT_TRANSPORT (tsocket);
240 thrift_transport_open (transport, NULL);
241 assert (thrift_transport_is_open (transport));
242
243 /* create a ThriftBinaryTransport */
244 tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
245 tsocket, NULL);
246 protocol = THRIFT_PROTOCOL (tb);
247 assert (protocol != NULL);
248
249 /* test structures */
250 assert (thrift_binary_protocol_write_struct_begin (protocol,
251 NULL, NULL) == 0);
252 assert (thrift_binary_protocol_write_struct_end (protocol, NULL) == 0);
253
254 assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
255 1, NULL) > 0);
256 assert (thrift_binary_protocol_write_field_end (protocol, NULL) == 0);
257
258 /* test write error */
259 transport_write_error = 1;
260 assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
261 1, NULL) == -1);
262 transport_write_error = 0;
263
264 /* test 2nd write error */
265 transport_write_count = 0;
266 transport_write_error_at = 1;
267 assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
268 1, NULL) == -1);
269 transport_write_error_at = -1;
270
271 /* test 2nd read failure on a field */
272 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
273
274 /* test write_field_stop */
275 assert (thrift_binary_protocol_write_field_stop (protocol, NULL) > 0);
276
277 /* write a map */
278 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
279 1, NULL) > 0);
280 assert (thrift_binary_protocol_write_map_end (protocol, NULL) == 0);
281
282 /* test 2nd read failure on a map */
283 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
284
285 /* test 3rd read failure on a map */
286 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
287 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
288
289 /* test 1st write failure on a map */
290 transport_write_error = 1;
291 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
292 1, NULL) == -1);
293 transport_write_error = 0;
294
295 /* test 2nd write failure on a map */
296 transport_write_count = 0;
297 transport_write_error_at = 1;
298 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
299 1, NULL) == -1);
300 transport_write_error_at = -1;
301
302 /* test 3rd write failure on a map */
303 transport_write_count = 0;
304 transport_write_error_at = 2;
305 assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
306 1, NULL) == -1);
307 transport_write_error_at = -1;
308
309 /* test negative map size */
310 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
311 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
312 thrift_binary_protocol_write_i32 (protocol, -10, NULL);
313
314 /* test list operations */
315 assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
316 1, NULL) > 0);
317 assert (thrift_binary_protocol_write_list_end (protocol, NULL) == 0);
318
319 /* test 2nd read failure on a list */
320 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
321
322 /* test negative list size */
323 thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
324 thrift_binary_protocol_write_i32 (protocol, -10, NULL);
325
326 /* test first write error on a list */
327 transport_write_error = 1;
328 assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
329 1, NULL) == -1);
330 transport_write_error = 0;
331
332 /* test 2nd write error on a list */
333 transport_write_count = 0;
334 transport_write_error_at = 1;
335 assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
336 1, NULL) == -1);
337 transport_write_error_at = -1;
338
339 /* test set operation s*/
340 assert (thrift_binary_protocol_write_set_begin (protocol, T_VOID,
341 1, NULL) > 0);
342 assert (thrift_binary_protocol_write_set_end (protocol, NULL) == 0);
343
344 /* invalid version */
345 assert (thrift_binary_protocol_write_i32 (protocol, -1, NULL) > 0);
346
347 /* sz > 0 for a message */
348 assert (thrift_binary_protocol_write_i32 (protocol, 1, NULL) > 0);
349
350 /* send a valid message */
351 thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
352 thrift_binary_protocol_write_string (protocol, "test", NULL);
353 thrift_binary_protocol_write_i32 (protocol, 1, NULL);
354
355 /* broken 2nd read */
356 thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
357
358 /* send a broken 3rd read */
359 thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
360 thrift_binary_protocol_write_string (protocol, "test", NULL);
361
362 /* send a valid message */
363 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
364 T_CALL, 1, NULL) > 0);
365
366 assert (thrift_binary_protocol_write_message_end (protocol, NULL) == 0);
367
368 /* send broken writes */
369 transport_write_error = 1;
370 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
371 T_CALL, 1, NULL) == -1);
372 transport_write_error = 0;
373
374 transport_write_count = 0;
375 transport_write_error_at = 2;
376 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
377 T_CALL, 1, NULL) == -1);
378 transport_write_error_at = -1;
379
380 transport_write_count = 0;
381 transport_write_error_at = 3;
382 assert (thrift_binary_protocol_write_message_begin (protocol, "test",
383 T_CALL, 1, NULL) == -1);
384 transport_write_error_at = -1;
385
386 /* clean up */
387 thrift_transport_close (transport, NULL);
388 g_object_unref (tsocket);
389 g_object_unref (protocol);
390 assert (wait (&status) == pid);
391 assert (status == 0);
392 }
393}
394
Chandler May1ccd81b2016-02-11 08:25:25 -0500395static void
396test_read_and_write_many_frames (void)
397{
398 int status;
399 pid_t pid;
400 ThriftSocket *tsocket = NULL;
401 ThriftTransport *transport = NULL;
402 ThriftFramedTransport *ft = NULL;
403 ThriftBinaryProtocol *tb = NULL;
404 ThriftProtocol *protocol = NULL;
405 gpointer binary = (gpointer *) TEST_STRING;
406 const guint32 len = strlen (TEST_STRING);
407 int port = TEST_PORT;
408
409 /* fork a server from the client */
410 pid = fork ();
411 assert (pid >= 0);
412
413 if (pid == 0)
414 {
415 /* child listens */
416 thrift_server_many_frames (port);
417 exit (0);
418 } else {
419 /* parent. wait a bit for the socket to be created. */
420 sleep (1);
421
422 /* create a ThriftSocket */
423 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
424 "port", port, NULL);
425 assert (tsocket != NULL);
426 transport = THRIFT_TRANSPORT (tsocket);
427
428 /* wrap in a framed transport */
429 ft = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport", transport,
430 "w_buf_size", 1, NULL);
431 assert (ft != NULL);
432 transport = THRIFT_TRANSPORT (ft);
433
434 thrift_transport_open (transport, NULL);
435 assert (thrift_transport_is_open (transport));
436
437 /* create a binary protocol */
438 tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
439 transport, NULL);
440 protocol = THRIFT_PROTOCOL (tb);
441 assert (protocol != NULL);
442
443 /* write a bunch of primitives */
444 assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
445 thrift_transport_flush (transport, NULL);
446 assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
447 thrift_transport_flush (transport, NULL);
448 assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
449 thrift_transport_flush (transport, NULL);
450 assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
451 thrift_transport_flush (transport, NULL);
452 assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
453 thrift_transport_flush (transport, NULL);
454 assert (thrift_binary_protocol_write_double (protocol,
455 TEST_DOUBLE, NULL) > 0);
456 thrift_transport_flush (transport, NULL);
457 assert (thrift_binary_protocol_write_string (protocol,
458 TEST_STRING, NULL) > 0);
459 thrift_transport_flush (transport, NULL);
Simon South30a8b652016-12-22 06:29:17 -0500460 assert (thrift_binary_protocol_write_string (protocol, "", NULL) > 0);
461 thrift_transport_flush (transport, NULL);
Chandler May1ccd81b2016-02-11 08:25:25 -0500462 assert (thrift_binary_protocol_write_binary (protocol, binary,
463 len, NULL) > 0);
464 thrift_transport_flush (transport, NULL);
465 assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
466 thrift_transport_flush (transport, NULL);
467 assert (thrift_binary_protocol_write_binary (protocol, binary,
468 len, NULL) > 0);
469 thrift_transport_flush (transport, NULL);
470
471 /* clean up */
472 thrift_transport_write_end (transport, NULL);
473 thrift_transport_close (transport, NULL);
474 g_object_unref (ft);
475 g_object_unref (tsocket);
476 g_object_unref (tb);
477 assert (wait (&status) == pid);
478 assert (status == 0);
479 }
480}
481
Roger Meier213a6642010-10-27 12:30:11 +0000482
483static void
484thrift_server_primitives (const int port)
485{
486 ThriftServerTransport *transport = NULL;
487 ThriftTransport *client = NULL;
488 ThriftBinaryProtocol *tbp = NULL;
489 ThriftProtocol *protocol = NULL;
490 gboolean value_boolean = FALSE;
491 gint8 value_byte = 0;
492 gint16 value_16 = 0;
493 gint32 value_32 = 0;
494 gint64 value_64 = 0;
495 gdouble value_double = 0;
496 gchar *string = NULL;
Simon South30a8b652016-12-22 06:29:17 -0500497 gchar *empty_string = NULL;
Roger Meier213a6642010-10-27 12:30:11 +0000498 gpointer binary = NULL;
499 guint32 len = 0;
500 void *comparator = (void *) TEST_STRING;
501
502 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
503 "port", port, NULL);
504 transport = THRIFT_SERVER_TRANSPORT (tsocket);
505 thrift_server_transport_listen (transport, NULL);
506 client = thrift_server_transport_accept (transport, NULL);
507 assert (client != NULL);
508
509 tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
510 client, NULL);
511 protocol = THRIFT_PROTOCOL (tbp);
512
513 assert (thrift_binary_protocol_read_bool (protocol,
514 &value_boolean, NULL) > 0);
515 assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0);
516 assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0);
517 assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0);
518 assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0);
519 assert (thrift_binary_protocol_read_double (protocol,
520 &value_double, NULL) > 0);
521 assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0);
Simon South30a8b652016-12-22 06:29:17 -0500522 assert (thrift_binary_protocol_read_string (protocol, &empty_string,
523 NULL) > 0);
Roger Meier213a6642010-10-27 12:30:11 +0000524 assert (thrift_binary_protocol_read_binary (protocol, &binary,
525 &len, NULL) > 0);
526
527 assert (value_boolean == TEST_BOOL);
Chandler May8b0fe282016-01-16 15:10:34 -0500528 assert (value_byte == TEST_BYTE);
529 assert (value_16 == TEST_I16);
530 assert (value_32 == TEST_I32);
531 assert (value_64 == TEST_I64);
532 assert (value_double == TEST_DOUBLE);
Roger Meier213a6642010-10-27 12:30:11 +0000533 assert (strcmp (TEST_STRING, string) == 0);
Simon South30a8b652016-12-22 06:29:17 -0500534 assert (strcmp ("", empty_string) == 0);
Roger Meier213a6642010-10-27 12:30:11 +0000535 assert (memcmp (comparator, binary, len) == 0);
536
537 g_free (string);
Simon South30a8b652016-12-22 06:29:17 -0500538 g_free (empty_string);
Roger Meier213a6642010-10-27 12:30:11 +0000539 g_free (binary);
540
Simon South30a8b652016-12-22 06:29:17 -0500541 assert (thrift_binary_protocol_read_binary (protocol, &binary,
542 &len, NULL) > 0);
543 assert (binary == NULL);
544 assert (len == 0);
Roger Meier213a6642010-10-27 12:30:11 +0000545 g_free (binary);
546
547 transport_read_count = 0;
548 transport_read_error_at = 0;
549 assert (thrift_binary_protocol_read_binary (protocol, &binary,
550 &len, NULL) == -1);
551 transport_read_error_at = -1;
552
553 transport_read_count = 0;
554 transport_read_error_at = 1;
555 assert (thrift_binary_protocol_read_binary (protocol, &binary,
556 &len, NULL) == -1);
557 transport_read_error_at = -1;
558
559 transport_read_error = 1;
560 assert (thrift_binary_protocol_read_bool (protocol,
561 &value_boolean, NULL) == -1);
562 assert (thrift_binary_protocol_read_byte (protocol,
563 &value_byte, NULL) == -1);
564 assert (thrift_binary_protocol_read_i16 (protocol,
565 &value_16, NULL) == -1);
566 assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) == -1);
567 assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) == -1);
568 assert (thrift_binary_protocol_read_double (protocol,
569 &value_double, NULL) == -1);
570 transport_read_error = 0;
571
572 /* test partial write failure */
573 thrift_protocol_read_i32 (protocol, &value_32, NULL);
574
575 thrift_transport_read_end (client, NULL);
576 thrift_transport_close (client, NULL);
577
578 g_object_unref (tbp);
579 g_object_unref (client);
580 g_object_unref (tsocket);
581}
582
583static void
584thrift_server_complex_types (const int port)
585{
586 ThriftServerTransport *transport = NULL;
587 ThriftTransport *client = NULL;
588 ThriftBinaryProtocol *tbp = NULL;
589 ThriftProtocol *protocol = NULL;
590 gchar *struct_name = NULL;
591 gchar *field_name = NULL;
592 gchar *message_name = NULL;
593 ThriftType element_type, key_type, value_type, field_type;
594 ThriftMessageType message_type;
595 gint8 value = 0;
596 gint16 field_id = 0;
597 guint32 size = 0;
598 gint32 seqid = 0;
599 gint32 version = 0;
600
601 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
602 "port", port, NULL);
603 transport = THRIFT_SERVER_TRANSPORT (tsocket);
604 thrift_server_transport_listen (transport, NULL);
605 client = thrift_server_transport_accept (transport, NULL);
606 assert (client != NULL);
607
608 tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
609 client, NULL);
610 protocol = THRIFT_PROTOCOL (tbp);
611
612 thrift_binary_protocol_read_struct_begin (protocol, &struct_name, NULL);
613 thrift_binary_protocol_read_struct_end (protocol, NULL);
614
615 thrift_binary_protocol_read_field_begin (protocol, &field_name, &field_type,
616 &field_id, NULL);
617 thrift_binary_protocol_read_field_end (protocol, NULL);
618
619 /* test first read error on a field */
620 transport_read_error = 1;
621 assert (thrift_binary_protocol_read_field_begin (protocol,
622 &field_name, &field_type,
623 &field_id, NULL) == -1);
624 transport_read_error = 0;
625
626 /* test 2nd write failure */
627 thrift_binary_protocol_read_byte (protocol, &value, NULL);
628
629 /* test 2nd read failure on a field */
630 transport_read_count = 0;
631 transport_read_error_at = 1;
632 assert (thrift_binary_protocol_read_field_begin (protocol,
633 &field_name, &field_type,
634 &field_id, NULL) == -1);
635 transport_read_error_at = -1;
636
637 /* test field stop */
638 thrift_binary_protocol_read_field_begin (protocol, &field_name, &field_type,
639 &field_id, NULL);
640
641 thrift_binary_protocol_read_map_begin (protocol, &key_type, &value_type,
642 &size, NULL);
643 thrift_binary_protocol_read_map_end (protocol, NULL);
644
645 /* test read failure on a map */
646 transport_read_count = 0;
647 transport_read_error_at = 0;
648 assert (thrift_binary_protocol_read_map_begin (protocol,
649 &key_type, &value_type,
650 &size, NULL) == -1);
651 transport_read_error_at = -1;
652
653 /* test 2nd read failure on a map */
654 transport_read_count = 0;
655 transport_read_error_at = 1;
656 assert (thrift_binary_protocol_read_map_begin (protocol,
657 &key_type, &value_type,
658 &size, NULL) == -1);
659 transport_read_error_at = -1;
660
661 /* test 3rd read failure on a map */
662 transport_read_count = 0;
663 transport_read_error_at = 2;
664 assert (thrift_binary_protocol_read_map_begin (protocol,
665 &key_type, &value_type,
666 &size, NULL) == -1);
667 transport_read_error_at = -1;
668
669 /* test 2nd write failure */
670 thrift_binary_protocol_read_byte (protocol, &value, NULL);
671
672 /* test 3rd write failure */
673 thrift_binary_protocol_read_byte (protocol, &value, NULL);
674 thrift_binary_protocol_read_byte (protocol, &value, NULL);
675
676 /* test negative map size */
677 assert (thrift_binary_protocol_read_map_begin (protocol,
678 &key_type, &value_type,
679 &size, NULL) == -1);
680
681 /* test list operations */
682 thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL);
683 thrift_binary_protocol_read_list_end (protocol, NULL);
684
685 /* test read failure */
686 transport_read_error = 1;
687 assert (thrift_binary_protocol_read_list_begin (protocol, &element_type,
688 &size, NULL) == -1);
689 transport_read_error = 0;
690
691 /* test 2nd read failure */
692 transport_read_count = 0;
693 transport_read_error_at = 1;
694 thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL);
695 transport_read_error_at = -1;
696
697 /* test negative list size failure */
698 thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL);
699
700 /* test 2nd write failure */
701 thrift_binary_protocol_read_byte (protocol, &value, NULL);
702
703 /* test set operations */
704 thrift_binary_protocol_read_set_begin (protocol, &element_type, &size, NULL);
705 thrift_binary_protocol_read_set_end (protocol, NULL);
706
707 /* broken read */
708 transport_read_error = 1;
709 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
710 &message_type, &seqid,
711 NULL) == -1);
712 transport_read_error = 0;
713
714 /* invalid protocol version */
715 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
716 &message_type, &seqid,
717 NULL) == -1);
718
719 /* sz > 0 */
720 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
721 &message_type, &seqid,
722 NULL) > 0);
723
724 /* read a valid message */
725 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
726 &message_type, &seqid,
727 NULL) > 0);
728 g_free (message_name);
729
730 /* broken 2nd read on a message */
731 transport_read_count = 0;
732 transport_read_error_at = 1;
733 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
734 &message_type, &seqid,
735 NULL) == -1);
736 transport_read_error_at = -1;
737
738 /* broken 3rd read on a message */
739 transport_read_count = 0;
740 transport_read_error_at = 3; /* read_string does two reads */
741 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
742 &message_type, &seqid,
743 NULL) == -1);
744 g_free (message_name);
745 transport_read_error_at = -1;
746
747 /* read a valid message */
748 assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
749 &message_type, &seqid,
750 NULL) > 0);
751 g_free (message_name);
752
753 assert (thrift_binary_protocol_read_message_end (protocol, NULL) == 0);
754
755 /* handle 2nd write failure on a message */
756 thrift_binary_protocol_read_i32 (protocol, &version, NULL);
757
758 /* handle 2nd write failure on a message */
759 thrift_binary_protocol_read_i32 (protocol, &version, NULL);
760 thrift_binary_protocol_read_string (protocol, &message_name, NULL);
761
762 g_object_unref (client);
Simon South38e71552015-08-03 10:51:16 +0000763 /* TODO: investigate g_object_unref (tbp); */
Roger Meier213a6642010-10-27 12:30:11 +0000764 g_object_unref (tsocket);
765}
766
Chandler May1ccd81b2016-02-11 08:25:25 -0500767static void
768thrift_server_many_frames (const int port)
769{
770 ThriftServerTransport *transport = NULL;
771 ThriftTransport *client = NULL;
772 ThriftBinaryProtocol *tbp = NULL;
773 ThriftProtocol *protocol = NULL;
774 ThriftServerSocket *tsocket = NULL;
775 gboolean value_boolean = FALSE;
776 gint8 value_byte = 0;
777 gint16 value_16 = 0;
778 gint32 value_32 = 0;
779 gint64 value_64 = 0;
780 gdouble value_double = 0;
781 gchar *string = NULL;
Simon South30a8b652016-12-22 06:29:17 -0500782 gchar *empty_string = NULL;
Chandler May1ccd81b2016-02-11 08:25:25 -0500783 gpointer binary = NULL;
784 guint32 len = 0;
785 void *comparator = (void *) TEST_STRING;
786
787 tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", port, NULL);
788 transport = THRIFT_SERVER_TRANSPORT (tsocket);
789 thrift_server_transport_listen (transport, NULL);
790
791 /* wrap the client in a framed transport */
792 client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
793 thrift_server_transport_accept (transport, NULL),
794 "r_buf_size", 1, NULL);
795 assert (client != NULL);
796
797 tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
798 client, NULL);
799 protocol = THRIFT_PROTOCOL (tbp);
800
801 assert (thrift_binary_protocol_read_bool (protocol,
802 &value_boolean, NULL) > 0);
803 assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0);
804 assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0);
805 assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0);
806 assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0);
807 assert (thrift_binary_protocol_read_double (protocol,
808 &value_double, NULL) > 0);
809 assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0);
Simon South30a8b652016-12-22 06:29:17 -0500810 assert (thrift_binary_protocol_read_string (protocol, &empty_string,
811 NULL) > 0);
Chandler May1ccd81b2016-02-11 08:25:25 -0500812 assert (thrift_binary_protocol_read_binary (protocol, &binary,
813 &len, NULL) > 0);
814
815 assert (value_boolean == TEST_BOOL);
816 assert (value_byte == TEST_BYTE);
817 assert (value_16 == TEST_I16);
818 assert (value_32 == TEST_I32);
819 assert (value_64 == TEST_I64);
820 assert (value_double == TEST_DOUBLE);
821 assert (strcmp (TEST_STRING, string) == 0);
Simon South30a8b652016-12-22 06:29:17 -0500822 assert (strcmp ("", empty_string) == 0);
Chandler May1ccd81b2016-02-11 08:25:25 -0500823 assert (memcmp (comparator, binary, len) == 0);
824
825 g_free (string);
Simon South30a8b652016-12-22 06:29:17 -0500826 g_free (empty_string);
827 g_free (binary);
828
829 assert (thrift_binary_protocol_read_binary (protocol, &binary,
830 &len, NULL) > 0);
831 assert (binary == NULL);
832 assert (len == 0);
Chandler May1ccd81b2016-02-11 08:25:25 -0500833 g_free (binary);
834
835 thrift_transport_read_end (client, NULL);
836 thrift_transport_close (client, NULL);
837
838 g_object_unref (tbp);
839 g_object_unref (client);
840 g_object_unref (tsocket);
841}
842
Roger Meier213a6642010-10-27 12:30:11 +0000843int
Roger Meierc1010922010-11-26 10:17:48 +0000844main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000845{
Jens Geyer1c190272015-07-28 23:15:18 +0200846#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meierc1010922010-11-26 10:17:48 +0000847 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200848#endif
849
Roger Meierc1010922010-11-26 10:17:48 +0000850 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000851
Roger Meierc2cc61a2010-11-30 19:53:29 +0000852 g_test_add_func ("/testbinaryprotocol/CreateAndDestroy", test_create_and_destroy);
853 g_test_add_func ("/testbinaryprotocol/Initialize", test_initialize);
854 g_test_add_func ("/testbinaryprotocol/ReadAndWritePrimitives", test_read_and_write_primitives);
855 g_test_add_func ("/testbinaryprotocol/ReadAndWriteComplexTypes", test_read_and_write_complex_types);
Chandler May1ccd81b2016-02-11 08:25:25 -0500856 g_test_add_func ("/testbinaryprotocol/ReadAndWriteManyFrames",
857 test_read_and_write_many_frames);
Roger Meierc1010922010-11-26 10:17:48 +0000858
859 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000860}