blob: 3269bf5be42f116a2cf90b19be23ca3e72d48596 [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_framed_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_FRAMED_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_FRAMED_TRANSPORT,
67 "transport", THRIFT_TRANSPORT (tsocket), NULL);
68
69 /* this shouldn't work */
70 assert (thrift_framed_transport_open (transport, NULL) == FALSE);
71 assert (thrift_framed_transport_is_open (transport) == TRUE);
72 assert (thrift_framed_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_FRAMED_TRANSPORT,
82 "transport", THRIFT_TRANSPORT (tsocket), NULL);
83
84 assert (thrift_framed_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_FRAMED_TRANSPORT,
116 "transport", THRIFT_TRANSPORT (tsocket),
117 "w_buf_size", 4, NULL);
118
119 assert (thrift_framed_transport_open (transport, NULL) == TRUE);
120 assert (thrift_framed_transport_is_open (transport));
121
122 /* write 10 bytes */
123 thrift_framed_transport_write (transport, buf, 10, NULL);
124 thrift_framed_transport_flush (transport, NULL);
125
126 thrift_framed_transport_write (transport, buf, 1, NULL);
127 thrift_framed_transport_flush (transport, NULL);
128
129 thrift_framed_transport_write (transport, buf, 10, NULL);
130 thrift_framed_transport_flush (transport, NULL);
131
132 thrift_framed_transport_write (transport, buf, 10, NULL);
133 thrift_framed_transport_flush (transport, NULL);
134
135 thrift_framed_transport_write_end (transport, NULL);
136 thrift_framed_transport_flush (transport, NULL);
137 thrift_framed_transport_close (transport, NULL);
138
139 g_object_unref (transport);
140 g_object_unref (tsocket);
141
142 assert ( wait (&status) == pid );
143 assert ( status == 0 );
144 }
145}
146
147static void
148thrift_server (const int port)
149{
150 int bytes = 0;
151 ThriftServerTransport *transport = NULL;
152 ThriftTransport *client = NULL;
153 guchar buf[10]; /* a buffer */
154 guchar match[10] = TEST_DATA;
155
156 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
157 "port", port, NULL);
158
159 transport = THRIFT_SERVER_TRANSPORT (tsocket);
160 thrift_server_transport_listen (transport, NULL);
161
162 /* wrap the client in a BufferedTransport */
163 client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
164 thrift_server_transport_accept (transport, NULL),
165 "r_buf_size", 5, NULL);
166 assert (client != NULL);
167
168 /* read 10 bytes */
169 bytes = thrift_framed_transport_read (client, buf, 10, NULL);
170 assert (bytes == 10); /* make sure we've read 10 bytes */
171 assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
172
173 bytes = thrift_framed_transport_read (client, buf, 6, NULL);
174 bytes = thrift_framed_transport_read (client, buf, 5, NULL);
175 bytes = thrift_framed_transport_read (client, buf, 1, NULL);
176
177 bytes = thrift_framed_transport_read (client, buf, 12, NULL);
178
179 thrift_framed_transport_read_end (client, NULL);
180 thrift_framed_transport_close (client, NULL);
181 g_object_unref (client);
182 g_object_unref (tsocket);
183}
184
185int
Roger Meierc1010922010-11-26 10:17:48 +0000186main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000187{
188 g_type_init();
Roger Meierc1010922010-11-26 10:17:48 +0000189 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000190
Roger Meierc1010922010-11-26 10:17:48 +0000191 g_test_add_func ("/testframedtransport/CreateAndDestroy", test_create_and_destroy);
192 g_test_add_func ("/testframedtransport/OpenAndClose", test_open_and_close);
193 g_test_add_func ("/testframedtransport/ReadAndWrite", test_read_and_write);
194
195 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000196}