blob: cc9c56c129cd84ccce3563a6afc9e59187770beb [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 <netdb.h>
21
Roger Meiere3da7682013-01-11 11:41:53 +010022#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>
Roger Meier213a6642010-10-27 12:30:11 +000026
Chandler May1fa273b2016-01-24 03:27:27 +000027static const gchar TEST_DATA[11] = "abcdefghij";
Roger Meier213a6642010-10-27 12:30:11 +000028
Roger Meiere3da7682013-01-11 11:41:53 +010029#include "../src/thrift/c_glib/transport/thrift_memory_buffer.c"
Roger Meier213a6642010-10-27 12:30:11 +000030
31/* test object creation and destruction */
32static void
Chandler May1fa273b2016-01-24 03:27:27 +000033test_create_and_destroy (void)
34{
35 GObject *object = NULL;
36 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
37 "buf_size", 10,
38 NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -040039 g_assert (object != NULL);
Chandler May1fa273b2016-01-24 03:27:27 +000040 g_object_unref (object);
41}
42
43static void
44test_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);
James E. King, III43f4bf22017-10-28 12:54:02 -040050 g_assert (object != NULL);
Chandler May1fa273b2016-01-24 03:27:27 +000051 g_object_unref (object);
52}
53
54static void
55test_create_and_destroy_default (void)
Roger Meier213a6642010-10-27 12:30:11 +000056{
57 GObject *object = NULL;
58 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -040059 g_assert (object != NULL);
Roger Meier213a6642010-10-27 12:30:11 +000060 g_object_unref (object);
61}
62
63static void
Chandler May1fa273b2016-01-24 03:27:27 +000064test_create_and_destroy_external (void)
65{
66 GObject *object = NULL;
67 GByteArray *buf = g_byte_array_new ();
James E. King, III43f4bf22017-10-28 12:54:02 -040068 g_assert (buf != NULL);
Chandler May1fa273b2016-01-24 03:27:27 +000069 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
70 "buf", buf,
71 NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -040072 g_assert (object != NULL);
Chandler May1fa273b2016-01-24 03:27:27 +000073 g_object_unref (object);
74}
75
76static void
77test_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);
James E. King, III43f4bf22017-10-28 12:54:02 -040086 g_assert (object != NULL);
Chandler May1fa273b2016-01-24 03:27:27 +000087
88 g_value_init (&val, G_TYPE_POINTER);
89 g_object_get_property (object, "buf", &val);
90 buf = (GByteArray*) g_value_get_pointer (&val);
James E. King, III43f4bf22017-10-28 12:54:02 -040091 g_assert (buf != NULL);
Chandler May1fa273b2016-01-24 03:27:27 +000092
93 g_byte_array_unref (buf);
94 g_value_unset (&val);
95 g_object_unref (object);
96}
97
98static void
99test_open_and_close (void)
Roger Meier213a6642010-10-27 12:30:11 +0000100{
101 ThriftMemoryBuffer *tbuffer = NULL;
102
103 /* create a ThriftMemoryBuffer */
104 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
105
Chandler May1fa273b2016-01-24 03:27:27 +0000106 /* no-ops */
James E. King, III43f4bf22017-10-28 12:54:02 -0400107 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);
Chandler May1fa273b2016-01-24 03:27:27 +0000110
Roger Meier213a6642010-10-27 12:30:11 +0000111 g_object_unref (tbuffer);
112}
113
114static void
Chandler May1fa273b2016-01-24 03:27:27 +0000115test_read_and_write (void)
Roger Meier213a6642010-10-27 12:30:11 +0000116{
117 ThriftMemoryBuffer *tbuffer = NULL;
Chandler May1fa273b2016-01-24 03:27:27 +0000118 gint got, want;
119 gchar read[10];
120 gchar *b;
Roger Meier213a6642010-10-27 12:30:11 +0000121 GError *error = NULL;
122
123 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400124 g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
Chandler May1fa273b2016-01-24 03:27:27 +0000125 (gpointer) TEST_DATA,
Roger Meier213a6642010-10-27 12:30:11 +0000126 10, &error) == FALSE);
James E. King, III43f4bf22017-10-28 12:54:02 -0400127 g_assert (error != NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000128 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);
James E. King, III43f4bf22017-10-28 12:54:02 -0400133 g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
Chandler May1fa273b2016-01-24 03:27:27 +0000134 (gpointer) TEST_DATA, 10, &error) == TRUE);
James E. King, III43f4bf22017-10-28 12:54:02 -0400135 g_assert (error == NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000136
Chandler May1fa273b2016-01-24 03:27:27 +0000137 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);
James E. King, III43f4bf22017-10-28 12:54:02 -0400143 g_assert (got > 0 && got <= want);
144 g_assert (error == NULL);
Chandler May1fa273b2016-01-24 03:27:27 +0000145 b += got;
146 want -= got;
147 }
James E. King, III43f4bf22017-10-28 12:54:02 -0400148 g_assert (memcmp (read, TEST_DATA, 10) == 0);
Chandler May1fa273b2016-01-24 03:27:27 +0000149 g_object_unref (tbuffer);
150}
151
152static void
153test_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) {
James E. King, III43f4bf22017-10-28 12:54:02 -0400163 g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
Chandler May1fa273b2016-01-24 03:27:27 +0000164 (gpointer) TEST_DATA, 10, &error) == TRUE);
James E. King, III43f4bf22017-10-28 12:54:02 -0400165 g_assert (error == NULL);
Chandler May1fa273b2016-01-24 03:27:27 +0000166 }
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);
James E. King, III43f4bf22017-10-28 12:54:02 -0400175 g_assert (got > 0 && got <= want);
176 g_assert (error == NULL);
Chandler May1fa273b2016-01-24 03:27:27 +0000177 b += got;
178 want -= got;
179 }
James E. King, III43f4bf22017-10-28 12:54:02 -0400180 g_assert (memcmp (read, TEST_DATA, 10) == 0);
Chandler May1fa273b2016-01-24 03:27:27 +0000181 }
182 g_object_unref (tbuffer);
183}
184
185static void
186test_read_and_write_external (void)
187{
188 ThriftMemoryBuffer *tbuffer = NULL;
Chandler May1fa273b2016-01-24 03:27:27 +0000189 GError *error = NULL;
190 GByteArray *buf = g_byte_array_new ();
James E. King, III43f4bf22017-10-28 12:54:02 -0400191 g_assert (buf != NULL);
Chandler May1fa273b2016-01-24 03:27:27 +0000192
193 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf", buf, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400194 g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
Chandler May1fa273b2016-01-24 03:27:27 +0000195 (gpointer) TEST_DATA, 10, &error) == TRUE);
James E. King, III43f4bf22017-10-28 12:54:02 -0400196 g_assert (error == NULL);
Chandler May1fa273b2016-01-24 03:27:27 +0000197
James E. King, III43f4bf22017-10-28 12:54:02 -0400198 g_assert (memcmp (buf->data, TEST_DATA, 10) == 0);
Chandler May1fa273b2016-01-24 03:27:27 +0000199 g_object_unref (tbuffer);
Roger Meier213a6642010-10-27 12:30:11 +0000200}
201
202int
Roger Meierc1010922010-11-26 10:17:48 +0000203main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000204{
Jens Geyer1c190272015-07-28 23:15:18 +0200205#if (!GLIB_CHECK_VERSION (2, 36, 0))
Chandler May1fa273b2016-01-24 03:27:27 +0000206 g_type_init ();
Jens Geyer1c190272015-07-28 23:15:18 +0200207#endif
208
Roger Meierc1010922010-11-26 10:17:48 +0000209 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000210
Roger Meierc1010922010-11-26 10:17:48 +0000211 g_test_add_func ("/testmemorybuffer/CreateAndDestroy", test_create_and_destroy);
Chandler May1fa273b2016-01-24 03:27:27 +0000212 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);
Roger Meierc1010922010-11-26 10:17:48 +0000216 g_test_add_func ("/testmemorybuffer/OpenAndClose", test_open_and_close);
217 g_test_add_func ("/testmemorybuffer/ReadAndWrite", test_read_and_write);
Chandler May1fa273b2016-01-24 03:27:27 +0000218 g_test_add_func ("/testmemorybuffer/ReadAndWriteUnlimited", test_read_and_write_default);
219 g_test_add_func ("/testmemorybuffer/ReadAndWriteExternal", test_read_and_write_external);
Roger Meierc1010922010-11-26 10:17:48 +0000220
221 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000222}