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> |
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_framed_transport.c" |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 32 | |
| 33 | static const char TEST_ADDRESS[] = "localhost"; |
| 34 | static const short TEST_PORT = 64444; |
| 35 | |
| 36 | static void thrift_server (const int port); |
| 37 | |
| 38 | /* test object creation and destruction */ |
| 39 | static void |
| 40 | test_create_and_destroy(void) |
| 41 | { |
| 42 | ThriftTransport *transport = NULL; |
| 43 | guint r_buf_size = 0; |
| 44 | guint w_buf_size = 0; |
| 45 | |
| 46 | GObject *object = NULL; |
| 47 | object = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, NULL); |
| 48 | assert (object != NULL); |
| 49 | g_object_get (G_OBJECT (object), "transport", &transport, |
| 50 | "r_buf_size", &r_buf_size, |
| 51 | "w_buf_size", &w_buf_size, NULL); |
| 52 | g_object_unref (object); |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | test_open_and_close(void) |
| 57 | { |
| 58 | ThriftSocket *tsocket = NULL; |
| 59 | ThriftTransport *transport = NULL; |
| 60 | GError *err = NULL; |
| 61 | |
| 62 | /* create a ThriftSocket */ |
| 63 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 64 | "port", 51188, NULL); |
| 65 | |
| 66 | /* create a BufferedTransport wrapper of the Socket */ |
| 67 | transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, |
| 68 | "transport", THRIFT_TRANSPORT (tsocket), NULL); |
| 69 | |
| 70 | /* this shouldn't work */ |
| 71 | assert (thrift_framed_transport_open (transport, NULL) == FALSE); |
| 72 | assert (thrift_framed_transport_is_open (transport) == TRUE); |
| 73 | assert (thrift_framed_transport_close (transport, NULL) == TRUE); |
| 74 | g_object_unref (transport); |
| 75 | g_object_unref (tsocket); |
| 76 | |
| 77 | /* try and underlying socket failure */ |
| 78 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken", |
| 79 | NULL); |
| 80 | |
| 81 | /* create a BufferedTransport wrapper of the Socket */ |
| 82 | transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, |
| 83 | "transport", THRIFT_TRANSPORT (tsocket), NULL); |
| 84 | |
| 85 | assert (thrift_framed_transport_open (transport, &err) == FALSE); |
| 86 | g_object_unref (transport); |
| 87 | g_object_unref (tsocket); |
| 88 | g_error_free (err); |
| 89 | err = NULL; |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | test_read_and_write(void) |
| 94 | { |
| 95 | int status; |
| 96 | pid_t pid; |
| 97 | ThriftSocket *tsocket = NULL; |
| 98 | ThriftTransport *transport = NULL; |
| 99 | int port = 51199; |
| 100 | guchar buf[10] = TEST_DATA; /* a buffer */ |
| 101 | |
| 102 | pid = fork (); |
| 103 | assert ( pid >= 0 ); |
| 104 | |
| 105 | if ( pid == 0 ) |
| 106 | { |
| 107 | /* child listens */ |
| 108 | thrift_server (port); |
| 109 | exit (0); |
| 110 | } else { |
| 111 | /* parent connects, wait a bit for the socket to be created */ |
| 112 | sleep (1); |
| 113 | |
| 114 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", |
| 115 | "port", port, NULL); |
| 116 | transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, |
| 117 | "transport", THRIFT_TRANSPORT (tsocket), |
| 118 | "w_buf_size", 4, NULL); |
| 119 | |
| 120 | assert (thrift_framed_transport_open (transport, NULL) == TRUE); |
| 121 | assert (thrift_framed_transport_is_open (transport)); |
| 122 | |
| 123 | /* write 10 bytes */ |
| 124 | thrift_framed_transport_write (transport, buf, 10, NULL); |
| 125 | thrift_framed_transport_flush (transport, NULL); |
| 126 | |
| 127 | thrift_framed_transport_write (transport, buf, 1, NULL); |
| 128 | thrift_framed_transport_flush (transport, NULL); |
| 129 | |
| 130 | thrift_framed_transport_write (transport, buf, 10, NULL); |
| 131 | thrift_framed_transport_flush (transport, NULL); |
| 132 | |
| 133 | thrift_framed_transport_write (transport, buf, 10, NULL); |
| 134 | thrift_framed_transport_flush (transport, NULL); |
| 135 | |
| 136 | thrift_framed_transport_write_end (transport, NULL); |
| 137 | thrift_framed_transport_flush (transport, NULL); |
| 138 | thrift_framed_transport_close (transport, NULL); |
| 139 | |
| 140 | g_object_unref (transport); |
| 141 | g_object_unref (tsocket); |
| 142 | |
| 143 | assert ( wait (&status) == pid ); |
| 144 | assert ( status == 0 ); |
| 145 | } |
| 146 | } |
| 147 | |
Roger Meier | 7fa9848 | 2014-09-01 20:21:33 +0200 | [diff] [blame] | 148 | /* test reading from the transport after the peer has unexpectedly |
| 149 | closed the connection */ |
| 150 | static void |
| 151 | test_read_after_peer_close(void) |
| 152 | { |
| 153 | int status; |
| 154 | pid_t pid; |
| 155 | int port = 51199; |
| 156 | GError *err = NULL; |
| 157 | |
| 158 | pid = fork (); |
| 159 | g_assert (pid >= 0); |
| 160 | |
| 161 | if (pid == 0) |
| 162 | { |
| 163 | ThriftServerTransport *server_transport = NULL; |
| 164 | ThriftTransport *client_transport = NULL; |
| 165 | |
| 166 | /* child listens */ |
| 167 | server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET, |
| 168 | "port", port, |
| 169 | NULL); |
| 170 | g_assert (server_transport != NULL); |
| 171 | |
| 172 | thrift_server_transport_listen (server_transport, &err); |
| 173 | g_assert (err == NULL); |
| 174 | |
| 175 | /* wrap the client transport in a ThriftFramedTransport */ |
| 176 | client_transport = g_object_new |
| 177 | (THRIFT_TYPE_FRAMED_TRANSPORT, |
| 178 | "transport", thrift_server_transport_accept (server_transport, &err), |
| 179 | "r_buf_size", 0, |
| 180 | NULL); |
| 181 | g_assert (err == NULL); |
| 182 | g_assert (client_transport != NULL); |
| 183 | |
| 184 | /* close the connection immediately after the client connects */ |
| 185 | thrift_transport_close (client_transport, NULL); |
| 186 | |
| 187 | g_object_unref (client_transport); |
| 188 | g_object_unref (server_transport); |
| 189 | |
| 190 | exit (0); |
| 191 | } else { |
| 192 | ThriftSocket *tsocket = NULL; |
| 193 | ThriftTransport *transport = NULL; |
| 194 | guchar buf[10]; /* a buffer */ |
| 195 | |
| 196 | /* parent connects, wait a bit for the socket to be created */ |
| 197 | sleep (1); |
| 198 | |
| 199 | tsocket = g_object_new (THRIFT_TYPE_SOCKET, |
| 200 | "hostname", "localhost", |
| 201 | "port", port, |
| 202 | NULL); |
| 203 | transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, |
| 204 | "transport", THRIFT_TRANSPORT (tsocket), |
| 205 | "w_buf_size", 0, |
| 206 | NULL); |
| 207 | |
| 208 | g_assert (thrift_transport_open (transport, NULL) == TRUE); |
| 209 | g_assert (thrift_transport_is_open (transport)); |
| 210 | |
| 211 | /* attempting to read from the transport after the peer has closed |
| 212 | the connection fails gracefully without generating a critical |
| 213 | warning or segmentation fault */ |
| 214 | thrift_transport_read (transport, buf, 10, &err); |
| 215 | g_assert (err != NULL); |
| 216 | |
| 217 | g_error_free (err); |
| 218 | err = NULL; |
| 219 | |
| 220 | thrift_transport_read_end (transport, &err); |
| 221 | g_assert (err == NULL); |
| 222 | |
| 223 | thrift_transport_close (transport, &err); |
| 224 | g_assert (err == NULL); |
| 225 | |
| 226 | g_object_unref (transport); |
| 227 | g_object_unref (tsocket); |
| 228 | |
| 229 | g_assert (wait (&status) == pid); |
| 230 | g_assert (status == 0); |
| 231 | } |
| 232 | } |
| 233 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 234 | static void |
| 235 | thrift_server (const int port) |
| 236 | { |
| 237 | int bytes = 0; |
| 238 | ThriftServerTransport *transport = NULL; |
| 239 | ThriftTransport *client = NULL; |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 240 | guchar buf[12]; /* a buffer */ |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 241 | guchar match[10] = TEST_DATA; |
| 242 | |
| 243 | ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, |
| 244 | "port", port, NULL); |
| 245 | |
| 246 | transport = THRIFT_SERVER_TRANSPORT (tsocket); |
| 247 | thrift_server_transport_listen (transport, NULL); |
| 248 | |
| 249 | /* wrap the client in a BufferedTransport */ |
| 250 | client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport", |
| 251 | thrift_server_transport_accept (transport, NULL), |
| 252 | "r_buf_size", 5, NULL); |
| 253 | assert (client != NULL); |
| 254 | |
| 255 | /* read 10 bytes */ |
| 256 | bytes = thrift_framed_transport_read (client, buf, 10, NULL); |
| 257 | assert (bytes == 10); /* make sure we've read 10 bytes */ |
| 258 | assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */ |
| 259 | |
| 260 | bytes = thrift_framed_transport_read (client, buf, 6, NULL); |
| 261 | bytes = thrift_framed_transport_read (client, buf, 5, NULL); |
| 262 | bytes = thrift_framed_transport_read (client, buf, 1, NULL); |
| 263 | |
| 264 | bytes = thrift_framed_transport_read (client, buf, 12, NULL); |
| 265 | |
| 266 | thrift_framed_transport_read_end (client, NULL); |
| 267 | thrift_framed_transport_close (client, NULL); |
| 268 | g_object_unref (client); |
| 269 | g_object_unref (tsocket); |
| 270 | } |
| 271 | |
| 272 | int |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 273 | main(int argc, char *argv[]) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 274 | { |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame^] | 275 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 276 | g_type_init(); |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame^] | 277 | #endif |
| 278 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 279 | g_test_init (&argc, &argv, NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 280 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 281 | g_test_add_func ("/testframedtransport/CreateAndDestroy", test_create_and_destroy); |
| 282 | g_test_add_func ("/testframedtransport/OpenAndClose", test_open_and_close); |
| 283 | g_test_add_func ("/testframedtransport/ReadAndWrite", test_read_and_write); |
Roger Meier | 7fa9848 | 2014-09-01 20:21:33 +0200 | [diff] [blame] | 284 | g_test_add_func ("/testframedtransport/ReadAfterPeerClose", test_read_after_peer_close); |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 285 | |
| 286 | return g_test_run (); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 287 | } |