blob: 03bc226d9c348edcc8455ebc5a80a3ce291f0fd8 [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>
Chandler May1ccd81b2016-02-11 08:25:25 -050043#include <thrift/c_glib/transport/thrift_framed_transport.h>
Chandler May6dde90b2016-01-10 06:01:10 +000044
45#define TEST_BOOL TRUE
46#define TEST_BYTE 123
47#define TEST_I16 12345
48#define TEST_I32 1234567890
49#define TEST_I64 123456789012345
50#define TEST_NI16 (-12345)
51#define TEST_NI32 (-1234567890)
52#define TEST_NI64 (-123456789012345)
53#define TEST_DOUBLE 1234567890.123
54#define TEST_STRING "this is a test string 1234567890!@#$%^&*()"
55#define TEST_PORT 51199
56
57static int transport_read_count = 0;
58static int transport_read_error = 0;
59static int transport_read_error_at = -1;
60gint32
Chandler May1ccd81b2016-02-11 08:25:25 -050061my_thrift_transport_read_all (ThriftTransport *transport, gpointer buf,
62 guint32 len, GError **error)
Chandler May6dde90b2016-01-10 06:01:10 +000063{
64 if (transport_read_count != transport_read_error_at
65 && transport_read_error == 0)
66 {
67 transport_read_count++;
Chandler May1ccd81b2016-02-11 08:25:25 -050068 return thrift_transport_read_all (transport, buf, len, error);
Chandler May6dde90b2016-01-10 06:01:10 +000069 }
70 return -1;
71}
72
73static int transport_write_count = 0;
74static int transport_write_error = 0;
75static int transport_write_error_at = -1;
76gboolean
77my_thrift_transport_write (ThriftTransport *transport, const gpointer buf,
78 const guint32 len, GError **error)
79{
80 if (transport_write_count != transport_write_error_at
81 && transport_write_error == 0)
82 {
83 transport_write_count++;
84 return thrift_transport_write (transport, buf, len, error);
85 }
86 return FALSE;
87}
88
Chandler May1ccd81b2016-02-11 08:25:25 -050089#define thrift_transport_read_all my_thrift_transport_read_all
Chandler May6dde90b2016-01-10 06:01:10 +000090#define thrift_transport_write my_thrift_transport_write
91#include "../src/thrift/c_glib/protocol/thrift_compact_protocol.c"
Chandler May1ccd81b2016-02-11 08:25:25 -050092#undef thrift_transport_read_all
Chandler May6dde90b2016-01-10 06:01:10 +000093#undef thrift_transport_write
94
95static void thrift_server_primitives (const int port);
96static void thrift_server_complex_types (const int port);
Chandler May1ccd81b2016-02-11 08:25:25 -050097static void thrift_server_many_frames (const int port);
Chandler May6dde90b2016-01-10 06:01:10 +000098
99static void
100test_create_and_destroy (void)
101{
102 GObject *object = NULL;
103
104 /* create an object and then destroy it */
105 object = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, NULL);
106 assert (object != NULL);
107 g_object_unref (object);
108}
109
110static void
111test_initialize (void)
112{
113 ThriftSocket *tsocket = NULL;
114 ThriftCompactProtocol *protocol = NULL;
115 ThriftSocket *temp = NULL;
116
117 /* create a ThriftTransport */
118 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
119 "port", 51188, NULL);
120 assert (tsocket != NULL);
121 /* create a ThriftCompactProtocol using the Transport */
122 protocol = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
123 tsocket, NULL);
124 assert (protocol != NULL);
125 /* fetch the properties */
126 g_object_get (G_OBJECT (protocol), "transport", &temp, NULL);
127 g_object_unref (temp);
128
129 /* clean up memory */
130 g_object_unref (protocol);
131 g_object_unref (tsocket);
132}
133
134static void
135test_read_and_write_primitives (void)
136{
137 int status;
138 pid_t pid;
139 ThriftSocket *tsocket = NULL;
140 ThriftTransport *transport = NULL;
141 ThriftCompactProtocol *tc = NULL;
142 ThriftProtocol *protocol = NULL;
143 gpointer binary = (gpointer *) TEST_STRING;
144 guint32 len = strlen (TEST_STRING);
145 int port = TEST_PORT;
146
147 /* fork a server from the client */
148 pid = fork ();
149 assert (pid >= 0);
150
151 if (pid == 0)
152 {
153 /* child listens */
154 thrift_server_primitives (port);
155 exit (0);
156 } else {
157 /* parent. wait a bit for the socket to be created. */
158 sleep (1);
159
160 /* create a ThriftSocket */
161 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
162 "port", port, NULL);
163 transport = THRIFT_TRANSPORT (tsocket);
164 thrift_transport_open (transport, NULL);
165 assert (thrift_transport_is_open (transport));
166
167 /* create a ThriftCompactTransport */
168 tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
169 tsocket, NULL);
170 protocol = THRIFT_PROTOCOL (tc);
171 assert (protocol != NULL);
172
173 /* write a bunch of primitives */
174 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
175 assert (thrift_compact_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
176 assert (thrift_compact_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
177 assert (thrift_compact_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
178 assert (thrift_compact_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
179 assert (thrift_compact_protocol_write_i16 (protocol, TEST_NI16, NULL) > 0);
180 assert (thrift_compact_protocol_write_i32 (protocol, TEST_NI32, NULL) > 0);
181 assert (thrift_compact_protocol_write_i64 (protocol, TEST_NI64, NULL) > 0);
182 assert (thrift_compact_protocol_write_i16 (protocol, 2, NULL) > 0);
183 assert (thrift_compact_protocol_write_i32 (protocol, 2, NULL) > 0);
184 assert (thrift_compact_protocol_write_i64 (protocol, 2, NULL) > 0);
185 assert (thrift_compact_protocol_write_i16 (protocol, -2, NULL) > 0);
186 assert (thrift_compact_protocol_write_i32 (protocol, -2, NULL) > 0);
187 assert (thrift_compact_protocol_write_i64 (protocol, -2, NULL) > 0);
188 assert (thrift_compact_protocol_write_double (protocol,
189 TEST_DOUBLE, NULL) > 0);
190 assert (thrift_compact_protocol_write_string (protocol,
191 TEST_STRING, NULL) > 0);
Simon South30a8b652016-12-22 06:29:17 -0500192 assert (thrift_compact_protocol_write_string (protocol, "", NULL) > 0);
Chandler May6dde90b2016-01-10 06:01:10 +0000193 assert (thrift_compact_protocol_write_binary (protocol, binary,
194 len, NULL) > 0);
195 assert (thrift_compact_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
196 assert (thrift_compact_protocol_write_binary (protocol, binary,
197 len, NULL) > 0);
198
199 /* test write errors */
200 transport_write_error = 1;
201 assert (thrift_compact_protocol_write_byte (protocol, TEST_BYTE,
202 NULL) == -1);
203 assert (thrift_compact_protocol_write_i16 (protocol, TEST_I16, NULL) == -1);
204 assert (thrift_compact_protocol_write_i32 (protocol, TEST_I32, NULL) == -1);
205 assert (thrift_compact_protocol_write_i64 (protocol, TEST_I64, NULL) == -1);
206 assert (thrift_compact_protocol_write_i16 (protocol, TEST_NI16,
207 NULL) == -1);
208 assert (thrift_compact_protocol_write_i32 (protocol, TEST_NI32,
209 NULL) == -1);
210 assert (thrift_compact_protocol_write_i64 (protocol, TEST_NI64,
211 NULL) == -1);
212 assert (thrift_compact_protocol_write_double (protocol, TEST_DOUBLE,
213 NULL) == -1);
214 assert (thrift_compact_protocol_write_binary (protocol, binary, len,
215 NULL) == -1);
216 transport_write_error = 0;
217
218 /* test binary partial failure */
219 transport_write_count = 0;
220 transport_write_error_at = 1;
221 assert (thrift_compact_protocol_write_binary (protocol, binary,
222 len, NULL) == -1);
223 transport_write_error_at = -1;
224
225 /* clean up */
226 thrift_transport_close (transport, NULL);
227 g_object_unref (tsocket);
228 g_object_unref (protocol);
229 assert (wait (&status) == pid);
230 assert (status == 0);
231 }
232}
233
234static void
235test_read_and_write_complex_types (void)
236{
237 int status;
238 pid_t pid;
239 ThriftSocket *tsocket = NULL;
240 ThriftTransport *transport = NULL;
241 ThriftCompactProtocol *tc = NULL;
242 ThriftProtocol *protocol = NULL;
243 int port = TEST_PORT;
244
245 /* fork a server from the client */
246 pid = fork ();
247 assert (pid >= 0);
248
249 if (pid == 0)
250 {
251 /* child listens */
252 thrift_server_complex_types (port);
253 exit (0);
254 } else {
255 /* parent. wait a bit for the socket to be created. */
256 sleep (1);
257
258 /* create a ThriftSocket */
259 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
260 "port", port, NULL);
261 transport = THRIFT_TRANSPORT (tsocket);
262 thrift_transport_open (transport, NULL);
263 assert (thrift_transport_is_open (transport));
264
265 /* create a ThriftCompactTransport */
266 tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
267 tsocket, NULL);
268 protocol = THRIFT_PROTOCOL (tc);
269 assert (protocol != NULL);
270
271 /* test structures */
272 assert (thrift_compact_protocol_write_struct_begin (protocol,
273 NULL, NULL) == 0);
274 assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0);
275
276 /* test field state w.r.t. deltas */
277
278 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
279 T_DOUBLE, 1, NULL) == 1);
280 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
281 T_DOUBLE,
282 16, NULL) == 1);
283 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
284 T_DOUBLE,
285 17, NULL) == 1);
286 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
287 T_DOUBLE,
288 15, NULL) > 1);
289 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
290 T_DOUBLE,
291 30, NULL) == 1);
292 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
293 T_DOUBLE,
294 46, NULL) > 1);
295 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
296 T_DOUBLE,
297 47, NULL) == 1);
298
299 /* test fields */
300 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
301 T_DOUBLE,
302 1, NULL) > 1);
303 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
304
305 /* test field state w.r.t. structs */
306
307 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
308 T_DOUBLE,
309 1, NULL) > 1);
310 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
311 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
312 T_DOUBLE,
313 16, NULL) == 1);
314 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
315
316 assert (thrift_compact_protocol_write_struct_begin (protocol,
317 NULL, NULL) == 0);
318 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
319 T_DOUBLE,
320 17, NULL) > 1);
321 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
322
323 assert (thrift_compact_protocol_write_struct_begin (protocol,
324 NULL, NULL) == 0);
325 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
326 T_DOUBLE,
327 18, NULL) > 1);
328 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
329 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
330 T_DOUBLE,
331 19, NULL) == 1);
332 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
333 assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0);
334
335 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
336 T_DOUBLE,
337 18, NULL) == 1);
338 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
339 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
340 T_DOUBLE,
341 25, NULL) == 1);
342 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
343 assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0);
344
345 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
346 T_DOUBLE,
347 17, NULL) == 1);
348 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
349
350 /* test field state w.r.t. bools */
351
352 /* deltas */
353 /* non-bool field -> bool field -> non-bool field */
354 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
355 T_DOUBLE,
356 18, NULL) == 1);
357 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
358 assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
359 19, NULL) == 0);
360 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL,
361 NULL) == 1);
362 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
363 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
364 T_DOUBLE,
365 20, NULL) == 1);
366 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
367 /* bool -> bool field -> bool */
368 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
369 assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
370 21, NULL) == 0);
371 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL,
372 NULL) == 1);
373 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
374 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
375
376 /* no deltas */
377 /* non-bool field -> bool field -> non-bool field */
378 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
379 T_DOUBLE,
380 1, NULL) > 1);
381 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
382 assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
383 1, NULL) == 0);
384 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 1);
385 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
386 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
387 T_DOUBLE,
388 1, NULL) > 1);
389 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
390 /* bool -> bool field -> bool */
391 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
392 assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
393 1, NULL) == 0);
394 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 1);
395 assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
396 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
397
398 /* test write error */
399 transport_write_error = 1;
400 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
401 T_DOUBLE,
402 1, NULL) == -1);
403 transport_write_error = 0;
404
405 /* test 2nd write error */
406 transport_write_count = 0;
407 transport_write_error_at = 1;
408 assert (thrift_compact_protocol_write_field_begin (protocol, "test",
409 T_DOUBLE,
410 1, NULL) == -1);
411 transport_write_error_at = -1;
412
413 /* test 2nd read failure on a field */
414 thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL);
415
416 /* test write_field_stop */
417 assert (thrift_compact_protocol_write_field_stop (protocol, NULL) > 0);
418
419 /* write a map */
420 assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE,
421 T_DOUBLE,
422 1, NULL) > 0);
423 assert (thrift_compact_protocol_write_map_end (protocol, NULL) == 0);
424
425 /* test 1st read failure on map---nothing to do on our side */
426
427 /* test 2nd read failure on a map */
428 thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL);
429
430 /* test 1st write failure on a map */
431 transport_write_error = 1;
432 assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE,
433 T_DOUBLE,
434 1, NULL) == -1);
435 transport_write_error = 0;
436
437 /* test 2nd write failure on a map */
438 transport_write_count = 0;
439 transport_write_error_at = 1;
440 assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE,
441 T_DOUBLE,
442 1, NULL) == -1);
443 transport_write_error_at = -1;
444
445 /* test negative map size */
446 thrift_compact_protocol_write_varint32 (tc, -10, NULL);
447 thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL);
448
449 /* test list operations */
450 assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
451 15, NULL) > 0);
452 assert (thrift_compact_protocol_write_list_end (protocol, NULL) == 0);
453
454 /* test 1st read failure on a small list---nothing to do on our end */
455
456 /* test 1st read failure on a big list---nothing to do on our end */
457
458 /* test 2nd read failure on a big list */
459 thrift_compact_protocol_write_byte (protocol, (gint8) 0xf0, NULL);
460
461 /* test negative list size */
462 thrift_compact_protocol_write_byte (protocol, (gint8) 0xf0, NULL);
463 thrift_compact_protocol_write_varint32 (tc, -10, NULL);
464
465 /* test first write error on a small list */
466 transport_write_error = 1;
467 assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
468 14, NULL) == -1);
469 transport_write_error = 0;
470
471 /* test first write error on a big list */
472 transport_write_error = 1;
473 assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
474 15, NULL) == -1);
475 transport_write_error = 0;
476
477 /* test 2nd write error on a big list */
478 transport_write_count = 0;
479 transport_write_error_at = 1;
480 assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
481 15, NULL) == -1);
482 transport_write_error_at = -1;
483
484 /* test set operation s*/
485 assert (thrift_compact_protocol_write_set_begin (protocol, T_DOUBLE,
486 1, NULL) > 0);
487 assert (thrift_compact_protocol_write_set_end (protocol, NULL) == 0);
488
489 /* invalid protocol */
490 assert (thrift_compact_protocol_write_byte (protocol, 0, NULL) > 0);
491
492 /* invalid version */
493 assert (thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u,
494 NULL) > 0);
495 assert (thrift_compact_protocol_write_byte (protocol, 0, NULL) > 0);
496
497 /* send a valid message */
498 assert (thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u,
499 NULL) > 0);
500 assert (thrift_compact_protocol_write_byte (protocol, 0x01u, NULL) > 0);
501 thrift_compact_protocol_write_varint32 (tc, 1, NULL);
502 thrift_compact_protocol_write_string (protocol, "test", NULL);
503
504 /* broken 2nd read */
505 thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL);
506
507 /* send a broken 3rd read */
508 thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL);
509 thrift_compact_protocol_write_byte (protocol, 0x01u, NULL);
510
511 /* send a broken 4th read */
512 thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL);
513 thrift_compact_protocol_write_byte (protocol, 0x01u, NULL);
514 thrift_compact_protocol_write_varint32 (tc, 1, NULL);
515
516 /* send a valid message */
517 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
518 T_CALL, 1, NULL) > 0);
519
520 assert (thrift_compact_protocol_write_message_end (protocol, NULL) == 0);
521
522 /* send broken writes */
523 transport_write_error = 1;
524 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
525 T_CALL, 1, NULL) == -1);
526 transport_write_error = 0;
527
528 transport_write_count = 0;
529 transport_write_error_at = 1;
530 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
531 T_CALL, 1, NULL) == -1);
532 transport_write_error_at = -1;
533
534 transport_write_count = 0;
535 transport_write_error_at = 2;
536 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
537 T_CALL, 1, NULL) == -1);
538 transport_write_error_at = -1;
539
540 transport_write_count = 0;
541 transport_write_error_at = 3;
542 assert (thrift_compact_protocol_write_message_begin (protocol, "test",
543 T_CALL, 1, NULL) == -1);
544 transport_write_error_at = -1;
545
546 /* clean up */
547 thrift_transport_close (transport, NULL);
548 g_object_unref (tsocket);
549 g_object_unref (protocol);
550 assert (wait (&status) == pid);
551 assert (status == 0);
552 }
553}
554
Chandler May1ccd81b2016-02-11 08:25:25 -0500555static void
556test_read_and_write_many_frames (void)
557{
558 int status;
559 pid_t pid;
560 ThriftSocket *tsocket = NULL;
561 ThriftTransport *transport = NULL;
562 ThriftFramedTransport *ft = NULL;
563 ThriftCompactProtocol *tc = NULL;
564 ThriftProtocol *protocol = NULL;
565 gpointer binary = (gpointer *) TEST_STRING;
566 const guint32 len = strlen (TEST_STRING);
567 int port = TEST_PORT;
568
569 /* fork a server from the client */
570 pid = fork ();
571 assert (pid >= 0);
572
573 if (pid == 0)
574 {
575 /* child listens */
576 thrift_server_many_frames (port);
577 exit (0);
578 } else {
579 /* parent. wait a bit for the socket to be created. */
580 sleep (1);
581
582 /* create a ThriftSocket */
583 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
584 "port", port, NULL);
585 assert (tsocket != NULL);
586 transport = THRIFT_TRANSPORT (tsocket);
587
588 /* wrap in a framed transport */
589 ft = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport", transport,
590 "w_buf_size", 1, NULL);
591 assert (ft != NULL);
592 transport = THRIFT_TRANSPORT (ft);
593
594 thrift_transport_open (transport, NULL);
595 assert (thrift_transport_is_open (transport));
596
597 /* create a compact protocol */
598 tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
599 transport, NULL);
600 protocol = THRIFT_PROTOCOL (tc);
601 assert (protocol != NULL);
602
603 /* write a bunch of primitives */
604 assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL,
605 NULL) > 0);
606 thrift_transport_flush (transport, NULL);
607 assert (thrift_compact_protocol_write_byte (protocol, TEST_BYTE,
608 NULL) > 0);
609 thrift_transport_flush (transport, NULL);
610 assert (thrift_compact_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
611 thrift_transport_flush (transport, NULL);
612 assert (thrift_compact_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
613 thrift_transport_flush (transport, NULL);
614 assert (thrift_compact_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
615 thrift_transport_flush (transport, NULL);
616 assert (thrift_compact_protocol_write_i16 (protocol, TEST_NI16, NULL) > 0);
617 thrift_transport_flush (transport, NULL);
618 assert (thrift_compact_protocol_write_i32 (protocol, TEST_NI32, NULL) > 0);
619 thrift_transport_flush (transport, NULL);
620 assert (thrift_compact_protocol_write_i64 (protocol, TEST_NI64, NULL) > 0);
621 thrift_transport_flush (transport, NULL);
622 assert (thrift_compact_protocol_write_i16 (protocol, 2, NULL) > 0);
623 thrift_transport_flush (transport, NULL);
624 assert (thrift_compact_protocol_write_i32 (protocol, 2, NULL) > 0);
625 thrift_transport_flush (transport, NULL);
626 assert (thrift_compact_protocol_write_i64 (protocol, 2, NULL) > 0);
627 thrift_transport_flush (transport, NULL);
628 assert (thrift_compact_protocol_write_i16 (protocol, -2, NULL) > 0);
629 thrift_transport_flush (transport, NULL);
630 assert (thrift_compact_protocol_write_i32 (protocol, -2, NULL) > 0);
631 thrift_transport_flush (transport, NULL);
632 assert (thrift_compact_protocol_write_i64 (protocol, -2, NULL) > 0);
633 thrift_transport_flush (transport, NULL);
634 assert (thrift_compact_protocol_write_double (protocol,
635 TEST_DOUBLE, NULL) > 0);
636 thrift_transport_flush (transport, NULL);
637 assert (thrift_compact_protocol_write_string (protocol,
638 TEST_STRING, NULL) > 0);
639 thrift_transport_flush (transport, NULL);
Simon South30a8b652016-12-22 06:29:17 -0500640 assert (thrift_compact_protocol_write_string (protocol, "", NULL) > 0);
641 thrift_transport_flush (transport, NULL);
Chandler May1ccd81b2016-02-11 08:25:25 -0500642 assert (thrift_compact_protocol_write_binary (protocol, binary,
643 len, NULL) > 0);
644 thrift_transport_flush (transport, NULL);
645 assert (thrift_compact_protocol_write_binary (protocol, NULL,
646 0, NULL) > 0);
647 thrift_transport_flush (transport, NULL);
648 assert (thrift_compact_protocol_write_binary (protocol, binary,
649 len, NULL) > 0);
650 thrift_transport_flush (transport, NULL);
651
652 /* clean up */
653 thrift_transport_write_end (transport, NULL);
654 thrift_transport_close (transport, NULL);
655 g_object_unref (ft);
656 g_object_unref (tsocket);
657 g_object_unref (tc);
658 assert (wait (&status) == pid);
659 assert (status == 0);
660 }
661}
662
Chandler May6dde90b2016-01-10 06:01:10 +0000663
664static void
665thrift_server_primitives (const int port)
666{
667 ThriftServerTransport *transport = NULL;
668 ThriftTransport *client = NULL;
669 ThriftCompactProtocol *tc = NULL;
670 ThriftProtocol *protocol = NULL;
671 gboolean value_boolean = FALSE;
672 gint8 value_byte = 0,
673 zigzag_p16 = 0, zigzag_p32 = 0, zigzag_p64 = 0,
674 zigzag_n16 = 0, zigzag_n32 = 0, zigzag_n64 = 0;
675 gint16 value_16 = 0;
676 gint32 value_32 = 0;
677 gint64 value_64 = 0;
678 gint16 value_n16 = 0;
679 gint32 value_n32 = 0;
680 gint64 value_n64 = 0;
681 gdouble value_double = 0;
682 gchar *string = NULL;
Simon South30a8b652016-12-22 06:29:17 -0500683 gchar *empty_string = NULL;
Chandler May6dde90b2016-01-10 06:01:10 +0000684 gpointer binary = NULL;
685 guint32 len = 0;
686 void *comparator = (void *) TEST_STRING;
687
688 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
689 "port", port, NULL);
690 transport = THRIFT_SERVER_TRANSPORT (tsocket);
691 thrift_server_transport_listen (transport, NULL);
692 client = thrift_server_transport_accept (transport, NULL);
693 assert (client != NULL);
694
695 tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
696 client, NULL);
697 protocol = THRIFT_PROTOCOL (tc);
698
699 assert (thrift_compact_protocol_read_bool (protocol,
700 &value_boolean, NULL) > 0);
701 assert (thrift_compact_protocol_read_byte (protocol, &value_byte, NULL) > 0);
702 assert (thrift_compact_protocol_read_i16 (protocol, &value_16, NULL) > 0);
703 assert (thrift_compact_protocol_read_i32 (protocol, &value_32, NULL) > 0);
704 assert (thrift_compact_protocol_read_i64 (protocol, &value_64, NULL) > 0);
705 assert (thrift_compact_protocol_read_i16 (protocol, &value_n16, NULL) > 0);
706 assert (thrift_compact_protocol_read_i32 (protocol, &value_n32, NULL) > 0);
707 assert (thrift_compact_protocol_read_i64 (protocol, &value_n64, NULL) > 0);
708 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p16, NULL) > 0);
709 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p32, NULL) > 0);
710 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p64, NULL) > 0);
711 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n16, NULL) > 0);
712 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n32, NULL) > 0);
713 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n64, NULL) > 0);
714 assert (thrift_compact_protocol_read_double (protocol,
715 &value_double, NULL) > 0);
716 assert (thrift_compact_protocol_read_string (protocol, &string, NULL) > 0);
Simon South30a8b652016-12-22 06:29:17 -0500717 assert (thrift_compact_protocol_read_string (protocol, &empty_string,
718 NULL) > 0);
Chandler May6dde90b2016-01-10 06:01:10 +0000719 assert (thrift_compact_protocol_read_binary (protocol, &binary,
720 &len, NULL) > 0);
721
722 assert (value_boolean == TEST_BOOL);
723 assert (value_byte == TEST_BYTE);
724 assert (value_16 == TEST_I16);
725 assert (value_32 == TEST_I32);
726 assert (value_64 == TEST_I64);
727 assert (value_n16 == TEST_NI16);
728 assert (value_n32 == TEST_NI32);
729 assert (value_n64 == TEST_NI64);
730 assert (zigzag_p16 == 4);
731 assert (zigzag_p32 == 4);
732 assert (zigzag_p64 == 4);
733 assert (zigzag_n16 == 3);
734 assert (zigzag_n32 == 3);
735 assert (zigzag_n64 == 3);
736 assert (value_double == TEST_DOUBLE);
737 assert (strcmp (TEST_STRING, string) == 0);
Simon South30a8b652016-12-22 06:29:17 -0500738 assert (strcmp ("", empty_string) == 0);
Chandler May6dde90b2016-01-10 06:01:10 +0000739 assert (memcmp (comparator, binary, len) == 0);
740
741 g_free (string);
Simon South30a8b652016-12-22 06:29:17 -0500742 g_free (empty_string);
Chandler May6dde90b2016-01-10 06:01:10 +0000743 g_free (binary);
744
Simon South30a8b652016-12-22 06:29:17 -0500745 assert (thrift_compact_protocol_read_binary (protocol, &binary,
746 &len, NULL) > 0);
747 assert (binary == NULL);
748 assert (len == 0);
Chandler May6dde90b2016-01-10 06:01:10 +0000749 g_free (binary);
750
751 transport_read_count = 0;
752 transport_read_error_at = 0;
753 assert (thrift_compact_protocol_read_binary (protocol, &binary,
754 &len, NULL) == -1);
755 transport_read_error_at = -1;
756
757 transport_read_count = 0;
758 transport_read_error_at = 1;
759 assert (thrift_compact_protocol_read_binary (protocol, &binary,
760 &len, NULL) == -1);
761 transport_read_error_at = -1;
762
763 transport_read_error = 1;
764 assert (thrift_compact_protocol_read_bool (protocol,
765 &value_boolean, NULL) == -1);
766 assert (thrift_compact_protocol_read_byte (protocol,
767 &value_byte, NULL) == -1);
768 assert (thrift_compact_protocol_read_i16 (protocol,
769 &value_16, NULL) == -1);
770 assert (thrift_compact_protocol_read_i32 (protocol, &value_32, NULL) == -1);
771 assert (thrift_compact_protocol_read_i64 (protocol, &value_64, NULL) == -1);
772 assert (thrift_compact_protocol_read_i16 (protocol,
773 &value_n16, NULL) == -1);
774 assert (thrift_compact_protocol_read_i32 (protocol, &value_n32, NULL) == -1);
775 assert (thrift_compact_protocol_read_i64 (protocol, &value_n64, NULL) == -1);
776 assert (thrift_compact_protocol_read_double (protocol,
777 &value_double, NULL) == -1);
778 transport_read_error = 0;
779
780 /* test partial write failure */
781 thrift_protocol_read_i32 (protocol, &value_32, NULL);
782
783 thrift_transport_read_end (client, NULL);
784 thrift_transport_close (client, NULL);
785
786 g_object_unref (tc);
787 g_object_unref (client);
788 g_object_unref (tsocket);
789}
790
791static void
792thrift_server_complex_types (const int port)
793{
794 ThriftServerTransport *transport = NULL;
795 ThriftTransport *client = NULL;
796 ThriftCompactProtocol *tc = NULL;
797 ThriftProtocol *protocol = NULL;
798 gchar *struct_name = NULL;
799 gchar *field_name = NULL;
800 gchar *message_name = NULL;
801 ThriftType element_type, key_type, value_type, field_type;
802 ThriftMessageType message_type;
803 gboolean value_boolean = ! TEST_BOOL;
804 gint8 value = 0;
805 gint16 field_id = 0;
806 guint32 size = 0;
807 gint32 seqid = 0;
808 gint8 version_and_type = 0;
809 gint8 protocol_id = 0;
810
811 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
812 "port", port, NULL);
813 transport = THRIFT_SERVER_TRANSPORT (tsocket);
814 thrift_server_transport_listen (transport, NULL);
815 client = thrift_server_transport_accept (transport, NULL);
816 assert (client != NULL);
817
818 tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
819 client, NULL);
820 protocol = THRIFT_PROTOCOL (tc);
821
822 /* test struct operations */
823
824 thrift_compact_protocol_read_struct_begin (protocol, &struct_name, NULL);
825 thrift_compact_protocol_read_struct_end (protocol, NULL);
826
827 /* test field state w.r.t. deltas */
828
829 field_id = 0;
830 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
831 &field_type,
832 &field_id, NULL) == 1);
833 assert (field_id == 1);
834 field_id = 0;
835 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
836 &field_type,
837 &field_id, NULL) == 1);
838 assert (field_id == 16);
839 field_id = 0;
840 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
841 &field_type,
842 &field_id, NULL) == 1);
843 assert (field_id == 17);
844 field_id = 0;
845 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
846 &field_type,
847 &field_id, NULL) > 1);
848 assert (field_id == 15);
849 field_id = 0;
850 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
851 &field_type,
852 &field_id, NULL) == 1);
853 assert (field_id == 30);
854 field_id = 0;
855 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
856 &field_type,
857 &field_id, NULL) > 1);
858 assert (field_id == 46);
859 field_id = 0;
860 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
861 &field_type,
862 &field_id, NULL) == 1);
863 assert (field_id == 47);
864 field_id = 0;
865
866 /* test field operations */
867
868 thrift_compact_protocol_read_field_begin (protocol, &field_name, &field_type,
869 &field_id, NULL);
870 thrift_compact_protocol_read_field_end (protocol, NULL);
871
872 /* test field state w.r.t. structs */
873
874 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
875 &field_type,
876 &field_id, NULL) > 1);
877 assert (field_id == 1);
878 field_id = 0;
879 thrift_compact_protocol_read_field_end (protocol, NULL);
880 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
881 &field_type,
882 &field_id, NULL) == 1);
883 assert (field_id == 16);
884 field_id = 0;
885 thrift_compact_protocol_read_field_end (protocol, NULL);
886
887 assert (thrift_compact_protocol_read_struct_begin (protocol,
888 &struct_name, NULL) == 0);
889 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
890 &field_type,
891 &field_id, NULL) > 1);
892 assert (field_id == 17);
893 field_id = 0;
894 thrift_compact_protocol_read_field_end (protocol, NULL);
895
896 assert (thrift_compact_protocol_read_struct_begin (protocol,
897 &struct_name, NULL) == 0);
898 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
899 &field_type,
900 &field_id, NULL) > 1);
901 assert (field_id == 18);
902 field_id = 0;
903 thrift_compact_protocol_read_field_end (protocol, NULL);
904 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
905 &field_type,
906 &field_id, NULL) == 1);
907 assert (field_id == 19);
908 field_id = 0;
909 thrift_compact_protocol_read_field_end (protocol, NULL);
910 assert (thrift_compact_protocol_read_struct_end (protocol, NULL) == 0);
911
912 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
913 &field_type,
914 &field_id, NULL) == 1);
915 assert (field_id == 18);
916 field_id = 0;
917 thrift_compact_protocol_read_field_end (protocol, NULL);
918 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
919 &field_type,
920 &field_id, NULL) == 1);
921 assert (field_id == 25);
922 field_id = 0;
923 thrift_compact_protocol_read_field_end (protocol, NULL);
924 assert (thrift_compact_protocol_read_struct_end (protocol, NULL) == 0);
925
926 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
927 &field_type,
928 &field_id, NULL) == 1);
929 assert (field_id == 17);
930 field_id = 0;
931 thrift_compact_protocol_read_field_end (protocol, NULL);
932
933 /* test field state w.r.t. bools */
934
935 /* deltas */
936 /* non-bool field -> bool field -> non-bool field */
937 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
938 &field_type,
939 &field_id, NULL) == 1);
940 thrift_compact_protocol_read_field_end (protocol, NULL);
941 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
942 &field_type,
943 &field_id, NULL) == 1);
944 assert (field_type == T_BOOL);
945 assert (thrift_compact_protocol_read_bool (protocol,
946 &value_boolean, NULL) == 0);
947 assert (value_boolean == TEST_BOOL);
948 value_boolean = ! TEST_BOOL;
949 thrift_compact_protocol_read_field_end (protocol, NULL);
950 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
951 &field_type,
952 &field_id, NULL) == 1);
953 thrift_compact_protocol_read_field_end (protocol, NULL);
954 /* bool -> bool field -> bool */
955 assert (thrift_compact_protocol_read_bool (protocol,
956 &value_boolean, NULL) > 0);
957 assert (value_boolean == TEST_BOOL);
958 value_boolean = ! TEST_BOOL;
959 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
960 &field_type,
961 &field_id, NULL) == 1);
962 assert (field_type == T_BOOL);
963 assert (thrift_compact_protocol_read_bool (protocol,
964 &value_boolean, NULL) == 0);
965 assert (value_boolean == TEST_BOOL);
966 value_boolean = ! TEST_BOOL;
967 thrift_compact_protocol_read_field_end (protocol, NULL);
968 assert (thrift_compact_protocol_read_bool (protocol,
969 &value_boolean, NULL) > 0);
970 assert (value_boolean == TEST_BOOL);
971 value_boolean = ! TEST_BOOL;
972
973 /* no deltas */
974 /* non-bool field -> bool field -> non-bool field */
975 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
976 &field_type,
977 &field_id, NULL) > 1);
978 thrift_compact_protocol_read_field_end (protocol, NULL);
979 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
980 &field_type,
981 &field_id, NULL) > 1);
982 assert (field_type == T_BOOL);
983 assert (thrift_compact_protocol_read_bool (protocol,
984 &value_boolean, NULL) == 0);
985 assert (value_boolean == TEST_BOOL);
986 value_boolean = ! TEST_BOOL;
987 thrift_compact_protocol_read_field_end (protocol, NULL);
988 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
989 &field_type,
990 &field_id, NULL) > 1);
991 thrift_compact_protocol_read_field_end (protocol, NULL);
992 /* bool -> bool field -> bool */
993 assert (thrift_compact_protocol_read_bool (protocol,
994 &value_boolean, NULL) > 0);
995 assert (value_boolean == TEST_BOOL);
996 value_boolean = ! TEST_BOOL;
997 assert (thrift_compact_protocol_read_field_begin (protocol, &field_name,
998 &field_type,
999 &field_id, NULL) > 1);
1000 assert (field_type == T_BOOL);
1001 assert (thrift_compact_protocol_read_bool (protocol,
1002 &value_boolean, NULL) == 0);
1003 assert (value_boolean == TEST_BOOL);
1004 value_boolean = ! TEST_BOOL;
1005 thrift_compact_protocol_read_field_end (protocol, NULL);
1006 assert (thrift_compact_protocol_read_bool (protocol,
1007 &value_boolean, NULL) > 0);
1008 assert (value_boolean == TEST_BOOL);
1009 value_boolean = ! TEST_BOOL;
1010
1011 /* test first read error on a field */
1012 transport_read_error = 1;
1013 assert (thrift_compact_protocol_read_field_begin (protocol,
1014 &field_name, &field_type,
1015 &field_id, NULL) == -1);
1016 transport_read_error = 0;
1017
1018 /* test 2nd write failure */
1019 thrift_compact_protocol_read_byte (protocol, &value, NULL);
1020
1021 /* test 2nd read failure on a field */
1022 transport_read_count = 0;
1023 transport_read_error_at = 1;
1024 assert (thrift_compact_protocol_read_field_begin (protocol,
1025 &field_name, &field_type,
1026 &field_id, NULL) == -1);
1027 transport_read_error_at = -1;
1028
1029 /* test field stop */
1030 thrift_compact_protocol_read_field_begin (protocol, &field_name, &field_type,
1031 &field_id, NULL);
1032
1033 /* test map operations */
1034
1035 thrift_compact_protocol_read_map_begin (protocol, &key_type, &value_type,
1036 &size, NULL);
1037 thrift_compact_protocol_read_map_end (protocol, NULL);
1038
1039 /* test 1st read failure on a map */
1040 transport_read_count = 0;
1041 transport_read_error_at = 0;
1042 assert (thrift_compact_protocol_read_map_begin (protocol,
1043 &key_type, &value_type,
1044 &size, NULL) == -1);
1045 transport_read_error_at = -1;
1046
1047 /* test 2nd read failure on a map */
1048 transport_read_count = 0;
1049 transport_read_error_at = 1;
1050 assert (thrift_compact_protocol_read_map_begin (protocol,
1051 &key_type, &value_type,
1052 &size, NULL) == -1);
1053 transport_read_error_at = -1;
1054
1055 /* test 1st write failure on map---nothing to do on our side */
1056
1057 /* test 2nd write failure */
1058 thrift_compact_protocol_read_byte (protocol, &value, NULL);
1059
1060 /* test negative map size */
1061 assert (thrift_compact_protocol_read_map_begin (protocol,
1062 &key_type, &value_type,
1063 &size, NULL) == -1);
1064
1065 /* test list operations */
1066 thrift_compact_protocol_read_list_begin (protocol, &element_type, &size,
1067 NULL);
1068 thrift_compact_protocol_read_list_end (protocol, NULL);
1069
1070 /* test small list 1st read failure */
1071 transport_read_error = 1;
1072 assert (thrift_compact_protocol_read_list_begin (protocol, &element_type,
1073 &size, NULL) == -1);
1074 transport_read_error = 0;
1075
1076 /* test big list 1st read failure */
1077 transport_read_error = 1;
1078 assert (thrift_compact_protocol_read_list_begin (protocol, &element_type,
1079 &size, NULL) == -1);
1080 transport_read_error = 0;
1081
1082 /* test big list 2nd read failure */
1083 transport_read_count = 0;
1084 transport_read_error_at = 1;
1085 thrift_compact_protocol_read_list_begin (protocol, &element_type, &size,
1086 NULL);
1087 transport_read_error_at = -1;
1088
1089 /* test negative list size failure */
1090 thrift_compact_protocol_read_list_begin (protocol, &element_type, &size,
1091 NULL);
1092
1093 /* test small list 1st write failure---nothing to do on our end */
1094
1095 /* test big list 1st write failure---nothing to do on our end */
1096
1097 /* test big list 2nd write failure */
1098 thrift_compact_protocol_read_byte (protocol, &value, NULL);
1099
1100 /* test set operations */
1101 thrift_compact_protocol_read_set_begin (protocol, &element_type, &size, NULL);
1102 thrift_compact_protocol_read_set_end (protocol, NULL);
1103
1104 /* broken read */
1105 transport_read_error = 1;
1106 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1107 &message_type, &seqid,
1108 NULL) == -1);
1109 transport_read_error = 0;
1110
1111 /* invalid protocol */
1112 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1113 &message_type, &seqid,
1114 NULL) == -1);
1115
1116 /* invalid version */
1117 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1118 &message_type, &seqid,
1119 NULL) == -1);
1120
1121 /* read a valid message */
1122 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1123 &message_type, &seqid,
1124 NULL) > 0);
1125 g_free (message_name);
1126
1127 /* broken 2nd read on a message */
1128 transport_read_count = 0;
1129 transport_read_error_at = 1;
1130 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1131 &message_type, &seqid,
1132 NULL) == -1);
1133 transport_read_error_at = -1;
1134
1135 /* broken 3rd read on a message */
1136 transport_read_count = 0;
1137 transport_read_error_at = 2;
1138 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1139 &message_type, &seqid,
1140 NULL) == -1);
1141 transport_read_error_at = -1;
1142
1143 /* broken 4th read on a message */
1144 transport_read_count = 0;
1145 transport_read_error_at = 3;
1146 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1147 &message_type, &seqid,
1148 NULL) == -1);
1149 transport_read_error_at = -1;
1150
1151 /* read a valid message */
1152 assert (thrift_compact_protocol_read_message_begin (protocol, &message_name,
1153 &message_type, &seqid,
1154 NULL) > 0);
1155 g_free (message_name);
1156
1157 assert (thrift_compact_protocol_read_message_end (protocol, NULL) == 0);
1158
1159 /* handle 2nd write failure on a message */
1160 thrift_compact_protocol_read_byte (protocol, &protocol_id, NULL);
1161
1162 /* handle 3rd write failure on a message */
1163 thrift_compact_protocol_read_byte (protocol, &protocol_id, NULL);
1164 thrift_compact_protocol_read_byte (protocol, &version_and_type, NULL);
1165
1166 /* handle 4th write failure on a message */
1167 thrift_compact_protocol_read_byte (protocol, &protocol_id, NULL);
1168 thrift_compact_protocol_read_byte (protocol, &version_and_type, NULL);
1169 thrift_compact_protocol_read_varint32 (tc, &seqid, NULL);
1170
1171 g_object_unref (client);
1172 g_object_unref (tsocket);
1173}
1174
Chandler May1ccd81b2016-02-11 08:25:25 -05001175static void
1176thrift_server_many_frames (const int port)
1177{
1178 ThriftServerTransport *transport = NULL;
1179 ThriftTransport *client = NULL;
1180 ThriftCompactProtocol *tcp = NULL;
1181 ThriftProtocol *protocol = NULL;
1182 ThriftServerSocket *tsocket = NULL;
1183 gboolean value_boolean = FALSE;
1184 gint8 value_byte = 0,
1185 zigzag_p16 = 0, zigzag_p32 = 0, zigzag_p64 = 0,
1186 zigzag_n16 = 0, zigzag_n32 = 0, zigzag_n64 = 0;
1187 gint16 value_16 = 0;
1188 gint32 value_32 = 0;
1189 gint64 value_64 = 0;
1190 gint16 value_n16 = 0;
1191 gint32 value_n32 = 0;
1192 gint64 value_n64 = 0;
1193 gdouble value_double = 0;
1194 gchar *string = NULL;
Simon South30a8b652016-12-22 06:29:17 -05001195 gchar *empty_string = NULL;
Chandler May1ccd81b2016-02-11 08:25:25 -05001196 gpointer binary = NULL;
1197 guint32 len = 0;
1198 void *comparator = (void *) TEST_STRING;
1199
1200 tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", port, NULL);
1201 transport = THRIFT_SERVER_TRANSPORT (tsocket);
1202 thrift_server_transport_listen (transport, NULL);
1203
1204 /* wrap the client in a framed transport */
1205 client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
1206 thrift_server_transport_accept (transport, NULL),
1207 "r_buf_size", 1, NULL);
1208 assert (client != NULL);
1209
1210 tcp = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
1211 client, NULL);
1212 protocol = THRIFT_PROTOCOL (tcp);
1213
1214 assert (thrift_compact_protocol_read_bool (protocol,
1215 &value_boolean, NULL) > 0);
1216 assert (thrift_compact_protocol_read_byte (protocol, &value_byte, NULL) > 0);
1217 assert (thrift_compact_protocol_read_i16 (protocol, &value_16, NULL) > 0);
1218 assert (thrift_compact_protocol_read_i32 (protocol, &value_32, NULL) > 0);
1219 assert (thrift_compact_protocol_read_i64 (protocol, &value_64, NULL) > 0);
1220 assert (thrift_compact_protocol_read_i16 (protocol, &value_n16, NULL) > 0);
1221 assert (thrift_compact_protocol_read_i32 (protocol, &value_n32, NULL) > 0);
1222 assert (thrift_compact_protocol_read_i64 (protocol, &value_n64, NULL) > 0);
1223 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p16, NULL) > 0);
1224 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p32, NULL) > 0);
1225 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p64, NULL) > 0);
1226 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n16, NULL) > 0);
1227 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n32, NULL) > 0);
1228 assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n64, NULL) > 0);
1229 assert (thrift_compact_protocol_read_double (protocol,
1230 &value_double, NULL) > 0);
1231 assert (thrift_compact_protocol_read_string (protocol, &string, NULL) > 0);
Simon South30a8b652016-12-22 06:29:17 -05001232 assert (thrift_compact_protocol_read_string (protocol, &empty_string,
1233 NULL) > 0);
Chandler May1ccd81b2016-02-11 08:25:25 -05001234 assert (thrift_compact_protocol_read_binary (protocol, &binary,
1235 &len, NULL) > 0);
1236
1237 assert (value_boolean == TEST_BOOL);
1238 assert (value_byte == TEST_BYTE);
1239 assert (value_16 == TEST_I16);
1240 assert (value_32 == TEST_I32);
1241 assert (value_64 == TEST_I64);
1242 assert (value_n16 == TEST_NI16);
1243 assert (value_n32 == TEST_NI32);
1244 assert (value_n64 == TEST_NI64);
1245 assert (zigzag_p16 == 4);
1246 assert (zigzag_p32 == 4);
1247 assert (zigzag_p64 == 4);
1248 assert (zigzag_n16 == 3);
1249 assert (zigzag_n32 == 3);
1250 assert (zigzag_n64 == 3);
1251 assert (value_double == TEST_DOUBLE);
1252 assert (strcmp (TEST_STRING, string) == 0);
Simon South30a8b652016-12-22 06:29:17 -05001253 assert (strcmp ("", empty_string) == 0);
Chandler May1ccd81b2016-02-11 08:25:25 -05001254 assert (memcmp (comparator, binary, len) == 0);
1255
1256 g_free (string);
Simon South30a8b652016-12-22 06:29:17 -05001257 g_free (empty_string);
1258 g_free (binary);
1259
1260 assert (thrift_compact_protocol_read_binary (protocol, &binary,
1261 &len, NULL) > 0);
1262 assert (binary == NULL);
1263 assert (len == 0);
Chandler May1ccd81b2016-02-11 08:25:25 -05001264 g_free (binary);
1265
1266 thrift_transport_read_end (client, NULL);
1267 thrift_transport_close (client, NULL);
1268
1269 g_object_unref (tcp);
1270 g_object_unref (client);
1271 g_object_unref (tsocket);
1272}
1273
Chandler May6dde90b2016-01-10 06:01:10 +00001274int
1275main (int argc, char *argv[])
1276{
1277#if (!GLIB_CHECK_VERSION (2, 36, 0))
1278 g_type_init ();
1279#endif
1280
1281 g_test_init (&argc, &argv, NULL);
1282
1283 g_test_add_func ("/testcompactprotocol/CreateAndDestroy",
1284 test_create_and_destroy);
1285 g_test_add_func ("/testcompactprotocol/Initialize", test_initialize);
1286 g_test_add_func ("/testcompactprotocol/ReadAndWritePrimitives",
1287 test_read_and_write_primitives);
1288 g_test_add_func ("/testcompactprotocol/ReadAndWriteComplexTypes",
1289 test_read_and_write_complex_types);
Chandler May1ccd81b2016-02-11 08:25:25 -05001290 g_test_add_func ("/testcompactprotocol/ReadAndWriteManyFrames",
1291 test_read_and_write_many_frames);
Chandler May6dde90b2016-01-10 06:01:10 +00001292
1293 return g_test_run ();
1294}