Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 20 | #include <netdb.h> |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 21 | #include <signal.h> |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 22 | #include <sys/wait.h> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 23 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 24 | #include <thrift/c_glib/transport/thrift_transport.h> |
| 25 | #include <thrift/c_glib/transport/thrift_socket.h> |
| 26 | #include <thrift/c_glib/transport/thrift_server_transport.h> |
| 27 | #include <thrift/c_glib/transport/thrift_server_socket.h> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 28 | |
| 29 | #define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' } |
| 30 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 31 | #include "../src/thrift/c_glib/transport/thrift_buffered_transport.c" |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 32 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 33 | static void thrift_server (const int port); |
| 34 | |
| 35 | /* test object creation and destruction */ |
| 36 | static void |
| 37 | test_create_and_destroy(void) |
| 38 | { |
| 39 | ThriftTransport *transport = NULL; |
| 40 | guint r_buf_size = 0; |
| 41 | guint w_buf_size = 0; |
| 42 | |
| 43 | GObject *object = NULL; |
| 44 | object = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, NULL); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 45 | g_assert (object != NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 46 | g_object_get (G_OBJECT (object), "transport", &transport, |
| 47 | "r_buf_size", &r_buf_size, |
| 48 | "w_buf_size", &w_buf_size, NULL); |
| 49 | g_object_unref (object); |
| 50 | } |
| 51 | |
| 52 | static void |
| 53 | test_open_and_close(void) |
| 54 | { |
| 55 | ThriftSocket *tsocket = NULL; |
| 56 | ThriftTransport *transport = NULL; |
| 57 | GError *err = NULL; |
| 58 | |
| 59 | /* create a ThriftSocket */ |
| 60 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 61 | "port", 51188, NULL); |
| 62 | |
| 63 | /* create a BufferedTransport wrapper of the Socket */ |
| 64 | transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, |
| 65 | "transport", THRIFT_TRANSPORT (tsocket), NULL); |
| 66 | |
| 67 | /* this shouldn't work */ |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 68 | g_assert (thrift_buffered_transport_open (transport, NULL) == FALSE); |
| 69 | g_assert (thrift_buffered_transport_is_open (transport) == TRUE); |
| 70 | g_assert (thrift_buffered_transport_close (transport, NULL) == TRUE); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 71 | g_object_unref (transport); |
| 72 | g_object_unref (tsocket); |
| 73 | |
| 74 | /* try and underlying socket failure */ |
| 75 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken", |
| 76 | NULL); |
| 77 | |
| 78 | /* create a BufferedTransport wrapper of the Socket */ |
| 79 | transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, |
| 80 | "transport", THRIFT_TRANSPORT (tsocket), NULL); |
| 81 | |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 82 | g_assert (thrift_buffered_transport_open (transport, &err) == FALSE); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 83 | g_object_unref (transport); |
| 84 | g_object_unref (tsocket); |
| 85 | g_error_free (err); |
| 86 | err = NULL; |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | test_read_and_write(void) |
| 91 | { |
| 92 | int status; |
| 93 | pid_t pid; |
| 94 | ThriftSocket *tsocket = NULL; |
| 95 | ThriftTransport *transport = NULL; |
| 96 | int port = 51199; |
| 97 | guchar buf[10] = TEST_DATA; /* a buffer */ |
| 98 | |
| 99 | pid = fork (); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 100 | g_assert ( pid >= 0 ); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 101 | |
| 102 | if ( pid == 0 ) |
| 103 | { |
| 104 | /* child listens */ |
| 105 | thrift_server (port); |
| 106 | exit (0); |
| 107 | } else { |
| 108 | /* parent connects, wait a bit for the socket to be created */ |
| 109 | sleep (1); |
| 110 | |
| 111 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 112 | "port", port, NULL); |
| 113 | transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, |
| 114 | "transport", THRIFT_TRANSPORT (tsocket), |
| 115 | "w_buf_size", 4, NULL); |
| 116 | |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 117 | g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE); |
| 118 | g_assert (thrift_buffered_transport_is_open (transport)); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 119 | |
| 120 | /* write 10 bytes */ |
| 121 | thrift_buffered_transport_write (transport, buf, 10, NULL); |
| 122 | |
| 123 | /* write 1 byte at a time */ |
| 124 | thrift_buffered_transport_write (transport, buf, 1, NULL); |
| 125 | thrift_buffered_transport_write (transport, buf, 1, NULL); |
| 126 | thrift_buffered_transport_write (transport, buf, 1, NULL); |
| 127 | |
| 128 | /* overflow the buffer */ |
| 129 | thrift_buffered_transport_write (transport, buf, 2, NULL); |
| 130 | thrift_buffered_transport_write (transport, buf, 1, NULL); |
| 131 | thrift_buffered_transport_flush (transport, NULL); |
| 132 | |
| 133 | /* write 1 byte and flush */ |
| 134 | thrift_buffered_transport_write (transport, buf, 1, NULL); |
| 135 | thrift_buffered_transport_flush (transport, NULL); |
| 136 | |
| 137 | /* write and overflow buffer with 2 system calls */ |
| 138 | thrift_buffered_transport_write (transport, buf, 1, NULL); |
| 139 | thrift_buffered_transport_write (transport, buf, 3, NULL); |
| 140 | |
| 141 | /* write 10 bytes */ |
| 142 | thrift_buffered_transport_write (transport, buf, 10, NULL); |
| 143 | |
| 144 | thrift_buffered_transport_write_end (transport, NULL); |
| 145 | thrift_buffered_transport_flush (transport, NULL); |
| 146 | thrift_buffered_transport_close (transport, NULL); |
| 147 | |
| 148 | g_object_unref (transport); |
| 149 | g_object_unref (tsocket); |
| 150 | |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 151 | g_assert ( wait (&status) == pid ); |
| 152 | g_assert ( status == 0 ); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | static void |
| 157 | thrift_server (const int port) |
| 158 | { |
| 159 | int bytes = 0; |
| 160 | ThriftServerTransport *transport = NULL; |
| 161 | ThriftTransport *client = NULL; |
| 162 | guchar buf[10]; /* a buffer */ |
| 163 | guchar match[10] = TEST_DATA; |
| 164 | |
| 165 | ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, |
| 166 | "port", port, NULL); |
| 167 | |
| 168 | transport = THRIFT_SERVER_TRANSPORT (tsocket); |
| 169 | thrift_server_transport_listen (transport, NULL); |
| 170 | |
| 171 | /* wrap the client in a BufferedTransport */ |
| 172 | client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport", |
| 173 | thrift_server_transport_accept (transport, NULL), |
| 174 | "r_buf_size", 5, NULL); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 175 | g_assert (client != NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 176 | |
| 177 | /* read 10 bytes */ |
| 178 | bytes = thrift_buffered_transport_read (client, buf, 10, NULL); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 179 | g_assert (bytes == 10); /* make sure we've read 10 bytes */ |
| 180 | g_assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */ |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 181 | |
| 182 | /* read 1 byte */ |
| 183 | bytes = thrift_buffered_transport_read (client, buf, 1, NULL); |
| 184 | |
| 185 | bytes = thrift_buffered_transport_read (client, buf, 6, NULL); |
| 186 | bytes = thrift_buffered_transport_read (client, buf, 2, NULL); |
| 187 | bytes = thrift_buffered_transport_read (client, buf, 1, NULL); |
| 188 | |
| 189 | thrift_buffered_transport_read_end (client, NULL); |
| 190 | thrift_buffered_transport_close (client, NULL); |
| 191 | g_object_unref (client); |
| 192 | g_object_unref (tsocket); |
| 193 | } |
| 194 | |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 195 | static void |
| 196 | test_write_fail(void) |
| 197 | { |
| 198 | int status; |
| 199 | pid_t pid; |
| 200 | ThriftSocket *tsocket = NULL; |
| 201 | ThriftTransport *transport = NULL; |
| 202 | int port = 51198; |
| 203 | guchar buf[10] = TEST_DATA; /* a buffer */ |
| 204 | |
| 205 | /* SIGPIPE when send to disconnected socket */ |
| 206 | signal(SIGPIPE, SIG_IGN); |
| 207 | |
| 208 | pid = fork (); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 209 | g_assert ( pid >= 0 ); |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 210 | |
| 211 | if ( pid == 0 ) |
| 212 | { |
| 213 | /* child listens */ |
| 214 | ThriftServerTransport *transport = NULL; |
| 215 | ThriftTransport *client = NULL; |
| 216 | |
| 217 | ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, |
| 218 | "port", port, NULL); |
| 219 | |
| 220 | transport = THRIFT_SERVER_TRANSPORT (tsocket); |
| 221 | thrift_server_transport_listen (transport, NULL); |
| 222 | |
| 223 | /* wrap the client in a BufferedTransport */ |
| 224 | client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport", |
| 225 | thrift_server_transport_accept (transport, NULL), |
| 226 | "r_buf_size", 5, NULL); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 227 | g_assert (client != NULL); |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 228 | |
| 229 | /* just close socket */ |
| 230 | thrift_buffered_transport_close (client, NULL); |
| 231 | g_object_unref (client); |
| 232 | g_object_unref (tsocket); |
| 233 | exit (0); |
| 234 | } else { |
| 235 | /* parent connects, wait a bit for the socket to be created */ |
| 236 | sleep (1); |
| 237 | |
| 238 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 239 | "port", port, NULL); |
| 240 | transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, |
| 241 | "transport", THRIFT_TRANSPORT (tsocket), |
| 242 | "w_buf_size", 4, NULL); |
| 243 | |
| 244 | |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 245 | g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE); |
| 246 | g_assert (thrift_buffered_transport_is_open (transport)); |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 247 | |
| 248 | /* recognize disconnection */ |
| 249 | sleep(1); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 250 | g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE); |
| 251 | g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE); |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 252 | |
| 253 | /* write and overflow buffer */ |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 254 | g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE); |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 255 | |
| 256 | /* write 1 and flush */ |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 257 | g_assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE); |
| 258 | g_assert (thrift_buffered_transport_flush (transport, NULL) == FALSE); |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 259 | |
| 260 | thrift_buffered_transport_close (transport, NULL); |
| 261 | |
| 262 | g_object_unref (transport); |
| 263 | g_object_unref (tsocket); |
| 264 | |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame^] | 265 | g_assert ( wait (&status) == pid ); |
| 266 | g_assert ( status == 0 ); |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 270 | int |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 271 | main(int argc, char *argv[]) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 272 | { |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 273 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 274 | g_type_init(); |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 275 | #endif |
| 276 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 277 | g_test_init (&argc, &argv, NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 278 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 279 | g_test_add_func ("/testbufferedtransport/CreateAndDestroy", test_create_and_destroy); |
| 280 | g_test_add_func ("/testbufferedtransport/OpenAndClose", test_open_and_close); |
| 281 | g_test_add_func ("/testbufferedtransport/ReadAndWrite", test_read_and_write); |
Roger Meier | 5c80b56 | 2014-05-04 22:19:46 +0200 | [diff] [blame] | 282 | g_test_add_func ("/testbufferedtransport/WriteFail", test_write_fail); |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 283 | |
| 284 | return g_test_run (); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 285 | } |
| 286 | |