blob: 9713e8c86efc365e8093679ba9ed73adc0b61c29 [file] [log] [blame]
Roger Meierb3c84092014-09-01 21:53:40 +02001/*
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#include <glib-object.h>
21#include <inttypes.h>
22#include <signal.h>
23#include <stdio.h>
24#include <string.h>
25
26#include <sys/time.h>
27
28#include <thrift/c_glib/thrift.h>
29#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
Chandler May6dde90b2016-01-10 06:01:10 +000030#include <thrift/c_glib/protocol/thrift_compact_protocol.h>
Roger Meierb3c84092014-09-01 21:53:40 +020031#include <thrift/c_glib/transport/thrift_buffered_transport.h>
32#include <thrift/c_glib/transport/thrift_framed_transport.h>
James E. King, III36628a22017-02-13 15:25:41 -050033#include <thrift/c_glib/transport/thrift_ssl_socket.h>
Roger Meierb3c84092014-09-01 21:53:40 +020034#include <thrift/c_glib/transport/thrift_socket.h>
35#include <thrift/c_glib/transport/thrift_transport.h>
36
37#include "../gen-c_glib/t_test_thrift_test.h"
38
39/* Handle SIGPIPE signals (indicating the server has closed the
40 connection prematurely) by outputting an error message before
41 exiting. */
42static void
Roger Meier15df0762014-09-29 20:50:56 +020043sigpipe_handler (int signal_number)
44{
Roger Meierb3c84092014-09-01 21:53:40 +020045 THRIFT_UNUSED_VAR (signal_number);
46
47 /* Flush standard output to make sure the test results so far are
48 logged */
49 fflush (stdout);
50
51 fputs ("Broken pipe (server closed connection prematurely)\n", stderr);
52 fflush (stderr);
53
54 /* Re-raise the signal, this time invoking the default signal
55 handler, to terminate the program */
56 raise (SIGPIPE);
57}
58
59/* Compare two gint32 values. Used for sorting and finding integer
60 values within a GList. */
61static gint
Roger Meier15df0762014-09-29 20:50:56 +020062gint32_compare (gconstpointer a, gconstpointer b)
63{
Roger Meierb3c84092014-09-01 21:53:40 +020064 gint32 int32_a = *(gint32 *)a;
65 gint32 int32_b = *(gint32 *)b;
66 int result = 0;
67
68 if (int32_a < int32_b)
69 result = -1;
70 else if (int32_a > int32_b)
71 result = 1;
72
73 return result;
74}
75
76int
Roger Meier15df0762014-09-29 20:50:56 +020077main (int argc, char **argv)
78{
James E. King, III36628a22017-02-13 15:25:41 -050079 static gchar * host = NULL;
80 static gint port = 9090;
81 static gboolean ssl = FALSE;
82 static gchar * transport_option = NULL;
83 static gchar * protocol_option = NULL;
84 static gint num_tests = 1;
Roger Meierb3c84092014-09-01 21:53:40 +020085
86 static
87 GOptionEntry option_entries[] ={
James E. King, III36628a22017-02-13 15:25:41 -050088 { "host", 'h', 0, G_OPTION_ARG_STRING, &host,
Roger Meierb3c84092014-09-01 21:53:40 +020089 "Host to connect (=localhost)", NULL },
James E. King, III36628a22017-02-13 15:25:41 -050090 { "port", 'p', 0, G_OPTION_ARG_INT, &port,
Roger Meierb3c84092014-09-01 21:53:40 +020091 "Port number to connect (=9090)", NULL },
James E. King, III36628a22017-02-13 15:25:41 -050092 { "ssl", 's', 0, G_OPTION_ARG_NONE, &ssl,
93 "Enable SSL", NULL },
94 { "transport", 't', 0, G_OPTION_ARG_STRING, &transport_option,
Roger Meierb3c84092014-09-01 21:53:40 +020095 "Transport: buffered, framed (=buffered)", NULL },
James E. King, III36628a22017-02-13 15:25:41 -050096 { "protocol", 'r', 0, G_OPTION_ARG_STRING, &protocol_option,
Chandler May6dde90b2016-01-10 06:01:10 +000097 "Protocol: binary, compact (=binary)", NULL },
James E. King, III36628a22017-02-13 15:25:41 -050098 { "testloops", 'n', 0, G_OPTION_ARG_INT, &num_tests,
Roger Meierb3c84092014-09-01 21:53:40 +020099 "Number of tests (=1)", NULL },
100 { NULL }
101 };
102
103 struct sigaction sigpipe_action;
104
James E. King, III36628a22017-02-13 15:25:41 -0500105 GType socket_type = THRIFT_TYPE_SOCKET;
106 gchar *socket_name = "ip";
Roger Meierb3c84092014-09-01 21:53:40 +0200107 GType transport_type = THRIFT_TYPE_BUFFERED_TRANSPORT;
108 gchar *transport_name = "buffered";
109 GType protocol_type = THRIFT_TYPE_BINARY_PROTOCOL;
110 gchar *protocol_name = "binary";
111
112 ThriftSocket *socket;
113 ThriftTransport *transport;
114 ThriftProtocol *protocol;
115
116 TTestThriftTestIf *test_client;
117
118 struct timeval time_start, time_stop, time_elapsed;
119 guint64 time_elapsed_usec, time_total_usec = 0;
120 guint64 time_min_usec = G_MAXUINT64, time_max_usec = 0, time_avg_usec;
121
122 GOptionContext *option_context;
123 gboolean options_valid = TRUE;
124 int test_num = 0;
125 int fail_count = 0;
126 GError *error = NULL;
127
Roger Meier15df0762014-09-29 20:50:56 +0200128#if (!GLIB_CHECK_VERSION (2, 36, 0))
129 g_type_init ();
130#endif
131
Roger Meierb3c84092014-09-01 21:53:40 +0200132 /* Configure and parse our command-line options */
133 option_context = g_option_context_new (NULL);
134 g_option_context_add_main_entries (option_context,
135 option_entries,
136 NULL);
Roger Meier15df0762014-09-29 20:50:56 +0200137 if (!g_option_context_parse (option_context,
138 &argc,
139 &argv,
140 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200141 fprintf (stderr, "%s\n", error->message);
142 return 255;
143 }
144 g_option_context_free (option_context);
145
146 /* Set remaining default values for unspecified options */
147 if (host == NULL)
148 host = g_strdup ("localhost");
149
150 /* Validate the parsed options */
Chandler May6dde90b2016-01-10 06:01:10 +0000151 if (protocol_option != NULL) {
152 if (strncmp (protocol_option, "compact", 8) == 0) {
153 protocol_type = THRIFT_TYPE_COMPACT_PROTOCOL;
154 protocol_name = "compact";
155 }
156 else if (strncmp (protocol_option, "binary", 7) != 0) {
157 fprintf (stderr, "Unknown protocol type %s\n", protocol_option);
158 options_valid = FALSE;
159 }
Roger Meierb3c84092014-09-01 21:53:40 +0200160 }
161
162 if (transport_option != NULL) {
163 if (strncmp (transport_option, "framed", 7) == 0) {
164 transport_type = THRIFT_TYPE_FRAMED_TRANSPORT;
165 transport_name = "framed";
166 }
167 else if (strncmp (transport_option, "buffered", 9) != 0) {
168 fprintf (stderr, "Unknown transport type %s\n", transport_option);
169 options_valid = FALSE;
170 }
171 }
172
James E. King, III36628a22017-02-13 15:25:41 -0500173 if (ssl) {
174 socket_type = THRIFT_TYPE_SSL_SOCKET;
175 socket_name = "ip-ssl";
176 printf("Type name %s\n", g_type_name (socket_type));
177 }
178
Roger Meier15df0762014-09-29 20:50:56 +0200179 if (!options_valid)
Roger Meierb3c84092014-09-01 21:53:40 +0200180 return 254;
181
James E. King, III36628a22017-02-13 15:25:41 -0500182 printf ("Connecting (%s/%s) to: %s/%s:%d\n",
Roger Meierb3c84092014-09-01 21:53:40 +0200183 transport_name,
184 protocol_name,
James E. King, III36628a22017-02-13 15:25:41 -0500185 socket_name,
Roger Meierb3c84092014-09-01 21:53:40 +0200186 host,
187 port);
188
189 /* Install our SIGPIPE handler, which outputs an error message to
190 standard error before exiting so testers can know what
191 happened */
192 memset (&sigpipe_action, 0, sizeof (sigpipe_action));
193 sigpipe_action.sa_handler = sigpipe_handler;
194 sigpipe_action.sa_flags = SA_RESETHAND;
195 sigaction (SIGPIPE, &sigpipe_action, NULL);
196
James E. King, III36628a22017-02-13 15:25:41 -0500197 if (ssl) {
198 thrift_ssl_socket_initialize_openssl();
199 }
200
Roger Meierb3c84092014-09-01 21:53:40 +0200201 /* Establish all our connection objects */
James E. King, III36628a22017-02-13 15:25:41 -0500202 socket = g_object_new (socket_type,
Roger Meierb3c84092014-09-01 21:53:40 +0200203 "hostname", host,
204 "port", port,
205 NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500206
207 if (ssl && !thrift_ssl_load_cert_from_file(THRIFT_SSL_SOCKET(socket), "../keys/CA.pem")) {
208 fprintf(stderr, "Unable to load validation certificate ../keys/CA.pem - did you run in the test/c_glib directory?\n");
209 g_object_unref (socket);
210 return 253;
211 }
212
Roger Meierb3c84092014-09-01 21:53:40 +0200213 transport = g_object_new (transport_type,
214 "transport", socket,
215 NULL);
216 protocol = g_object_new (protocol_type,
217 "transport", transport,
218 NULL);
219 test_client = g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT,
220 "input_protocol", protocol,
221 "output_protocol", protocol,
222 NULL);
223
224 /* Execute the actual tests */
225 for (test_num = 0; test_num < num_tests; ++test_num) {
Roger Meier15df0762014-09-29 20:50:56 +0200226 if (thrift_transport_open (transport, &error)) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900227 gchar *string = NULL;
228 gboolean boolean = 0;
229 gint8 byte = 0;
230 gint32 int32 = 0;
231 gint64 int64 = 0;
232 gdouble dub = 0;
Roger Meierb3c84092014-09-01 21:53:40 +0200233
234 gint byte_thing, i32_thing, inner_byte_thing, inner_i32_thing;
235 gint64 i64_thing, inner_i64_thing;
236
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900237 TTestXtruct *xtruct_out, *xtruct_out2, *xtruct_in, *inner_xtruct_in;
Roger Meierb3c84092014-09-01 21:53:40 +0200238 TTestXtruct2 *xtruct2_out, *xtruct2_in;
239
240 GHashTable *map_out, *map_in, *inner_map_in;
241 GHashTable *set_out, *set_in;
242 gpointer key, value;
243 gint32 *i32_key_ptr, *i32_value_ptr;
244 GHashTableIter hash_table_iter, inner_hash_table_iter;
245 GList *keys_out, *keys_in, *keys_elem;
246
247 GArray *list_out, *list_in;
248
249 TTestNumberz numberz;
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900250 TTestNumberz numberz2;
Roger Meierb3c84092014-09-01 21:53:40 +0200251
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900252 TTestUserId user_id, *user_id_ptr, *user_id_ptr2;
Roger Meierb3c84092014-09-01 21:53:40 +0200253
254 TTestInsanity *insanity_out, *insanity_in;
255 GHashTable *user_map;
256 GHashTableIter user_map_iter;
257 GPtrArray *xtructs;
258
259 TTestXception *xception = NULL;
260 TTestXception2 *xception2 = NULL;
261
262 gboolean oneway_result;
263 struct timeval oneway_start, oneway_end, oneway_elapsed;
264 gint oneway_elapsed_usec;
265
266 gboolean first;
267 gint32 i, j;
268
269 printf ("Test #%d, connect %s:%d\n", test_num + 1, host, port);
270 gettimeofday (&time_start, NULL);
271
272 /* These test routines have been ported from the C++ test
273 client, care being taken to ensure their output remains as
274 close as possible to the original to facilitate diffs.
275
276 For simplicity comments have been omitted, but every routine
277 has the same basic structure:
278
279 - Create and populate data structures as necessary.
280
281 - Format and output (to the console) a representation of the
282 outgoing data.
283
284 - Issue the remote method call to the server.
285
286 - Format and output a representation of the returned data.
287
288 - Verify the returned data matches what was expected.
289
290 - Deallocate any created data structures.
291
292 Note the recognized values and expected behaviour of each
293 remote method are described in ThriftTest.thrift, which
294 you'll find in the top-level "test" folder. */
295
296 /**
297 * VOID TEST
298 */
299 printf ("testVoid()");
Roger Meier15df0762014-09-29 20:50:56 +0200300 if (t_test_thrift_test_if_test_void (test_client, &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200301 printf (" = void\n");
302 }
303 else {
James E. King, III36628a22017-02-13 15:25:41 -0500304 if(error!=NULL){
Roger Meierb3c84092014-09-01 21:53:40 +0200305 printf ("%s\n", error->message);
James E. King, III36628a22017-02-13 15:25:41 -0500306 g_error_free (error);
307 error = NULL;
308 }
Roger Meierb3c84092014-09-01 21:53:40 +0200309 fail_count++;
310 }
311
312 /**
313 * STRING TEST
314 */
315 printf ("testString(\"Test\")");
316 if (t_test_thrift_test_if_test_string (test_client,
317 &string,
318 "Test",
Roger Meier15df0762014-09-29 20:50:56 +0200319 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200320 printf (" = \"%s\"\n", string);
321 if (strncmp (string, "Test", 5) != 0)
322 fail_count++;
323
324 g_free (string);
325 string = NULL;
326 }
327 else {
328 printf ("%s\n", error->message);
329 g_error_free (error);
330 error = NULL;
331
332 fail_count++;
333 }
334
335 /**
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900336 * BOOL TEST
337 */
338 printf ("testByte(true)");
339 if (t_test_thrift_test_if_test_bool (test_client,
340 &boolean,
341 1,
342 &error)) {
343 printf (" = %s\n", boolean ? "true" : "false");
344 if (boolean != 1)
345 fail_count++;
346 }
347 else {
348 printf ("%s\n", error->message);
349 g_error_free (error);
350 error = NULL;
351
352 fail_count++;
353 }
354 printf ("testByte(false)");
355 if (t_test_thrift_test_if_test_bool (test_client,
356 &boolean,
357 0,
358 &error)) {
359 printf (" = %s\n", boolean ? "true" : "false");
360 if (boolean != 0)
361 fail_count++;
362 }
363 else {
364 printf ("%s\n", error->message);
365 g_error_free (error);
366 error = NULL;
367
368 fail_count++;
369 }
370
371 /**
Roger Meierb3c84092014-09-01 21:53:40 +0200372 * BYTE TEST
373 */
374 printf ("testByte(1)");
375 if (t_test_thrift_test_if_test_byte (test_client,
376 &byte,
377 1,
Roger Meier15df0762014-09-29 20:50:56 +0200378 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200379 printf (" = %d\n", byte);
380 if (byte != 1)
381 fail_count++;
382 }
383 else {
384 printf ("%s\n", error->message);
385 g_error_free (error);
386 error = NULL;
387
388 fail_count++;
389 }
Nobuaki Sukegawa30f465d2015-10-10 10:45:42 +0900390 printf ("testByte(-1)");
391 if (t_test_thrift_test_if_test_byte (test_client,
392 &byte,
393 -1,
394 &error)) {
395 printf (" = %d\n", byte);
396 if (byte != -1)
397 fail_count++;
398 }
399 else {
400 printf ("%s\n", error->message);
401 g_error_free (error);
402 error = NULL;
403
404 fail_count++;
405 }
Roger Meierb3c84092014-09-01 21:53:40 +0200406
407 /**
408 * I32 TEST
409 */
410 printf ("testI32(-1)");
411 if (t_test_thrift_test_if_test_i32 (test_client,
412 &int32,
413 -1,
Roger Meier15df0762014-09-29 20:50:56 +0200414 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200415 printf (" = %d\n", int32);
416 if (int32 != -1)
417 fail_count++;
418 }
419 else {
420 printf ("%s\n", error->message);
421 g_error_free (error);
422 error = NULL;
423
424 fail_count++;
425 }
426
427 /**
428 * I64 TEST
429 */
430 printf ("testI64(-34359738368)");
431 if (t_test_thrift_test_if_test_i64 (test_client,
432 &int64,
433 (gint64)-34359738368,
Roger Meier15df0762014-09-29 20:50:56 +0200434 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200435 printf (" = %" PRId64 "\n", int64);
436 if (int64 != (gint64)-34359738368)
437 fail_count++;
438 }
439 else {
440 printf ("%s\n", error->message);
441 g_error_free (error);
442 error = NULL;
443
444 fail_count++;
445 }
446
447 /**
448 * DOUBLE TEST
449 */
450 printf("testDouble(-5.2098523)");
451 if (t_test_thrift_test_if_test_double (test_client,
452 &dub,
453 -5.2098523,
Roger Meier15df0762014-09-29 20:50:56 +0200454 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200455 printf (" = %f\n", dub);
456 if ((dub - (-5.2098523)) > 0.001)
457 fail_count++;
458 }
459 else {
460 printf ("%s\n", error->message);
461 g_error_free (error);
462 error = NULL;
463
464 fail_count++;
465 }
466
James E. King, III36628a22017-02-13 15:25:41 -0500467 // TODO: add testBinary()
468
Roger Meierb3c84092014-09-01 21:53:40 +0200469 /**
470 * STRUCT TEST
471 */
472 printf ("testStruct({\"Zero\", 1, -3, -5})");
473 xtruct_out = g_object_new (T_TEST_TYPE_XTRUCT,
474 "string_thing", "Zero",
475 "byte_thing", 1,
476 "i32_thing", -3,
477 "i64_thing", -5LL,
478 NULL);
479 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
480
481 if (t_test_thrift_test_if_test_struct (test_client,
482 &xtruct_in,
483 xtruct_out,
Roger Meier15df0762014-09-29 20:50:56 +0200484 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200485 g_object_get (xtruct_in,
486 "string_thing", &string,
487 "byte_thing", &byte_thing,
488 "i32_thing", &i32_thing,
489 "i64_thing", &i64_thing,
490 NULL);
491
492 printf (" = {\"%s\", %d, %d, %" PRId64 "}\n",
493 string,
494 byte_thing,
495 i32_thing,
496 i64_thing);
497 if ((string == NULL || strncmp (string, "Zero", 5) != 0) ||
498 byte_thing != 1 ||
499 i32_thing != -3 ||
500 i64_thing != (gint64)-5)
501 fail_count++;
502 }
503 else {
504 printf ("%s\n", error->message);
505 g_error_free (error);
506 error = NULL;
507
508 fail_count++;
509 }
510 g_object_unref (xtruct_in);
511
512 /**
513 * NESTED STRUCT TEST
514 */
515 printf ("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
516 xtruct2_out = g_object_new (T_TEST_TYPE_XTRUCT2,
517 "byte_thing", 1,
518 "struct_thing", xtruct_out,
519 "i32_thing", 5,
520 NULL);
521 xtruct2_in = g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
522
523 if (t_test_thrift_test_if_test_nest (test_client,
524 &xtruct2_in,
525 xtruct2_out,
Roger Meier15df0762014-09-29 20:50:56 +0200526 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200527 g_object_get (xtruct2_in,
528 "byte_thing", &byte_thing,
529 "struct_thing", &xtruct_in,
530 "i32_thing", &i32_thing,
531 NULL);
532 g_object_get (xtruct_in,
533 "string_thing", &string,
534 "byte_thing", &inner_byte_thing,
535 "i32_thing", &inner_i32_thing,
536 "i64_thing", &inner_i64_thing,
537 NULL);
538
539 printf (" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n",
540 byte_thing,
541 string,
542 inner_byte_thing,
543 inner_i32_thing,
544 inner_i64_thing,
545 i32_thing);
546 if (byte_thing != 1 ||
547 (string == NULL || strncmp (string, "Zero", 5) != 0) ||
548 inner_byte_thing != 1 ||
549 inner_i32_thing != -3 ||
550 inner_i64_thing != (gint64)-5 ||
551 i32_thing != 5)
552 fail_count++;
553 }
554 else {
555 printf ("%s\n", error->message);
556 g_error_free (error);
557 error = NULL;
558
559 fail_count++;
560 }
561
562 g_object_unref (xtruct_in);
563 g_object_unref (xtruct2_in);
564 g_object_unref (xtruct2_out);
565 g_object_unref (xtruct_out);
566
567 /**
568 * MAP TEST
569 */
570 map_out = g_hash_table_new_full (g_int_hash,
571 g_int_equal,
572 g_free,
573 g_free);
574 for (i = 0; i < 5; ++i) {
575 i32_key_ptr = g_malloc (sizeof *i32_key_ptr);
576 i32_value_ptr = g_malloc (sizeof *i32_value_ptr);
577
578 *i32_key_ptr = i;
579 *i32_value_ptr = i - 10;
580
581 g_hash_table_insert (map_out, i32_key_ptr, i32_value_ptr);
582 }
583 printf ("testMap({");
584 first = TRUE;
585 g_hash_table_iter_init (&hash_table_iter, map_out);
586 while (g_hash_table_iter_next (&hash_table_iter,
587 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200588 &value)) {
589 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200590 first = FALSE;
591 else
592 printf (", ");
593
594 printf ("%d => %d", *(gint32 *)key, *(gint32 *)value);
595 }
596 printf ("})");
597
598 map_in = g_hash_table_new_full (g_int_hash,
599 g_int_equal,
600 g_free,
601 g_free);
602
603 if (t_test_thrift_test_if_test_map (test_client,
604 &map_in,
605 map_out,
Roger Meier15df0762014-09-29 20:50:56 +0200606 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200607 printf (" = {");
608 first = TRUE;
609 g_hash_table_iter_init (&hash_table_iter, map_in);
610 while (g_hash_table_iter_next (&hash_table_iter,
611 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200612 &value)) {
613 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200614 first = FALSE;
615 else
616 printf (", ");
617
618 printf ("%d => %d", *(gint32 *)key, *(gint32 *)value);
619 }
620 printf ("}\n");
621
622 if (g_hash_table_size (map_in) != g_hash_table_size (map_out))
623 fail_count++;
624 else {
625 g_hash_table_iter_init (&hash_table_iter, map_out);
626 while (g_hash_table_iter_next (&hash_table_iter,
627 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200628 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200629 gpointer in_value = g_hash_table_lookup (map_in, key);
630 if (in_value == NULL ||
631 *(gint32 *)in_value != *(gint32 *)value) {
632 fail_count++;
633 break;
634 }
635 }
636 }
637 }
638 else {
639 printf ("%s\n", error->message);
640 g_error_free (error);
641 error = NULL;
642
643 fail_count++;
644 }
645
646 g_hash_table_unref (map_in);
647 g_hash_table_unref (map_out);
648
649 /**
650 * STRING MAP TEST
651 */
652 map_out = g_hash_table_new_full (g_str_hash,
653 g_str_equal,
654 NULL,
655 NULL);
656 g_hash_table_insert (map_out, "a", "2");
657 g_hash_table_insert (map_out, "b", "blah");
658 g_hash_table_insert (map_out, "some", "thing");
659 printf ("testStringMap({");
660 first = TRUE;
661 g_hash_table_iter_init (&hash_table_iter, map_out);
662 while (g_hash_table_iter_next (&hash_table_iter,
663 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200664 &value)) {
665 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200666 first = FALSE;
667 else
668 printf (", ");
669
670 printf ("\"%s\" => \"%s\"", (gchar *)key, (gchar *)value);
671 }
672 printf (")}");
673
674 map_in = g_hash_table_new_full (g_str_hash,
675 g_str_equal,
676 g_free,
677 g_free);
678
679 if (t_test_thrift_test_if_test_string_map (test_client,
680 &map_in,
681 map_out,
Roger Meier15df0762014-09-29 20:50:56 +0200682 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200683 printf (" = {");
684 first = TRUE;
685 g_hash_table_iter_init (&hash_table_iter, map_in);
686 while (g_hash_table_iter_next (&hash_table_iter,
687 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200688 &value)) {
689 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200690 first = FALSE;
691 else
692 printf (", ");
693
694 printf ("\"%s\" => \"%s\"", (gchar *)key, (gchar *)value);
695 }
696 printf ("}\n");
697
698 if (g_hash_table_size (map_in) != g_hash_table_size (map_out))
699 fail_count++;
700 else {
701 g_hash_table_iter_init (&hash_table_iter, map_out);
702 while (g_hash_table_iter_next (&hash_table_iter,
703 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200704 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200705 gpointer in_value = g_hash_table_lookup (map_in, key);
706 if (in_value == NULL ||
707 strcmp ((gchar *)in_value, (gchar *)value) != 0) {
708 fail_count++;
709 break;
710 }
711 }
712 }
713 }
714 else {
715 printf ("%s\n", error->message);
716 g_error_free (error);
717 error = NULL;
718
719 fail_count++;
720 }
721
722 g_hash_table_unref (map_in);
723 g_hash_table_unref (map_out);
724
725 /**
726 * SET TEST
727 */
728 set_out = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL);
729 for (i = -2; i < 3; ++i) {
730 i32_key_ptr = g_malloc (sizeof *i32_key_ptr);
731 *i32_key_ptr = i;
732
733 g_hash_table_insert (set_out, i32_key_ptr, NULL);
734 }
735 printf ("testSet({");
736 first = TRUE;
737 keys_out = g_hash_table_get_keys (set_out);
738 keys_elem = keys_out;
739 while (keys_elem != NULL) {
Roger Meier15df0762014-09-29 20:50:56 +0200740 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200741 first = FALSE;
742 else
743 printf (", ");
744
745 printf ("%d", *(gint32 *)keys_elem->data);
746
747 keys_elem = keys_elem->next;
748 }
749 printf ("})");
750
751 set_in = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL);
752
753 if (t_test_thrift_test_if_test_set (test_client,
754 &set_in,
755 set_out,
Roger Meier15df0762014-09-29 20:50:56 +0200756 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200757 printf(" = {");
758 first = TRUE;
759 keys_in = g_hash_table_get_keys (set_in);
760 keys_elem = keys_in;
761 while (keys_elem != NULL) {
Roger Meier15df0762014-09-29 20:50:56 +0200762 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200763 first = FALSE;
764 else
765 printf (", ");
766
767 printf ("%d", *(gint32 *)keys_elem->data);
768
769 keys_elem = keys_elem->next;
770 }
771 printf ("}\n");
772
773 if (g_list_length (keys_in) != g_list_length (keys_out))
774 fail_count++;
775 else {
776 keys_elem = keys_out;
777 while (keys_elem != NULL) {
778 if (g_list_find_custom (keys_in,
779 keys_elem->data,
780 gint32_compare) == NULL) {
781 fail_count++;
782 break;
783 }
784
785 keys_elem = keys_elem->next;
786 }
787 }
788
789 g_list_free (keys_in);
790 }
791 else {
792 printf ("%s\n", error->message);
793 g_error_free (error);
794 error = NULL;
795
796 fail_count++;
797 }
798
799 g_hash_table_unref (set_in);
800 g_list_free (keys_out);
801 g_hash_table_unref (set_out);
802
803 /**
804 * LIST TEST
805 */
806 list_out = g_array_new (FALSE, TRUE, sizeof (gint32));
807 for (i = -2; i < 3; ++i) {
808 g_array_append_val (list_out, i);
809 }
810 printf ("testList({");
811 first = TRUE;
812 for (i = 0; i < (gint32)list_out->len; ++i) {
Roger Meier15df0762014-09-29 20:50:56 +0200813 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200814 first = FALSE;
815 else
816 printf (", ");
817
818 printf ("%d", g_array_index (list_out, gint32, i));
819 }
820 printf ("})");
821
822 list_in = g_array_new (FALSE, TRUE, sizeof (gint32));
823
824 if (t_test_thrift_test_if_test_list (test_client,
825 &list_in,
826 list_out,
Roger Meier15df0762014-09-29 20:50:56 +0200827 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200828 printf (" = {");
829 first = TRUE;
830 for (i = 0; i < (gint32)list_in->len; ++i) {
Roger Meier15df0762014-09-29 20:50:56 +0200831 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200832 first = FALSE;
833 else
834 printf (", ");
835
836 printf ("%d", g_array_index (list_in, gint32, i));
837 }
838 printf ("}\n");
839
840 if (list_in->len != list_out->len ||
841 memcmp (list_in->data,
842 list_out->data,
843 list_in->len * sizeof (gint32)) != 0)
844 fail_count++;
845 }
846 else {
847 printf ("%s\n", error->message);
848 g_error_free (error);
849 error = NULL;
850
851 fail_count++;
852 }
853
854 g_array_unref (list_in);
855 g_array_unref (list_out);
856
857 /**
858 * ENUM TEST
859 */
860 printf("testEnum(ONE)");
861 if (t_test_thrift_test_if_test_enum (test_client,
862 &numberz,
863 T_TEST_NUMBERZ_ONE,
Roger Meier15df0762014-09-29 20:50:56 +0200864 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200865 printf(" = %d\n", numberz);
866 if (numberz != T_TEST_NUMBERZ_ONE)
867 fail_count++;
868 }
869 else {
870 printf ("%s\n", error->message);
871 g_error_free (error);
872 error = NULL;
873
874 fail_count++;
875 }
876
877 printf("testEnum(TWO)");
878 if (t_test_thrift_test_if_test_enum (test_client,
879 &numberz,
880 T_TEST_NUMBERZ_TWO,
Roger Meier15df0762014-09-29 20:50:56 +0200881 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200882 printf(" = %d\n", numberz);
883 if (numberz != T_TEST_NUMBERZ_TWO)
884 fail_count++;
885 }
886 else {
887 printf ("%s\n", error->message);
888 g_error_free (error);
889 error = NULL;
890
891 fail_count++;
892 }
893
894 printf("testEnum(THREE)");
895 if (t_test_thrift_test_if_test_enum (test_client,
896 &numberz,
897 T_TEST_NUMBERZ_THREE,
Roger Meier15df0762014-09-29 20:50:56 +0200898 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200899 printf(" = %d\n", numberz);
900 if (numberz != T_TEST_NUMBERZ_THREE)
901 fail_count++;
902 }
903 else {
904 printf ("%s\n", error->message);
905 g_error_free (error);
906 error = NULL;
907
908 fail_count++;
909 }
910
911 printf("testEnum(FIVE)");
912 if (t_test_thrift_test_if_test_enum (test_client,
913 &numberz,
914 T_TEST_NUMBERZ_FIVE,
Roger Meier15df0762014-09-29 20:50:56 +0200915 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200916 printf(" = %d\n", numberz);
917 if (numberz != T_TEST_NUMBERZ_FIVE)
918 fail_count++;
919 }
920 else {
921 printf ("%s\n", error->message);
922 g_error_free (error);
923 error = NULL;
924
925 fail_count++;
926 }
927
928 printf("testEnum(EIGHT)");
929 if (t_test_thrift_test_if_test_enum (test_client,
930 &numberz,
931 T_TEST_NUMBERZ_EIGHT,
Roger Meier15df0762014-09-29 20:50:56 +0200932 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200933 printf(" = %d\n", numberz);
934 if (numberz != T_TEST_NUMBERZ_EIGHT)
935 fail_count++;
936 }
937 else {
938 printf ("%s\n", error->message);
939 g_error_free (error);
940 error = NULL;
941
942 fail_count++;
943 }
944
945 /**
946 * TYPEDEF TEST
947 */
948 printf ("testTypedef(309858235082523)");
949 if (t_test_thrift_test_if_test_typedef (test_client,
950 &user_id,
951 309858235082523LL,
Roger Meier15df0762014-09-29 20:50:56 +0200952 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200953 printf(" = %" PRId64 "\n", user_id);
954 if (user_id != 309858235082523LL)
955 fail_count++;
956 }
957 else {
958 printf ("%s\n", error->message);
959 g_error_free (error);
960 error = NULL;
961
962 fail_count++;
963 }
964
965 /**
966 * NESTED MAP TEST
967 */
968 printf ("testMapMap(1)");
969 map_in = g_hash_table_new_full (g_int_hash,
970 g_int_equal,
971 g_free,
972 (GDestroyNotify)g_hash_table_unref);
973 if (t_test_thrift_test_if_test_map_map (test_client,
974 &map_in,
975 1,
Roger Meier15df0762014-09-29 20:50:56 +0200976 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200977 g_hash_table_iter_init (&hash_table_iter, map_in);
978
979 printf (" = {");
980 while (g_hash_table_iter_next (&hash_table_iter,
981 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200982 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200983 printf ("%d => {", *(gint32 *)key);
984
985 g_hash_table_iter_init (&inner_hash_table_iter,
986 (GHashTable *)value);
987 while (g_hash_table_iter_next (&inner_hash_table_iter,
988 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200989 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200990 printf ("%d => %d, ", *(gint32 *)key, *(gint32 *)value);
991 }
992
993 printf ("}, ");
994 }
995 printf ("}\n");
996
997 if (g_hash_table_size (map_in) != 2)
998 fail_count++;
999 else {
1000 gint32 inner_keys[] = {1, 2, 3, 4};
1001 gint32 i32_key;
1002
1003 i32_key = -4;
1004 inner_map_in = g_hash_table_lookup (map_in, &i32_key);
1005 if (inner_map_in == NULL ||
1006 g_hash_table_size (inner_map_in) != 4)
1007 fail_count++;
1008 else {
1009 keys_in = g_hash_table_get_keys (inner_map_in);
1010 keys_in = g_list_sort (keys_in, gint32_compare);
1011
1012 for (i = 0; i < 4; i++) {
1013 keys_elem = g_list_nth (keys_in, 3 - i);
1014
1015 if (*(gint32 *)keys_elem->data != (-1 * inner_keys[i]) ||
1016 *(gint32 *)g_hash_table_lookup (inner_map_in,
1017 keys_elem->data) !=
1018 (-1 * inner_keys[i])) {
1019 fail_count++;
1020 break;
1021 }
1022 }
1023
1024 g_list_free (keys_in);
1025 }
1026
1027 i32_key = 4;
1028 inner_map_in = g_hash_table_lookup (map_in, &i32_key);
1029 if (inner_map_in == NULL ||
1030 g_hash_table_size (inner_map_in) != 4)
1031 fail_count++;
1032 else {
1033 keys_in = g_hash_table_get_keys (inner_map_in);
1034 keys_in = g_list_sort (keys_in, gint32_compare);
1035
1036 for (i = 0; i < 4; i++) {
1037 keys_elem = g_list_nth (keys_in, i);
1038
1039 if (*(gint32 *)keys_elem->data != inner_keys[i] ||
1040 *(gint32 *)g_hash_table_lookup (inner_map_in,
1041 keys_elem->data) !=
1042 inner_keys[i]) {
1043 fail_count++;
1044 break;
1045 }
1046 }
1047
1048 g_list_free (keys_in);
1049 }
1050 }
1051 }
1052 else {
1053 printf ("%s\n", error->message);
1054 g_error_free (error);
1055 error = NULL;
1056
1057 fail_count++;
1058 }
1059
1060 g_hash_table_unref (map_in);
1061
1062 /**
1063 * INSANITY TEST
1064 */
1065 insanity_out = g_object_new (T_TEST_TYPE_INSANITY, NULL);
1066 g_object_get (insanity_out,
1067 "userMap", &user_map,
1068 "xtructs", &xtructs,
1069 NULL);
1070
1071 numberz = T_TEST_NUMBERZ_FIVE;
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001072 numberz2 = T_TEST_NUMBERZ_EIGHT;
Roger Meierb3c84092014-09-01 21:53:40 +02001073 user_id_ptr = g_malloc (sizeof *user_id_ptr);
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001074 *user_id_ptr = 5;
1075 user_id_ptr2 = g_malloc (sizeof *user_id_ptr);
1076 *user_id_ptr2 = 8;
Roger Meierb3c84092014-09-01 21:53:40 +02001077 g_hash_table_insert (user_map, (gpointer)numberz, user_id_ptr);
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001078 g_hash_table_insert (user_map, (gpointer)numberz2, user_id_ptr2);
Roger Meierb3c84092014-09-01 21:53:40 +02001079 g_hash_table_unref (user_map);
1080
1081 xtruct_out = g_object_new (T_TEST_TYPE_XTRUCT,
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001082 "string_thing", "Hello2",
1083 "byte_thing", 2,
1084 "i32_thing", 2,
1085 "i64_thing", 2LL,
Roger Meierb3c84092014-09-01 21:53:40 +02001086 NULL);
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001087 xtruct_out2 = g_object_new (T_TEST_TYPE_XTRUCT,
1088 "string_thing", "Goodbye4",
1089 "byte_thing", 4,
1090 "i32_thing", 4,
1091 "i64_thing", 4LL,
1092 NULL);
1093 g_ptr_array_add (xtructs, xtruct_out2);
Roger Meierb3c84092014-09-01 21:53:40 +02001094 g_ptr_array_add (xtructs, xtruct_out);
1095 g_ptr_array_unref (xtructs);
1096
1097 map_in = g_hash_table_new_full (g_int64_hash,
1098 g_int64_equal,
1099 g_free,
1100 (GDestroyNotify)g_hash_table_unref);
1101
1102 printf("testInsanity()");
1103 if (t_test_thrift_test_if_test_insanity (test_client,
1104 &map_in,
1105 insanity_out,
Roger Meier15df0762014-09-29 20:50:56 +02001106 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001107 printf (" = {");
1108 g_hash_table_iter_init (&hash_table_iter, map_in);
1109 while (g_hash_table_iter_next (&hash_table_iter,
1110 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001111 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001112 printf ("%" PRId64 " => {", *(TTestUserId *)key);
1113
1114 g_hash_table_iter_init (&inner_hash_table_iter,
1115 (GHashTable *)value);
1116 while (g_hash_table_iter_next (&inner_hash_table_iter,
1117 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001118 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001119 printf ("%d => {", (TTestNumberz)key);
1120
1121 g_object_get ((TTestInsanity *)value,
1122 "userMap", &user_map,
1123 "xtructs", &xtructs,
1124 NULL);
1125
1126 printf ("{");
1127 g_hash_table_iter_init (&user_map_iter, user_map);
1128 while (g_hash_table_iter_next (&user_map_iter,
1129 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001130 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001131 printf ("%d => %" PRId64 ", ",
1132 (TTestNumberz)key,
1133 *(TTestUserId *)value);
1134 }
1135 printf ("}, ");
1136 g_hash_table_unref (user_map);
1137
1138 printf("{");
1139 for (i = 0; i < (gint32)xtructs->len; ++i) {
1140 xtruct_in = g_ptr_array_index (xtructs, i);
1141 g_object_get (xtruct_in,
1142 "string_thing", &string,
1143 "byte_thing", &byte_thing,
1144 "i32_thing", &i32_thing,
1145 "i64_thing", &i64_thing,
1146 NULL);
1147
1148 printf ("{\"%s\", %d, %d, %" PRId64 "}, ",
1149 string,
1150 byte_thing,
1151 i32_thing,
1152 i64_thing);
1153 }
1154 printf ("}");
1155 g_ptr_array_unref (xtructs);
1156
1157 printf ("}, ");
1158 }
1159 printf("}, ");
1160 }
1161 printf("}\n");
1162
1163 if (g_hash_table_size (map_in) != 2)
1164 fail_count++;
1165 else {
1166 TTestNumberz numberz_key_values[] = {
1167 T_TEST_NUMBERZ_TWO, T_TEST_NUMBERZ_THREE
1168 };
1169 gint user_map_values[] = { 5, 8 };
1170 TTestUserId user_id_key;
1171
1172 user_id_key = 1;
1173 inner_map_in = g_hash_table_lookup (map_in, &user_id_key);
1174 if (inner_map_in == NULL ||
1175 g_hash_table_size (inner_map_in) != 2)
1176 fail_count++;
1177 else {
1178 TTestNumberz numberz_key;
1179
1180 for (i = 0; i < 2; ++i) {
1181 numberz_key = numberz_key_values[i];
1182 insanity_in =
1183 g_hash_table_lookup (inner_map_in,
1184 (gconstpointer)numberz_key);
1185 if (insanity_in == NULL)
1186 fail_count++;
1187 else {
1188 g_object_get (insanity_in,
1189 "userMap", &user_map,
1190 "xtructs", &xtructs,
1191 NULL);
1192
1193 if (user_map == NULL)
1194 fail_count++;
1195 else {
1196 if (g_hash_table_size (user_map) != 2)
1197 fail_count++;
1198 else {
1199 for (j = 0; j < 2; ++j) {
1200 numberz_key = (TTestNumberz)user_map_values[j];
1201
1202 value =
1203 g_hash_table_lookup (user_map,
1204 (gconstpointer)numberz_key);
1205 if (value == NULL ||
1206 *(TTestUserId *)value != (TTestUserId)user_map_values[j])
1207 fail_count++;
1208 }
1209 }
1210
1211 g_hash_table_unref (user_map);
1212 }
1213
1214 if (xtructs == NULL)
1215 fail_count++;
1216 else {
1217 if (xtructs->len != 2)
1218 fail_count++;
1219 else {
1220 xtruct_in = g_ptr_array_index (xtructs, 0);
1221 g_object_get (xtruct_in,
1222 "string_thing", &string,
1223 "byte_thing", &byte_thing,
1224 "i32_thing", &i32_thing,
1225 "i64_thing", &i64_thing,
1226 NULL);
1227 if ((string == NULL ||
1228 strncmp (string, "Goodbye4", 9) != 0) ||
1229 byte_thing != 4 ||
1230 i32_thing != 4 ||
1231 i64_thing != 4)
1232 fail_count++;
1233
1234 if (string != NULL)
1235 g_free (string);
1236
1237 xtruct_in = g_ptr_array_index (xtructs, 1);
1238 g_object_get (xtruct_in,
1239 "string_thing", &string,
1240 "byte_thing", &byte_thing,
1241 "i32_thing", &i32_thing,
1242 "i64_thing", &i64_thing,
1243 NULL);
1244 if ((string == NULL ||
1245 strncmp (string, "Hello2", 7) != 0) ||
1246 byte_thing != 2 ||
1247 i32_thing != 2 ||
1248 i64_thing != 2)
1249 fail_count++;
1250
1251 if (string != NULL)
1252 g_free (string);
1253 }
1254
1255 g_ptr_array_unref (xtructs);
1256 }
1257 }
1258 }
1259 }
1260
1261 user_id_key = 2;
1262 inner_map_in = g_hash_table_lookup (map_in, &user_id_key);
1263 if (inner_map_in == NULL ||
1264 g_hash_table_size (inner_map_in) != 1)
1265 fail_count++;
1266 else {
1267 insanity_in =
1268 g_hash_table_lookup (inner_map_in,
1269 (gconstpointer)T_TEST_NUMBERZ_SIX);
1270 if (insanity_in == NULL)
1271 fail_count++;
1272 else {
1273 g_object_get (insanity_in,
1274 "userMap", &user_map,
1275 "xtructs", &xtructs,
1276 NULL);
1277
1278 if (user_map == NULL)
1279 fail_count++;
1280 else {
1281 if (g_hash_table_size (user_map) != 0)
1282 fail_count++;
1283
1284 g_hash_table_unref (user_map);
1285 }
1286
1287 if (xtructs == NULL)
1288 fail_count++;
1289 else {
1290 if (xtructs->len != 0)
1291 fail_count++;
1292
1293 g_ptr_array_unref (xtructs);
1294 }
1295 }
1296 }
1297 }
1298 }
1299 else {
1300 printf ("%s\n", error->message);
1301 g_error_free (error);
1302 error = NULL;
1303
1304 fail_count++;
1305 }
1306
1307 g_hash_table_unref (map_in);
1308 g_object_unref (insanity_out);
1309
1310 /* test exception */
1311 printf ("testClient.testException(\"Xception\") =>");
Roger Meier15df0762014-09-29 20:50:56 +02001312 if (!t_test_thrift_test_if_test_exception (test_client,
1313 "Xception",
1314 &xception,
1315 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001316 xception != NULL) {
1317 g_object_get (xception,
1318 "errorCode", &int32,
1319 "message", &string,
1320 NULL);
1321 printf (" {%u, \"%s\"}\n", int32, string);
1322 g_free (string);
1323
1324 g_object_unref (xception);
1325 xception = NULL;
1326
1327 g_error_free (error);
1328 error = NULL;
1329 }
1330 else {
1331 printf (" void\nFAILURE\n");
1332 fail_count++;
1333
1334 if (xception != NULL) {
1335 g_object_unref (xception);
1336 xception = NULL;
1337 }
1338
1339 if (error != NULL) {
1340 g_error_free (error);
1341 error = NULL;
1342 }
1343 }
1344
1345 printf ("testClient.testException(\"TException\") =>");
Roger Meier15df0762014-09-29 20:50:56 +02001346 if (!t_test_thrift_test_if_test_exception (test_client,
1347 "TException",
1348 &xception,
1349 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001350 xception == NULL &&
1351 error != NULL) {
1352 printf (" Caught TException\n");
1353
1354 g_error_free (error);
1355 error = NULL;
1356 }
1357 else {
1358 printf (" void\nFAILURE\n");
1359 fail_count++;
1360
1361 if (xception != NULL) {
1362 g_object_unref (xception);
1363 xception = NULL;
1364 }
1365
1366 if (error != NULL) {
1367 g_error_free (error);
1368 error = NULL;
1369 }
1370 }
1371
1372 printf ("testClient.testException(\"success\") =>");
1373 if (t_test_thrift_test_if_test_exception (test_client,
1374 "success",
1375 &xception,
Roger Meier15df0762014-09-29 20:50:56 +02001376 &error))
Roger Meierb3c84092014-09-01 21:53:40 +02001377 printf (" void\n");
1378 else {
1379 printf (" void\nFAILURE\n");
1380 fail_count++;
1381
1382 if (xception != NULL) {
1383 g_object_unref (xception);
1384 xception = NULL;
1385 }
1386
1387 g_error_free (error);
1388 error = NULL;
1389 }
1390
1391 g_assert (error == NULL);
1392
1393 /* test multi exception */
1394 printf ("testClient.testMultiException(\"Xception\", \"test 1\") =>");
1395 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier15df0762014-09-29 20:50:56 +02001396 if (!t_test_thrift_test_if_test_multi_exception (test_client,
1397 &xtruct_in,
1398 "Xception",
1399 "test 1",
1400 &xception,
1401 &xception2,
1402 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001403 xception != NULL &&
1404 xception2 == NULL) {
1405 g_object_get (xception,
1406 "errorCode", &int32,
1407 "message", &string,
1408 NULL);
1409 printf (" {%u, \"%s\"}\n", int32, string);
1410 g_free (string);
1411
1412 g_object_unref (xception);
1413 xception = NULL;
1414
1415 g_error_free (error);
1416 error = NULL;
1417 }
1418 else {
1419 printf (" result\nFAILURE\n");
1420 fail_count++;
1421
1422 if (xception != NULL) {
1423 g_object_unref (xception);
1424 xception = NULL;
1425 }
1426
1427 if (xception2 != NULL) {
1428 g_object_unref (xception2);
1429 xception = NULL;
1430 }
1431
1432 if (error != NULL) {
1433 g_error_free (error);
1434 error = NULL;
1435 }
1436 }
1437 g_object_unref (xtruct_in);
1438
1439 printf ("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
1440 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier15df0762014-09-29 20:50:56 +02001441 if (!t_test_thrift_test_if_test_multi_exception (test_client,
1442 &xtruct_in,
1443 "Xception2",
1444 "test 2",
1445 &xception,
1446 &xception2,
1447 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001448 xception == NULL &&
1449 xception2 != NULL) {
1450 g_object_get (xception2,
1451 "errorCode", &int32,
1452 "struct_thing", &inner_xtruct_in,
1453 NULL);
1454 g_object_get (inner_xtruct_in,
1455 "string_thing", &string,
1456 NULL);
1457 printf (" {%u, {\"%s\"}}\n", int32, string);
1458 g_free (string);
1459
1460 g_object_unref (inner_xtruct_in);
1461 inner_xtruct_in = NULL;
1462
1463 g_object_unref (xception2);
1464 xception2 = NULL;
1465
1466 g_error_free (error);
1467 error = NULL;
1468 }
1469 else {
1470 printf (" result\nFAILURE\n");
1471 fail_count++;
1472
1473 if (xception != NULL) {
1474 g_object_unref (xception);
1475 xception = NULL;
1476 }
1477
1478 if (xception2 != NULL) {
1479 g_object_unref (xception2);
1480 xception = NULL;
1481 }
1482
1483 if (error != NULL) {
1484 g_error_free (error);
1485 error = NULL;
1486 }
1487 }
1488 g_object_unref (xtruct_in);
1489
1490 printf ("testClient.testMultiException(\"success\", \"test 3\") =>");
1491 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
1492 if (t_test_thrift_test_if_test_multi_exception (test_client,
1493 &xtruct_in,
1494 "success",
1495 "test 3",
1496 &xception,
1497 &xception2,
Roger Meier15df0762014-09-29 20:50:56 +02001498 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001499 xception == NULL &&
1500 xception2 == NULL) {
1501 g_object_get (xtruct_in,
1502 "string_thing", &string,
1503 NULL);
1504 printf (" {{\"%s\"}}\n", string);
1505 g_free (string);
1506 }
1507 else {
1508 printf (" result\nFAILURE\n");
1509 fail_count++;
1510
1511 if (xception != NULL) {
1512 g_object_unref (xception);
1513 xception = NULL;
1514 }
1515
1516 if (xception2 != NULL) {
1517 g_object_unref (xception2);
1518 xception = NULL;
1519 }
1520
1521 if (error != NULL) {
1522 g_error_free (error);
1523 error = NULL;
1524 }
1525 }
1526 g_object_unref (xtruct_in);
1527
1528 /* test oneway void */
1529 printf ("testClient.testOneway(1) =>");
1530 gettimeofday (&oneway_start, NULL);
1531 oneway_result = t_test_thrift_test_if_test_oneway (test_client,
1532 1,
1533 &error);
1534 gettimeofday (&oneway_end, NULL);
1535 timersub (&oneway_end, &oneway_start, &oneway_elapsed);
1536 oneway_elapsed_usec =
1537 oneway_elapsed.tv_sec * 1000 * 1000 + oneway_elapsed.tv_usec;
1538
Roger Meier15df0762014-09-29 20:50:56 +02001539 if (oneway_result) {
Roger Meierb3c84092014-09-01 21:53:40 +02001540 if (oneway_elapsed_usec > 200 * 1000) {
1541 printf (" FAILURE - took %.2f ms\n",
1542 (double)oneway_elapsed_usec / 1000.0);
1543 fail_count++;
1544 }
1545 else
1546 printf (" success - took %.2f ms\n",
1547 (double)oneway_elapsed_usec / 1000.0);
1548 }
1549 else {
1550 printf ("%s\n", error->message);
1551 g_error_free (error);
1552 error = NULL;
1553
1554 fail_count++;
1555 }
1556
1557 /**
1558 * redo a simple test after the oneway to make sure we aren't "off by
1559 * one" -- if the server treated oneway void like normal void, this next
1560 * test will fail since it will get the void confirmation rather than
1561 * the correct result. In this circumstance, the client will receive the
1562 * error:
1563 *
1564 * application error: Wrong method name
1565 */
1566 /**
1567 * I32 TEST
1568 */
1569 printf ("re-test testI32(-1)");
1570 if (t_test_thrift_test_if_test_i32 (test_client,
1571 &int32,
1572 -1,
Roger Meier15df0762014-09-29 20:50:56 +02001573 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001574 printf (" = %d\n", int32);
1575 if (int32 != -1)
1576 fail_count++;
1577 }
1578 else {
1579 printf ("%s\n", error->message);
1580 g_error_free (error);
1581 error = NULL;
1582
1583 fail_count++;
1584 }
1585
1586 gettimeofday (&time_stop, NULL);
1587 timersub (&time_stop, &time_start, &time_elapsed);
1588 time_elapsed_usec =
1589 time_elapsed.tv_sec * 1000 * 1000 + time_elapsed.tv_usec;
1590
1591 printf("Total time: %" PRIu64 " us\n", time_elapsed_usec);
1592
1593 time_total_usec += time_elapsed_usec;
1594 if (time_elapsed_usec < time_min_usec)
1595 time_min_usec = time_elapsed_usec;
1596 if (time_elapsed_usec > time_max_usec)
1597 time_max_usec = time_elapsed_usec;
1598
1599 thrift_transport_close (transport, &error);
1600 }
1601 else {
1602 printf ("Connect failed: %s\n", error->message);
James E. King, III36628a22017-02-13 15:25:41 -05001603 g_object_unref (socket);
Roger Meierb3c84092014-09-01 21:53:40 +02001604 g_error_free (error);
1605 error = NULL;
1606
1607 return 1;
1608 }
1609 }
1610
1611 /* All done---output statistics */
1612 puts ("\nAll tests done.");
1613
1614 time_avg_usec = time_total_usec / num_tests;
1615
1616 printf ("Min time: %" PRIu64 " us\n", time_min_usec);
1617 printf ("Max time: %" PRIu64 " us\n", time_max_usec);
1618 printf ("Avg time: %" PRIu64 " us\n", time_avg_usec);
1619
1620 g_object_unref (test_client);
1621 g_object_unref (protocol);
1622 g_object_unref (transport);
1623 g_free (host);
1624
James E. King, III36628a22017-02-13 15:25:41 -05001625 if (ssl) {
1626 thrift_ssl_socket_finalize_openssl();
1627 }
1628
Roger Meierb3c84092014-09-01 21:53:40 +02001629 return fail_count;
1630}