Chandler May | 6dde90b | 2016-01-10 06:01:10 +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 | |
| 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 () */ |
| 23 | #if !defined(__APPLE__) && !defined(__FreeBSD__) && \ |
| 24 | !defined(__OpenBSD__) && !defined(__NetBSD__) |
| 25 | #include <features.h> |
| 26 | #endif |
| 27 | |
| 28 | #ifdef __GLIBC__ |
| 29 | #define __NO_STRING_INLINES 1 |
| 30 | #endif |
| 31 | |
| 32 | #include <unistd.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | #include <assert.h> |
| 36 | #include <netdb.h> |
| 37 | #include <string.h> |
| 38 | #include <sys/wait.h> |
| 39 | |
| 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> |
Chandler May | 1ccd81b | 2016-02-11 08:25:25 -0500 | [diff] [blame^] | 43 | #include <thrift/c_glib/transport/thrift_framed_transport.h> |
Chandler May | 6dde90b | 2016-01-10 06:01:10 +0000 | [diff] [blame] | 44 | |
| 45 | #define TEST_BOOL TRUE |
| 46 | #define TEST_BYTE 123 |
| 47 | #define TEST_I16 12345 |
| 48 | #define TEST_I32 1234567890 |
| 49 | #define TEST_I64 123456789012345 |
| 50 | #define TEST_NI16 (-12345) |
| 51 | #define TEST_NI32 (-1234567890) |
| 52 | #define TEST_NI64 (-123456789012345) |
| 53 | #define TEST_DOUBLE 1234567890.123 |
| 54 | #define TEST_STRING "this is a test string 1234567890!@#$%^&*()" |
| 55 | #define TEST_PORT 51199 |
| 56 | |
| 57 | static int transport_read_count = 0; |
| 58 | static int transport_read_error = 0; |
| 59 | static int transport_read_error_at = -1; |
| 60 | gint32 |
Chandler May | 1ccd81b | 2016-02-11 08:25:25 -0500 | [diff] [blame^] | 61 | my_thrift_transport_read_all (ThriftTransport *transport, gpointer buf, |
| 62 | guint32 len, GError **error) |
Chandler May | 6dde90b | 2016-01-10 06:01:10 +0000 | [diff] [blame] | 63 | { |
| 64 | if (transport_read_count != transport_read_error_at |
| 65 | && transport_read_error == 0) |
| 66 | { |
| 67 | transport_read_count++; |
Chandler May | 1ccd81b | 2016-02-11 08:25:25 -0500 | [diff] [blame^] | 68 | return thrift_transport_read_all (transport, buf, len, error); |
Chandler May | 6dde90b | 2016-01-10 06:01:10 +0000 | [diff] [blame] | 69 | } |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | static int transport_write_count = 0; |
| 74 | static int transport_write_error = 0; |
| 75 | static int transport_write_error_at = -1; |
| 76 | gboolean |
| 77 | my_thrift_transport_write (ThriftTransport *transport, const gpointer buf, |
| 78 | const guint32 len, GError **error) |
| 79 | { |
| 80 | if (transport_write_count != transport_write_error_at |
| 81 | && transport_write_error == 0) |
| 82 | { |
| 83 | transport_write_count++; |
| 84 | return thrift_transport_write (transport, buf, len, error); |
| 85 | } |
| 86 | return FALSE; |
| 87 | } |
| 88 | |
Chandler May | 1ccd81b | 2016-02-11 08:25:25 -0500 | [diff] [blame^] | 89 | #define thrift_transport_read_all my_thrift_transport_read_all |
Chandler May | 6dde90b | 2016-01-10 06:01:10 +0000 | [diff] [blame] | 90 | #define thrift_transport_write my_thrift_transport_write |
| 91 | #include "../src/thrift/c_glib/protocol/thrift_compact_protocol.c" |
Chandler May | 1ccd81b | 2016-02-11 08:25:25 -0500 | [diff] [blame^] | 92 | #undef thrift_transport_read_all |
Chandler May | 6dde90b | 2016-01-10 06:01:10 +0000 | [diff] [blame] | 93 | #undef thrift_transport_write |
| 94 | |
| 95 | static void thrift_server_primitives (const int port); |
| 96 | static void thrift_server_complex_types (const int port); |
Chandler May | 1ccd81b | 2016-02-11 08:25:25 -0500 | [diff] [blame^] | 97 | static void thrift_server_many_frames (const int port); |
Chandler May | 6dde90b | 2016-01-10 06:01:10 +0000 | [diff] [blame] | 98 | |
| 99 | static void |
| 100 | test_create_and_destroy (void) |
| 101 | { |
| 102 | GObject *object = NULL; |
| 103 | |
| 104 | /* create an object and then destroy it */ |
| 105 | object = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, NULL); |
| 106 | assert (object != NULL); |
| 107 | g_object_unref (object); |
| 108 | } |
| 109 | |
| 110 | static void |
| 111 | test_initialize (void) |
| 112 | { |
| 113 | ThriftSocket *tsocket = NULL; |
| 114 | ThriftCompactProtocol *protocol = NULL; |
| 115 | ThriftSocket *temp = NULL; |
| 116 | |
| 117 | /* create a ThriftTransport */ |
| 118 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 119 | "port", 51188, NULL); |
| 120 | assert (tsocket != NULL); |
| 121 | /* create a ThriftCompactProtocol using the Transport */ |
| 122 | protocol = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport", |
| 123 | tsocket, NULL); |
| 124 | assert (protocol != NULL); |
| 125 | /* fetch the properties */ |
| 126 | g_object_get (G_OBJECT (protocol), "transport", &temp, NULL); |
| 127 | g_object_unref (temp); |
| 128 | |
| 129 | /* clean up memory */ |
| 130 | g_object_unref (protocol); |
| 131 | g_object_unref (tsocket); |
| 132 | } |
| 133 | |
| 134 | static void |
| 135 | test_read_and_write_primitives (void) |
| 136 | { |
| 137 | int status; |
| 138 | pid_t pid; |
| 139 | ThriftSocket *tsocket = NULL; |
| 140 | ThriftTransport *transport = NULL; |
| 141 | ThriftCompactProtocol *tc = NULL; |
| 142 | ThriftProtocol *protocol = NULL; |
| 143 | gpointer binary = (gpointer *) TEST_STRING; |
| 144 | guint32 len = strlen (TEST_STRING); |
| 145 | int port = TEST_PORT; |
| 146 | |
| 147 | /* fork a server from the client */ |
| 148 | pid = fork (); |
| 149 | assert (pid >= 0); |
| 150 | |
| 151 | if (pid == 0) |
| 152 | { |
| 153 | /* child listens */ |
| 154 | thrift_server_primitives (port); |
| 155 | exit (0); |
| 156 | } else { |
| 157 | /* parent. wait a bit for the socket to be created. */ |
| 158 | sleep (1); |
| 159 | |
| 160 | /* create a ThriftSocket */ |
| 161 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 162 | "port", port, NULL); |
| 163 | transport = THRIFT_TRANSPORT (tsocket); |
| 164 | thrift_transport_open (transport, NULL); |
| 165 | assert (thrift_transport_is_open (transport)); |
| 166 | |
| 167 | /* create a ThriftCompactTransport */ |
| 168 | tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport", |
| 169 | tsocket, NULL); |
| 170 | protocol = THRIFT_PROTOCOL (tc); |
| 171 | assert (protocol != NULL); |
| 172 | |
| 173 | /* write a bunch of primitives */ |
| 174 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0); |
| 175 | assert (thrift_compact_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0); |
| 176 | assert (thrift_compact_protocol_write_i16 (protocol, TEST_I16, NULL) > 0); |
| 177 | assert (thrift_compact_protocol_write_i32 (protocol, TEST_I32, NULL) > 0); |
| 178 | assert (thrift_compact_protocol_write_i64 (protocol, TEST_I64, NULL) > 0); |
| 179 | assert (thrift_compact_protocol_write_i16 (protocol, TEST_NI16, NULL) > 0); |
| 180 | assert (thrift_compact_protocol_write_i32 (protocol, TEST_NI32, NULL) > 0); |
| 181 | assert (thrift_compact_protocol_write_i64 (protocol, TEST_NI64, NULL) > 0); |
| 182 | assert (thrift_compact_protocol_write_i16 (protocol, 2, NULL) > 0); |
| 183 | assert (thrift_compact_protocol_write_i32 (protocol, 2, NULL) > 0); |
| 184 | assert (thrift_compact_protocol_write_i64 (protocol, 2, NULL) > 0); |
| 185 | assert (thrift_compact_protocol_write_i16 (protocol, -2, NULL) > 0); |
| 186 | assert (thrift_compact_protocol_write_i32 (protocol, -2, NULL) > 0); |
| 187 | assert (thrift_compact_protocol_write_i64 (protocol, -2, NULL) > 0); |
| 188 | assert (thrift_compact_protocol_write_double (protocol, |
| 189 | TEST_DOUBLE, NULL) > 0); |
| 190 | assert (thrift_compact_protocol_write_string (protocol, |
| 191 | TEST_STRING, NULL) > 0); |
| 192 | assert (thrift_compact_protocol_write_binary (protocol, binary, |
| 193 | len, NULL) > 0); |
| 194 | assert (thrift_compact_protocol_write_binary (protocol, NULL, 0, NULL) > 0); |
| 195 | assert (thrift_compact_protocol_write_binary (protocol, binary, |
| 196 | len, NULL) > 0); |
| 197 | |
| 198 | /* test write errors */ |
| 199 | transport_write_error = 1; |
| 200 | assert (thrift_compact_protocol_write_byte (protocol, TEST_BYTE, |
| 201 | NULL) == -1); |
| 202 | assert (thrift_compact_protocol_write_i16 (protocol, TEST_I16, NULL) == -1); |
| 203 | assert (thrift_compact_protocol_write_i32 (protocol, TEST_I32, NULL) == -1); |
| 204 | assert (thrift_compact_protocol_write_i64 (protocol, TEST_I64, NULL) == -1); |
| 205 | assert (thrift_compact_protocol_write_i16 (protocol, TEST_NI16, |
| 206 | NULL) == -1); |
| 207 | assert (thrift_compact_protocol_write_i32 (protocol, TEST_NI32, |
| 208 | NULL) == -1); |
| 209 | assert (thrift_compact_protocol_write_i64 (protocol, TEST_NI64, |
| 210 | NULL) == -1); |
| 211 | assert (thrift_compact_protocol_write_double (protocol, TEST_DOUBLE, |
| 212 | NULL) == -1); |
| 213 | assert (thrift_compact_protocol_write_binary (protocol, binary, len, |
| 214 | NULL) == -1); |
| 215 | transport_write_error = 0; |
| 216 | |
| 217 | /* test binary partial failure */ |
| 218 | transport_write_count = 0; |
| 219 | transport_write_error_at = 1; |
| 220 | assert (thrift_compact_protocol_write_binary (protocol, binary, |
| 221 | len, NULL) == -1); |
| 222 | transport_write_error_at = -1; |
| 223 | |
| 224 | /* clean up */ |
| 225 | thrift_transport_close (transport, NULL); |
| 226 | g_object_unref (tsocket); |
| 227 | g_object_unref (protocol); |
| 228 | assert (wait (&status) == pid); |
| 229 | assert (status == 0); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static void |
| 234 | test_read_and_write_complex_types (void) |
| 235 | { |
| 236 | int status; |
| 237 | pid_t pid; |
| 238 | ThriftSocket *tsocket = NULL; |
| 239 | ThriftTransport *transport = NULL; |
| 240 | ThriftCompactProtocol *tc = NULL; |
| 241 | ThriftProtocol *protocol = NULL; |
| 242 | int port = TEST_PORT; |
| 243 | |
| 244 | /* fork a server from the client */ |
| 245 | pid = fork (); |
| 246 | assert (pid >= 0); |
| 247 | |
| 248 | if (pid == 0) |
| 249 | { |
| 250 | /* child listens */ |
| 251 | thrift_server_complex_types (port); |
| 252 | exit (0); |
| 253 | } else { |
| 254 | /* parent. wait a bit for the socket to be created. */ |
| 255 | sleep (1); |
| 256 | |
| 257 | /* create a ThriftSocket */ |
| 258 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 259 | "port", port, NULL); |
| 260 | transport = THRIFT_TRANSPORT (tsocket); |
| 261 | thrift_transport_open (transport, NULL); |
| 262 | assert (thrift_transport_is_open (transport)); |
| 263 | |
| 264 | /* create a ThriftCompactTransport */ |
| 265 | tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport", |
| 266 | tsocket, NULL); |
| 267 | protocol = THRIFT_PROTOCOL (tc); |
| 268 | assert (protocol != NULL); |
| 269 | |
| 270 | /* test structures */ |
| 271 | assert (thrift_compact_protocol_write_struct_begin (protocol, |
| 272 | NULL, NULL) == 0); |
| 273 | assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0); |
| 274 | |
| 275 | /* test field state w.r.t. deltas */ |
| 276 | |
| 277 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 278 | T_DOUBLE, 1, NULL) == 1); |
| 279 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 280 | T_DOUBLE, |
| 281 | 16, NULL) == 1); |
| 282 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 283 | T_DOUBLE, |
| 284 | 17, NULL) == 1); |
| 285 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 286 | T_DOUBLE, |
| 287 | 15, NULL) > 1); |
| 288 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 289 | T_DOUBLE, |
| 290 | 30, NULL) == 1); |
| 291 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 292 | T_DOUBLE, |
| 293 | 46, NULL) > 1); |
| 294 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 295 | T_DOUBLE, |
| 296 | 47, NULL) == 1); |
| 297 | |
| 298 | /* test fields */ |
| 299 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 300 | T_DOUBLE, |
| 301 | 1, NULL) > 1); |
| 302 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 303 | |
| 304 | /* test field state w.r.t. structs */ |
| 305 | |
| 306 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 307 | T_DOUBLE, |
| 308 | 1, NULL) > 1); |
| 309 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 310 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 311 | T_DOUBLE, |
| 312 | 16, NULL) == 1); |
| 313 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 314 | |
| 315 | assert (thrift_compact_protocol_write_struct_begin (protocol, |
| 316 | NULL, NULL) == 0); |
| 317 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 318 | T_DOUBLE, |
| 319 | 17, NULL) > 1); |
| 320 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 321 | |
| 322 | assert (thrift_compact_protocol_write_struct_begin (protocol, |
| 323 | NULL, NULL) == 0); |
| 324 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 325 | T_DOUBLE, |
| 326 | 18, NULL) > 1); |
| 327 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 328 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 329 | T_DOUBLE, |
| 330 | 19, NULL) == 1); |
| 331 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 332 | assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0); |
| 333 | |
| 334 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 335 | T_DOUBLE, |
| 336 | 18, NULL) == 1); |
| 337 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 338 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 339 | T_DOUBLE, |
| 340 | 25, NULL) == 1); |
| 341 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 342 | assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0); |
| 343 | |
| 344 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 345 | T_DOUBLE, |
| 346 | 17, NULL) == 1); |
| 347 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 348 | |
| 349 | /* test field state w.r.t. bools */ |
| 350 | |
| 351 | /* deltas */ |
| 352 | /* non-bool field -> bool field -> non-bool field */ |
| 353 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 354 | T_DOUBLE, |
| 355 | 18, NULL) == 1); |
| 356 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 357 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL, |
| 358 | 19, NULL) == 0); |
| 359 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, |
| 360 | NULL) == 1); |
| 361 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 362 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 363 | T_DOUBLE, |
| 364 | 20, NULL) == 1); |
| 365 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 366 | /* bool -> bool field -> bool */ |
| 367 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0); |
| 368 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL, |
| 369 | 21, NULL) == 0); |
| 370 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, |
| 371 | NULL) == 1); |
| 372 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 373 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0); |
| 374 | |
| 375 | /* no deltas */ |
| 376 | /* non-bool field -> bool field -> non-bool field */ |
| 377 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 378 | T_DOUBLE, |
| 379 | 1, NULL) > 1); |
| 380 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 381 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL, |
| 382 | 1, NULL) == 0); |
| 383 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 1); |
| 384 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 385 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 386 | T_DOUBLE, |
| 387 | 1, NULL) > 1); |
| 388 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 389 | /* bool -> bool field -> bool */ |
| 390 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0); |
| 391 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL, |
| 392 | 1, NULL) == 0); |
| 393 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 1); |
| 394 | assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0); |
| 395 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0); |
| 396 | |
| 397 | /* test write error */ |
| 398 | transport_write_error = 1; |
| 399 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 400 | T_DOUBLE, |
| 401 | 1, NULL) == -1); |
| 402 | transport_write_error = 0; |
| 403 | |
| 404 | /* test 2nd write error */ |
| 405 | transport_write_count = 0; |
| 406 | transport_write_error_at = 1; |
| 407 | assert (thrift_compact_protocol_write_field_begin (protocol, "test", |
| 408 | T_DOUBLE, |
| 409 | 1, NULL) == -1); |
| 410 | transport_write_error_at = -1; |
| 411 | |
| 412 | /* test 2nd read failure on a field */ |
| 413 | thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL); |
| 414 | |
| 415 | /* test write_field_stop */ |
| 416 | assert (thrift_compact_protocol_write_field_stop (protocol, NULL) > 0); |
| 417 | |
| 418 | /* write a map */ |
| 419 | assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE, |
| 420 | T_DOUBLE, |
| 421 | 1, NULL) > 0); |
| 422 | assert (thrift_compact_protocol_write_map_end (protocol, NULL) == 0); |
| 423 | |
| 424 | /* test 1st read failure on map---nothing to do on our side */ |
| 425 | |
| 426 | /* test 2nd read failure on a map */ |
| 427 | thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL); |
| 428 | |
| 429 | /* test 1st write failure on a map */ |
| 430 | transport_write_error = 1; |
| 431 | assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE, |
| 432 | T_DOUBLE, |
| 433 | 1, NULL) == -1); |
| 434 | transport_write_error = 0; |
| 435 | |
| 436 | /* test 2nd write failure on a map */ |
| 437 | transport_write_count = 0; |
| 438 | transport_write_error_at = 1; |
| 439 | assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE, |
| 440 | T_DOUBLE, |
| 441 | 1, NULL) == -1); |
| 442 | transport_write_error_at = -1; |
| 443 | |
| 444 | /* test negative map size */ |
| 445 | thrift_compact_protocol_write_varint32 (tc, -10, NULL); |
| 446 | thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL); |
| 447 | |
| 448 | /* test list operations */ |
| 449 | assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE, |
| 450 | 15, NULL) > 0); |
| 451 | assert (thrift_compact_protocol_write_list_end (protocol, NULL) == 0); |
| 452 | |
| 453 | /* test 1st read failure on a small list---nothing to do on our end */ |
| 454 | |
| 455 | /* test 1st read failure on a big list---nothing to do on our end */ |
| 456 | |
| 457 | /* test 2nd read failure on a big list */ |
| 458 | thrift_compact_protocol_write_byte (protocol, (gint8) 0xf0, NULL); |
| 459 | |
| 460 | /* test negative list size */ |
| 461 | thrift_compact_protocol_write_byte (protocol, (gint8) 0xf0, NULL); |
| 462 | thrift_compact_protocol_write_varint32 (tc, -10, NULL); |
| 463 | |
| 464 | /* test first write error on a small list */ |
| 465 | transport_write_error = 1; |
| 466 | assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE, |
| 467 | 14, NULL) == -1); |
| 468 | transport_write_error = 0; |
| 469 | |
| 470 | /* test first write error on a big list */ |
| 471 | transport_write_error = 1; |
| 472 | assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE, |
| 473 | 15, NULL) == -1); |
| 474 | transport_write_error = 0; |
| 475 | |
| 476 | /* test 2nd write error on a big list */ |
| 477 | transport_write_count = 0; |
| 478 | transport_write_error_at = 1; |
| 479 | assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE, |
| 480 | 15, NULL) == -1); |
| 481 | transport_write_error_at = -1; |
| 482 | |
| 483 | /* test set operation s*/ |
| 484 | assert (thrift_compact_protocol_write_set_begin (protocol, T_DOUBLE, |
| 485 | 1, NULL) > 0); |
| 486 | assert (thrift_compact_protocol_write_set_end (protocol, NULL) == 0); |
| 487 | |
| 488 | /* invalid protocol */ |
| 489 | assert (thrift_compact_protocol_write_byte (protocol, 0, NULL) > 0); |
| 490 | |
| 491 | /* invalid version */ |
| 492 | assert (thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, |
| 493 | NULL) > 0); |
| 494 | assert (thrift_compact_protocol_write_byte (protocol, 0, NULL) > 0); |
| 495 | |
| 496 | /* send a valid message */ |
| 497 | assert (thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, |
| 498 | NULL) > 0); |
| 499 | assert (thrift_compact_protocol_write_byte (protocol, 0x01u, NULL) > 0); |
| 500 | thrift_compact_protocol_write_varint32 (tc, 1, NULL); |
| 501 | thrift_compact_protocol_write_string (protocol, "test", NULL); |
| 502 | |
| 503 | /* broken 2nd read */ |
| 504 | thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL); |
| 505 | |
| 506 | /* send a broken 3rd read */ |
| 507 | thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL); |
| 508 | thrift_compact_protocol_write_byte (protocol, 0x01u, NULL); |
| 509 | |
| 510 | /* send a broken 4th read */ |
| 511 | thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL); |
| 512 | thrift_compact_protocol_write_byte (protocol, 0x01u, NULL); |
| 513 | thrift_compact_protocol_write_varint32 (tc, 1, NULL); |
| 514 | |
| 515 | /* send a valid message */ |
| 516 | assert (thrift_compact_protocol_write_message_begin (protocol, "test", |
| 517 | T_CALL, 1, NULL) > 0); |
| 518 | |
| 519 | assert (thrift_compact_protocol_write_message_end (protocol, NULL) == 0); |
| 520 | |
| 521 | /* send broken writes */ |
| 522 | transport_write_error = 1; |
| 523 | assert (thrift_compact_protocol_write_message_begin (protocol, "test", |
| 524 | T_CALL, 1, NULL) == -1); |
| 525 | transport_write_error = 0; |
| 526 | |
| 527 | transport_write_count = 0; |
| 528 | transport_write_error_at = 1; |
| 529 | assert (thrift_compact_protocol_write_message_begin (protocol, "test", |
| 530 | T_CALL, 1, NULL) == -1); |
| 531 | transport_write_error_at = -1; |
| 532 | |
| 533 | transport_write_count = 0; |
| 534 | transport_write_error_at = 2; |
| 535 | assert (thrift_compact_protocol_write_message_begin (protocol, "test", |
| 536 | T_CALL, 1, NULL) == -1); |
| 537 | transport_write_error_at = -1; |
| 538 | |
| 539 | transport_write_count = 0; |
| 540 | transport_write_error_at = 3; |
| 541 | assert (thrift_compact_protocol_write_message_begin (protocol, "test", |
| 542 | T_CALL, 1, NULL) == -1); |
| 543 | transport_write_error_at = -1; |
| 544 | |
| 545 | /* clean up */ |
| 546 | thrift_transport_close (transport, NULL); |
| 547 | g_object_unref (tsocket); |
| 548 | g_object_unref (protocol); |
| 549 | assert (wait (&status) == pid); |
| 550 | assert (status == 0); |
| 551 | } |
| 552 | } |
| 553 | |
Chandler May | 1ccd81b | 2016-02-11 08:25:25 -0500 | [diff] [blame^] | 554 | static void |
| 555 | test_read_and_write_many_frames (void) |
| 556 | { |
| 557 | int status; |
| 558 | pid_t pid; |
| 559 | ThriftSocket *tsocket = NULL; |
| 560 | ThriftTransport *transport = NULL; |
| 561 | ThriftFramedTransport *ft = NULL; |
| 562 | ThriftCompactProtocol *tc = NULL; |
| 563 | ThriftProtocol *protocol = NULL; |
| 564 | gpointer binary = (gpointer *) TEST_STRING; |
| 565 | const guint32 len = strlen (TEST_STRING); |
| 566 | int port = TEST_PORT; |
| 567 | |
| 568 | /* fork a server from the client */ |
| 569 | pid = fork (); |
| 570 | assert (pid >= 0); |
| 571 | |
| 572 | if (pid == 0) |
| 573 | { |
| 574 | /* child listens */ |
| 575 | thrift_server_many_frames (port); |
| 576 | exit (0); |
| 577 | } else { |
| 578 | /* parent. wait a bit for the socket to be created. */ |
| 579 | sleep (1); |
| 580 | |
| 581 | /* create a ThriftSocket */ |
| 582 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 583 | "port", port, NULL); |
| 584 | assert (tsocket != NULL); |
| 585 | transport = THRIFT_TRANSPORT (tsocket); |
| 586 | |
| 587 | /* wrap in a framed transport */ |
| 588 | ft = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport", transport, |
| 589 | "w_buf_size", 1, NULL); |
| 590 | assert (ft != NULL); |
| 591 | transport = THRIFT_TRANSPORT (ft); |
| 592 | |
| 593 | thrift_transport_open (transport, NULL); |
| 594 | assert (thrift_transport_is_open (transport)); |
| 595 | |
| 596 | /* create a compact protocol */ |
| 597 | tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport", |
| 598 | transport, NULL); |
| 599 | protocol = THRIFT_PROTOCOL (tc); |
| 600 | assert (protocol != NULL); |
| 601 | |
| 602 | /* write a bunch of primitives */ |
| 603 | assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, |
| 604 | NULL) > 0); |
| 605 | thrift_transport_flush (transport, NULL); |
| 606 | assert (thrift_compact_protocol_write_byte (protocol, TEST_BYTE, |
| 607 | NULL) > 0); |
| 608 | thrift_transport_flush (transport, NULL); |
| 609 | assert (thrift_compact_protocol_write_i16 (protocol, TEST_I16, NULL) > 0); |
| 610 | thrift_transport_flush (transport, NULL); |
| 611 | assert (thrift_compact_protocol_write_i32 (protocol, TEST_I32, NULL) > 0); |
| 612 | thrift_transport_flush (transport, NULL); |
| 613 | assert (thrift_compact_protocol_write_i64 (protocol, TEST_I64, NULL) > 0); |
| 614 | thrift_transport_flush (transport, NULL); |
| 615 | assert (thrift_compact_protocol_write_i16 (protocol, TEST_NI16, NULL) > 0); |
| 616 | thrift_transport_flush (transport, NULL); |
| 617 | assert (thrift_compact_protocol_write_i32 (protocol, TEST_NI32, NULL) > 0); |
| 618 | thrift_transport_flush (transport, NULL); |
| 619 | assert (thrift_compact_protocol_write_i64 (protocol, TEST_NI64, NULL) > 0); |
| 620 | thrift_transport_flush (transport, NULL); |
| 621 | assert (thrift_compact_protocol_write_i16 (protocol, 2, NULL) > 0); |
| 622 | thrift_transport_flush (transport, NULL); |
| 623 | assert (thrift_compact_protocol_write_i32 (protocol, 2, NULL) > 0); |
| 624 | thrift_transport_flush (transport, NULL); |
| 625 | assert (thrift_compact_protocol_write_i64 (protocol, 2, NULL) > 0); |
| 626 | thrift_transport_flush (transport, NULL); |
| 627 | assert (thrift_compact_protocol_write_i16 (protocol, -2, NULL) > 0); |
| 628 | thrift_transport_flush (transport, NULL); |
| 629 | assert (thrift_compact_protocol_write_i32 (protocol, -2, NULL) > 0); |
| 630 | thrift_transport_flush (transport, NULL); |
| 631 | assert (thrift_compact_protocol_write_i64 (protocol, -2, NULL) > 0); |
| 632 | thrift_transport_flush (transport, NULL); |
| 633 | assert (thrift_compact_protocol_write_double (protocol, |
| 634 | TEST_DOUBLE, NULL) > 0); |
| 635 | thrift_transport_flush (transport, NULL); |
| 636 | assert (thrift_compact_protocol_write_string (protocol, |
| 637 | TEST_STRING, NULL) > 0); |
| 638 | thrift_transport_flush (transport, NULL); |
| 639 | assert (thrift_compact_protocol_write_binary (protocol, binary, |
| 640 | len, NULL) > 0); |
| 641 | thrift_transport_flush (transport, NULL); |
| 642 | assert (thrift_compact_protocol_write_binary (protocol, NULL, |
| 643 | 0, NULL) > 0); |
| 644 | thrift_transport_flush (transport, NULL); |
| 645 | assert (thrift_compact_protocol_write_binary (protocol, binary, |
| 646 | len, NULL) > 0); |
| 647 | thrift_transport_flush (transport, NULL); |
| 648 | |
| 649 | /* clean up */ |
| 650 | thrift_transport_write_end (transport, NULL); |
| 651 | thrift_transport_close (transport, NULL); |
| 652 | g_object_unref (ft); |
| 653 | g_object_unref (tsocket); |
| 654 | g_object_unref (tc); |
| 655 | assert (wait (&status) == pid); |
| 656 | assert (status == 0); |
| 657 | } |
| 658 | } |
| 659 | |
Chandler May | 6dde90b | 2016-01-10 06:01:10 +0000 | [diff] [blame] | 660 | |
| 661 | static void |
| 662 | thrift_server_primitives (const int port) |
| 663 | { |
| 664 | ThriftServerTransport *transport = NULL; |
| 665 | ThriftTransport *client = NULL; |
| 666 | ThriftCompactProtocol *tc = NULL; |
| 667 | ThriftProtocol *protocol = NULL; |
| 668 | gboolean value_boolean = FALSE; |
| 669 | gint8 value_byte = 0, |
| 670 | zigzag_p16 = 0, zigzag_p32 = 0, zigzag_p64 = 0, |
| 671 | zigzag_n16 = 0, zigzag_n32 = 0, zigzag_n64 = 0; |
| 672 | gint16 value_16 = 0; |
| 673 | gint32 value_32 = 0; |
| 674 | gint64 value_64 = 0; |
| 675 | gint16 value_n16 = 0; |
| 676 | gint32 value_n32 = 0; |
| 677 | gint64 value_n64 = 0; |
| 678 | gdouble value_double = 0; |
| 679 | gchar *string = NULL; |
| 680 | gpointer binary = NULL; |
| 681 | guint32 len = 0; |
| 682 | void *comparator = (void *) TEST_STRING; |
| 683 | |
| 684 | ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, |
| 685 | "port", port, NULL); |
| 686 | transport = THRIFT_SERVER_TRANSPORT (tsocket); |
| 687 | thrift_server_transport_listen (transport, NULL); |
| 688 | client = thrift_server_transport_accept (transport, NULL); |
| 689 | assert (client != NULL); |
| 690 | |
| 691 | tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport", |
| 692 | client, NULL); |
| 693 | protocol = THRIFT_PROTOCOL (tc); |
| 694 | |
| 695 | assert (thrift_compact_protocol_read_bool (protocol, |
| 696 | &value_boolean, NULL) > 0); |
| 697 | assert (thrift_compact_protocol_read_byte (protocol, &value_byte, NULL) > 0); |
| 698 | assert (thrift_compact_protocol_read_i16 (protocol, &value_16, NULL) > 0); |
| 699 | assert (thrift_compact_protocol_read_i32 (protocol, &value_32, NULL) > 0); |
| 700 | assert (thrift_compact_protocol_read_i64 (protocol, &value_64, NULL) > 0); |
| 701 | assert (thrift_compact_protocol_read_i16 (protocol, &value_n16, NULL) > 0); |
| 702 | assert (thrift_compact_protocol_read_i32 (protocol, &value_n32, NULL) > 0); |
| 703 | assert (thrift_compact_protocol_read_i64 (protocol, &value_n64, NULL) > 0); |
| 704 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p16, NULL) > 0); |
| 705 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p32, NULL) > 0); |
| 706 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p64, NULL) > 0); |
| 707 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n16, NULL) > 0); |
| 708 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n32, NULL) > 0); |
| 709 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n64, NULL) > 0); |
| 710 | assert (thrift_compact_protocol_read_double (protocol, |
| 711 | &value_double, NULL) > 0); |
| 712 | assert (thrift_compact_protocol_read_string (protocol, &string, NULL) > 0); |
| 713 | assert (thrift_compact_protocol_read_binary (protocol, &binary, |
| 714 | &len, NULL) > 0); |
| 715 | |
| 716 | assert (value_boolean == TEST_BOOL); |
| 717 | assert (value_byte == TEST_BYTE); |
| 718 | assert (value_16 == TEST_I16); |
| 719 | assert (value_32 == TEST_I32); |
| 720 | assert (value_64 == TEST_I64); |
| 721 | assert (value_n16 == TEST_NI16); |
| 722 | assert (value_n32 == TEST_NI32); |
| 723 | assert (value_n64 == TEST_NI64); |
| 724 | assert (zigzag_p16 == 4); |
| 725 | assert (zigzag_p32 == 4); |
| 726 | assert (zigzag_p64 == 4); |
| 727 | assert (zigzag_n16 == 3); |
| 728 | assert (zigzag_n32 == 3); |
| 729 | assert (zigzag_n64 == 3); |
| 730 | assert (value_double == TEST_DOUBLE); |
| 731 | assert (strcmp (TEST_STRING, string) == 0); |
| 732 | assert (memcmp (comparator, binary, len) == 0); |
| 733 | |
| 734 | g_free (string); |
| 735 | g_free (binary); |
| 736 | |
| 737 | thrift_compact_protocol_read_binary (protocol, &binary, &len, NULL); |
| 738 | g_free (binary); |
| 739 | |
| 740 | transport_read_count = 0; |
| 741 | transport_read_error_at = 0; |
| 742 | assert (thrift_compact_protocol_read_binary (protocol, &binary, |
| 743 | &len, NULL) == -1); |
| 744 | transport_read_error_at = -1; |
| 745 | |
| 746 | transport_read_count = 0; |
| 747 | transport_read_error_at = 1; |
| 748 | assert (thrift_compact_protocol_read_binary (protocol, &binary, |
| 749 | &len, NULL) == -1); |
| 750 | transport_read_error_at = -1; |
| 751 | |
| 752 | transport_read_error = 1; |
| 753 | assert (thrift_compact_protocol_read_bool (protocol, |
| 754 | &value_boolean, NULL) == -1); |
| 755 | assert (thrift_compact_protocol_read_byte (protocol, |
| 756 | &value_byte, NULL) == -1); |
| 757 | assert (thrift_compact_protocol_read_i16 (protocol, |
| 758 | &value_16, NULL) == -1); |
| 759 | assert (thrift_compact_protocol_read_i32 (protocol, &value_32, NULL) == -1); |
| 760 | assert (thrift_compact_protocol_read_i64 (protocol, &value_64, NULL) == -1); |
| 761 | assert (thrift_compact_protocol_read_i16 (protocol, |
| 762 | &value_n16, NULL) == -1); |
| 763 | assert (thrift_compact_protocol_read_i32 (protocol, &value_n32, NULL) == -1); |
| 764 | assert (thrift_compact_protocol_read_i64 (protocol, &value_n64, NULL) == -1); |
| 765 | assert (thrift_compact_protocol_read_double (protocol, |
| 766 | &value_double, NULL) == -1); |
| 767 | transport_read_error = 0; |
| 768 | |
| 769 | /* test partial write failure */ |
| 770 | thrift_protocol_read_i32 (protocol, &value_32, NULL); |
| 771 | |
| 772 | thrift_transport_read_end (client, NULL); |
| 773 | thrift_transport_close (client, NULL); |
| 774 | |
| 775 | g_object_unref (tc); |
| 776 | g_object_unref (client); |
| 777 | g_object_unref (tsocket); |
| 778 | } |
| 779 | |
| 780 | static void |
| 781 | thrift_server_complex_types (const int port) |
| 782 | { |
| 783 | ThriftServerTransport *transport = NULL; |
| 784 | ThriftTransport *client = NULL; |
| 785 | ThriftCompactProtocol *tc = NULL; |
| 786 | ThriftProtocol *protocol = NULL; |
| 787 | gchar *struct_name = NULL; |
| 788 | gchar *field_name = NULL; |
| 789 | gchar *message_name = NULL; |
| 790 | ThriftType element_type, key_type, value_type, field_type; |
| 791 | ThriftMessageType message_type; |
| 792 | gboolean value_boolean = ! TEST_BOOL; |
| 793 | gint8 value = 0; |
| 794 | gint16 field_id = 0; |
| 795 | guint32 size = 0; |
| 796 | gint32 seqid = 0; |
| 797 | gint8 version_and_type = 0; |
| 798 | gint8 protocol_id = 0; |
| 799 | |
| 800 | ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, |
| 801 | "port", port, NULL); |
| 802 | transport = THRIFT_SERVER_TRANSPORT (tsocket); |
| 803 | thrift_server_transport_listen (transport, NULL); |
| 804 | client = thrift_server_transport_accept (transport, NULL); |
| 805 | assert (client != NULL); |
| 806 | |
| 807 | tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport", |
| 808 | client, NULL); |
| 809 | protocol = THRIFT_PROTOCOL (tc); |
| 810 | |
| 811 | /* test struct operations */ |
| 812 | |
| 813 | thrift_compact_protocol_read_struct_begin (protocol, &struct_name, NULL); |
| 814 | thrift_compact_protocol_read_struct_end (protocol, NULL); |
| 815 | |
| 816 | /* test field state w.r.t. deltas */ |
| 817 | |
| 818 | field_id = 0; |
| 819 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 820 | &field_type, |
| 821 | &field_id, NULL) == 1); |
| 822 | assert (field_id == 1); |
| 823 | field_id = 0; |
| 824 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 825 | &field_type, |
| 826 | &field_id, NULL) == 1); |
| 827 | assert (field_id == 16); |
| 828 | field_id = 0; |
| 829 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 830 | &field_type, |
| 831 | &field_id, NULL) == 1); |
| 832 | assert (field_id == 17); |
| 833 | field_id = 0; |
| 834 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 835 | &field_type, |
| 836 | &field_id, NULL) > 1); |
| 837 | assert (field_id == 15); |
| 838 | field_id = 0; |
| 839 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 840 | &field_type, |
| 841 | &field_id, NULL) == 1); |
| 842 | assert (field_id == 30); |
| 843 | field_id = 0; |
| 844 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 845 | &field_type, |
| 846 | &field_id, NULL) > 1); |
| 847 | assert (field_id == 46); |
| 848 | field_id = 0; |
| 849 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 850 | &field_type, |
| 851 | &field_id, NULL) == 1); |
| 852 | assert (field_id == 47); |
| 853 | field_id = 0; |
| 854 | |
| 855 | /* test field operations */ |
| 856 | |
| 857 | thrift_compact_protocol_read_field_begin (protocol, &field_name, &field_type, |
| 858 | &field_id, NULL); |
| 859 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 860 | |
| 861 | /* test field state w.r.t. structs */ |
| 862 | |
| 863 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 864 | &field_type, |
| 865 | &field_id, NULL) > 1); |
| 866 | assert (field_id == 1); |
| 867 | field_id = 0; |
| 868 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 869 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 870 | &field_type, |
| 871 | &field_id, NULL) == 1); |
| 872 | assert (field_id == 16); |
| 873 | field_id = 0; |
| 874 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 875 | |
| 876 | assert (thrift_compact_protocol_read_struct_begin (protocol, |
| 877 | &struct_name, NULL) == 0); |
| 878 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 879 | &field_type, |
| 880 | &field_id, NULL) > 1); |
| 881 | assert (field_id == 17); |
| 882 | field_id = 0; |
| 883 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 884 | |
| 885 | assert (thrift_compact_protocol_read_struct_begin (protocol, |
| 886 | &struct_name, NULL) == 0); |
| 887 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 888 | &field_type, |
| 889 | &field_id, NULL) > 1); |
| 890 | assert (field_id == 18); |
| 891 | field_id = 0; |
| 892 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 893 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 894 | &field_type, |
| 895 | &field_id, NULL) == 1); |
| 896 | assert (field_id == 19); |
| 897 | field_id = 0; |
| 898 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 899 | assert (thrift_compact_protocol_read_struct_end (protocol, NULL) == 0); |
| 900 | |
| 901 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 902 | &field_type, |
| 903 | &field_id, NULL) == 1); |
| 904 | assert (field_id == 18); |
| 905 | field_id = 0; |
| 906 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 907 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 908 | &field_type, |
| 909 | &field_id, NULL) == 1); |
| 910 | assert (field_id == 25); |
| 911 | field_id = 0; |
| 912 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 913 | assert (thrift_compact_protocol_read_struct_end (protocol, NULL) == 0); |
| 914 | |
| 915 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 916 | &field_type, |
| 917 | &field_id, NULL) == 1); |
| 918 | assert (field_id == 17); |
| 919 | field_id = 0; |
| 920 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 921 | |
| 922 | /* test field state w.r.t. bools */ |
| 923 | |
| 924 | /* deltas */ |
| 925 | /* non-bool field -> bool field -> non-bool field */ |
| 926 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 927 | &field_type, |
| 928 | &field_id, NULL) == 1); |
| 929 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 930 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 931 | &field_type, |
| 932 | &field_id, NULL) == 1); |
| 933 | assert (field_type == T_BOOL); |
| 934 | assert (thrift_compact_protocol_read_bool (protocol, |
| 935 | &value_boolean, NULL) == 0); |
| 936 | assert (value_boolean == TEST_BOOL); |
| 937 | value_boolean = ! TEST_BOOL; |
| 938 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 939 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 940 | &field_type, |
| 941 | &field_id, NULL) == 1); |
| 942 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 943 | /* bool -> bool field -> bool */ |
| 944 | assert (thrift_compact_protocol_read_bool (protocol, |
| 945 | &value_boolean, NULL) > 0); |
| 946 | assert (value_boolean == TEST_BOOL); |
| 947 | value_boolean = ! TEST_BOOL; |
| 948 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 949 | &field_type, |
| 950 | &field_id, NULL) == 1); |
| 951 | assert (field_type == T_BOOL); |
| 952 | assert (thrift_compact_protocol_read_bool (protocol, |
| 953 | &value_boolean, NULL) == 0); |
| 954 | assert (value_boolean == TEST_BOOL); |
| 955 | value_boolean = ! TEST_BOOL; |
| 956 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 957 | assert (thrift_compact_protocol_read_bool (protocol, |
| 958 | &value_boolean, NULL) > 0); |
| 959 | assert (value_boolean == TEST_BOOL); |
| 960 | value_boolean = ! TEST_BOOL; |
| 961 | |
| 962 | /* no deltas */ |
| 963 | /* non-bool field -> bool field -> non-bool field */ |
| 964 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 965 | &field_type, |
| 966 | &field_id, NULL) > 1); |
| 967 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 968 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 969 | &field_type, |
| 970 | &field_id, NULL) > 1); |
| 971 | assert (field_type == T_BOOL); |
| 972 | assert (thrift_compact_protocol_read_bool (protocol, |
| 973 | &value_boolean, NULL) == 0); |
| 974 | assert (value_boolean == TEST_BOOL); |
| 975 | value_boolean = ! TEST_BOOL; |
| 976 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 977 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 978 | &field_type, |
| 979 | &field_id, NULL) > 1); |
| 980 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 981 | /* bool -> bool field -> bool */ |
| 982 | assert (thrift_compact_protocol_read_bool (protocol, |
| 983 | &value_boolean, NULL) > 0); |
| 984 | assert (value_boolean == TEST_BOOL); |
| 985 | value_boolean = ! TEST_BOOL; |
| 986 | assert (thrift_compact_protocol_read_field_begin (protocol, &field_name, |
| 987 | &field_type, |
| 988 | &field_id, NULL) > 1); |
| 989 | assert (field_type == T_BOOL); |
| 990 | assert (thrift_compact_protocol_read_bool (protocol, |
| 991 | &value_boolean, NULL) == 0); |
| 992 | assert (value_boolean == TEST_BOOL); |
| 993 | value_boolean = ! TEST_BOOL; |
| 994 | thrift_compact_protocol_read_field_end (protocol, NULL); |
| 995 | assert (thrift_compact_protocol_read_bool (protocol, |
| 996 | &value_boolean, NULL) > 0); |
| 997 | assert (value_boolean == TEST_BOOL); |
| 998 | value_boolean = ! TEST_BOOL; |
| 999 | |
| 1000 | /* test first read error on a field */ |
| 1001 | transport_read_error = 1; |
| 1002 | assert (thrift_compact_protocol_read_field_begin (protocol, |
| 1003 | &field_name, &field_type, |
| 1004 | &field_id, NULL) == -1); |
| 1005 | transport_read_error = 0; |
| 1006 | |
| 1007 | /* test 2nd write failure */ |
| 1008 | thrift_compact_protocol_read_byte (protocol, &value, NULL); |
| 1009 | |
| 1010 | /* test 2nd read failure on a field */ |
| 1011 | transport_read_count = 0; |
| 1012 | transport_read_error_at = 1; |
| 1013 | assert (thrift_compact_protocol_read_field_begin (protocol, |
| 1014 | &field_name, &field_type, |
| 1015 | &field_id, NULL) == -1); |
| 1016 | transport_read_error_at = -1; |
| 1017 | |
| 1018 | /* test field stop */ |
| 1019 | thrift_compact_protocol_read_field_begin (protocol, &field_name, &field_type, |
| 1020 | &field_id, NULL); |
| 1021 | |
| 1022 | /* test map operations */ |
| 1023 | |
| 1024 | thrift_compact_protocol_read_map_begin (protocol, &key_type, &value_type, |
| 1025 | &size, NULL); |
| 1026 | thrift_compact_protocol_read_map_end (protocol, NULL); |
| 1027 | |
| 1028 | /* test 1st read failure on a map */ |
| 1029 | transport_read_count = 0; |
| 1030 | transport_read_error_at = 0; |
| 1031 | assert (thrift_compact_protocol_read_map_begin (protocol, |
| 1032 | &key_type, &value_type, |
| 1033 | &size, NULL) == -1); |
| 1034 | transport_read_error_at = -1; |
| 1035 | |
| 1036 | /* test 2nd read failure on a map */ |
| 1037 | transport_read_count = 0; |
| 1038 | transport_read_error_at = 1; |
| 1039 | assert (thrift_compact_protocol_read_map_begin (protocol, |
| 1040 | &key_type, &value_type, |
| 1041 | &size, NULL) == -1); |
| 1042 | transport_read_error_at = -1; |
| 1043 | |
| 1044 | /* test 1st write failure on map---nothing to do on our side */ |
| 1045 | |
| 1046 | /* test 2nd write failure */ |
| 1047 | thrift_compact_protocol_read_byte (protocol, &value, NULL); |
| 1048 | |
| 1049 | /* test negative map size */ |
| 1050 | assert (thrift_compact_protocol_read_map_begin (protocol, |
| 1051 | &key_type, &value_type, |
| 1052 | &size, NULL) == -1); |
| 1053 | |
| 1054 | /* test list operations */ |
| 1055 | thrift_compact_protocol_read_list_begin (protocol, &element_type, &size, |
| 1056 | NULL); |
| 1057 | thrift_compact_protocol_read_list_end (protocol, NULL); |
| 1058 | |
| 1059 | /* test small list 1st read failure */ |
| 1060 | transport_read_error = 1; |
| 1061 | assert (thrift_compact_protocol_read_list_begin (protocol, &element_type, |
| 1062 | &size, NULL) == -1); |
| 1063 | transport_read_error = 0; |
| 1064 | |
| 1065 | /* test big list 1st read failure */ |
| 1066 | transport_read_error = 1; |
| 1067 | assert (thrift_compact_protocol_read_list_begin (protocol, &element_type, |
| 1068 | &size, NULL) == -1); |
| 1069 | transport_read_error = 0; |
| 1070 | |
| 1071 | /* test big list 2nd read failure */ |
| 1072 | transport_read_count = 0; |
| 1073 | transport_read_error_at = 1; |
| 1074 | thrift_compact_protocol_read_list_begin (protocol, &element_type, &size, |
| 1075 | NULL); |
| 1076 | transport_read_error_at = -1; |
| 1077 | |
| 1078 | /* test negative list size failure */ |
| 1079 | thrift_compact_protocol_read_list_begin (protocol, &element_type, &size, |
| 1080 | NULL); |
| 1081 | |
| 1082 | /* test small list 1st write failure---nothing to do on our end */ |
| 1083 | |
| 1084 | /* test big list 1st write failure---nothing to do on our end */ |
| 1085 | |
| 1086 | /* test big list 2nd write failure */ |
| 1087 | thrift_compact_protocol_read_byte (protocol, &value, NULL); |
| 1088 | |
| 1089 | /* test set operations */ |
| 1090 | thrift_compact_protocol_read_set_begin (protocol, &element_type, &size, NULL); |
| 1091 | thrift_compact_protocol_read_set_end (protocol, NULL); |
| 1092 | |
| 1093 | /* broken read */ |
| 1094 | transport_read_error = 1; |
| 1095 | assert (thrift_compact_protocol_read_message_begin (protocol, &message_name, |
| 1096 | &message_type, &seqid, |
| 1097 | NULL) == -1); |
| 1098 | transport_read_error = 0; |
| 1099 | |
| 1100 | /* invalid protocol */ |
| 1101 | assert (thrift_compact_protocol_read_message_begin (protocol, &message_name, |
| 1102 | &message_type, &seqid, |
| 1103 | NULL) == -1); |
| 1104 | |
| 1105 | /* invalid version */ |
| 1106 | assert (thrift_compact_protocol_read_message_begin (protocol, &message_name, |
| 1107 | &message_type, &seqid, |
| 1108 | NULL) == -1); |
| 1109 | |
| 1110 | /* read a valid message */ |
| 1111 | assert (thrift_compact_protocol_read_message_begin (protocol, &message_name, |
| 1112 | &message_type, &seqid, |
| 1113 | NULL) > 0); |
| 1114 | g_free (message_name); |
| 1115 | |
| 1116 | /* broken 2nd read on a message */ |
| 1117 | transport_read_count = 0; |
| 1118 | transport_read_error_at = 1; |
| 1119 | assert (thrift_compact_protocol_read_message_begin (protocol, &message_name, |
| 1120 | &message_type, &seqid, |
| 1121 | NULL) == -1); |
| 1122 | transport_read_error_at = -1; |
| 1123 | |
| 1124 | /* broken 3rd read on a message */ |
| 1125 | transport_read_count = 0; |
| 1126 | transport_read_error_at = 2; |
| 1127 | assert (thrift_compact_protocol_read_message_begin (protocol, &message_name, |
| 1128 | &message_type, &seqid, |
| 1129 | NULL) == -1); |
| 1130 | transport_read_error_at = -1; |
| 1131 | |
| 1132 | /* broken 4th read on a message */ |
| 1133 | transport_read_count = 0; |
| 1134 | transport_read_error_at = 3; |
| 1135 | assert (thrift_compact_protocol_read_message_begin (protocol, &message_name, |
| 1136 | &message_type, &seqid, |
| 1137 | NULL) == -1); |
| 1138 | transport_read_error_at = -1; |
| 1139 | |
| 1140 | /* read a valid message */ |
| 1141 | assert (thrift_compact_protocol_read_message_begin (protocol, &message_name, |
| 1142 | &message_type, &seqid, |
| 1143 | NULL) > 0); |
| 1144 | g_free (message_name); |
| 1145 | |
| 1146 | assert (thrift_compact_protocol_read_message_end (protocol, NULL) == 0); |
| 1147 | |
| 1148 | /* handle 2nd write failure on a message */ |
| 1149 | thrift_compact_protocol_read_byte (protocol, &protocol_id, NULL); |
| 1150 | |
| 1151 | /* handle 3rd write failure on a message */ |
| 1152 | thrift_compact_protocol_read_byte (protocol, &protocol_id, NULL); |
| 1153 | thrift_compact_protocol_read_byte (protocol, &version_and_type, NULL); |
| 1154 | |
| 1155 | /* handle 4th write failure on a message */ |
| 1156 | thrift_compact_protocol_read_byte (protocol, &protocol_id, NULL); |
| 1157 | thrift_compact_protocol_read_byte (protocol, &version_and_type, NULL); |
| 1158 | thrift_compact_protocol_read_varint32 (tc, &seqid, NULL); |
| 1159 | |
| 1160 | g_object_unref (client); |
| 1161 | g_object_unref (tsocket); |
| 1162 | } |
| 1163 | |
Chandler May | 1ccd81b | 2016-02-11 08:25:25 -0500 | [diff] [blame^] | 1164 | static void |
| 1165 | thrift_server_many_frames (const int port) |
| 1166 | { |
| 1167 | ThriftServerTransport *transport = NULL; |
| 1168 | ThriftTransport *client = NULL; |
| 1169 | ThriftCompactProtocol *tcp = NULL; |
| 1170 | ThriftProtocol *protocol = NULL; |
| 1171 | ThriftServerSocket *tsocket = NULL; |
| 1172 | gboolean value_boolean = FALSE; |
| 1173 | gint8 value_byte = 0, |
| 1174 | zigzag_p16 = 0, zigzag_p32 = 0, zigzag_p64 = 0, |
| 1175 | zigzag_n16 = 0, zigzag_n32 = 0, zigzag_n64 = 0; |
| 1176 | gint16 value_16 = 0; |
| 1177 | gint32 value_32 = 0; |
| 1178 | gint64 value_64 = 0; |
| 1179 | gint16 value_n16 = 0; |
| 1180 | gint32 value_n32 = 0; |
| 1181 | gint64 value_n64 = 0; |
| 1182 | gdouble value_double = 0; |
| 1183 | gchar *string = NULL; |
| 1184 | gpointer binary = NULL; |
| 1185 | guint32 len = 0; |
| 1186 | void *comparator = (void *) TEST_STRING; |
| 1187 | |
| 1188 | tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", port, NULL); |
| 1189 | transport = THRIFT_SERVER_TRANSPORT (tsocket); |
| 1190 | thrift_server_transport_listen (transport, NULL); |
| 1191 | |
| 1192 | /* wrap the client in a framed transport */ |
| 1193 | client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport", |
| 1194 | thrift_server_transport_accept (transport, NULL), |
| 1195 | "r_buf_size", 1, NULL); |
| 1196 | assert (client != NULL); |
| 1197 | |
| 1198 | tcp = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport", |
| 1199 | client, NULL); |
| 1200 | protocol = THRIFT_PROTOCOL (tcp); |
| 1201 | |
| 1202 | assert (thrift_compact_protocol_read_bool (protocol, |
| 1203 | &value_boolean, NULL) > 0); |
| 1204 | assert (thrift_compact_protocol_read_byte (protocol, &value_byte, NULL) > 0); |
| 1205 | assert (thrift_compact_protocol_read_i16 (protocol, &value_16, NULL) > 0); |
| 1206 | assert (thrift_compact_protocol_read_i32 (protocol, &value_32, NULL) > 0); |
| 1207 | assert (thrift_compact_protocol_read_i64 (protocol, &value_64, NULL) > 0); |
| 1208 | assert (thrift_compact_protocol_read_i16 (protocol, &value_n16, NULL) > 0); |
| 1209 | assert (thrift_compact_protocol_read_i32 (protocol, &value_n32, NULL) > 0); |
| 1210 | assert (thrift_compact_protocol_read_i64 (protocol, &value_n64, NULL) > 0); |
| 1211 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p16, NULL) > 0); |
| 1212 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p32, NULL) > 0); |
| 1213 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_p64, NULL) > 0); |
| 1214 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n16, NULL) > 0); |
| 1215 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n32, NULL) > 0); |
| 1216 | assert (thrift_compact_protocol_read_byte (protocol, &zigzag_n64, NULL) > 0); |
| 1217 | assert (thrift_compact_protocol_read_double (protocol, |
| 1218 | &value_double, NULL) > 0); |
| 1219 | assert (thrift_compact_protocol_read_string (protocol, &string, NULL) > 0); |
| 1220 | assert (thrift_compact_protocol_read_binary (protocol, &binary, |
| 1221 | &len, NULL) > 0); |
| 1222 | |
| 1223 | assert (value_boolean == TEST_BOOL); |
| 1224 | assert (value_byte == TEST_BYTE); |
| 1225 | assert (value_16 == TEST_I16); |
| 1226 | assert (value_32 == TEST_I32); |
| 1227 | assert (value_64 == TEST_I64); |
| 1228 | assert (value_n16 == TEST_NI16); |
| 1229 | assert (value_n32 == TEST_NI32); |
| 1230 | assert (value_n64 == TEST_NI64); |
| 1231 | assert (zigzag_p16 == 4); |
| 1232 | assert (zigzag_p32 == 4); |
| 1233 | assert (zigzag_p64 == 4); |
| 1234 | assert (zigzag_n16 == 3); |
| 1235 | assert (zigzag_n32 == 3); |
| 1236 | assert (zigzag_n64 == 3); |
| 1237 | assert (value_double == TEST_DOUBLE); |
| 1238 | assert (strcmp (TEST_STRING, string) == 0); |
| 1239 | assert (memcmp (comparator, binary, len) == 0); |
| 1240 | |
| 1241 | g_free (string); |
| 1242 | g_free (binary); |
| 1243 | |
| 1244 | thrift_transport_read_end (client, NULL); |
| 1245 | thrift_transport_close (client, NULL); |
| 1246 | |
| 1247 | g_object_unref (tcp); |
| 1248 | g_object_unref (client); |
| 1249 | g_object_unref (tsocket); |
| 1250 | } |
| 1251 | |
Chandler May | 6dde90b | 2016-01-10 06:01:10 +0000 | [diff] [blame] | 1252 | int |
| 1253 | main (int argc, char *argv[]) |
| 1254 | { |
| 1255 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
| 1256 | g_type_init (); |
| 1257 | #endif |
| 1258 | |
| 1259 | g_test_init (&argc, &argv, NULL); |
| 1260 | |
| 1261 | g_test_add_func ("/testcompactprotocol/CreateAndDestroy", |
| 1262 | test_create_and_destroy); |
| 1263 | g_test_add_func ("/testcompactprotocol/Initialize", test_initialize); |
| 1264 | g_test_add_func ("/testcompactprotocol/ReadAndWritePrimitives", |
| 1265 | test_read_and_write_primitives); |
| 1266 | g_test_add_func ("/testcompactprotocol/ReadAndWriteComplexTypes", |
| 1267 | test_read_and_write_complex_types); |
Chandler May | 1ccd81b | 2016-02-11 08:25:25 -0500 | [diff] [blame^] | 1268 | g_test_add_func ("/testcompactprotocol/ReadAndWriteManyFrames", |
| 1269 | test_read_and_write_many_frames); |
Chandler May | 6dde90b | 2016-01-10 06:01:10 +0000 | [diff] [blame] | 1270 | |
| 1271 | return g_test_run (); |
| 1272 | } |