Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 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 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 20 | #include <assert.h> |
| 21 | #include <netdb.h> |
| 22 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 23 | #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 Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 27 | |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 28 | static const gchar TEST_DATA[11] = "abcdefghij"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 29 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 30 | #include "../src/thrift/c_glib/transport/thrift_memory_buffer.c" |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 31 | |
| 32 | /* test object creation and destruction */ |
| 33 | static void |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 34 | test_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 | |
| 44 | static void |
| 45 | test_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 | |
| 55 | static void |
| 56 | test_create_and_destroy_default (void) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 57 | { |
| 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 | |
| 64 | static void |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 65 | test_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 | |
| 77 | static void |
| 78 | test_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 | |
| 99 | static void |
| 100 | test_open_and_close (void) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 101 | { |
| 102 | ThriftMemoryBuffer *tbuffer = NULL; |
| 103 | |
| 104 | /* create a ThriftMemoryBuffer */ |
| 105 | tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL); |
| 106 | |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 107 | /* no-ops */ |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 108 | 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 May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 111 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 112 | g_object_unref (tbuffer); |
| 113 | } |
| 114 | |
| 115 | static void |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 116 | test_read_and_write (void) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 117 | { |
| 118 | ThriftMemoryBuffer *tbuffer = NULL; |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 119 | gint got, want; |
| 120 | gchar read[10]; |
| 121 | gchar *b; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 122 | 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 May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 126 | (gpointer) TEST_DATA, |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 127 | 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 May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 135 | (gpointer) TEST_DATA, 10, &error) == TRUE); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 136 | assert (error == NULL); |
| 137 | |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 138 | 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 | |
| 153 | static void |
| 154 | test_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 | |
| 186 | static void |
| 187 | test_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 Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 198 | assert (error == NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 199 | |
| 200 | assert (memcmp (buf->data, TEST_DATA, 10) == 0); |
| 201 | g_object_unref (tbuffer); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | int |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 205 | main(int argc, char *argv[]) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 206 | { |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 207 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 208 | g_type_init (); |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 209 | #endif |
| 210 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 211 | g_test_init (&argc, &argv, NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 212 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 213 | g_test_add_func ("/testmemorybuffer/CreateAndDestroy", test_create_and_destroy); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 214 | 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 Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 218 | g_test_add_func ("/testmemorybuffer/OpenAndClose", test_open_and_close); |
| 219 | g_test_add_func ("/testmemorybuffer/ReadAndWrite", test_read_and_write); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 220 | g_test_add_func ("/testmemorybuffer/ReadAndWriteUnlimited", test_read_and_write_default); |
| 221 | g_test_add_func ("/testmemorybuffer/ReadAndWriteExternal", test_read_and_write_external); |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 222 | |
| 223 | return g_test_run (); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 224 | } |