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 <assert.h> |
| 21 | #include <netdb.h> |
| 22 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame^] | 23 | #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 Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 27 | |
| 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 */ |
| 33 | static void |
| 34 | test_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 | |
| 42 | static void |
| 43 | test_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 | |
| 57 | static void |
| 58 | test_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 | |
| 84 | int |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 85 | main(int argc, char *argv[]) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 86 | { |
| 87 | g_type_init(); |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 88 | g_test_init (&argc, &argv, NULL); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 89 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 90 | 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 Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 95 | } |