Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 20 | /* Disable string-function optimizations when glibc is used, as these produce |
| 21 | compiler warnings about string length when a string function is used inside |
| 22 | a call to assert () */ |
Roger Meier | ff4a8ed | 2015-09-19 20:49:50 +0200 | [diff] [blame] | 23 | #if !defined(__APPLE__) && !defined(__FreeBSD__) && \ |
| 24 | !defined(__OpenBSD__) && !defined(__NetBSD__) |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 25 | #include <features.h> |
Roger Meier | ff4a8ed | 2015-09-19 20:49:50 +0200 | [diff] [blame] | 26 | #endif |
| 27 | |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 28 | #ifdef __GLIBC__ |
| 29 | #define __NO_STRING_INLINES 1 |
| 30 | #endif |
| 31 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | #include <assert.h> |
| 36 | #include <netdb.h> |
| 37 | #include <string.h> |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 38 | #include <sys/wait.h> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 39 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 40 | #include <thrift/c_glib/protocol/thrift_protocol.h> |
| 41 | #include <thrift/c_glib/transport/thrift_socket.h> |
| 42 | #include <thrift/c_glib/transport/thrift_server_socket.h> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 43 | |
| 44 | #define TEST_BOOL TRUE |
| 45 | #define TEST_BYTE 123 |
| 46 | #define TEST_I16 12345 |
| 47 | #define TEST_I32 1234567890 |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 48 | #define TEST_I64 123456789012345 |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 49 | #define TEST_DOUBLE 1234567890.123 |
| 50 | #define TEST_STRING "this is a test string 1234567890!@#$%^&*()" |
| 51 | #define TEST_PORT 51199 |
| 52 | |
| 53 | static int transport_read_count = 0; |
| 54 | static int transport_read_error = 0; |
| 55 | static int transport_read_error_at = -1; |
| 56 | gint32 |
| 57 | my_thrift_transport_read (ThriftTransport *transport, gpointer buf, |
| 58 | guint32 len, GError **error) |
| 59 | { |
| 60 | if (transport_read_count != transport_read_error_at |
| 61 | && transport_read_error == 0) |
| 62 | { |
| 63 | transport_read_count++; |
| 64 | return thrift_transport_read (transport, buf, len, error); |
| 65 | } |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | static int transport_write_count = 0; |
| 70 | static int transport_write_error = 0; |
| 71 | static int transport_write_error_at = -1; |
| 72 | gboolean |
| 73 | my_thrift_transport_write (ThriftTransport *transport, const gpointer buf, |
| 74 | const guint32 len, GError **error) |
| 75 | { |
| 76 | if (transport_write_count != transport_write_error_at |
| 77 | && transport_write_error == 0) |
| 78 | { |
| 79 | transport_write_count++; |
| 80 | return thrift_transport_write (transport, buf, len, error); |
| 81 | } |
| 82 | return FALSE; |
| 83 | } |
| 84 | |
| 85 | #define thrift_transport_read my_thrift_transport_read |
| 86 | #define thrift_transport_write my_thrift_transport_write |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 87 | #include "../src/thrift/c_glib/protocol/thrift_binary_protocol.c" |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 88 | #undef thrift_transport_read |
| 89 | #undef thrift_transport_write |
| 90 | |
| 91 | static void thrift_server_primitives (const int port); |
| 92 | static void thrift_server_complex_types (const int port); |
| 93 | |
| 94 | static void |
| 95 | test_create_and_destroy(void) |
| 96 | { |
| 97 | GObject *object = NULL; |
| 98 | |
| 99 | /* create an object and then destroy it */ |
| 100 | object = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, NULL); |
| 101 | assert (object != NULL); |
| 102 | g_object_unref (object); |
| 103 | } |
| 104 | |
| 105 | static void |
| 106 | test_initialize(void) |
| 107 | { |
| 108 | ThriftSocket *tsocket = NULL; |
| 109 | ThriftBinaryProtocol *protocol = NULL; |
| 110 | ThriftSocket *temp = NULL; |
| 111 | |
| 112 | /* create a ThriftTransport */ |
| 113 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 114 | "port", 51188, NULL); |
| 115 | assert (tsocket != NULL); |
| 116 | /* create a ThriftBinaryProtocol using the Transport */ |
| 117 | protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport", |
| 118 | tsocket, NULL); |
| 119 | assert (protocol != NULL); |
| 120 | /* fetch the properties */ |
| 121 | g_object_get (G_OBJECT(protocol), "transport", &temp, NULL); |
| 122 | g_object_unref (temp); |
| 123 | |
| 124 | /* clean up memory */ |
| 125 | g_object_unref (protocol); |
| 126 | g_object_unref (tsocket); |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | test_read_and_write_primitives(void) |
| 131 | { |
| 132 | int status; |
| 133 | pid_t pid; |
| 134 | ThriftSocket *tsocket = NULL; |
| 135 | ThriftTransport *transport = NULL; |
| 136 | ThriftBinaryProtocol *tb = NULL; |
| 137 | ThriftProtocol *protocol = NULL; |
| 138 | gpointer binary = (gpointer *) TEST_STRING; |
| 139 | guint32 len = strlen (TEST_STRING); |
| 140 | int port = TEST_PORT; |
| 141 | |
| 142 | /* fork a server from the client */ |
| 143 | pid = fork (); |
| 144 | assert (pid >= 0); |
| 145 | |
| 146 | if (pid == 0) |
| 147 | { |
| 148 | /* child listens */ |
| 149 | thrift_server_primitives (port); |
| 150 | exit (0); |
| 151 | } else { |
| 152 | /* parent. wait a bit for the socket to be created. */ |
| 153 | sleep (1); |
| 154 | |
| 155 | /* create a ThriftSocket */ |
| 156 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 157 | "port", port, NULL); |
| 158 | transport = THRIFT_TRANSPORT (tsocket); |
| 159 | thrift_transport_open (transport, NULL); |
| 160 | assert (thrift_transport_is_open (transport)); |
| 161 | |
| 162 | /* create a ThriftBinaryTransport */ |
| 163 | tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport", |
| 164 | tsocket, NULL); |
| 165 | protocol = THRIFT_PROTOCOL (tb); |
| 166 | assert (protocol != NULL); |
| 167 | |
| 168 | /* write a bunch of primitives */ |
| 169 | assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0); |
| 170 | assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0); |
| 171 | assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0); |
| 172 | assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0); |
| 173 | assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0); |
| 174 | assert (thrift_binary_protocol_write_double (protocol, |
| 175 | TEST_DOUBLE, NULL) > 0); |
| 176 | assert (thrift_binary_protocol_write_string (protocol, |
| 177 | TEST_STRING, NULL) > 0); |
| 178 | assert (thrift_binary_protocol_write_binary (protocol, binary, |
| 179 | len, NULL) > 0); |
| 180 | assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0); |
| 181 | assert (thrift_binary_protocol_write_binary (protocol, binary, |
| 182 | len, NULL) > 0); |
| 183 | |
| 184 | /* test write errors */ |
| 185 | transport_write_error = 1; |
| 186 | assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, |
| 187 | NULL) == -1); |
| 188 | assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) == -1); |
| 189 | assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) == -1); |
| 190 | assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) == -1); |
| 191 | assert (thrift_binary_protocol_write_double (protocol, TEST_DOUBLE, |
| 192 | NULL) == -1); |
| 193 | assert (thrift_binary_protocol_write_binary (protocol, binary, len, |
| 194 | NULL) == -1); |
| 195 | transport_write_error = 0; |
| 196 | |
| 197 | /* test binary partial failure */ |
| 198 | transport_write_count = 0; |
| 199 | transport_write_error_at = 1; |
| 200 | assert (thrift_binary_protocol_write_binary (protocol, binary, |
| 201 | len, NULL) == -1); |
| 202 | transport_write_error_at = -1; |
| 203 | |
| 204 | /* clean up */ |
| 205 | thrift_transport_close (transport, NULL); |
| 206 | g_object_unref (tsocket); |
| 207 | g_object_unref (protocol); |
| 208 | assert (wait (&status) == pid); |
| 209 | assert (status == 0); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | test_read_and_write_complex_types (void) |
| 215 | { |
| 216 | int status; |
| 217 | pid_t pid; |
| 218 | ThriftSocket *tsocket = NULL; |
| 219 | ThriftTransport *transport = NULL; |
| 220 | ThriftBinaryProtocol *tb = NULL; |
| 221 | ThriftProtocol *protocol = NULL; |
| 222 | int port = TEST_PORT; |
| 223 | |
| 224 | /* fork a server from the client */ |
| 225 | pid = fork (); |
| 226 | assert (pid >= 0); |
| 227 | |
| 228 | if (pid == 0) |
| 229 | { |
| 230 | /* child listens */ |
| 231 | thrift_server_complex_types (port); |
| 232 | exit (0); |
| 233 | } else { |
| 234 | /* parent. wait a bit for the socket to be created. */ |
| 235 | sleep (1); |
| 236 | |
| 237 | /* create a ThriftSocket */ |
| 238 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 239 | "port", port, NULL); |
| 240 | transport = THRIFT_TRANSPORT (tsocket); |
| 241 | thrift_transport_open (transport, NULL); |
| 242 | assert (thrift_transport_is_open (transport)); |
| 243 | |
| 244 | /* create a ThriftBinaryTransport */ |
| 245 | tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport", |
| 246 | tsocket, NULL); |
| 247 | protocol = THRIFT_PROTOCOL (tb); |
| 248 | assert (protocol != NULL); |
| 249 | |
| 250 | /* test structures */ |
| 251 | assert (thrift_binary_protocol_write_struct_begin (protocol, |
| 252 | NULL, NULL) == 0); |
| 253 | assert (thrift_binary_protocol_write_struct_end (protocol, NULL) == 0); |
| 254 | |
| 255 | assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID, |
| 256 | 1, NULL) > 0); |
| 257 | assert (thrift_binary_protocol_write_field_end (protocol, NULL) == 0); |
| 258 | |
| 259 | /* test write error */ |
| 260 | transport_write_error = 1; |
| 261 | assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID, |
| 262 | 1, NULL) == -1); |
| 263 | transport_write_error = 0; |
| 264 | |
| 265 | /* test 2nd write error */ |
| 266 | transport_write_count = 0; |
| 267 | transport_write_error_at = 1; |
| 268 | assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID, |
| 269 | 1, NULL) == -1); |
| 270 | transport_write_error_at = -1; |
| 271 | |
| 272 | /* test 2nd read failure on a field */ |
| 273 | thrift_binary_protocol_write_byte (protocol, T_VOID, NULL); |
| 274 | |
| 275 | /* test write_field_stop */ |
| 276 | assert (thrift_binary_protocol_write_field_stop (protocol, NULL) > 0); |
| 277 | |
| 278 | /* write a map */ |
| 279 | assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID, |
| 280 | 1, NULL) > 0); |
| 281 | assert (thrift_binary_protocol_write_map_end (protocol, NULL) == 0); |
| 282 | |
| 283 | /* test 2nd read failure on a map */ |
| 284 | thrift_binary_protocol_write_byte (protocol, T_VOID, NULL); |
| 285 | |
| 286 | /* test 3rd read failure on a map */ |
| 287 | thrift_binary_protocol_write_byte (protocol, T_VOID, NULL); |
| 288 | thrift_binary_protocol_write_byte (protocol, T_VOID, NULL); |
| 289 | |
| 290 | /* test 1st write failure on a map */ |
| 291 | transport_write_error = 1; |
| 292 | assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID, |
| 293 | 1, NULL) == -1); |
| 294 | transport_write_error = 0; |
| 295 | |
| 296 | /* test 2nd write failure on a map */ |
| 297 | transport_write_count = 0; |
| 298 | transport_write_error_at = 1; |
| 299 | assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID, |
| 300 | 1, NULL) == -1); |
| 301 | transport_write_error_at = -1; |
| 302 | |
| 303 | /* test 3rd write failure on a map */ |
| 304 | transport_write_count = 0; |
| 305 | transport_write_error_at = 2; |
| 306 | assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID, |
| 307 | 1, NULL) == -1); |
| 308 | transport_write_error_at = -1; |
| 309 | |
| 310 | /* test negative map size */ |
| 311 | thrift_binary_protocol_write_byte (protocol, T_VOID, NULL); |
| 312 | thrift_binary_protocol_write_byte (protocol, T_VOID, NULL); |
| 313 | thrift_binary_protocol_write_i32 (protocol, -10, NULL); |
| 314 | |
| 315 | /* test list operations */ |
| 316 | assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID, |
| 317 | 1, NULL) > 0); |
| 318 | assert (thrift_binary_protocol_write_list_end (protocol, NULL) == 0); |
| 319 | |
| 320 | /* test 2nd read failure on a list */ |
| 321 | thrift_binary_protocol_write_byte (protocol, T_VOID, NULL); |
| 322 | |
| 323 | /* test negative list size */ |
| 324 | thrift_binary_protocol_write_byte (protocol, T_VOID, NULL); |
| 325 | thrift_binary_protocol_write_i32 (protocol, -10, NULL); |
| 326 | |
| 327 | /* test first write error on a list */ |
| 328 | transport_write_error = 1; |
| 329 | assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID, |
| 330 | 1, NULL) == -1); |
| 331 | transport_write_error = 0; |
| 332 | |
| 333 | /* test 2nd write error on a list */ |
| 334 | transport_write_count = 0; |
| 335 | transport_write_error_at = 1; |
| 336 | assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID, |
| 337 | 1, NULL) == -1); |
| 338 | transport_write_error_at = -1; |
| 339 | |
| 340 | /* test set operation s*/ |
| 341 | assert (thrift_binary_protocol_write_set_begin (protocol, T_VOID, |
| 342 | 1, NULL) > 0); |
| 343 | assert (thrift_binary_protocol_write_set_end (protocol, NULL) == 0); |
| 344 | |
| 345 | /* invalid version */ |
| 346 | assert (thrift_binary_protocol_write_i32 (protocol, -1, NULL) > 0); |
| 347 | |
| 348 | /* sz > 0 for a message */ |
| 349 | assert (thrift_binary_protocol_write_i32 (protocol, 1, NULL) > 0); |
| 350 | |
| 351 | /* send a valid message */ |
| 352 | thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL); |
| 353 | thrift_binary_protocol_write_string (protocol, "test", NULL); |
| 354 | thrift_binary_protocol_write_i32 (protocol, 1, NULL); |
| 355 | |
| 356 | /* broken 2nd read */ |
| 357 | thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL); |
| 358 | |
| 359 | /* send a broken 3rd read */ |
| 360 | thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL); |
| 361 | thrift_binary_protocol_write_string (protocol, "test", NULL); |
| 362 | |
| 363 | /* send a valid message */ |
| 364 | assert (thrift_binary_protocol_write_message_begin (protocol, "test", |
| 365 | T_CALL, 1, NULL) > 0); |
| 366 | |
| 367 | assert (thrift_binary_protocol_write_message_end (protocol, NULL) == 0); |
| 368 | |
| 369 | /* send broken writes */ |
| 370 | transport_write_error = 1; |
| 371 | assert (thrift_binary_protocol_write_message_begin (protocol, "test", |
| 372 | T_CALL, 1, NULL) == -1); |
| 373 | transport_write_error = 0; |
| 374 | |
| 375 | transport_write_count = 0; |
| 376 | transport_write_error_at = 2; |
| 377 | assert (thrift_binary_protocol_write_message_begin (protocol, "test", |
| 378 | T_CALL, 1, NULL) == -1); |
| 379 | transport_write_error_at = -1; |
| 380 | |
| 381 | transport_write_count = 0; |
| 382 | transport_write_error_at = 3; |
| 383 | assert (thrift_binary_protocol_write_message_begin (protocol, "test", |
| 384 | T_CALL, 1, NULL) == -1); |
| 385 | transport_write_error_at = -1; |
| 386 | |
| 387 | /* clean up */ |
| 388 | thrift_transport_close (transport, NULL); |
| 389 | g_object_unref (tsocket); |
| 390 | g_object_unref (protocol); |
| 391 | assert (wait (&status) == pid); |
| 392 | assert (status == 0); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | |
| 397 | static void |
| 398 | thrift_server_primitives (const int port) |
| 399 | { |
| 400 | ThriftServerTransport *transport = NULL; |
| 401 | ThriftTransport *client = NULL; |
| 402 | ThriftBinaryProtocol *tbp = NULL; |
| 403 | ThriftProtocol *protocol = NULL; |
| 404 | gboolean value_boolean = FALSE; |
| 405 | gint8 value_byte = 0; |
| 406 | gint16 value_16 = 0; |
| 407 | gint32 value_32 = 0; |
| 408 | gint64 value_64 = 0; |
| 409 | gdouble value_double = 0; |
| 410 | gchar *string = NULL; |
| 411 | gpointer binary = NULL; |
| 412 | guint32 len = 0; |
| 413 | void *comparator = (void *) TEST_STRING; |
| 414 | |
| 415 | ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, |
| 416 | "port", port, NULL); |
| 417 | transport = THRIFT_SERVER_TRANSPORT (tsocket); |
| 418 | thrift_server_transport_listen (transport, NULL); |
| 419 | client = thrift_server_transport_accept (transport, NULL); |
| 420 | assert (client != NULL); |
| 421 | |
| 422 | tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport", |
| 423 | client, NULL); |
| 424 | protocol = THRIFT_PROTOCOL (tbp); |
| 425 | |
| 426 | assert (thrift_binary_protocol_read_bool (protocol, |
| 427 | &value_boolean, NULL) > 0); |
| 428 | assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0); |
| 429 | assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0); |
| 430 | assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0); |
| 431 | assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0); |
| 432 | assert (thrift_binary_protocol_read_double (protocol, |
| 433 | &value_double, NULL) > 0); |
| 434 | assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0); |
| 435 | assert (thrift_binary_protocol_read_binary (protocol, &binary, |
| 436 | &len, NULL) > 0); |
| 437 | |
| 438 | assert (value_boolean == TEST_BOOL); |
| 439 | assert (value_byte = TEST_BYTE); |
| 440 | assert (value_16 = TEST_I16); |
| 441 | assert (value_32 = TEST_I32); |
| 442 | assert (value_64 = TEST_I64); |
| 443 | assert (value_double = TEST_DOUBLE); |
| 444 | assert (strcmp (TEST_STRING, string) == 0); |
| 445 | assert (memcmp (comparator, binary, len) == 0); |
| 446 | |
| 447 | g_free (string); |
| 448 | g_free (binary); |
| 449 | |
| 450 | thrift_binary_protocol_read_binary (protocol, &binary, &len, NULL); |
| 451 | g_free (binary); |
| 452 | |
| 453 | transport_read_count = 0; |
| 454 | transport_read_error_at = 0; |
| 455 | assert (thrift_binary_protocol_read_binary (protocol, &binary, |
| 456 | &len, NULL) == -1); |
| 457 | transport_read_error_at = -1; |
| 458 | |
| 459 | transport_read_count = 0; |
| 460 | transport_read_error_at = 1; |
| 461 | assert (thrift_binary_protocol_read_binary (protocol, &binary, |
| 462 | &len, NULL) == -1); |
| 463 | transport_read_error_at = -1; |
| 464 | |
| 465 | transport_read_error = 1; |
| 466 | assert (thrift_binary_protocol_read_bool (protocol, |
| 467 | &value_boolean, NULL) == -1); |
| 468 | assert (thrift_binary_protocol_read_byte (protocol, |
| 469 | &value_byte, NULL) == -1); |
| 470 | assert (thrift_binary_protocol_read_i16 (protocol, |
| 471 | &value_16, NULL) == -1); |
| 472 | assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) == -1); |
| 473 | assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) == -1); |
| 474 | assert (thrift_binary_protocol_read_double (protocol, |
| 475 | &value_double, NULL) == -1); |
| 476 | transport_read_error = 0; |
| 477 | |
| 478 | /* test partial write failure */ |
| 479 | thrift_protocol_read_i32 (protocol, &value_32, NULL); |
| 480 | |
| 481 | thrift_transport_read_end (client, NULL); |
| 482 | thrift_transport_close (client, NULL); |
| 483 | |
| 484 | g_object_unref (tbp); |
| 485 | g_object_unref (client); |
| 486 | g_object_unref (tsocket); |
| 487 | } |
| 488 | |
| 489 | static void |
| 490 | thrift_server_complex_types (const int port) |
| 491 | { |
| 492 | ThriftServerTransport *transport = NULL; |
| 493 | ThriftTransport *client = NULL; |
| 494 | ThriftBinaryProtocol *tbp = NULL; |
| 495 | ThriftProtocol *protocol = NULL; |
| 496 | gchar *struct_name = NULL; |
| 497 | gchar *field_name = NULL; |
| 498 | gchar *message_name = NULL; |
| 499 | ThriftType element_type, key_type, value_type, field_type; |
| 500 | ThriftMessageType message_type; |
| 501 | gint8 value = 0; |
| 502 | gint16 field_id = 0; |
| 503 | guint32 size = 0; |
| 504 | gint32 seqid = 0; |
| 505 | gint32 version = 0; |
| 506 | |
| 507 | ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, |
| 508 | "port", port, NULL); |
| 509 | transport = THRIFT_SERVER_TRANSPORT (tsocket); |
| 510 | thrift_server_transport_listen (transport, NULL); |
| 511 | client = thrift_server_transport_accept (transport, NULL); |
| 512 | assert (client != NULL); |
| 513 | |
| 514 | tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport", |
| 515 | client, NULL); |
| 516 | protocol = THRIFT_PROTOCOL (tbp); |
| 517 | |
| 518 | thrift_binary_protocol_read_struct_begin (protocol, &struct_name, NULL); |
| 519 | thrift_binary_protocol_read_struct_end (protocol, NULL); |
| 520 | |
| 521 | thrift_binary_protocol_read_field_begin (protocol, &field_name, &field_type, |
| 522 | &field_id, NULL); |
| 523 | thrift_binary_protocol_read_field_end (protocol, NULL); |
| 524 | |
| 525 | /* test first read error on a field */ |
| 526 | transport_read_error = 1; |
| 527 | assert (thrift_binary_protocol_read_field_begin (protocol, |
| 528 | &field_name, &field_type, |
| 529 | &field_id, NULL) == -1); |
| 530 | transport_read_error = 0; |
| 531 | |
| 532 | /* test 2nd write failure */ |
| 533 | thrift_binary_protocol_read_byte (protocol, &value, NULL); |
| 534 | |
| 535 | /* test 2nd read failure on a field */ |
| 536 | transport_read_count = 0; |
| 537 | transport_read_error_at = 1; |
| 538 | assert (thrift_binary_protocol_read_field_begin (protocol, |
| 539 | &field_name, &field_type, |
| 540 | &field_id, NULL) == -1); |
| 541 | transport_read_error_at = -1; |
| 542 | |
| 543 | /* test field stop */ |
| 544 | thrift_binary_protocol_read_field_begin (protocol, &field_name, &field_type, |
| 545 | &field_id, NULL); |
| 546 | |
| 547 | thrift_binary_protocol_read_map_begin (protocol, &key_type, &value_type, |
| 548 | &size, NULL); |
| 549 | thrift_binary_protocol_read_map_end (protocol, NULL); |
| 550 | |
| 551 | /* test read failure on a map */ |
| 552 | transport_read_count = 0; |
| 553 | transport_read_error_at = 0; |
| 554 | assert (thrift_binary_protocol_read_map_begin (protocol, |
| 555 | &key_type, &value_type, |
| 556 | &size, NULL) == -1); |
| 557 | transport_read_error_at = -1; |
| 558 | |
| 559 | /* test 2nd read failure on a map */ |
| 560 | transport_read_count = 0; |
| 561 | transport_read_error_at = 1; |
| 562 | assert (thrift_binary_protocol_read_map_begin (protocol, |
| 563 | &key_type, &value_type, |
| 564 | &size, NULL) == -1); |
| 565 | transport_read_error_at = -1; |
| 566 | |
| 567 | /* test 3rd read failure on a map */ |
| 568 | transport_read_count = 0; |
| 569 | transport_read_error_at = 2; |
| 570 | assert (thrift_binary_protocol_read_map_begin (protocol, |
| 571 | &key_type, &value_type, |
| 572 | &size, NULL) == -1); |
| 573 | transport_read_error_at = -1; |
| 574 | |
| 575 | /* test 2nd write failure */ |
| 576 | thrift_binary_protocol_read_byte (protocol, &value, NULL); |
| 577 | |
| 578 | /* test 3rd write failure */ |
| 579 | thrift_binary_protocol_read_byte (protocol, &value, NULL); |
| 580 | thrift_binary_protocol_read_byte (protocol, &value, NULL); |
| 581 | |
| 582 | /* test negative map size */ |
| 583 | assert (thrift_binary_protocol_read_map_begin (protocol, |
| 584 | &key_type, &value_type, |
| 585 | &size, NULL) == -1); |
| 586 | |
| 587 | /* test list operations */ |
| 588 | thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL); |
| 589 | thrift_binary_protocol_read_list_end (protocol, NULL); |
| 590 | |
| 591 | /* test read failure */ |
| 592 | transport_read_error = 1; |
| 593 | assert (thrift_binary_protocol_read_list_begin (protocol, &element_type, |
| 594 | &size, NULL) == -1); |
| 595 | transport_read_error = 0; |
| 596 | |
| 597 | /* test 2nd read failure */ |
| 598 | transport_read_count = 0; |
| 599 | transport_read_error_at = 1; |
| 600 | thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL); |
| 601 | transport_read_error_at = -1; |
| 602 | |
| 603 | /* test negative list size failure */ |
| 604 | thrift_binary_protocol_read_list_begin (protocol, &element_type, &size, NULL); |
| 605 | |
| 606 | /* test 2nd write failure */ |
| 607 | thrift_binary_protocol_read_byte (protocol, &value, NULL); |
| 608 | |
| 609 | /* test set operations */ |
| 610 | thrift_binary_protocol_read_set_begin (protocol, &element_type, &size, NULL); |
| 611 | thrift_binary_protocol_read_set_end (protocol, NULL); |
| 612 | |
| 613 | /* broken read */ |
| 614 | transport_read_error = 1; |
| 615 | assert (thrift_binary_protocol_read_message_begin (protocol, &message_name, |
| 616 | &message_type, &seqid, |
| 617 | NULL) == -1); |
| 618 | transport_read_error = 0; |
| 619 | |
| 620 | /* invalid protocol version */ |
| 621 | assert (thrift_binary_protocol_read_message_begin (protocol, &message_name, |
| 622 | &message_type, &seqid, |
| 623 | NULL) == -1); |
| 624 | |
| 625 | /* sz > 0 */ |
| 626 | assert (thrift_binary_protocol_read_message_begin (protocol, &message_name, |
| 627 | &message_type, &seqid, |
| 628 | NULL) > 0); |
| 629 | |
| 630 | /* read a valid message */ |
| 631 | assert (thrift_binary_protocol_read_message_begin (protocol, &message_name, |
| 632 | &message_type, &seqid, |
| 633 | NULL) > 0); |
| 634 | g_free (message_name); |
| 635 | |
| 636 | /* broken 2nd read on a message */ |
| 637 | transport_read_count = 0; |
| 638 | transport_read_error_at = 1; |
| 639 | assert (thrift_binary_protocol_read_message_begin (protocol, &message_name, |
| 640 | &message_type, &seqid, |
| 641 | NULL) == -1); |
| 642 | transport_read_error_at = -1; |
| 643 | |
| 644 | /* broken 3rd read on a message */ |
| 645 | transport_read_count = 0; |
| 646 | transport_read_error_at = 3; /* read_string does two reads */ |
| 647 | assert (thrift_binary_protocol_read_message_begin (protocol, &message_name, |
| 648 | &message_type, &seqid, |
| 649 | NULL) == -1); |
| 650 | g_free (message_name); |
| 651 | transport_read_error_at = -1; |
| 652 | |
| 653 | /* read a valid message */ |
| 654 | assert (thrift_binary_protocol_read_message_begin (protocol, &message_name, |
| 655 | &message_type, &seqid, |
| 656 | NULL) > 0); |
| 657 | g_free (message_name); |
| 658 | |
| 659 | assert (thrift_binary_protocol_read_message_end (protocol, NULL) == 0); |
| 660 | |
| 661 | /* handle 2nd write failure on a message */ |
| 662 | thrift_binary_protocol_read_i32 (protocol, &version, NULL); |
| 663 | |
| 664 | /* handle 2nd write failure on a message */ |
| 665 | thrift_binary_protocol_read_i32 (protocol, &version, NULL); |
| 666 | thrift_binary_protocol_read_string (protocol, &message_name, NULL); |
| 667 | |
| 668 | g_object_unref (client); |
Simon South | 38e7155 | 2015-08-03 10:51:16 +0000 | [diff] [blame] | 669 | /* TODO: investigate g_object_unref (tbp); */ |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 670 | g_object_unref (tsocket); |
| 671 | } |
| 672 | |
| 673 | int |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 674 | main(int argc, char *argv[]) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 675 | { |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 676 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 677 | g_type_init(); |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 678 | #endif |
| 679 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 680 | g_test_init (&argc, &argv, NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 681 | |
Roger Meier | c2cc61a | 2010-11-30 19:53:29 +0000 | [diff] [blame] | 682 | g_test_add_func ("/testbinaryprotocol/CreateAndDestroy", test_create_and_destroy); |
| 683 | g_test_add_func ("/testbinaryprotocol/Initialize", test_initialize); |
| 684 | g_test_add_func ("/testbinaryprotocol/ReadAndWritePrimitives", test_read_and_write_primitives); |
| 685 | g_test_add_func ("/testbinaryprotocol/ReadAndWriteComplexTypes", test_read_and_write_complex_types); |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 686 | |
| 687 | return g_test_run (); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 688 | } |