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