Simon South | e71f20c | 2016-02-11 06:52:37 -0500 | [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 | |
| 20 | #include <assert.h> |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <errno.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <glib.h> |
| 28 | #include <glib/gstdio.h> |
| 29 | |
| 30 | #include <thrift/c_glib/transport/thrift_transport.h> |
| 31 | #include <thrift/c_glib/transport/thrift_fd_transport.h> |
| 32 | |
| 33 | static const gchar TEST_DATA[12] = "abcde01234!"; |
| 34 | |
| 35 | static void |
| 36 | test_create_and_destroy (void) |
| 37 | { |
| 38 | GObject *object; |
| 39 | object = g_object_new (THRIFT_TYPE_FD_TRANSPORT, "fd", -1, NULL); |
| 40 | assert (object != NULL); |
| 41 | g_object_unref (object); |
| 42 | } |
| 43 | |
| 44 | static void |
| 45 | test_open_and_close (void) |
| 46 | { |
| 47 | ThriftTransport *transport; |
| 48 | ThriftTransportClass *klass; |
| 49 | GError *error; |
| 50 | gint fd; |
| 51 | gchar *filename; |
| 52 | |
| 53 | error = NULL; |
| 54 | filename = NULL; |
| 55 | |
| 56 | fd = g_file_open_tmp (NULL, &filename, &error); |
| 57 | assert (fd >= 0); |
| 58 | |
| 59 | transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT, |
| 60 | "fd", fd, |
| 61 | NULL)); |
| 62 | klass = THRIFT_TRANSPORT_GET_CLASS (transport); |
| 63 | |
| 64 | /* open is no-op */ |
| 65 | assert (klass->is_open (transport)); |
| 66 | assert (klass->peek (transport, &error)); |
| 67 | assert (klass->open (transport, &error)); |
| 68 | assert (klass->is_open (transport)); |
| 69 | assert (klass->peek (transport, &error)); |
| 70 | |
| 71 | assert (klass->close (transport, &error)); |
| 72 | assert (! klass->open (transport, &error)); |
| 73 | assert (! klass->is_open (transport)); |
| 74 | assert (! klass->peek (transport, &error)); |
| 75 | |
| 76 | /* already closed */ |
| 77 | assert (close (fd) != 0); |
| 78 | assert (errno == EBADF); |
| 79 | |
| 80 | g_object_unref (transport); |
| 81 | |
| 82 | g_remove (filename); |
| 83 | g_free (filename); |
| 84 | |
| 85 | /* test bad fd */ |
| 86 | transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT, |
| 87 | "fd", -1, |
| 88 | NULL)); |
| 89 | klass = THRIFT_TRANSPORT_GET_CLASS (transport); |
| 90 | |
| 91 | assert (! klass->is_open (transport)); |
| 92 | error = NULL; |
| 93 | assert (! klass->peek (transport, &error)); |
| 94 | error = NULL; |
| 95 | assert (! klass->open (transport, &error)); |
| 96 | error = NULL; |
| 97 | assert (! klass->close (transport, &error)); |
| 98 | |
| 99 | g_object_unref (transport); |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | test_read_and_write (void) |
| 104 | { |
| 105 | gchar out_buf[8]; |
| 106 | gchar *b; |
| 107 | gint want, got; |
| 108 | ThriftTransport *transport; |
| 109 | ThriftTransportClass *klass; |
| 110 | GError *error; |
| 111 | gint fd; |
| 112 | gchar *filename; |
| 113 | |
| 114 | error = NULL; |
| 115 | filename = NULL; |
| 116 | |
| 117 | fd = g_file_open_tmp (NULL, &filename, &error); |
| 118 | assert (fd >= 0); |
| 119 | |
| 120 | /* write */ |
| 121 | transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT, |
| 122 | "fd", fd, |
| 123 | NULL)); |
| 124 | klass = THRIFT_TRANSPORT_GET_CLASS (transport); |
| 125 | assert (klass->is_open (transport)); |
| 126 | assert (klass->write (transport, (gpointer) TEST_DATA, 11, &error)); |
| 127 | assert (klass->flush (transport, &error)); |
| 128 | assert (klass->close (transport, &error)); |
| 129 | g_object_unref (transport); |
| 130 | |
| 131 | /* read */ |
| 132 | fd = open(filename, O_RDONLY, S_IRUSR | S_IWUSR); |
| 133 | assert (fd >= 0); |
| 134 | |
| 135 | transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT, |
| 136 | "fd", fd, |
| 137 | NULL)); |
| 138 | klass = THRIFT_TRANSPORT_GET_CLASS (transport); |
| 139 | |
| 140 | memset(out_buf, 0, 8); |
| 141 | b = out_buf; |
| 142 | want = 7; |
| 143 | while (want > 0) { |
| 144 | got = klass->read (transport, (gpointer) b, want, &error); |
| 145 | assert (got > 0 && got <= want); |
| 146 | b += got; |
| 147 | want -= got; |
| 148 | } |
| 149 | assert (memcmp (out_buf, TEST_DATA, 7) == 0); |
| 150 | |
| 151 | memset(out_buf, 0, 8); |
| 152 | b = out_buf; |
| 153 | want = 4; |
| 154 | while (want > 0) { |
| 155 | got = klass->read (transport, (gpointer) b, want, &error); |
| 156 | assert (got > 0 && got <= want); |
| 157 | b += got; |
| 158 | want -= got; |
| 159 | } |
| 160 | assert (memcmp (out_buf, TEST_DATA + 7, 4) == 0); |
| 161 | |
| 162 | assert (klass->close (transport, &error)); |
| 163 | g_object_unref (transport); |
| 164 | |
| 165 | /* clean up */ |
| 166 | |
| 167 | g_remove (filename); |
| 168 | g_free (filename); |
| 169 | } |
| 170 | |
| 171 | int |
| 172 | main (int argc, char *argv[]) |
| 173 | { |
| 174 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
| 175 | g_type_init (); |
| 176 | #endif |
| 177 | |
| 178 | g_test_init (&argc, &argv, NULL); |
| 179 | |
| 180 | g_test_add_func ("/testfdtransport/CreateAndDestroy", test_create_and_destroy); |
| 181 | g_test_add_func ("/testfdtransport/OpenAndClose", test_open_and_close); |
| 182 | g_test_add_func ("/testfdtransport/ReadAndWrite", test_read_and_write); |
| 183 | |
| 184 | return g_test_run (); |
| 185 | } |