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