blob: a6ef86969a303a87f62c721e0db396d493e3a2f3 [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>
Gonzalo Aguilar Delgadobc0082e2016-03-04 13:16:22 +010031#include <thrift/c_glib/protocol/thrift_multiplexed_protocol.h>
Roger Meierb3c84092014-09-01 21:53:40 +020032#include <thrift/c_glib/transport/thrift_buffered_transport.h>
33#include <thrift/c_glib/transport/thrift_framed_transport.h>
James E. King, III36628a22017-02-13 15:25:41 -050034#include <thrift/c_glib/transport/thrift_ssl_socket.h>
Roger Meierb3c84092014-09-01 21:53:40 +020035#include <thrift/c_glib/transport/thrift_socket.h>
36#include <thrift/c_glib/transport/thrift_transport.h>
37
38#include "../gen-c_glib/t_test_thrift_test.h"
39
40/* Handle SIGPIPE signals (indicating the server has closed the
41 connection prematurely) by outputting an error message before
42 exiting. */
43static void
Roger Meier15df0762014-09-29 20:50:56 +020044sigpipe_handler (int signal_number)
45{
Roger Meierb3c84092014-09-01 21:53:40 +020046 THRIFT_UNUSED_VAR (signal_number);
47
48 /* Flush standard output to make sure the test results so far are
49 logged */
50 fflush (stdout);
51
52 fputs ("Broken pipe (server closed connection prematurely)\n", stderr);
53 fflush (stderr);
54
55 /* Re-raise the signal, this time invoking the default signal
56 handler, to terminate the program */
57 raise (SIGPIPE);
58}
59
60/* Compare two gint32 values. Used for sorting and finding integer
61 values within a GList. */
62static gint
Roger Meier15df0762014-09-29 20:50:56 +020063gint32_compare (gconstpointer a, gconstpointer b)
64{
Roger Meierb3c84092014-09-01 21:53:40 +020065 gint32 int32_a = *(gint32 *)a;
66 gint32 int32_b = *(gint32 *)b;
67 int result = 0;
68
69 if (int32_a < int32_b)
70 result = -1;
71 else if (int32_a > int32_b)
72 result = 1;
73
74 return result;
75}
76
Gonzalo Aguilar Delgadobc0082e2016-03-04 13:16:22 +010077/**
78 * It gets a multiplexed protocol which uses binary underneath
79 * @param transport the underlying transport
80 * @param service_name the single supported service name
81 * @todo need to allow multiple services to fully test multiplexed
82 * @return a multiplexed protocol wrapping the correct underlying protocol
83 */
84ThriftProtocol *
85get_multiplexed_protocol(ThriftTransport *transport, gchar *service_name)
86{
87 ThriftProtocol * result_protocol=NULL;
88 ThriftProtocol * multiplexed_protocol=NULL;
89
90 multiplexed_protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
91 "transport", transport,
92 NULL);
93
94 result_protocol = g_object_new (THRIFT_TYPE_MULTIPLEXED_PROTOCOL,
95 "transport", transport,
96 "protocol", multiplexed_protocol,
97 "service-name", service_name,
98 NULL);
99
100 return result_protocol;
101}
102
Roger Meierb3c84092014-09-01 21:53:40 +0200103int
Roger Meier15df0762014-09-29 20:50:56 +0200104main (int argc, char **argv)
105{
James E. King, III36628a22017-02-13 15:25:41 -0500106 static gchar * host = NULL;
107 static gint port = 9090;
108 static gboolean ssl = FALSE;
109 static gchar * transport_option = NULL;
110 static gchar * protocol_option = NULL;
111 static gint num_tests = 1;
Roger Meierb3c84092014-09-01 21:53:40 +0200112
113 static
114 GOptionEntry option_entries[] ={
James E. King, III36628a22017-02-13 15:25:41 -0500115 { "host", 'h', 0, G_OPTION_ARG_STRING, &host,
Roger Meierb3c84092014-09-01 21:53:40 +0200116 "Host to connect (=localhost)", NULL },
James E. King, III36628a22017-02-13 15:25:41 -0500117 { "port", 'p', 0, G_OPTION_ARG_INT, &port,
Roger Meierb3c84092014-09-01 21:53:40 +0200118 "Port number to connect (=9090)", NULL },
James E. King, III36628a22017-02-13 15:25:41 -0500119 { "ssl", 's', 0, G_OPTION_ARG_NONE, &ssl,
120 "Enable SSL", NULL },
121 { "transport", 't', 0, G_OPTION_ARG_STRING, &transport_option,
Roger Meierb3c84092014-09-01 21:53:40 +0200122 "Transport: buffered, framed (=buffered)", NULL },
James E. King, III36628a22017-02-13 15:25:41 -0500123 { "protocol", 'r', 0, G_OPTION_ARG_STRING, &protocol_option,
Gonzalo Aguilar Delgadobc0082e2016-03-04 13:16:22 +0100124 "Protocol: binary, compact, multiplexed (=binary)", NULL },
James E. King, III36628a22017-02-13 15:25:41 -0500125 { "testloops", 'n', 0, G_OPTION_ARG_INT, &num_tests,
Roger Meierb3c84092014-09-01 21:53:40 +0200126 "Number of tests (=1)", NULL },
127 { NULL }
128 };
129
130 struct sigaction sigpipe_action;
131
James E. King, III36628a22017-02-13 15:25:41 -0500132 GType socket_type = THRIFT_TYPE_SOCKET;
133 gchar *socket_name = "ip";
Roger Meierb3c84092014-09-01 21:53:40 +0200134 GType transport_type = THRIFT_TYPE_BUFFERED_TRANSPORT;
135 gchar *transport_name = "buffered";
136 GType protocol_type = THRIFT_TYPE_BINARY_PROTOCOL;
137 gchar *protocol_name = "binary";
138
139 ThriftSocket *socket;
140 ThriftTransport *transport;
141 ThriftProtocol *protocol;
142
143 TTestThriftTestIf *test_client;
144
145 struct timeval time_start, time_stop, time_elapsed;
146 guint64 time_elapsed_usec, time_total_usec = 0;
147 guint64 time_min_usec = G_MAXUINT64, time_max_usec = 0, time_avg_usec;
148
149 GOptionContext *option_context;
150 gboolean options_valid = TRUE;
151 int test_num = 0;
152 int fail_count = 0;
153 GError *error = NULL;
154
Roger Meier15df0762014-09-29 20:50:56 +0200155#if (!GLIB_CHECK_VERSION (2, 36, 0))
156 g_type_init ();
157#endif
158
Roger Meierb3c84092014-09-01 21:53:40 +0200159 /* Configure and parse our command-line options */
160 option_context = g_option_context_new (NULL);
161 g_option_context_add_main_entries (option_context,
162 option_entries,
163 NULL);
Roger Meier15df0762014-09-29 20:50:56 +0200164 if (!g_option_context_parse (option_context,
165 &argc,
166 &argv,
167 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200168 fprintf (stderr, "%s\n", error->message);
169 return 255;
170 }
171 g_option_context_free (option_context);
172
173 /* Set remaining default values for unspecified options */
174 if (host == NULL)
175 host = g_strdup ("localhost");
176
177 /* Validate the parsed options */
Chandler May6dde90b2016-01-10 06:01:10 +0000178 if (protocol_option != NULL) {
179 if (strncmp (protocol_option, "compact", 8) == 0) {
180 protocol_type = THRIFT_TYPE_COMPACT_PROTOCOL;
181 protocol_name = "compact";
182 }
Gonzalo Aguilar Delgadobc0082e2016-03-04 13:16:22 +0100183 else if (strncmp (protocol_option, "multiplexed", 12) == 0) {
184 protocol_type = THRIFT_TYPE_MULTIPLEXED_PROTOCOL;
185 protocol_name = "multiplexed(binary)";
186 }
187 else if (strncmp (protocol_option, "binary", 7) == 0) {
188 printf("We are going with default binary protocol");
189 }
190 else {
Chandler May6dde90b2016-01-10 06:01:10 +0000191 fprintf (stderr, "Unknown protocol type %s\n", protocol_option);
192 options_valid = FALSE;
193 }
Roger Meierb3c84092014-09-01 21:53:40 +0200194 }
195
196 if (transport_option != NULL) {
197 if (strncmp (transport_option, "framed", 7) == 0) {
198 transport_type = THRIFT_TYPE_FRAMED_TRANSPORT;
199 transport_name = "framed";
200 }
201 else if (strncmp (transport_option, "buffered", 9) != 0) {
202 fprintf (stderr, "Unknown transport type %s\n", transport_option);
203 options_valid = FALSE;
204 }
205 }
206
James E. King, III36628a22017-02-13 15:25:41 -0500207 if (ssl) {
208 socket_type = THRIFT_TYPE_SSL_SOCKET;
209 socket_name = "ip-ssl";
210 printf("Type name %s\n", g_type_name (socket_type));
211 }
212
Roger Meier15df0762014-09-29 20:50:56 +0200213 if (!options_valid)
Roger Meierb3c84092014-09-01 21:53:40 +0200214 return 254;
215
James E. King, III36628a22017-02-13 15:25:41 -0500216 printf ("Connecting (%s/%s) to: %s/%s:%d\n",
Roger Meierb3c84092014-09-01 21:53:40 +0200217 transport_name,
218 protocol_name,
James E. King, III36628a22017-02-13 15:25:41 -0500219 socket_name,
Roger Meierb3c84092014-09-01 21:53:40 +0200220 host,
221 port);
222
223 /* Install our SIGPIPE handler, which outputs an error message to
224 standard error before exiting so testers can know what
225 happened */
226 memset (&sigpipe_action, 0, sizeof (sigpipe_action));
227 sigpipe_action.sa_handler = sigpipe_handler;
228 sigpipe_action.sa_flags = SA_RESETHAND;
229 sigaction (SIGPIPE, &sigpipe_action, NULL);
230
James E. King, III36628a22017-02-13 15:25:41 -0500231 if (ssl) {
232 thrift_ssl_socket_initialize_openssl();
233 }
234
Roger Meierb3c84092014-09-01 21:53:40 +0200235 /* Establish all our connection objects */
James E. King, III36628a22017-02-13 15:25:41 -0500236 socket = g_object_new (socket_type,
Roger Meierb3c84092014-09-01 21:53:40 +0200237 "hostname", host,
238 "port", port,
239 NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500240
241 if (ssl && !thrift_ssl_load_cert_from_file(THRIFT_SSL_SOCKET(socket), "../keys/CA.pem")) {
242 fprintf(stderr, "Unable to load validation certificate ../keys/CA.pem - did you run in the test/c_glib directory?\n");
243 g_object_unref (socket);
244 return 253;
245 }
246
Roger Meierb3c84092014-09-01 21:53:40 +0200247 transport = g_object_new (transport_type,
248 "transport", socket,
249 NULL);
Gonzalo Aguilar Delgadobc0082e2016-03-04 13:16:22 +0100250
251 if(protocol_type==THRIFT_TYPE_MULTIPLEXED_PROTOCOL) {
252 // TODO: A multiplexed test should also test "Second" (see Java TestServer)
253 // The context comes from the name of the thrift file. If multiple thrift
254 // schemas are used we have to redo the way this is done.
255 protocol = get_multiplexed_protocol(transport, "ThriftTest");
256 if (NULL == protocol) {
257 g_object_unref (transport);
258 g_object_unref (socket);
259 return 252;
260 }
261 }else{
262 protocol = g_object_new (protocol_type,
263 "transport", transport,
264 NULL);
265 }
Roger Meierb3c84092014-09-01 21:53:40 +0200266 test_client = g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT,
267 "input_protocol", protocol,
268 "output_protocol", protocol,
269 NULL);
270
271 /* Execute the actual tests */
272 for (test_num = 0; test_num < num_tests; ++test_num) {
Roger Meier15df0762014-09-29 20:50:56 +0200273 if (thrift_transport_open (transport, &error)) {
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900274 gchar *string = NULL;
275 gboolean boolean = 0;
276 gint8 byte = 0;
277 gint32 int32 = 0;
278 gint64 int64 = 0;
279 gdouble dub = 0;
Roger Meierb3c84092014-09-01 21:53:40 +0200280
281 gint byte_thing, i32_thing, inner_byte_thing, inner_i32_thing;
282 gint64 i64_thing, inner_i64_thing;
283
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900284 TTestXtruct *xtruct_out, *xtruct_out2, *xtruct_in, *inner_xtruct_in;
Roger Meierb3c84092014-09-01 21:53:40 +0200285 TTestXtruct2 *xtruct2_out, *xtruct2_in;
286
287 GHashTable *map_out, *map_in, *inner_map_in;
288 GHashTable *set_out, *set_in;
289 gpointer key, value;
290 gint32 *i32_key_ptr, *i32_value_ptr;
291 GHashTableIter hash_table_iter, inner_hash_table_iter;
292 GList *keys_out, *keys_in, *keys_elem;
293
294 GArray *list_out, *list_in;
295
296 TTestNumberz numberz;
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900297 TTestNumberz numberz2;
Roger Meierb3c84092014-09-01 21:53:40 +0200298
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900299 TTestUserId user_id, *user_id_ptr, *user_id_ptr2;
Roger Meierb3c84092014-09-01 21:53:40 +0200300
301 TTestInsanity *insanity_out, *insanity_in;
302 GHashTable *user_map;
303 GHashTableIter user_map_iter;
304 GPtrArray *xtructs;
305
306 TTestXception *xception = NULL;
307 TTestXception2 *xception2 = NULL;
308
309 gboolean oneway_result;
310 struct timeval oneway_start, oneway_end, oneway_elapsed;
311 gint oneway_elapsed_usec;
312
313 gboolean first;
314 gint32 i, j;
315
316 printf ("Test #%d, connect %s:%d\n", test_num + 1, host, port);
317 gettimeofday (&time_start, NULL);
318
319 /* These test routines have been ported from the C++ test
320 client, care being taken to ensure their output remains as
321 close as possible to the original to facilitate diffs.
322
323 For simplicity comments have been omitted, but every routine
324 has the same basic structure:
325
326 - Create and populate data structures as necessary.
327
328 - Format and output (to the console) a representation of the
329 outgoing data.
330
331 - Issue the remote method call to the server.
332
333 - Format and output a representation of the returned data.
334
335 - Verify the returned data matches what was expected.
336
337 - Deallocate any created data structures.
338
339 Note the recognized values and expected behaviour of each
340 remote method are described in ThriftTest.thrift, which
341 you'll find in the top-level "test" folder. */
342
343 /**
344 * VOID TEST
345 */
346 printf ("testVoid()");
Roger Meier15df0762014-09-29 20:50:56 +0200347 if (t_test_thrift_test_if_test_void (test_client, &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200348 printf (" = void\n");
349 }
350 else {
James E. King, III36628a22017-02-13 15:25:41 -0500351 if(error!=NULL){
Roger Meierb3c84092014-09-01 21:53:40 +0200352 printf ("%s\n", error->message);
James E. King, III36628a22017-02-13 15:25:41 -0500353 g_error_free (error);
354 error = NULL;
355 }
Roger Meierb3c84092014-09-01 21:53:40 +0200356 fail_count++;
357 }
358
359 /**
360 * STRING TEST
361 */
362 printf ("testString(\"Test\")");
363 if (t_test_thrift_test_if_test_string (test_client,
364 &string,
365 "Test",
Roger Meier15df0762014-09-29 20:50:56 +0200366 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200367 printf (" = \"%s\"\n", string);
368 if (strncmp (string, "Test", 5) != 0)
369 fail_count++;
370
371 g_free (string);
372 string = NULL;
373 }
374 else {
375 printf ("%s\n", error->message);
376 g_error_free (error);
377 error = NULL;
378
379 fail_count++;
380 }
381
382 /**
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900383 * BOOL TEST
384 */
385 printf ("testByte(true)");
386 if (t_test_thrift_test_if_test_bool (test_client,
387 &boolean,
388 1,
389 &error)) {
390 printf (" = %s\n", boolean ? "true" : "false");
391 if (boolean != 1)
392 fail_count++;
393 }
394 else {
395 printf ("%s\n", error->message);
396 g_error_free (error);
397 error = NULL;
398
399 fail_count++;
400 }
401 printf ("testByte(false)");
402 if (t_test_thrift_test_if_test_bool (test_client,
403 &boolean,
404 0,
405 &error)) {
406 printf (" = %s\n", boolean ? "true" : "false");
407 if (boolean != 0)
408 fail_count++;
409 }
410 else {
411 printf ("%s\n", error->message);
412 g_error_free (error);
413 error = NULL;
414
415 fail_count++;
416 }
417
418 /**
Roger Meierb3c84092014-09-01 21:53:40 +0200419 * BYTE TEST
420 */
421 printf ("testByte(1)");
422 if (t_test_thrift_test_if_test_byte (test_client,
423 &byte,
424 1,
Roger Meier15df0762014-09-29 20:50:56 +0200425 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200426 printf (" = %d\n", byte);
427 if (byte != 1)
428 fail_count++;
429 }
430 else {
431 printf ("%s\n", error->message);
432 g_error_free (error);
433 error = NULL;
434
435 fail_count++;
436 }
Nobuaki Sukegawa30f465d2015-10-10 10:45:42 +0900437 printf ("testByte(-1)");
438 if (t_test_thrift_test_if_test_byte (test_client,
439 &byte,
440 -1,
441 &error)) {
442 printf (" = %d\n", byte);
443 if (byte != -1)
444 fail_count++;
445 }
446 else {
447 printf ("%s\n", error->message);
448 g_error_free (error);
449 error = NULL;
450
451 fail_count++;
452 }
Roger Meierb3c84092014-09-01 21:53:40 +0200453
454 /**
455 * I32 TEST
456 */
457 printf ("testI32(-1)");
458 if (t_test_thrift_test_if_test_i32 (test_client,
459 &int32,
460 -1,
Roger Meier15df0762014-09-29 20:50:56 +0200461 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200462 printf (" = %d\n", int32);
463 if (int32 != -1)
464 fail_count++;
465 }
466 else {
467 printf ("%s\n", error->message);
468 g_error_free (error);
469 error = NULL;
470
471 fail_count++;
472 }
473
474 /**
475 * I64 TEST
476 */
477 printf ("testI64(-34359738368)");
478 if (t_test_thrift_test_if_test_i64 (test_client,
479 &int64,
480 (gint64)-34359738368,
Roger Meier15df0762014-09-29 20:50:56 +0200481 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200482 printf (" = %" PRId64 "\n", int64);
483 if (int64 != (gint64)-34359738368)
484 fail_count++;
485 }
486 else {
487 printf ("%s\n", error->message);
488 g_error_free (error);
489 error = NULL;
490
491 fail_count++;
492 }
493
494 /**
495 * DOUBLE TEST
496 */
497 printf("testDouble(-5.2098523)");
498 if (t_test_thrift_test_if_test_double (test_client,
499 &dub,
500 -5.2098523,
Roger Meier15df0762014-09-29 20:50:56 +0200501 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200502 printf (" = %f\n", dub);
503 if ((dub - (-5.2098523)) > 0.001)
504 fail_count++;
505 }
506 else {
507 printf ("%s\n", error->message);
508 g_error_free (error);
509 error = NULL;
510
511 fail_count++;
512 }
513
James E. King, III36628a22017-02-13 15:25:41 -0500514 // TODO: add testBinary()
515
Roger Meierb3c84092014-09-01 21:53:40 +0200516 /**
517 * STRUCT TEST
518 */
519 printf ("testStruct({\"Zero\", 1, -3, -5})");
520 xtruct_out = g_object_new (T_TEST_TYPE_XTRUCT,
521 "string_thing", "Zero",
522 "byte_thing", 1,
523 "i32_thing", -3,
524 "i64_thing", -5LL,
525 NULL);
526 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
527
528 if (t_test_thrift_test_if_test_struct (test_client,
529 &xtruct_in,
530 xtruct_out,
Roger Meier15df0762014-09-29 20:50:56 +0200531 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200532 g_object_get (xtruct_in,
533 "string_thing", &string,
534 "byte_thing", &byte_thing,
535 "i32_thing", &i32_thing,
536 "i64_thing", &i64_thing,
537 NULL);
538
539 printf (" = {\"%s\", %d, %d, %" PRId64 "}\n",
540 string,
541 byte_thing,
542 i32_thing,
543 i64_thing);
544 if ((string == NULL || strncmp (string, "Zero", 5) != 0) ||
545 byte_thing != 1 ||
546 i32_thing != -3 ||
547 i64_thing != (gint64)-5)
548 fail_count++;
549 }
550 else {
551 printf ("%s\n", error->message);
552 g_error_free (error);
553 error = NULL;
554
555 fail_count++;
556 }
557 g_object_unref (xtruct_in);
558
559 /**
560 * NESTED STRUCT TEST
561 */
562 printf ("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
563 xtruct2_out = g_object_new (T_TEST_TYPE_XTRUCT2,
564 "byte_thing", 1,
565 "struct_thing", xtruct_out,
566 "i32_thing", 5,
567 NULL);
568 xtruct2_in = g_object_new (T_TEST_TYPE_XTRUCT2, NULL);
569
570 if (t_test_thrift_test_if_test_nest (test_client,
571 &xtruct2_in,
572 xtruct2_out,
Roger Meier15df0762014-09-29 20:50:56 +0200573 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200574 g_object_get (xtruct2_in,
575 "byte_thing", &byte_thing,
576 "struct_thing", &xtruct_in,
577 "i32_thing", &i32_thing,
578 NULL);
579 g_object_get (xtruct_in,
580 "string_thing", &string,
581 "byte_thing", &inner_byte_thing,
582 "i32_thing", &inner_i32_thing,
583 "i64_thing", &inner_i64_thing,
584 NULL);
585
586 printf (" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n",
587 byte_thing,
588 string,
589 inner_byte_thing,
590 inner_i32_thing,
591 inner_i64_thing,
592 i32_thing);
593 if (byte_thing != 1 ||
594 (string == NULL || strncmp (string, "Zero", 5) != 0) ||
595 inner_byte_thing != 1 ||
596 inner_i32_thing != -3 ||
597 inner_i64_thing != (gint64)-5 ||
598 i32_thing != 5)
599 fail_count++;
600 }
601 else {
602 printf ("%s\n", error->message);
603 g_error_free (error);
604 error = NULL;
605
606 fail_count++;
607 }
608
609 g_object_unref (xtruct_in);
610 g_object_unref (xtruct2_in);
611 g_object_unref (xtruct2_out);
612 g_object_unref (xtruct_out);
613
614 /**
615 * MAP TEST
616 */
617 map_out = g_hash_table_new_full (g_int_hash,
618 g_int_equal,
619 g_free,
620 g_free);
621 for (i = 0; i < 5; ++i) {
622 i32_key_ptr = g_malloc (sizeof *i32_key_ptr);
623 i32_value_ptr = g_malloc (sizeof *i32_value_ptr);
624
625 *i32_key_ptr = i;
626 *i32_value_ptr = i - 10;
627
628 g_hash_table_insert (map_out, i32_key_ptr, i32_value_ptr);
629 }
630 printf ("testMap({");
631 first = TRUE;
632 g_hash_table_iter_init (&hash_table_iter, map_out);
633 while (g_hash_table_iter_next (&hash_table_iter,
634 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200635 &value)) {
636 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200637 first = FALSE;
638 else
639 printf (", ");
640
641 printf ("%d => %d", *(gint32 *)key, *(gint32 *)value);
642 }
643 printf ("})");
644
645 map_in = g_hash_table_new_full (g_int_hash,
646 g_int_equal,
647 g_free,
648 g_free);
649
650 if (t_test_thrift_test_if_test_map (test_client,
651 &map_in,
652 map_out,
Roger Meier15df0762014-09-29 20:50:56 +0200653 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200654 printf (" = {");
655 first = TRUE;
656 g_hash_table_iter_init (&hash_table_iter, map_in);
657 while (g_hash_table_iter_next (&hash_table_iter,
658 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200659 &value)) {
660 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200661 first = FALSE;
662 else
663 printf (", ");
664
665 printf ("%d => %d", *(gint32 *)key, *(gint32 *)value);
666 }
667 printf ("}\n");
668
669 if (g_hash_table_size (map_in) != g_hash_table_size (map_out))
670 fail_count++;
671 else {
672 g_hash_table_iter_init (&hash_table_iter, map_out);
673 while (g_hash_table_iter_next (&hash_table_iter,
674 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200675 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200676 gpointer in_value = g_hash_table_lookup (map_in, key);
677 if (in_value == NULL ||
678 *(gint32 *)in_value != *(gint32 *)value) {
679 fail_count++;
680 break;
681 }
682 }
683 }
684 }
685 else {
686 printf ("%s\n", error->message);
687 g_error_free (error);
688 error = NULL;
689
690 fail_count++;
691 }
692
693 g_hash_table_unref (map_in);
694 g_hash_table_unref (map_out);
695
696 /**
697 * STRING MAP TEST
698 */
699 map_out = g_hash_table_new_full (g_str_hash,
700 g_str_equal,
701 NULL,
702 NULL);
703 g_hash_table_insert (map_out, "a", "2");
704 g_hash_table_insert (map_out, "b", "blah");
705 g_hash_table_insert (map_out, "some", "thing");
706 printf ("testStringMap({");
707 first = TRUE;
708 g_hash_table_iter_init (&hash_table_iter, map_out);
709 while (g_hash_table_iter_next (&hash_table_iter,
710 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200711 &value)) {
712 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200713 first = FALSE;
714 else
715 printf (", ");
716
717 printf ("\"%s\" => \"%s\"", (gchar *)key, (gchar *)value);
718 }
719 printf (")}");
720
721 map_in = g_hash_table_new_full (g_str_hash,
722 g_str_equal,
723 g_free,
724 g_free);
725
726 if (t_test_thrift_test_if_test_string_map (test_client,
727 &map_in,
728 map_out,
Roger Meier15df0762014-09-29 20:50:56 +0200729 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200730 printf (" = {");
731 first = TRUE;
732 g_hash_table_iter_init (&hash_table_iter, map_in);
733 while (g_hash_table_iter_next (&hash_table_iter,
734 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200735 &value)) {
736 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200737 first = FALSE;
738 else
739 printf (", ");
740
741 printf ("\"%s\" => \"%s\"", (gchar *)key, (gchar *)value);
742 }
743 printf ("}\n");
744
745 if (g_hash_table_size (map_in) != g_hash_table_size (map_out))
746 fail_count++;
747 else {
748 g_hash_table_iter_init (&hash_table_iter, map_out);
749 while (g_hash_table_iter_next (&hash_table_iter,
750 &key,
Roger Meier15df0762014-09-29 20:50:56 +0200751 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200752 gpointer in_value = g_hash_table_lookup (map_in, key);
753 if (in_value == NULL ||
754 strcmp ((gchar *)in_value, (gchar *)value) != 0) {
755 fail_count++;
756 break;
757 }
758 }
759 }
760 }
761 else {
762 printf ("%s\n", error->message);
763 g_error_free (error);
764 error = NULL;
765
766 fail_count++;
767 }
768
769 g_hash_table_unref (map_in);
770 g_hash_table_unref (map_out);
771
772 /**
773 * SET TEST
774 */
775 set_out = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL);
776 for (i = -2; i < 3; ++i) {
777 i32_key_ptr = g_malloc (sizeof *i32_key_ptr);
778 *i32_key_ptr = i;
779
780 g_hash_table_insert (set_out, i32_key_ptr, NULL);
781 }
782 printf ("testSet({");
783 first = TRUE;
784 keys_out = g_hash_table_get_keys (set_out);
785 keys_elem = keys_out;
786 while (keys_elem != NULL) {
Roger Meier15df0762014-09-29 20:50:56 +0200787 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200788 first = FALSE;
789 else
790 printf (", ");
791
792 printf ("%d", *(gint32 *)keys_elem->data);
793
794 keys_elem = keys_elem->next;
795 }
796 printf ("})");
797
798 set_in = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL);
799
800 if (t_test_thrift_test_if_test_set (test_client,
801 &set_in,
802 set_out,
Roger Meier15df0762014-09-29 20:50:56 +0200803 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200804 printf(" = {");
805 first = TRUE;
806 keys_in = g_hash_table_get_keys (set_in);
807 keys_elem = keys_in;
808 while (keys_elem != NULL) {
Roger Meier15df0762014-09-29 20:50:56 +0200809 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200810 first = FALSE;
811 else
812 printf (", ");
813
814 printf ("%d", *(gint32 *)keys_elem->data);
815
816 keys_elem = keys_elem->next;
817 }
818 printf ("}\n");
819
820 if (g_list_length (keys_in) != g_list_length (keys_out))
821 fail_count++;
822 else {
823 keys_elem = keys_out;
824 while (keys_elem != NULL) {
825 if (g_list_find_custom (keys_in,
826 keys_elem->data,
827 gint32_compare) == NULL) {
828 fail_count++;
829 break;
830 }
831
832 keys_elem = keys_elem->next;
833 }
834 }
835
836 g_list_free (keys_in);
837 }
838 else {
839 printf ("%s\n", error->message);
840 g_error_free (error);
841 error = NULL;
842
843 fail_count++;
844 }
845
846 g_hash_table_unref (set_in);
847 g_list_free (keys_out);
848 g_hash_table_unref (set_out);
849
850 /**
851 * LIST TEST
852 */
853 list_out = g_array_new (FALSE, TRUE, sizeof (gint32));
854 for (i = -2; i < 3; ++i) {
855 g_array_append_val (list_out, i);
856 }
857 printf ("testList({");
858 first = TRUE;
859 for (i = 0; i < (gint32)list_out->len; ++i) {
Roger Meier15df0762014-09-29 20:50:56 +0200860 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200861 first = FALSE;
862 else
863 printf (", ");
864
865 printf ("%d", g_array_index (list_out, gint32, i));
866 }
867 printf ("})");
868
869 list_in = g_array_new (FALSE, TRUE, sizeof (gint32));
870
871 if (t_test_thrift_test_if_test_list (test_client,
872 &list_in,
873 list_out,
Roger Meier15df0762014-09-29 20:50:56 +0200874 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200875 printf (" = {");
876 first = TRUE;
877 for (i = 0; i < (gint32)list_in->len; ++i) {
Roger Meier15df0762014-09-29 20:50:56 +0200878 if (first)
Roger Meierb3c84092014-09-01 21:53:40 +0200879 first = FALSE;
880 else
881 printf (", ");
882
883 printf ("%d", g_array_index (list_in, gint32, i));
884 }
885 printf ("}\n");
886
887 if (list_in->len != list_out->len ||
888 memcmp (list_in->data,
889 list_out->data,
890 list_in->len * sizeof (gint32)) != 0)
891 fail_count++;
892 }
893 else {
894 printf ("%s\n", error->message);
895 g_error_free (error);
896 error = NULL;
897
898 fail_count++;
899 }
900
901 g_array_unref (list_in);
902 g_array_unref (list_out);
903
904 /**
905 * ENUM TEST
906 */
907 printf("testEnum(ONE)");
908 if (t_test_thrift_test_if_test_enum (test_client,
909 &numberz,
910 T_TEST_NUMBERZ_ONE,
Roger Meier15df0762014-09-29 20:50:56 +0200911 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200912 printf(" = %d\n", numberz);
913 if (numberz != T_TEST_NUMBERZ_ONE)
914 fail_count++;
915 }
916 else {
917 printf ("%s\n", error->message);
918 g_error_free (error);
919 error = NULL;
920
921 fail_count++;
922 }
923
924 printf("testEnum(TWO)");
925 if (t_test_thrift_test_if_test_enum (test_client,
926 &numberz,
927 T_TEST_NUMBERZ_TWO,
Roger Meier15df0762014-09-29 20:50:56 +0200928 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200929 printf(" = %d\n", numberz);
930 if (numberz != T_TEST_NUMBERZ_TWO)
931 fail_count++;
932 }
933 else {
934 printf ("%s\n", error->message);
935 g_error_free (error);
936 error = NULL;
937
938 fail_count++;
939 }
940
941 printf("testEnum(THREE)");
942 if (t_test_thrift_test_if_test_enum (test_client,
943 &numberz,
944 T_TEST_NUMBERZ_THREE,
Roger Meier15df0762014-09-29 20:50:56 +0200945 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200946 printf(" = %d\n", numberz);
947 if (numberz != T_TEST_NUMBERZ_THREE)
948 fail_count++;
949 }
950 else {
951 printf ("%s\n", error->message);
952 g_error_free (error);
953 error = NULL;
954
955 fail_count++;
956 }
957
958 printf("testEnum(FIVE)");
959 if (t_test_thrift_test_if_test_enum (test_client,
960 &numberz,
961 T_TEST_NUMBERZ_FIVE,
Roger Meier15df0762014-09-29 20:50:56 +0200962 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200963 printf(" = %d\n", numberz);
964 if (numberz != T_TEST_NUMBERZ_FIVE)
965 fail_count++;
966 }
967 else {
968 printf ("%s\n", error->message);
969 g_error_free (error);
970 error = NULL;
971
972 fail_count++;
973 }
974
975 printf("testEnum(EIGHT)");
976 if (t_test_thrift_test_if_test_enum (test_client,
977 &numberz,
978 T_TEST_NUMBERZ_EIGHT,
Roger Meier15df0762014-09-29 20:50:56 +0200979 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +0200980 printf(" = %d\n", numberz);
981 if (numberz != T_TEST_NUMBERZ_EIGHT)
982 fail_count++;
983 }
984 else {
985 printf ("%s\n", error->message);
986 g_error_free (error);
987 error = NULL;
988
989 fail_count++;
990 }
991
992 /**
993 * TYPEDEF TEST
994 */
995 printf ("testTypedef(309858235082523)");
996 if (t_test_thrift_test_if_test_typedef (test_client,
997 &user_id,
998 309858235082523LL,
Roger Meier15df0762014-09-29 20:50:56 +0200999 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001000 printf(" = %" PRId64 "\n", user_id);
1001 if (user_id != 309858235082523LL)
1002 fail_count++;
1003 }
1004 else {
1005 printf ("%s\n", error->message);
1006 g_error_free (error);
1007 error = NULL;
1008
1009 fail_count++;
1010 }
1011
1012 /**
1013 * NESTED MAP TEST
1014 */
1015 printf ("testMapMap(1)");
1016 map_in = g_hash_table_new_full (g_int_hash,
1017 g_int_equal,
1018 g_free,
1019 (GDestroyNotify)g_hash_table_unref);
1020 if (t_test_thrift_test_if_test_map_map (test_client,
1021 &map_in,
1022 1,
Roger Meier15df0762014-09-29 20:50:56 +02001023 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001024 g_hash_table_iter_init (&hash_table_iter, map_in);
1025
1026 printf (" = {");
1027 while (g_hash_table_iter_next (&hash_table_iter,
1028 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001029 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001030 printf ("%d => {", *(gint32 *)key);
1031
1032 g_hash_table_iter_init (&inner_hash_table_iter,
1033 (GHashTable *)value);
1034 while (g_hash_table_iter_next (&inner_hash_table_iter,
1035 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001036 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001037 printf ("%d => %d, ", *(gint32 *)key, *(gint32 *)value);
1038 }
1039
1040 printf ("}, ");
1041 }
1042 printf ("}\n");
1043
1044 if (g_hash_table_size (map_in) != 2)
1045 fail_count++;
1046 else {
1047 gint32 inner_keys[] = {1, 2, 3, 4};
1048 gint32 i32_key;
1049
1050 i32_key = -4;
1051 inner_map_in = g_hash_table_lookup (map_in, &i32_key);
1052 if (inner_map_in == NULL ||
1053 g_hash_table_size (inner_map_in) != 4)
1054 fail_count++;
1055 else {
1056 keys_in = g_hash_table_get_keys (inner_map_in);
1057 keys_in = g_list_sort (keys_in, gint32_compare);
1058
1059 for (i = 0; i < 4; i++) {
1060 keys_elem = g_list_nth (keys_in, 3 - i);
1061
1062 if (*(gint32 *)keys_elem->data != (-1 * inner_keys[i]) ||
1063 *(gint32 *)g_hash_table_lookup (inner_map_in,
1064 keys_elem->data) !=
1065 (-1 * inner_keys[i])) {
1066 fail_count++;
1067 break;
1068 }
1069 }
1070
1071 g_list_free (keys_in);
1072 }
1073
1074 i32_key = 4;
1075 inner_map_in = g_hash_table_lookup (map_in, &i32_key);
1076 if (inner_map_in == NULL ||
1077 g_hash_table_size (inner_map_in) != 4)
1078 fail_count++;
1079 else {
1080 keys_in = g_hash_table_get_keys (inner_map_in);
1081 keys_in = g_list_sort (keys_in, gint32_compare);
1082
1083 for (i = 0; i < 4; i++) {
1084 keys_elem = g_list_nth (keys_in, i);
1085
1086 if (*(gint32 *)keys_elem->data != inner_keys[i] ||
1087 *(gint32 *)g_hash_table_lookup (inner_map_in,
1088 keys_elem->data) !=
1089 inner_keys[i]) {
1090 fail_count++;
1091 break;
1092 }
1093 }
1094
1095 g_list_free (keys_in);
1096 }
1097 }
1098 }
1099 else {
1100 printf ("%s\n", error->message);
1101 g_error_free (error);
1102 error = NULL;
1103
1104 fail_count++;
1105 }
1106
1107 g_hash_table_unref (map_in);
1108
1109 /**
1110 * INSANITY TEST
1111 */
1112 insanity_out = g_object_new (T_TEST_TYPE_INSANITY, NULL);
1113 g_object_get (insanity_out,
1114 "userMap", &user_map,
1115 "xtructs", &xtructs,
1116 NULL);
1117
1118 numberz = T_TEST_NUMBERZ_FIVE;
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001119 numberz2 = T_TEST_NUMBERZ_EIGHT;
Roger Meierb3c84092014-09-01 21:53:40 +02001120 user_id_ptr = g_malloc (sizeof *user_id_ptr);
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001121 *user_id_ptr = 5;
1122 user_id_ptr2 = g_malloc (sizeof *user_id_ptr);
1123 *user_id_ptr2 = 8;
Roger Meierb3c84092014-09-01 21:53:40 +02001124 g_hash_table_insert (user_map, (gpointer)numberz, user_id_ptr);
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001125 g_hash_table_insert (user_map, (gpointer)numberz2, user_id_ptr2);
Roger Meierb3c84092014-09-01 21:53:40 +02001126 g_hash_table_unref (user_map);
1127
1128 xtruct_out = g_object_new (T_TEST_TYPE_XTRUCT,
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001129 "string_thing", "Hello2",
1130 "byte_thing", 2,
1131 "i32_thing", 2,
1132 "i64_thing", 2LL,
Roger Meierb3c84092014-09-01 21:53:40 +02001133 NULL);
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +09001134 xtruct_out2 = g_object_new (T_TEST_TYPE_XTRUCT,
1135 "string_thing", "Goodbye4",
1136 "byte_thing", 4,
1137 "i32_thing", 4,
1138 "i64_thing", 4LL,
1139 NULL);
1140 g_ptr_array_add (xtructs, xtruct_out2);
Roger Meierb3c84092014-09-01 21:53:40 +02001141 g_ptr_array_add (xtructs, xtruct_out);
1142 g_ptr_array_unref (xtructs);
1143
1144 map_in = g_hash_table_new_full (g_int64_hash,
1145 g_int64_equal,
1146 g_free,
1147 (GDestroyNotify)g_hash_table_unref);
1148
1149 printf("testInsanity()");
1150 if (t_test_thrift_test_if_test_insanity (test_client,
1151 &map_in,
1152 insanity_out,
Roger Meier15df0762014-09-29 20:50:56 +02001153 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001154 printf (" = {");
1155 g_hash_table_iter_init (&hash_table_iter, map_in);
1156 while (g_hash_table_iter_next (&hash_table_iter,
1157 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001158 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001159 printf ("%" PRId64 " => {", *(TTestUserId *)key);
1160
1161 g_hash_table_iter_init (&inner_hash_table_iter,
1162 (GHashTable *)value);
1163 while (g_hash_table_iter_next (&inner_hash_table_iter,
1164 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001165 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001166 printf ("%d => {", (TTestNumberz)key);
1167
1168 g_object_get ((TTestInsanity *)value,
1169 "userMap", &user_map,
1170 "xtructs", &xtructs,
1171 NULL);
1172
1173 printf ("{");
1174 g_hash_table_iter_init (&user_map_iter, user_map);
1175 while (g_hash_table_iter_next (&user_map_iter,
1176 &key,
Roger Meier15df0762014-09-29 20:50:56 +02001177 &value)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001178 printf ("%d => %" PRId64 ", ",
1179 (TTestNumberz)key,
1180 *(TTestUserId *)value);
1181 }
1182 printf ("}, ");
1183 g_hash_table_unref (user_map);
1184
1185 printf("{");
1186 for (i = 0; i < (gint32)xtructs->len; ++i) {
1187 xtruct_in = g_ptr_array_index (xtructs, i);
1188 g_object_get (xtruct_in,
1189 "string_thing", &string,
1190 "byte_thing", &byte_thing,
1191 "i32_thing", &i32_thing,
1192 "i64_thing", &i64_thing,
1193 NULL);
1194
1195 printf ("{\"%s\", %d, %d, %" PRId64 "}, ",
1196 string,
1197 byte_thing,
1198 i32_thing,
1199 i64_thing);
1200 }
1201 printf ("}");
1202 g_ptr_array_unref (xtructs);
1203
1204 printf ("}, ");
1205 }
1206 printf("}, ");
1207 }
1208 printf("}\n");
1209
1210 if (g_hash_table_size (map_in) != 2)
1211 fail_count++;
1212 else {
1213 TTestNumberz numberz_key_values[] = {
1214 T_TEST_NUMBERZ_TWO, T_TEST_NUMBERZ_THREE
1215 };
1216 gint user_map_values[] = { 5, 8 };
1217 TTestUserId user_id_key;
1218
1219 user_id_key = 1;
1220 inner_map_in = g_hash_table_lookup (map_in, &user_id_key);
1221 if (inner_map_in == NULL ||
1222 g_hash_table_size (inner_map_in) != 2)
1223 fail_count++;
1224 else {
1225 TTestNumberz numberz_key;
1226
1227 for (i = 0; i < 2; ++i) {
1228 numberz_key = numberz_key_values[i];
1229 insanity_in =
1230 g_hash_table_lookup (inner_map_in,
1231 (gconstpointer)numberz_key);
1232 if (insanity_in == NULL)
1233 fail_count++;
1234 else {
1235 g_object_get (insanity_in,
1236 "userMap", &user_map,
1237 "xtructs", &xtructs,
1238 NULL);
1239
1240 if (user_map == NULL)
1241 fail_count++;
1242 else {
1243 if (g_hash_table_size (user_map) != 2)
1244 fail_count++;
1245 else {
1246 for (j = 0; j < 2; ++j) {
1247 numberz_key = (TTestNumberz)user_map_values[j];
1248
1249 value =
1250 g_hash_table_lookup (user_map,
1251 (gconstpointer)numberz_key);
1252 if (value == NULL ||
1253 *(TTestUserId *)value != (TTestUserId)user_map_values[j])
1254 fail_count++;
1255 }
1256 }
1257
1258 g_hash_table_unref (user_map);
1259 }
1260
1261 if (xtructs == NULL)
1262 fail_count++;
1263 else {
1264 if (xtructs->len != 2)
1265 fail_count++;
1266 else {
1267 xtruct_in = g_ptr_array_index (xtructs, 0);
1268 g_object_get (xtruct_in,
1269 "string_thing", &string,
1270 "byte_thing", &byte_thing,
1271 "i32_thing", &i32_thing,
1272 "i64_thing", &i64_thing,
1273 NULL);
1274 if ((string == NULL ||
1275 strncmp (string, "Goodbye4", 9) != 0) ||
1276 byte_thing != 4 ||
1277 i32_thing != 4 ||
1278 i64_thing != 4)
1279 fail_count++;
1280
1281 if (string != NULL)
1282 g_free (string);
1283
1284 xtruct_in = g_ptr_array_index (xtructs, 1);
1285 g_object_get (xtruct_in,
1286 "string_thing", &string,
1287 "byte_thing", &byte_thing,
1288 "i32_thing", &i32_thing,
1289 "i64_thing", &i64_thing,
1290 NULL);
1291 if ((string == NULL ||
1292 strncmp (string, "Hello2", 7) != 0) ||
1293 byte_thing != 2 ||
1294 i32_thing != 2 ||
1295 i64_thing != 2)
1296 fail_count++;
1297
1298 if (string != NULL)
1299 g_free (string);
1300 }
1301
1302 g_ptr_array_unref (xtructs);
1303 }
1304 }
1305 }
1306 }
1307
1308 user_id_key = 2;
1309 inner_map_in = g_hash_table_lookup (map_in, &user_id_key);
1310 if (inner_map_in == NULL ||
1311 g_hash_table_size (inner_map_in) != 1)
1312 fail_count++;
1313 else {
1314 insanity_in =
1315 g_hash_table_lookup (inner_map_in,
1316 (gconstpointer)T_TEST_NUMBERZ_SIX);
1317 if (insanity_in == NULL)
1318 fail_count++;
1319 else {
1320 g_object_get (insanity_in,
1321 "userMap", &user_map,
1322 "xtructs", &xtructs,
1323 NULL);
1324
1325 if (user_map == NULL)
1326 fail_count++;
1327 else {
1328 if (g_hash_table_size (user_map) != 0)
1329 fail_count++;
1330
1331 g_hash_table_unref (user_map);
1332 }
1333
1334 if (xtructs == NULL)
1335 fail_count++;
1336 else {
1337 if (xtructs->len != 0)
1338 fail_count++;
1339
1340 g_ptr_array_unref (xtructs);
1341 }
1342 }
1343 }
1344 }
1345 }
1346 else {
1347 printf ("%s\n", error->message);
1348 g_error_free (error);
1349 error = NULL;
1350
1351 fail_count++;
1352 }
1353
1354 g_hash_table_unref (map_in);
1355 g_object_unref (insanity_out);
1356
1357 /* test exception */
1358 printf ("testClient.testException(\"Xception\") =>");
Roger Meier15df0762014-09-29 20:50:56 +02001359 if (!t_test_thrift_test_if_test_exception (test_client,
1360 "Xception",
1361 &xception,
1362 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001363 xception != NULL) {
1364 g_object_get (xception,
1365 "errorCode", &int32,
1366 "message", &string,
1367 NULL);
1368 printf (" {%u, \"%s\"}\n", int32, string);
1369 g_free (string);
1370
1371 g_object_unref (xception);
1372 xception = NULL;
1373
1374 g_error_free (error);
1375 error = NULL;
1376 }
1377 else {
1378 printf (" void\nFAILURE\n");
1379 fail_count++;
1380
1381 if (xception != NULL) {
1382 g_object_unref (xception);
1383 xception = NULL;
1384 }
1385
1386 if (error != NULL) {
1387 g_error_free (error);
1388 error = NULL;
1389 }
1390 }
1391
1392 printf ("testClient.testException(\"TException\") =>");
Roger Meier15df0762014-09-29 20:50:56 +02001393 if (!t_test_thrift_test_if_test_exception (test_client,
1394 "TException",
1395 &xception,
1396 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001397 xception == NULL &&
1398 error != NULL) {
1399 printf (" Caught TException\n");
1400
1401 g_error_free (error);
1402 error = NULL;
1403 }
1404 else {
1405 printf (" void\nFAILURE\n");
1406 fail_count++;
1407
1408 if (xception != NULL) {
1409 g_object_unref (xception);
1410 xception = NULL;
1411 }
1412
1413 if (error != NULL) {
1414 g_error_free (error);
1415 error = NULL;
1416 }
1417 }
1418
1419 printf ("testClient.testException(\"success\") =>");
1420 if (t_test_thrift_test_if_test_exception (test_client,
1421 "success",
1422 &xception,
Roger Meier15df0762014-09-29 20:50:56 +02001423 &error))
Roger Meierb3c84092014-09-01 21:53:40 +02001424 printf (" void\n");
1425 else {
1426 printf (" void\nFAILURE\n");
1427 fail_count++;
1428
1429 if (xception != NULL) {
1430 g_object_unref (xception);
1431 xception = NULL;
1432 }
1433
1434 g_error_free (error);
1435 error = NULL;
1436 }
1437
1438 g_assert (error == NULL);
1439
1440 /* test multi exception */
1441 printf ("testClient.testMultiException(\"Xception\", \"test 1\") =>");
1442 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier15df0762014-09-29 20:50:56 +02001443 if (!t_test_thrift_test_if_test_multi_exception (test_client,
1444 &xtruct_in,
1445 "Xception",
1446 "test 1",
1447 &xception,
1448 &xception2,
1449 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001450 xception != NULL &&
1451 xception2 == NULL) {
1452 g_object_get (xception,
1453 "errorCode", &int32,
1454 "message", &string,
1455 NULL);
1456 printf (" {%u, \"%s\"}\n", int32, string);
1457 g_free (string);
1458
1459 g_object_unref (xception);
1460 xception = NULL;
1461
1462 g_error_free (error);
1463 error = NULL;
1464 }
1465 else {
1466 printf (" result\nFAILURE\n");
1467 fail_count++;
1468
1469 if (xception != NULL) {
1470 g_object_unref (xception);
1471 xception = NULL;
1472 }
1473
1474 if (xception2 != NULL) {
1475 g_object_unref (xception2);
1476 xception = NULL;
1477 }
1478
1479 if (error != NULL) {
1480 g_error_free (error);
1481 error = NULL;
1482 }
1483 }
1484 g_object_unref (xtruct_in);
1485
1486 printf ("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
1487 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
Roger Meier15df0762014-09-29 20:50:56 +02001488 if (!t_test_thrift_test_if_test_multi_exception (test_client,
1489 &xtruct_in,
1490 "Xception2",
1491 "test 2",
1492 &xception,
1493 &xception2,
1494 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001495 xception == NULL &&
1496 xception2 != NULL) {
1497 g_object_get (xception2,
1498 "errorCode", &int32,
1499 "struct_thing", &inner_xtruct_in,
1500 NULL);
1501 g_object_get (inner_xtruct_in,
1502 "string_thing", &string,
1503 NULL);
1504 printf (" {%u, {\"%s\"}}\n", int32, string);
1505 g_free (string);
1506
1507 g_object_unref (inner_xtruct_in);
1508 inner_xtruct_in = NULL;
1509
1510 g_object_unref (xception2);
1511 xception2 = NULL;
1512
1513 g_error_free (error);
1514 error = NULL;
1515 }
1516 else {
1517 printf (" result\nFAILURE\n");
1518 fail_count++;
1519
1520 if (xception != NULL) {
1521 g_object_unref (xception);
1522 xception = NULL;
1523 }
1524
1525 if (xception2 != NULL) {
1526 g_object_unref (xception2);
1527 xception = NULL;
1528 }
1529
1530 if (error != NULL) {
1531 g_error_free (error);
1532 error = NULL;
1533 }
1534 }
1535 g_object_unref (xtruct_in);
1536
1537 printf ("testClient.testMultiException(\"success\", \"test 3\") =>");
1538 xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL);
1539 if (t_test_thrift_test_if_test_multi_exception (test_client,
1540 &xtruct_in,
1541 "success",
1542 "test 3",
1543 &xception,
1544 &xception2,
Roger Meier15df0762014-09-29 20:50:56 +02001545 &error) &&
Roger Meierb3c84092014-09-01 21:53:40 +02001546 xception == NULL &&
1547 xception2 == NULL) {
1548 g_object_get (xtruct_in,
1549 "string_thing", &string,
1550 NULL);
1551 printf (" {{\"%s\"}}\n", string);
1552 g_free (string);
1553 }
1554 else {
1555 printf (" result\nFAILURE\n");
1556 fail_count++;
1557
1558 if (xception != NULL) {
1559 g_object_unref (xception);
1560 xception = NULL;
1561 }
1562
1563 if (xception2 != NULL) {
1564 g_object_unref (xception2);
1565 xception = NULL;
1566 }
1567
1568 if (error != NULL) {
1569 g_error_free (error);
1570 error = NULL;
1571 }
1572 }
1573 g_object_unref (xtruct_in);
1574
1575 /* test oneway void */
1576 printf ("testClient.testOneway(1) =>");
1577 gettimeofday (&oneway_start, NULL);
1578 oneway_result = t_test_thrift_test_if_test_oneway (test_client,
1579 1,
1580 &error);
1581 gettimeofday (&oneway_end, NULL);
1582 timersub (&oneway_end, &oneway_start, &oneway_elapsed);
1583 oneway_elapsed_usec =
1584 oneway_elapsed.tv_sec * 1000 * 1000 + oneway_elapsed.tv_usec;
1585
Roger Meier15df0762014-09-29 20:50:56 +02001586 if (oneway_result) {
Roger Meierb3c84092014-09-01 21:53:40 +02001587 if (oneway_elapsed_usec > 200 * 1000) {
1588 printf (" FAILURE - took %.2f ms\n",
1589 (double)oneway_elapsed_usec / 1000.0);
1590 fail_count++;
1591 }
1592 else
1593 printf (" success - took %.2f ms\n",
1594 (double)oneway_elapsed_usec / 1000.0);
1595 }
1596 else {
1597 printf ("%s\n", error->message);
1598 g_error_free (error);
1599 error = NULL;
1600
1601 fail_count++;
1602 }
1603
1604 /**
1605 * redo a simple test after the oneway to make sure we aren't "off by
1606 * one" -- if the server treated oneway void like normal void, this next
1607 * test will fail since it will get the void confirmation rather than
1608 * the correct result. In this circumstance, the client will receive the
1609 * error:
1610 *
1611 * application error: Wrong method name
1612 */
1613 /**
1614 * I32 TEST
1615 */
1616 printf ("re-test testI32(-1)");
1617 if (t_test_thrift_test_if_test_i32 (test_client,
1618 &int32,
1619 -1,
Roger Meier15df0762014-09-29 20:50:56 +02001620 &error)) {
Roger Meierb3c84092014-09-01 21:53:40 +02001621 printf (" = %d\n", int32);
1622 if (int32 != -1)
1623 fail_count++;
1624 }
1625 else {
1626 printf ("%s\n", error->message);
1627 g_error_free (error);
1628 error = NULL;
1629
1630 fail_count++;
1631 }
1632
1633 gettimeofday (&time_stop, NULL);
1634 timersub (&time_stop, &time_start, &time_elapsed);
1635 time_elapsed_usec =
1636 time_elapsed.tv_sec * 1000 * 1000 + time_elapsed.tv_usec;
1637
1638 printf("Total time: %" PRIu64 " us\n", time_elapsed_usec);
1639
1640 time_total_usec += time_elapsed_usec;
1641 if (time_elapsed_usec < time_min_usec)
1642 time_min_usec = time_elapsed_usec;
1643 if (time_elapsed_usec > time_max_usec)
1644 time_max_usec = time_elapsed_usec;
1645
1646 thrift_transport_close (transport, &error);
1647 }
1648 else {
1649 printf ("Connect failed: %s\n", error->message);
James E. King, III36628a22017-02-13 15:25:41 -05001650 g_object_unref (socket);
Roger Meierb3c84092014-09-01 21:53:40 +02001651 g_error_free (error);
1652 error = NULL;
1653
1654 return 1;
1655 }
1656 }
1657
1658 /* All done---output statistics */
1659 puts ("\nAll tests done.");
1660
1661 time_avg_usec = time_total_usec / num_tests;
1662
1663 printf ("Min time: %" PRIu64 " us\n", time_min_usec);
1664 printf ("Max time: %" PRIu64 " us\n", time_max_usec);
1665 printf ("Avg time: %" PRIu64 " us\n", time_avg_usec);
1666
1667 g_object_unref (test_client);
1668 g_object_unref (protocol);
1669 g_object_unref (transport);
1670 g_free (host);
1671
James E. King, III36628a22017-02-13 15:25:41 -05001672 if (ssl) {
1673 thrift_ssl_socket_finalize_openssl();
1674 }
1675
Roger Meierb3c84092014-09-01 21:53:40 +02001676 return fail_count;
1677}