blob: 658b86aeb5fd862e45f359f074c33eb29b525488 [file] [log] [blame]
zeshuai007c80b8bb2020-07-23 09:43:41 +08001/*
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 <netdb.h>
21
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>
26
27static const gchar TEST_DATA[11] = "abcdefghij";
28
29#include "../src/thrift/c_glib/transport/thrift_memory_buffer.c"
30
31#define MAX_MESSAGE_SIZE 2
32
33static void
34test_open_and_close (void)
35{
36 ThriftMemoryBuffer *tbuffer = NULL;
37 ThriftConfiguration *tconfiguration = NULL;
38
39 /* create a ThriftConfiguration */
40 tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION,
41 "max_message_size", MAX_MESSAGE_SIZE,
42 "max_frame_size", MAX_MESSAGE_SIZE,
43 NULL);
44 /* create a ThriftMemoryBuffer */
45 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "configuration", tconfiguration, NULL);
46
47 /* no-ops */
48 g_assert (thrift_memory_buffer_open (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
49 g_assert (thrift_memory_buffer_is_open (THRIFT_TRANSPORT (tbuffer)) == TRUE);
50 g_assert (thrift_memory_buffer_close (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
51
52 g_object_unref (tbuffer);
53 g_object_unref (tconfiguration);
54}
55
56static void
57test_read_and_write (void)
58{
59 ThriftConfiguration *tconfiguration = NULL;
60 ThriftMemoryBuffer *tbuffer = NULL;
61 gint got, want;
62 gchar read[10];
63 gchar *b;
64 GError *error = NULL;
65
66 tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE, NULL);
67 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, "configuration", tconfiguration, NULL);
68 THRIFT_TRANSPORT_GET_CLASS (tbuffer)->resetConsumedMessageSize(THRIFT_TRANSPORT(tbuffer), -1, NULL);
69 g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
70 (gpointer) TEST_DATA, 10, &error) == TRUE);
71 g_assert (error == NULL);
72
73 memset(read, 0, 10);
74 b = read;
75 want = 10;
76 got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
77 (gpointer) b, want, &error);
78 g_assert (got < 0);
79 g_object_unref (tbuffer);
80 g_object_unref (tconfiguration);
81}
82
83int
84main(int argc, char *argv[])
85{
86#if (!GLIB_CHECK_VERSION (2, 36, 0))
87 g_type_init ();
88#endif
89
90 g_test_init (&argc, &argv, NULL);
91
92 g_test_add_func ("/testthriftmemorybufferreadcheck/OpenAndClose", test_open_and_close);
93 g_test_add_func ("/testthriftmemorybufferreadcheck/ReadAndWrite", test_read_and_write);
94
95 return g_test_run ();
96}