blob: b716f5fe9d832215f19b905b4bbe1e09ac947dfe [file] [log] [blame]
Roger Meierc1010922010-11-26 10:17:48 +00001/*
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 Meier213a6642010-10-27 12:30:11 +000020#include <assert.h>
21#include <netdb.h>
22
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/transport/thrift_transport.h>
24#include <thrift/transport/thrift_socket.h>
25#include <thrift/transport/thrift_server_transport.h>
26#include <thrift/transport/thrift_server_socket.h>
Roger Meier213a6642010-10-27 12:30:11 +000027
28#define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
29
30#include "../src/transport/thrift_memory_buffer.c"
31
32/* test object creation and destruction */
33static void
34test_create_and_destroy(void)
35{
36 GObject *object = NULL;
37 object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
38 assert (object != NULL);
39 g_object_unref (object);
40}
41
42static void
43test_open_and_close(void)
44{
45 ThriftMemoryBuffer *tbuffer = NULL;
46
47 /* create a ThriftMemoryBuffer */
48 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
49
50 /* this shouldn't work */
51 assert (thrift_memory_buffer_open (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
52 assert (thrift_memory_buffer_is_open (THRIFT_TRANSPORT (tbuffer)) == TRUE);
53 assert (thrift_memory_buffer_close (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
54 g_object_unref (tbuffer);
55}
56
57static void
58test_read_and_write(void)
59{
60 ThriftMemoryBuffer *tbuffer = NULL;
61 guchar buf[10] = TEST_DATA;
62 guchar read[10];
63 GError *error = NULL;
64
65 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL);
66 assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
67 (gpointer) buf,
68 10, &error) == FALSE);
69 assert (error != NULL);
70 g_error_free (error);
71 error = NULL;
72 g_object_unref (tbuffer);
73
74 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, NULL);
75 assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
76 (gpointer) buf, 10, &error) == TRUE);
77 assert (error == NULL);
78
79 assert (thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
80 &read, 10, &error) > 0);
81 assert (error == NULL);
82}
83
84int
Roger Meierc1010922010-11-26 10:17:48 +000085main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +000086{
87 g_type_init();
Roger Meierc1010922010-11-26 10:17:48 +000088 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +000089
Roger Meierc1010922010-11-26 10:17:48 +000090 g_test_add_func ("/testmemorybuffer/CreateAndDestroy", test_create_and_destroy);
91 g_test_add_func ("/testmemorybuffer/OpenAndClose", test_open_and_close);
92 g_test_add_func ("/testmemorybuffer/ReadAndWrite", test_read_and_write);
93
94 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +000095}