blob: 23951f773740694b59757d9a881f0d7e3952330a [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_framed_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_FRAMED_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_FRAMED_TRANSPORT,
48 "transport", THRIFT_TRANSPORT (tsocket), NULL);
49
50 /* this shouldn't work */
51 assert (thrift_framed_transport_open (transport, NULL) == FALSE);
52 assert (thrift_framed_transport_is_open (transport) == TRUE);
53 assert (thrift_framed_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_FRAMED_TRANSPORT,
63 "transport", THRIFT_TRANSPORT (tsocket), NULL);
64
65 assert (thrift_framed_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_FRAMED_TRANSPORT,
97 "transport", THRIFT_TRANSPORT (tsocket),
98 "w_buf_size", 4, NULL);
99
100 assert (thrift_framed_transport_open (transport, NULL) == TRUE);
101 assert (thrift_framed_transport_is_open (transport));
102
103 /* write 10 bytes */
104 thrift_framed_transport_write (transport, buf, 10, NULL);
105 thrift_framed_transport_flush (transport, NULL);
106
107 thrift_framed_transport_write (transport, buf, 1, NULL);
108 thrift_framed_transport_flush (transport, NULL);
109
110 thrift_framed_transport_write (transport, buf, 10, NULL);
111 thrift_framed_transport_flush (transport, NULL);
112
113 thrift_framed_transport_write (transport, buf, 10, NULL);
114 thrift_framed_transport_flush (transport, NULL);
115
116 thrift_framed_transport_write_end (transport, NULL);
117 thrift_framed_transport_flush (transport, NULL);
118 thrift_framed_transport_close (transport, NULL);
119
120 g_object_unref (transport);
121 g_object_unref (tsocket);
122
123 assert ( wait (&status) == pid );
124 assert ( status == 0 );
125 }
126}
127
128static void
129thrift_server (const int port)
130{
131 int bytes = 0;
132 ThriftServerTransport *transport = NULL;
133 ThriftTransport *client = NULL;
134 guchar buf[10]; /* a buffer */
135 guchar match[10] = TEST_DATA;
136
137 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
138 "port", port, NULL);
139
140 transport = THRIFT_SERVER_TRANSPORT (tsocket);
141 thrift_server_transport_listen (transport, NULL);
142
143 /* wrap the client in a BufferedTransport */
144 client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
145 thrift_server_transport_accept (transport, NULL),
146 "r_buf_size", 5, NULL);
147 assert (client != NULL);
148
149 /* read 10 bytes */
150 bytes = thrift_framed_transport_read (client, buf, 10, NULL);
151 assert (bytes == 10); /* make sure we've read 10 bytes */
152 assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
153
154 bytes = thrift_framed_transport_read (client, buf, 6, NULL);
155 bytes = thrift_framed_transport_read (client, buf, 5, NULL);
156 bytes = thrift_framed_transport_read (client, buf, 1, NULL);
157
158 bytes = thrift_framed_transport_read (client, buf, 12, NULL);
159
160 thrift_framed_transport_read_end (client, NULL);
161 thrift_framed_transport_close (client, NULL);
162 g_object_unref (client);
163 g_object_unref (tsocket);
164}
165
166int
167main(void)
168{
169 g_type_init();
170 test_create_and_destroy();
171 test_open_and_close();
172 test_read_and_write();
173
174 return 0;
175}
176