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