]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/c_glib/test/testfdtransport.c
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
27 #include <glib/gstdio.h>
29 #include <thrift/c_glib/transport/thrift_transport.h>
30 #include <thrift/c_glib/transport/thrift_fd_transport.h>
32 static const gchar TEST_DATA
[12] = "abcde01234!";
35 test_create_and_destroy (void)
38 object
= g_object_new (THRIFT_TYPE_FD_TRANSPORT
, "fd", -1, NULL
);
39 g_assert (object
!= NULL
);
40 g_object_unref (object
);
44 test_open_and_close (void)
46 ThriftTransport
*transport
;
47 ThriftTransportClass
*klass
;
55 fd
= g_file_open_tmp (NULL
, &filename
, &error
);
58 transport
= THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT
,
61 klass
= THRIFT_TRANSPORT_GET_CLASS (transport
);
64 g_assert (klass
->is_open (transport
));
65 g_assert (klass
->peek (transport
, &error
));
66 g_assert (klass
->open (transport
, &error
));
67 g_assert (klass
->is_open (transport
));
68 g_assert (klass
->peek (transport
, &error
));
70 g_assert (klass
->close (transport
, &error
));
71 g_assert (! klass
->open (transport
, &error
));
72 g_assert (! klass
->is_open (transport
));
73 g_assert (! klass
->peek (transport
, &error
));
76 g_assert (close (fd
) != 0);
77 g_assert (errno
== EBADF
);
79 g_object_unref (transport
);
85 transport
= THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT
,
88 klass
= THRIFT_TRANSPORT_GET_CLASS (transport
);
90 g_assert (! klass
->is_open (transport
));
92 g_assert (! klass
->peek (transport
, &error
));
94 g_assert (! klass
->open (transport
, &error
));
96 g_assert (! klass
->close (transport
, &error
));
98 g_object_unref (transport
);
102 test_read_and_write (void)
107 ThriftTransport
*transport
;
108 ThriftTransportClass
*klass
;
116 fd
= g_file_open_tmp (NULL
, &filename
, &error
);
120 transport
= THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT
,
123 klass
= THRIFT_TRANSPORT_GET_CLASS (transport
);
124 g_assert (klass
->is_open (transport
));
125 g_assert (klass
->write (transport
, (gpointer
) TEST_DATA
, 11, &error
));
126 g_assert (klass
->flush (transport
, &error
));
127 g_assert (klass
->close (transport
, &error
));
128 g_object_unref (transport
);
131 fd
= open(filename
, O_RDONLY
, S_IRUSR
| S_IWUSR
);
134 transport
= THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT
,
137 klass
= THRIFT_TRANSPORT_GET_CLASS (transport
);
139 memset(out_buf
, 0, 8);
143 got
= klass
->read (transport
, (gpointer
) b
, want
, &error
);
144 g_assert (got
> 0 && got
<= want
);
148 g_assert (memcmp (out_buf
, TEST_DATA
, 7) == 0);
150 memset(out_buf
, 0, 8);
154 got
= klass
->read (transport
, (gpointer
) b
, want
, &error
);
155 g_assert (got
> 0 && got
<= want
);
159 g_assert (memcmp (out_buf
, TEST_DATA
+ 7, 4) == 0);
161 g_assert (klass
->close (transport
, &error
));
162 g_object_unref (transport
);
171 main (int argc
, char *argv
[])
173 #if (!GLIB_CHECK_VERSION (2, 36, 0))
177 g_test_init (&argc
, &argv
, NULL
);
179 g_test_add_func ("/testfdtransport/CreateAndDestroy", test_create_and_destroy
);
180 g_test_add_func ("/testfdtransport/OpenAndClose", test_open_and_close
);
181 g_test_add_func ("/testfdtransport/ReadAndWrite", test_read_and_write
);
183 return g_test_run ();