1 /*
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
20 #include <netdb.h>
21
22 #include <thrift/c_glib/transport/thrift_transport.h>
23 #include <thrift/c_glib/transport/thrift_socket.h>
24 #include <thrift/c_glib/transport/thrift_server_transport.h>
25 #include <thrift/c_glib/transport/thrift_server_socket.h>
26
27 static const gchar TEST_DATA[11] = "abcdefghij";
28
29 #include "../src/thrift/c_glib/transport/thrift_memory_buffer.c"
30
31 /* test object creation and destruction */
32 static void
test_create_and_destroy(void)33 test_create_and_destroy (void)
34 {
35 GObject *object = NULL;
36 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
37 "buf_size", 10,
38 NULL);
39 g_assert (object != NULL);
40 g_object_unref (object);
41 }
42
43 static void
test_create_and_destroy_large(void)44 test_create_and_destroy_large (void)
45 {
46 GObject *object = NULL;
47 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
48 "buf_size", 10 * 1024 * 1024,
49 NULL);
50 g_assert (object != NULL);
51 g_object_unref (object);
52 }
53
54 static void
test_create_and_destroy_default(void)55 test_create_and_destroy_default (void)
56 {
57 GObject *object = NULL;
58 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
59 g_assert (object != NULL);
60 g_object_unref (object);
61 }
62
63 static void
test_create_and_destroy_external(void)64 test_create_and_destroy_external (void)
65 {
66 GObject *object = NULL;
67 GByteArray *buf = g_byte_array_new ();
68 g_assert (buf != NULL);
69 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
70 "buf", buf,
71 NULL);
72 g_assert (object != NULL);
73 g_object_unref (object);
74 }
75
76 static void
test_create_and_destroy_unowned(void)77 test_create_and_destroy_unowned (void)
78 {
79 GObject *object = NULL;
80 GValue val = G_VALUE_INIT;
81 GByteArray *buf;
82
83 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
84 "owner", FALSE,
85 NULL);
86 g_assert (object != NULL);
87
88 g_value_init (&val, G_TYPE_POINTER);
89 g_object_get_property (object, "buf", &val);
90 buf = (GByteArray*) g_value_get_pointer (&val);
91 g_assert (buf != NULL);
92
93 g_byte_array_unref (buf);
94 g_value_unset (&val);
95 g_object_unref (object);
96 }
97
98 static void
test_open_and_close(void)99 test_open_and_close (void)
100 {
101 ThriftMemoryBuffer *tbuffer = NULL;
102
103 /* create a ThriftMemoryBuffer */
104 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
105
106 /* no-ops */
107 g_assert (thrift_memory_buffer_open (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
108 g_assert (thrift_memory_buffer_is_open (THRIFT_TRANSPORT (tbuffer)) == TRUE);
109 g_assert (thrift_memory_buffer_close (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
110
111 g_object_unref (tbuffer);
112 }
113
114 static void
test_read_and_write(void)115 test_read_and_write (void)
116 {
117 ThriftMemoryBuffer *tbuffer = NULL;
118 gint got, want;
119 gchar read[10];
120 gchar *b;
121 GError *error = NULL;
122
123 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL);
124 g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
125 (gpointer) TEST_DATA,
126 10, &error) == FALSE);
127 g_assert (error != NULL);
128 g_error_free (error);
129 error = NULL;
130 g_object_unref (tbuffer);
131
132 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, NULL);
133 g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
134 (gpointer) TEST_DATA, 10, &error) == TRUE);
135 g_assert (error == NULL);
136
137 memset(read, 0, 10);
138 b = read;
139 want = 10;
140 while (want > 0) {
141 got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
142 (gpointer) b, want, &error);
143 g_assert (got > 0 && got <= want);
144 g_assert (error == NULL);
145 b += got;
146 want -= got;
147 }
148 g_assert (memcmp (read, TEST_DATA, 10) == 0);
149 g_object_unref (tbuffer);
150 }
151
152 static void
test_read_and_write_default(void)153 test_read_and_write_default (void)
154 {
155 ThriftMemoryBuffer *tbuffer = NULL;
156 gint got, want, i;
157 gchar read[10];
158 gchar *b;
159 GError *error = NULL;
160
161 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
162 for (i = 0; i < 100; ++i) {
163 g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
164 (gpointer) TEST_DATA, 10, &error) == TRUE);
165 g_assert (error == NULL);
166 }
167
168 for (i = 0; i < 100; ++i) {
169 memset(read, 0, 10);
170 b = read;
171 want = 10;
172 while (want > 0) {
173 got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
174 (gpointer) b, want, &error);
175 g_assert (got > 0 && got <= want);
176 g_assert (error == NULL);
177 b += got;
178 want -= got;
179 }
180 g_assert (memcmp (read, TEST_DATA, 10) == 0);
181 }
182 g_object_unref (tbuffer);
183 }
184
185 static void
test_read_and_write_external(void)186 test_read_and_write_external (void)
187 {
188 ThriftMemoryBuffer *tbuffer = NULL;
189 GError *error = NULL;
190 GByteArray *buf = g_byte_array_new ();
191 g_assert (buf != NULL);
192
193 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf", buf, NULL);
194 g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
195 (gpointer) TEST_DATA, 10, &error) == TRUE);
196 g_assert (error == NULL);
197
198 g_assert (memcmp (buf->data, TEST_DATA, 10) == 0);
199 g_object_unref (tbuffer);
200 }
201
202 int
main(int argc,char * argv[])203 main(int argc, char *argv[])
204 {
205 #if (!GLIB_CHECK_VERSION (2, 36, 0))
206 g_type_init ();
207 #endif
208
209 g_test_init (&argc, &argv, NULL);
210
211 g_test_add_func ("/testmemorybuffer/CreateAndDestroy", test_create_and_destroy);
212 g_test_add_func ("/testmemorybuffer/CreateAndDestroyLarge", test_create_and_destroy_large);
213 g_test_add_func ("/testmemorybuffer/CreateAndDestroyUnlimited", test_create_and_destroy_default);
214 g_test_add_func ("/testmemorybuffer/CreateAndDestroyExternal", test_create_and_destroy_external);
215 g_test_add_func ("/testmemorybuffer/CreateAndDestroyUnowned", test_create_and_destroy_unowned);
216 g_test_add_func ("/testmemorybuffer/OpenAndClose", test_open_and_close);
217 g_test_add_func ("/testmemorybuffer/ReadAndWrite", test_read_and_write);
218 g_test_add_func ("/testmemorybuffer/ReadAndWriteUnlimited", test_read_and_write_default);
219 g_test_add_func ("/testmemorybuffer/ReadAndWriteExternal", test_read_and_write_external);
220
221 return g_test_run ();
222 }
223