blob: fb49c8f6dce180f4827d8fde0330ffa4c59ac53e [file] [log] [blame]
Roger Meierc1010922010-11-26 10:17:48 +00001/*
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 Meier213a6642010-10-27 12:30:11 +000020#include <assert.h>
21#include <netdb.h>
22
23#include "transport/thrift_transport.h"
24#include "transport/thrift_socket.h"
25#include "transport/thrift_server_transport.h"
26#include "transport/thrift_server_socket.h"
27
28#define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
29
30#include "../src/transport/thrift_buffered_transport.c"
31
32static const char TEST_ADDRESS[] = "localhost";
33static const short TEST_PORT = 64444;
34
35static void thrift_server (const int port);
36
37/* test object creation and destruction */
38static void
39test_create_and_destroy(void)
40{
41 ThriftTransport *transport = NULL;
42 guint r_buf_size = 0;
43 guint w_buf_size = 0;
44
45 GObject *object = NULL;
46 object = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, NULL);
47 assert (object != NULL);
48 g_object_get (G_OBJECT (object), "transport", &transport,
49 "r_buf_size", &r_buf_size,
50 "w_buf_size", &w_buf_size, NULL);
51 g_object_unref (object);
52}
53
54static void
55test_open_and_close(void)
56{
57 ThriftSocket *tsocket = NULL;
58 ThriftTransport *transport = NULL;
59 GError *err = NULL;
60
61 /* create a ThriftSocket */
62 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
63 "port", 51188, NULL);
64
65 /* create a BufferedTransport wrapper of the Socket */
66 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
67 "transport", THRIFT_TRANSPORT (tsocket), NULL);
68
69 /* this shouldn't work */
70 assert (thrift_buffered_transport_open (transport, NULL) == FALSE);
71 assert (thrift_buffered_transport_is_open (transport) == TRUE);
72 assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
73 g_object_unref (transport);
74 g_object_unref (tsocket);
75
76 /* try and underlying socket failure */
77 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
78 NULL);
79
80 /* create a BufferedTransport wrapper of the Socket */
81 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
82 "transport", THRIFT_TRANSPORT (tsocket), NULL);
83
84 assert (thrift_buffered_transport_open (transport, &err) == FALSE);
85 g_object_unref (transport);
86 g_object_unref (tsocket);
87 g_error_free (err);
88 err = NULL;
89}
90
91static void
92test_read_and_write(void)
93{
94 int status;
95 pid_t pid;
96 ThriftSocket *tsocket = NULL;
97 ThriftTransport *transport = NULL;
98 int port = 51199;
99 guchar buf[10] = TEST_DATA; /* a buffer */
100
101 pid = fork ();
102 assert ( pid >= 0 );
103
104 if ( pid == 0 )
105 {
106 /* child listens */
107 thrift_server (port);
108 exit (0);
109 } else {
110 /* parent connects, wait a bit for the socket to be created */
111 sleep (1);
112
113 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
114 "port", port, NULL);
115 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
116 "transport", THRIFT_TRANSPORT (tsocket),
117 "w_buf_size", 4, NULL);
118
119 assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
120 assert (thrift_buffered_transport_is_open (transport));
121
122 /* write 10 bytes */
123 thrift_buffered_transport_write (transport, buf, 10, NULL);
124
125 /* write 1 byte at a time */
126 thrift_buffered_transport_write (transport, buf, 1, NULL);
127 thrift_buffered_transport_write (transport, buf, 1, NULL);
128 thrift_buffered_transport_write (transport, buf, 1, NULL);
129
130 /* overflow the buffer */
131 thrift_buffered_transport_write (transport, buf, 2, NULL);
132 thrift_buffered_transport_write (transport, buf, 1, NULL);
133 thrift_buffered_transport_flush (transport, NULL);
134
135 /* write 1 byte and flush */
136 thrift_buffered_transport_write (transport, buf, 1, NULL);
137 thrift_buffered_transport_flush (transport, NULL);
138
139 /* write and overflow buffer with 2 system calls */
140 thrift_buffered_transport_write (transport, buf, 1, NULL);
141 thrift_buffered_transport_write (transport, buf, 3, NULL);
142
143 /* write 10 bytes */
144 thrift_buffered_transport_write (transport, buf, 10, NULL);
145
146 thrift_buffered_transport_write_end (transport, NULL);
147 thrift_buffered_transport_flush (transport, NULL);
148 thrift_buffered_transport_close (transport, NULL);
149
150 g_object_unref (transport);
151 g_object_unref (tsocket);
152
153 assert ( wait (&status) == pid );
154 assert ( status == 0 );
155 }
156}
157
158static void
159thrift_server (const int port)
160{
161 int bytes = 0;
162 ThriftServerTransport *transport = NULL;
163 ThriftTransport *client = NULL;
164 guchar buf[10]; /* a buffer */
165 guchar match[10] = TEST_DATA;
166
167 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
168 "port", port, NULL);
169
170 transport = THRIFT_SERVER_TRANSPORT (tsocket);
171 thrift_server_transport_listen (transport, NULL);
172
173 /* wrap the client in a BufferedTransport */
174 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
175 thrift_server_transport_accept (transport, NULL),
176 "r_buf_size", 5, NULL);
177 assert (client != NULL);
178
179 /* read 10 bytes */
180 bytes = thrift_buffered_transport_read (client, buf, 10, NULL);
181 assert (bytes == 10); /* make sure we've read 10 bytes */
182 assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
183
184 /* read 1 byte */
185 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
186
187 bytes = thrift_buffered_transport_read (client, buf, 6, NULL);
188 bytes = thrift_buffered_transport_read (client, buf, 2, NULL);
189 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
190
191 thrift_buffered_transport_read_end (client, NULL);
192 thrift_buffered_transport_close (client, NULL);
193 g_object_unref (client);
194 g_object_unref (tsocket);
195}
196
197int
Roger Meierc1010922010-11-26 10:17:48 +0000198main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000199{
200 g_type_init();
Roger Meierc1010922010-11-26 10:17:48 +0000201 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000202
Roger Meierc1010922010-11-26 10:17:48 +0000203 g_test_add_func ("/testbufferedtransport/CreateAndDestroy", test_create_and_destroy);
204 g_test_add_func ("/testbufferedtransport/OpenAndClose", test_open_and_close);
205 g_test_add_func ("/testbufferedtransport/ReadAndWrite", test_read_and_write);
206
207 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000208}
209