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