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