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 <netdb.h> |
| 21 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 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> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 26 | |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 27 | static const gchar TEST_DATA[11] = "abcdefghij"; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 28 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame] | 29 | #include "../src/thrift/c_glib/transport/thrift_memory_buffer.c" |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 30 | |
| 31 | /* test object creation and destruction */ |
| 32 | static void |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 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); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 39 | g_assert (object != NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 40 | g_object_unref (object); |
| 41 | } |
| 42 | |
| 43 | static 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); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 50 | g_assert (object != NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 51 | g_object_unref (object); |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | test_create_and_destroy_default (void) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 56 | { |
| 57 | GObject *object = NULL; |
| 58 | object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 59 | g_assert (object != NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 60 | g_object_unref (object); |
| 61 | } |
| 62 | |
| 63 | static void |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 64 | test_create_and_destroy_external (void) |
| 65 | { |
| 66 | GObject *object = NULL; |
| 67 | GByteArray *buf = g_byte_array_new (); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 68 | g_assert (buf != NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 69 | object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, |
| 70 | "buf", buf, |
| 71 | NULL); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 72 | g_assert (object != NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 73 | g_object_unref (object); |
| 74 | } |
| 75 | |
| 76 | static 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); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 86 | g_assert (object != NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 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); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 91 | g_assert (buf != NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 92 | |
| 93 | g_byte_array_unref (buf); |
| 94 | g_value_unset (&val); |
| 95 | g_object_unref (object); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | test_open_and_close (void) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 100 | { |
| 101 | ThriftMemoryBuffer *tbuffer = NULL; |
| 102 | |
| 103 | /* create a ThriftMemoryBuffer */ |
| 104 | tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL); |
| 105 | |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 106 | /* no-ops */ |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 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); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 110 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 111 | g_object_unref (tbuffer); |
| 112 | } |
| 113 | |
| 114 | static void |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 115 | test_read_and_write (void) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 116 | { |
| 117 | ThriftMemoryBuffer *tbuffer = NULL; |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 118 | gint got, want; |
| 119 | gchar read[10]; |
| 120 | gchar *b; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 121 | GError *error = NULL; |
| 122 | |
| 123 | tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 124 | g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer), |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 125 | (gpointer) TEST_DATA, |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 126 | 10, &error) == FALSE); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 127 | g_assert (error != NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 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); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 133 | g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer), |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 134 | (gpointer) TEST_DATA, 10, &error) == TRUE); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 135 | g_assert (error == NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 136 | |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 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); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 143 | g_assert (got > 0 && got <= want); |
| 144 | g_assert (error == NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 145 | b += got; |
| 146 | want -= got; |
| 147 | } |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 148 | g_assert (memcmp (read, TEST_DATA, 10) == 0); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 149 | g_object_unref (tbuffer); |
| 150 | } |
| 151 | |
| 152 | static 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) { |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 163 | g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer), |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 164 | (gpointer) TEST_DATA, 10, &error) == TRUE); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 165 | g_assert (error == NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 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); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 175 | g_assert (got > 0 && got <= want); |
| 176 | g_assert (error == NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 177 | b += got; |
| 178 | want -= got; |
| 179 | } |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 180 | g_assert (memcmp (read, TEST_DATA, 10) == 0); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 181 | } |
| 182 | g_object_unref (tbuffer); |
| 183 | } |
| 184 | |
| 185 | static void |
| 186 | test_read_and_write_external (void) |
| 187 | { |
| 188 | ThriftMemoryBuffer *tbuffer = NULL; |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 189 | GError *error = NULL; |
| 190 | GByteArray *buf = g_byte_array_new (); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 191 | g_assert (buf != NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 192 | |
| 193 | tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf", buf, NULL); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 194 | g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer), |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 195 | (gpointer) TEST_DATA, 10, &error) == TRUE); |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 196 | g_assert (error == NULL); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 197 | |
James E. King, III | 43f4bf2 | 2017-10-28 12:54:02 -0400 | [diff] [blame] | 198 | g_assert (memcmp (buf->data, TEST_DATA, 10) == 0); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 199 | g_object_unref (tbuffer); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 203 | main(int argc, char *argv[]) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 204 | { |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 205 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 206 | g_type_init (); |
Jens Geyer | 1c19027 | 2015-07-28 23:15:18 +0200 | [diff] [blame] | 207 | #endif |
| 208 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 209 | g_test_init (&argc, &argv, NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 210 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 211 | g_test_add_func ("/testmemorybuffer/CreateAndDestroy", test_create_and_destroy); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 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); |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 216 | g_test_add_func ("/testmemorybuffer/OpenAndClose", test_open_and_close); |
| 217 | g_test_add_func ("/testmemorybuffer/ReadAndWrite", test_read_and_write); |
Chandler May | 1fa273b | 2016-01-24 03:27:27 +0000 | [diff] [blame] | 218 | g_test_add_func ("/testmemorybuffer/ReadAndWriteUnlimited", test_read_and_write_default); |
| 219 | g_test_add_func ("/testmemorybuffer/ReadAndWriteExternal", test_read_and_write_external); |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 220 | |
| 221 | return g_test_run (); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 222 | } |