blob: 2ad8c0f9d8aedf036bd96d98e5825f00e412c3f6 [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
Roger Meiere3da7682013-01-11 11:41:53 +010023#include <thrift/c_glib/transport/thrift_transport.h>
24#include <thrift/c_glib/transport/thrift_socket.h>
25#include <thrift/c_glib/transport/thrift_server_transport.h>
26#include <thrift/c_glib/transport/thrift_server_socket.h>
Roger Meier213a6642010-10-27 12:30:11 +000027
Chandler May1fa273b2016-01-24 03:27:27 +000028static const gchar TEST_DATA[11] = "abcdefghij";
Roger Meier213a6642010-10-27 12:30:11 +000029
Roger Meiere3da7682013-01-11 11:41:53 +010030#include "../src/thrift/c_glib/transport/thrift_memory_buffer.c"
Roger Meier213a6642010-10-27 12:30:11 +000031
32/* test object creation and destruction */
33static void
Chandler May1fa273b2016-01-24 03:27:27 +000034test_create_and_destroy (void)
35{
36 GObject *object = NULL;
37 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
38 "buf_size", 10,
39 NULL);
40 assert (object != NULL);
41 g_object_unref (object);
42}
43
44static void
45test_create_and_destroy_large (void)
46{
47 GObject *object = NULL;
48 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
49 "buf_size", 10 * 1024 * 1024,
50 NULL);
51 assert (object != NULL);
52 g_object_unref (object);
53}
54
55static void
56test_create_and_destroy_default (void)
Roger Meier213a6642010-10-27 12:30:11 +000057{
58 GObject *object = NULL;
59 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
60 assert (object != NULL);
61 g_object_unref (object);
62}
63
64static void
Chandler May1fa273b2016-01-24 03:27:27 +000065test_create_and_destroy_external (void)
66{
67 GObject *object = NULL;
68 GByteArray *buf = g_byte_array_new ();
69 assert (buf != NULL);
70 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
71 "buf", buf,
72 NULL);
73 assert (object != NULL);
74 g_object_unref (object);
75}
76
77static void
78test_create_and_destroy_unowned (void)
79{
80 GObject *object = NULL;
81 GValue val = G_VALUE_INIT;
82 GByteArray *buf;
83
84 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
85 "owner", FALSE,
86 NULL);
87 assert (object != NULL);
88
89 g_value_init (&val, G_TYPE_POINTER);
90 g_object_get_property (object, "buf", &val);
91 buf = (GByteArray*) g_value_get_pointer (&val);
92 assert (buf != NULL);
93
94 g_byte_array_unref (buf);
95 g_value_unset (&val);
96 g_object_unref (object);
97}
98
99static void
100test_open_and_close (void)
Roger Meier213a6642010-10-27 12:30:11 +0000101{
102 ThriftMemoryBuffer *tbuffer = NULL;
103
104 /* create a ThriftMemoryBuffer */
105 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
106
Chandler May1fa273b2016-01-24 03:27:27 +0000107 /* no-ops */
Roger Meier213a6642010-10-27 12:30:11 +0000108 assert (thrift_memory_buffer_open (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
109 assert (thrift_memory_buffer_is_open (THRIFT_TRANSPORT (tbuffer)) == TRUE);
110 assert (thrift_memory_buffer_close (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
Chandler May1fa273b2016-01-24 03:27:27 +0000111
Roger Meier213a6642010-10-27 12:30:11 +0000112 g_object_unref (tbuffer);
113}
114
115static void
Chandler May1fa273b2016-01-24 03:27:27 +0000116test_read_and_write (void)
Roger Meier213a6642010-10-27 12:30:11 +0000117{
118 ThriftMemoryBuffer *tbuffer = NULL;
Chandler May1fa273b2016-01-24 03:27:27 +0000119 gint got, want;
120 gchar read[10];
121 gchar *b;
Roger Meier213a6642010-10-27 12:30:11 +0000122 GError *error = NULL;
123
124 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL);
125 assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
Chandler May1fa273b2016-01-24 03:27:27 +0000126 (gpointer) TEST_DATA,
Roger Meier213a6642010-10-27 12:30:11 +0000127 10, &error) == FALSE);
128 assert (error != NULL);
129 g_error_free (error);
130 error = NULL;
131 g_object_unref (tbuffer);
132
133 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, NULL);
134 assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
Chandler May1fa273b2016-01-24 03:27:27 +0000135 (gpointer) TEST_DATA, 10, &error) == TRUE);
Roger Meier213a6642010-10-27 12:30:11 +0000136 assert (error == NULL);
137
Chandler May1fa273b2016-01-24 03:27:27 +0000138 memset(read, 0, 10);
139 b = read;
140 want = 10;
141 while (want > 0) {
142 got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
143 (gpointer) b, want, &error);
144 assert (got > 0 && got <= want);
145 assert (error == NULL);
146 b += got;
147 want -= got;
148 }
149 assert (memcmp (read, TEST_DATA, 10) == 0);
150 g_object_unref (tbuffer);
151}
152
153static void
154test_read_and_write_default (void)
155{
156 ThriftMemoryBuffer *tbuffer = NULL;
157 gint got, want, i;
158 gchar read[10];
159 gchar *b;
160 GError *error = NULL;
161
162 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
163 for (i = 0; i < 100; ++i) {
164 assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
165 (gpointer) TEST_DATA, 10, &error) == TRUE);
166 assert (error == NULL);
167 }
168
169 for (i = 0; i < 100; ++i) {
170 memset(read, 0, 10);
171 b = read;
172 want = 10;
173 while (want > 0) {
174 got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
175 (gpointer) b, want, &error);
176 assert (got > 0 && got <= want);
177 assert (error == NULL);
178 b += got;
179 want -= got;
180 }
181 assert (memcmp (read, TEST_DATA, 10) == 0);
182 }
183 g_object_unref (tbuffer);
184}
185
186static void
187test_read_and_write_external (void)
188{
189 ThriftMemoryBuffer *tbuffer = NULL;
190 gchar *b;
191 GError *error = NULL;
192 GByteArray *buf = g_byte_array_new ();
193 assert (buf != NULL);
194
195 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf", buf, NULL);
196 assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
197 (gpointer) TEST_DATA, 10, &error) == TRUE);
Roger Meier213a6642010-10-27 12:30:11 +0000198 assert (error == NULL);
Chandler May1fa273b2016-01-24 03:27:27 +0000199
200 assert (memcmp (buf->data, TEST_DATA, 10) == 0);
201 g_object_unref (tbuffer);
Roger Meier213a6642010-10-27 12:30:11 +0000202}
203
204int
Roger Meierc1010922010-11-26 10:17:48 +0000205main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000206{
Jens Geyer1c190272015-07-28 23:15:18 +0200207#if (!GLIB_CHECK_VERSION (2, 36, 0))
Chandler May1fa273b2016-01-24 03:27:27 +0000208 g_type_init ();
Jens Geyer1c190272015-07-28 23:15:18 +0200209#endif
210
Roger Meierc1010922010-11-26 10:17:48 +0000211 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000212
Roger Meierc1010922010-11-26 10:17:48 +0000213 g_test_add_func ("/testmemorybuffer/CreateAndDestroy", test_create_and_destroy);
Chandler May1fa273b2016-01-24 03:27:27 +0000214 g_test_add_func ("/testmemorybuffer/CreateAndDestroyLarge", test_create_and_destroy_large);
215 g_test_add_func ("/testmemorybuffer/CreateAndDestroyUnlimited", test_create_and_destroy_default);
216 g_test_add_func ("/testmemorybuffer/CreateAndDestroyExternal", test_create_and_destroy_external);
217 g_test_add_func ("/testmemorybuffer/CreateAndDestroyUnowned", test_create_and_destroy_unowned);
Roger Meierc1010922010-11-26 10:17:48 +0000218 g_test_add_func ("/testmemorybuffer/OpenAndClose", test_open_and_close);
219 g_test_add_func ("/testmemorybuffer/ReadAndWrite", test_read_and_write);
Chandler May1fa273b2016-01-24 03:27:27 +0000220 g_test_add_func ("/testmemorybuffer/ReadAndWriteUnlimited", test_read_and_write_default);
221 g_test_add_func ("/testmemorybuffer/ReadAndWriteExternal", test_read_and_write_external);
Roger Meierc1010922010-11-26 10:17:48 +0000222
223 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000224}