Upgrading dependencies to gorealis v2 and thrift 0.12.0

This commit is contained in:
Renan DelValle 2018-12-26 17:25:59 -08:00
parent 7cbbea498b
commit 54b8d7942a
No known key found for this signature in database
GPG key ID: C240AD6D6F443EC9
1327 changed files with 137391 additions and 61476 deletions

View file

@ -111,6 +111,8 @@ include_directories("${PROJECT_SOURCE_DIR}/test/c_glib/src" "${CMAKE_CURRENT_BIN
add_executable(testthrifttest testthrifttest.c
${PROJECT_SOURCE_DIR}/test/c_glib/src/thrift_test_handler.c
${PROJECT_SOURCE_DIR}/test/c_glib/src/thrift_test_handler.h
${PROJECT_SOURCE_DIR}/test/c_glib/src/thrift_second_service_handler.c
${PROJECT_SOURCE_DIR}/test/c_glib/src/thrift_second_service_handler.h
gen-c_glib/t_test_thrift_test_types.h)
target_link_libraries(testthrifttest testgenc)
add_test(NAME testthrifttest COMMAND testthrifttest)

View file

@ -37,16 +37,17 @@ BUILT_SOURCES = \
gen-c_glib/t_test_thrift_test_types.h
AM_CPPFLAGS = -I../src -I./gen-c_glib
AM_CFLAGS = -g -Wall -Wextra -pedantic $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) \
AM_CFLAGS = -g -Wall -Wextra -pedantic $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) $(OPENSSL_INCLUDES) \
@GCOV_CFLAGS@
AM_CXXFLAGS = $(AM_CFLAGS)
AM_LDFLAGS = $(GLIB_LIBS) $(GOBJECT_LIBS) @GCOV_LDFLAGS@
AM_LDFLAGS = $(GLIB_LIBS) $(GOBJECT_LIBS) $(OPENSSL_LIBS) @GCOV_LDFLAGS@
check_PROGRAMS = \
testserialization \
testapplicationexception \
testcontainertest \
testtransportsocket \
testtransportsslsocket \
testbinaryprotocol \
testcompactprotocol \
testbufferedtransport \
@ -100,6 +101,16 @@ testtransportsocket_LDADD = \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_transport.o \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o
testtransportsslsocket_SOURCES = testtransportsslsocket.c
testtransportsslsocket_LDADD = \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_transport.o \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_socket.o \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_buffered_transport.o \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_transport.o \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o
testbinaryprotocol_SOURCES = testbinaryprotocol.c
testbinaryprotocol_LDADD = \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/protocol/libthrift_c_glib_la-thrift_protocol.o \
@ -226,8 +237,6 @@ nodist_libtestgencpp_la_SOURCES = \
gen-cpp/ThriftTest_types.h
libtestgencpp_la_CPPFLAGS = -I../../cpp/src $(BOOST_CPPFLAGS) -I./gen-cpp
THRIFT = $(top_builddir)/compiler/cpp/thrift
gen-c_glib/t_test_container_test_types.c gen-c_glib/t_test_container_test_types.h gen-c_glib/t_test_container_service.c gen-c_glib/t_test_container_service.h: ContainerTest.thrift $(THRIFT)
$(THRIFT) --gen c_glib $<

View file

@ -19,7 +19,7 @@
/* Disable string-function optimizations when glibc is used, as these produce
compiler warnings about string length when a string function is used inside
a call to assert () */
a call to g_assert () */
#ifdef __GLIBC__
#include <features.h>
#define __NO_STRING_INLINES 1
@ -28,7 +28,6 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <netdb.h>
#include <string.h>
#include <sys/wait.h>
@ -96,7 +95,7 @@ test_create_and_destroy(void)
/* create an object and then destroy it */
object = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, NULL);
assert (object != NULL);
g_assert (object != NULL);
g_object_unref (object);
}
@ -110,11 +109,11 @@ test_initialize(void)
/* create a ThriftTransport */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", 51188, NULL);
assert (tsocket != NULL);
g_assert (tsocket != NULL);
/* create a ThriftBinaryProtocol using the Transport */
protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
tsocket, NULL);
assert (protocol != NULL);
g_assert (protocol != NULL);
/* fetch the properties */
g_object_get (G_OBJECT(protocol), "transport", &temp, NULL);
g_object_unref (temp);
@ -139,7 +138,7 @@ test_read_and_write_primitives(void)
/* fork a server from the client */
pid = fork ();
assert (pid >= 0);
g_assert (pid >= 0);
if (pid == 0)
{
@ -155,47 +154,48 @@ test_read_and_write_primitives(void)
"port", port, NULL);
transport = THRIFT_TRANSPORT (tsocket);
thrift_transport_open (transport, NULL);
assert (thrift_transport_is_open (transport));
g_assert (thrift_transport_is_open (transport));
/* create a ThriftBinaryTransport */
tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
tsocket, NULL);
protocol = THRIFT_PROTOCOL (tb);
assert (protocol != NULL);
g_assert (protocol != NULL);
/* write a bunch of primitives */
assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
assert (thrift_binary_protocol_write_double (protocol,
g_assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
g_assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
g_assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
g_assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
g_assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
g_assert (thrift_binary_protocol_write_double (protocol,
TEST_DOUBLE, NULL) > 0);
assert (thrift_binary_protocol_write_string (protocol,
g_assert (thrift_binary_protocol_write_string (protocol,
TEST_STRING, NULL) > 0);
assert (thrift_binary_protocol_write_binary (protocol, binary,
g_assert (thrift_binary_protocol_write_string (protocol, "", NULL) > 0);
g_assert (thrift_binary_protocol_write_binary (protocol, binary,
len, NULL) > 0);
assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
assert (thrift_binary_protocol_write_binary (protocol, binary,
g_assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
g_assert (thrift_binary_protocol_write_binary (protocol, binary,
len, NULL) > 0);
/* test write errors */
transport_write_error = 1;
assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE,
g_assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE,
NULL) == -1);
assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) == -1);
assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) == -1);
assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) == -1);
assert (thrift_binary_protocol_write_double (protocol, TEST_DOUBLE,
g_assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) == -1);
g_assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) == -1);
g_assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) == -1);
g_assert (thrift_binary_protocol_write_double (protocol, TEST_DOUBLE,
NULL) == -1);
assert (thrift_binary_protocol_write_binary (protocol, binary, len,
g_assert (thrift_binary_protocol_write_binary (protocol, binary, len,
NULL) == -1);
transport_write_error = 0;
/* test binary partial failure */
transport_write_count = 0;
transport_write_error_at = 1;
assert (thrift_binary_protocol_write_binary (protocol, binary,
g_assert (thrift_binary_protocol_write_binary (protocol, binary,
len, NULL) == -1);
transport_write_error_at = -1;
@ -203,8 +203,8 @@ test_read_and_write_primitives(void)
thrift_transport_close (transport, NULL);
g_object_unref (tsocket);
g_object_unref (protocol);
assert (wait (&status) == pid);
assert (status == 0);
g_assert (wait (&status) == pid);
g_assert (status == 0);
}
}
@ -221,7 +221,7 @@ test_read_and_write_complex_types (void)
/* fork a server from the client */
pid = fork ();
assert (pid >= 0);
g_assert (pid >= 0);
if (pid == 0)
{
@ -237,33 +237,33 @@ test_read_and_write_complex_types (void)
"port", port, NULL);
transport = THRIFT_TRANSPORT (tsocket);
thrift_transport_open (transport, NULL);
assert (thrift_transport_is_open (transport));
g_assert (thrift_transport_is_open (transport));
/* create a ThriftBinaryTransport */
tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
tsocket, NULL);
protocol = THRIFT_PROTOCOL (tb);
assert (protocol != NULL);
g_assert (protocol != NULL);
/* test structures */
assert (thrift_binary_protocol_write_struct_begin (protocol,
g_assert (thrift_binary_protocol_write_struct_begin (protocol,
NULL, NULL) == 0);
assert (thrift_binary_protocol_write_struct_end (protocol, NULL) == 0);
g_assert (thrift_binary_protocol_write_struct_end (protocol, NULL) == 0);
assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
g_assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
1, NULL) > 0);
assert (thrift_binary_protocol_write_field_end (protocol, NULL) == 0);
g_assert (thrift_binary_protocol_write_field_end (protocol, NULL) == 0);
/* test write error */
transport_write_error = 1;
assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
g_assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
1, NULL) == -1);
transport_write_error = 0;
/* test 2nd write error */
transport_write_count = 0;
transport_write_error_at = 1;
assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
g_assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
1, NULL) == -1);
transport_write_error_at = -1;
@ -271,12 +271,12 @@ test_read_and_write_complex_types (void)
thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
/* test write_field_stop */
assert (thrift_binary_protocol_write_field_stop (protocol, NULL) > 0);
g_assert (thrift_binary_protocol_write_field_stop (protocol, NULL) > 0);
/* write a map */
assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
g_assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
1, NULL) > 0);
assert (thrift_binary_protocol_write_map_end (protocol, NULL) == 0);
g_assert (thrift_binary_protocol_write_map_end (protocol, NULL) == 0);
/* test 2nd read failure on a map */
thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
@ -287,21 +287,21 @@ test_read_and_write_complex_types (void)
/* test 1st write failure on a map */
transport_write_error = 1;
assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
g_assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
1, NULL) == -1);
transport_write_error = 0;
/* test 2nd write failure on a map */
transport_write_count = 0;
transport_write_error_at = 1;
assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
g_assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
1, NULL) == -1);
transport_write_error_at = -1;
/* test 3rd write failure on a map */
transport_write_count = 0;
transport_write_error_at = 2;
assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
g_assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
1, NULL) == -1);
transport_write_error_at = -1;
@ -311,9 +311,9 @@ test_read_and_write_complex_types (void)
thrift_binary_protocol_write_i32 (protocol, -10, NULL);
/* test list operations */
assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
g_assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
1, NULL) > 0);
assert (thrift_binary_protocol_write_list_end (protocol, NULL) == 0);
g_assert (thrift_binary_protocol_write_list_end (protocol, NULL) == 0);
/* test 2nd read failure on a list */
thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
@ -324,27 +324,27 @@ test_read_and_write_complex_types (void)
/* test first write error on a list */
transport_write_error = 1;
assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
g_assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
1, NULL) == -1);
transport_write_error = 0;
/* test 2nd write error on a list */
transport_write_count = 0;
transport_write_error_at = 1;
assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
g_assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
1, NULL) == -1);
transport_write_error_at = -1;
/* test set operation s*/
assert (thrift_binary_protocol_write_set_begin (protocol, T_VOID,
g_assert (thrift_binary_protocol_write_set_begin (protocol, T_VOID,
1, NULL) > 0);
assert (thrift_binary_protocol_write_set_end (protocol, NULL) == 0);
g_assert (thrift_binary_protocol_write_set_end (protocol, NULL) == 0);
/* invalid version */
assert (thrift_binary_protocol_write_i32 (protocol, -1, NULL) > 0);
g_assert (thrift_binary_protocol_write_i32 (protocol, -1, NULL) > 0);
/* sz > 0 for a message */
assert (thrift_binary_protocol_write_i32 (protocol, 1, NULL) > 0);
g_assert (thrift_binary_protocol_write_i32 (protocol, 1, NULL) > 0);
/* send a valid message */
thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
@ -359,26 +359,26 @@ test_read_and_write_complex_types (void)
thrift_binary_protocol_write_string (protocol, "test", NULL);
/* send a valid message */
assert (thrift_binary_protocol_write_message_begin (protocol, "test",
g_assert (thrift_binary_protocol_write_message_begin (protocol, "test",
T_CALL, 1, NULL) > 0);
assert (thrift_binary_protocol_write_message_end (protocol, NULL) == 0);
g_assert (thrift_binary_protocol_write_message_end (protocol, NULL) == 0);
/* send broken writes */
transport_write_error = 1;
assert (thrift_binary_protocol_write_message_begin (protocol, "test",
g_assert (thrift_binary_protocol_write_message_begin (protocol, "test",
T_CALL, 1, NULL) == -1);
transport_write_error = 0;
transport_write_count = 0;
transport_write_error_at = 2;
assert (thrift_binary_protocol_write_message_begin (protocol, "test",
g_assert (thrift_binary_protocol_write_message_begin (protocol, "test",
T_CALL, 1, NULL) == -1);
transport_write_error_at = -1;
transport_write_count = 0;
transport_write_error_at = 3;
assert (thrift_binary_protocol_write_message_begin (protocol, "test",
g_assert (thrift_binary_protocol_write_message_begin (protocol, "test",
T_CALL, 1, NULL) == -1);
transport_write_error_at = -1;
@ -386,8 +386,8 @@ test_read_and_write_complex_types (void)
thrift_transport_close (transport, NULL);
g_object_unref (tsocket);
g_object_unref (protocol);
assert (wait (&status) == pid);
assert (status == 0);
g_assert (wait (&status) == pid);
g_assert (status == 0);
}
}
@ -407,7 +407,7 @@ test_read_and_write_many_frames (void)
/* fork a server from the client */
pid = fork ();
assert (pid >= 0);
g_assert (pid >= 0);
if (pid == 0)
{
@ -421,47 +421,49 @@ test_read_and_write_many_frames (void)
/* create a ThriftSocket */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
assert (tsocket != NULL);
g_assert (tsocket != NULL);
transport = THRIFT_TRANSPORT (tsocket);
/* wrap in a framed transport */
ft = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport", transport,
"w_buf_size", 1, NULL);
assert (ft != NULL);
g_assert (ft != NULL);
transport = THRIFT_TRANSPORT (ft);
thrift_transport_open (transport, NULL);
assert (thrift_transport_is_open (transport));
g_assert (thrift_transport_is_open (transport));
/* create a binary protocol */
tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
transport, NULL);
protocol = THRIFT_PROTOCOL (tb);
assert (protocol != NULL);
g_assert (protocol != NULL);
/* write a bunch of primitives */
assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
g_assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
thrift_transport_flush (transport, NULL);
assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
g_assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
thrift_transport_flush (transport, NULL);
assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
g_assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
thrift_transport_flush (transport, NULL);
assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
g_assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
thrift_transport_flush (transport, NULL);
assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
g_assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
thrift_transport_flush (transport, NULL);
assert (thrift_binary_protocol_write_double (protocol,
g_assert (thrift_binary_protocol_write_double (protocol,
TEST_DOUBLE, NULL) > 0);
thrift_transport_flush (transport, NULL);
assert (thrift_binary_protocol_write_string (protocol,
g_assert (thrift_binary_protocol_write_string (protocol,
TEST_STRING, NULL) > 0);
thrift_transport_flush (transport, NULL);
assert (thrift_binary_protocol_write_binary (protocol, binary,
g_assert (thrift_binary_protocol_write_string (protocol, "", NULL) > 0);
thrift_transport_flush (transport, NULL);
g_assert (thrift_binary_protocol_write_binary (protocol, binary,
len, NULL) > 0);
thrift_transport_flush (transport, NULL);
assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
g_assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
thrift_transport_flush (transport, NULL);
assert (thrift_binary_protocol_write_binary (protocol, binary,
g_assert (thrift_binary_protocol_write_binary (protocol, binary,
len, NULL) > 0);
thrift_transport_flush (transport, NULL);
@ -471,8 +473,8 @@ test_read_and_write_many_frames (void)
g_object_unref (ft);
g_object_unref (tsocket);
g_object_unref (tb);
assert (wait (&status) == pid);
assert (status == 0);
g_assert (wait (&status) == pid);
g_assert (status == 0);
}
}
@ -491,6 +493,7 @@ thrift_server_primitives (const int port)
gint64 value_64 = 0;
gdouble value_double = 0;
gchar *string = NULL;
gchar *empty_string = NULL;
gpointer binary = NULL;
guint32 len = 0;
void *comparator = (void *) TEST_STRING;
@ -500,61 +503,68 @@ thrift_server_primitives (const int port)
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
client = thrift_server_transport_accept (transport, NULL);
assert (client != NULL);
g_assert (client != NULL);
tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
client, NULL);
protocol = THRIFT_PROTOCOL (tbp);
assert (thrift_binary_protocol_read_bool (protocol,
g_assert (thrift_binary_protocol_read_bool (protocol,
&value_boolean, NULL) > 0);
assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0);
assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0);
assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0);
assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0);
assert (thrift_binary_protocol_read_double (protocol,
g_assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0);
g_assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0);
g_assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0);
g_assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0);
g_assert (thrift_binary_protocol_read_double (protocol,
&value_double, NULL) > 0);
assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0);
assert (thrift_binary_protocol_read_binary (protocol, &binary,
g_assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0);
g_assert (thrift_binary_protocol_read_string (protocol, &empty_string,
NULL) > 0);
g_assert (thrift_binary_protocol_read_binary (protocol, &binary,
&len, NULL) > 0);
assert (value_boolean == TEST_BOOL);
assert (value_byte == TEST_BYTE);
assert (value_16 == TEST_I16);
assert (value_32 == TEST_I32);
assert (value_64 == TEST_I64);
assert (value_double == TEST_DOUBLE);
assert (strcmp (TEST_STRING, string) == 0);
assert (memcmp (comparator, binary, len) == 0);
g_assert (value_boolean == TEST_BOOL);
g_assert (value_byte == TEST_BYTE);
g_assert (value_16 == TEST_I16);
g_assert (value_32 == TEST_I32);
g_assert (value_64 == TEST_I64);
g_assert (value_double == TEST_DOUBLE);
g_assert (strcmp (TEST_STRING, string) == 0);
g_assert (strcmp ("", empty_string) == 0);
g_assert (memcmp (comparator, binary, len) == 0);
g_free (string);
g_free (empty_string);
g_free (binary);
thrift_binary_protocol_read_binary (protocol, &binary, &len, NULL);
g_assert (thrift_binary_protocol_read_binary (protocol, &binary,
&len, NULL) > 0);
g_assert (binary == NULL);
g_assert (len == 0);
g_free (binary);
transport_read_count = 0;
transport_read_error_at = 0;
assert (thrift_binary_protocol_read_binary (protocol, &binary,
g_assert (thrift_binary_protocol_read_binary (protocol, &binary,
&len, NULL) == -1);
transport_read_error_at = -1;
transport_read_count = 0;
transport_read_error_at = 1;
assert (thrift_binary_protocol_read_binary (protocol, &binary,
g_assert (thrift_binary_protocol_read_binary (protocol, &binary,
&len, NULL) == -1);
transport_read_error_at = -1;
transport_read_error = 1;
assert (thrift_binary_protocol_read_bool (protocol,
g_assert (thrift_binary_protocol_read_bool (protocol,
&value_boolean, NULL) == -1);
assert (thrift_binary_protocol_read_byte (protocol,
g_assert (thrift_binary_protocol_read_byte (protocol,
&value_byte, NULL) == -1);
assert (thrift_binary_protocol_read_i16 (protocol,
g_assert (thrift_binary_protocol_read_i16 (protocol,
&value_16, NULL) == -1);
assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) == -1);
assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) == -1);
assert (thrift_binary_protocol_read_double (protocol,
g_assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) == -1);
g_assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) == -1);
g_assert (thrift_binary_protocol_read_double (protocol,
&value_double, NULL) == -1);
transport_read_error = 0;
@ -592,7 +602,7 @@ thrift_server_complex_types (const int port)
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
client = thrift_server_transport_accept (transport, NULL);
assert (client != NULL);
g_assert (client != NULL);
tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
client, NULL);
@ -607,7 +617,7 @@ thrift_server_complex_types (const int port)
/* test first read error on a field */
transport_read_error = 1;
assert (thrift_binary_protocol_read_field_begin (protocol,
g_assert (thrift_binary_protocol_read_field_begin (protocol,
&field_name, &field_type,
&field_id, NULL) == -1);
transport_read_error = 0;
@ -618,7 +628,7 @@ thrift_server_complex_types (const int port)
/* test 2nd read failure on a field */
transport_read_count = 0;
transport_read_error_at = 1;
assert (thrift_binary_protocol_read_field_begin (protocol,
g_assert (thrift_binary_protocol_read_field_begin (protocol,
&field_name, &field_type,
&field_id, NULL) == -1);
transport_read_error_at = -1;
@ -634,7 +644,7 @@ thrift_server_complex_types (const int port)
/* test read failure on a map */
transport_read_count = 0;
transport_read_error_at = 0;
assert (thrift_binary_protocol_read_map_begin (protocol,
g_assert (thrift_binary_protocol_read_map_begin (protocol,
&key_type, &value_type,
&size, NULL) == -1);
transport_read_error_at = -1;
@ -642,7 +652,7 @@ thrift_server_complex_types (const int port)
/* test 2nd read failure on a map */
transport_read_count = 0;
transport_read_error_at = 1;
assert (thrift_binary_protocol_read_map_begin (protocol,
g_assert (thrift_binary_protocol_read_map_begin (protocol,
&key_type, &value_type,
&size, NULL) == -1);
transport_read_error_at = -1;
@ -650,7 +660,7 @@ thrift_server_complex_types (const int port)
/* test 3rd read failure on a map */
transport_read_count = 0;
transport_read_error_at = 2;
assert (thrift_binary_protocol_read_map_begin (protocol,
g_assert (thrift_binary_protocol_read_map_begin (protocol,
&key_type, &value_type,
&size, NULL) == -1);
transport_read_error_at = -1;
@ -663,7 +673,7 @@ thrift_server_complex_types (const int port)
thrift_binary_protocol_read_byte (protocol, &value, NULL);
/* test negative map size */
assert (thrift_binary_protocol_read_map_begin (protocol,
g_assert (thrift_binary_protocol_read_map_begin (protocol,
&key_type, &value_type,
&size, NULL) == -1);
@ -673,7 +683,7 @@ thrift_server_complex_types (const int port)
/* test read failure */
transport_read_error = 1;
assert (thrift_binary_protocol_read_list_begin (protocol, &element_type,
g_assert (thrift_binary_protocol_read_list_begin (protocol, &element_type,
&size, NULL) == -1);
transport_read_error = 0;
@ -695,23 +705,23 @@ thrift_server_complex_types (const int port)
/* broken read */
transport_read_error = 1;
assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
g_assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
&message_type, &seqid,
NULL) == -1);
transport_read_error = 0;
/* invalid protocol version */
assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
g_assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
&message_type, &seqid,
NULL) == -1);
/* sz > 0 */
assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
g_assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
&message_type, &seqid,
NULL) > 0);
/* read a valid message */
assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
g_assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
&message_type, &seqid,
NULL) > 0);
g_free (message_name);
@ -719,7 +729,7 @@ thrift_server_complex_types (const int port)
/* broken 2nd read on a message */
transport_read_count = 0;
transport_read_error_at = 1;
assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
g_assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
&message_type, &seqid,
NULL) == -1);
transport_read_error_at = -1;
@ -727,19 +737,19 @@ thrift_server_complex_types (const int port)
/* broken 3rd read on a message */
transport_read_count = 0;
transport_read_error_at = 3; /* read_string does two reads */
assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
g_assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
&message_type, &seqid,
NULL) == -1);
g_free (message_name);
transport_read_error_at = -1;
/* read a valid message */
assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
g_assert (thrift_binary_protocol_read_message_begin (protocol, &message_name,
&message_type, &seqid,
NULL) > 0);
g_free (message_name);
assert (thrift_binary_protocol_read_message_end (protocol, NULL) == 0);
g_assert (thrift_binary_protocol_read_message_end (protocol, NULL) == 0);
/* handle 2nd write failure on a message */
thrift_binary_protocol_read_i32 (protocol, &version, NULL);
@ -768,6 +778,7 @@ thrift_server_many_frames (const int port)
gint64 value_64 = 0;
gdouble value_double = 0;
gchar *string = NULL;
gchar *empty_string = NULL;
gpointer binary = NULL;
guint32 len = 0;
void *comparator = (void *) TEST_STRING;
@ -780,34 +791,44 @@ thrift_server_many_frames (const int port)
client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
thrift_server_transport_accept (transport, NULL),
"r_buf_size", 1, NULL);
assert (client != NULL);
g_assert (client != NULL);
tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
client, NULL);
protocol = THRIFT_PROTOCOL (tbp);
assert (thrift_binary_protocol_read_bool (protocol,
g_assert (thrift_binary_protocol_read_bool (protocol,
&value_boolean, NULL) > 0);
assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0);
assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0);
assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0);
assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0);
assert (thrift_binary_protocol_read_double (protocol,
g_assert (thrift_binary_protocol_read_byte (protocol, &value_byte, NULL) > 0);
g_assert (thrift_binary_protocol_read_i16 (protocol, &value_16, NULL) > 0);
g_assert (thrift_binary_protocol_read_i32 (protocol, &value_32, NULL) > 0);
g_assert (thrift_binary_protocol_read_i64 (protocol, &value_64, NULL) > 0);
g_assert (thrift_binary_protocol_read_double (protocol,
&value_double, NULL) > 0);
assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0);
assert (thrift_binary_protocol_read_binary (protocol, &binary,
g_assert (thrift_binary_protocol_read_string (protocol, &string, NULL) > 0);
g_assert (thrift_binary_protocol_read_string (protocol, &empty_string,
NULL) > 0);
g_assert (thrift_binary_protocol_read_binary (protocol, &binary,
&len, NULL) > 0);
assert (value_boolean == TEST_BOOL);
assert (value_byte == TEST_BYTE);
assert (value_16 == TEST_I16);
assert (value_32 == TEST_I32);
assert (value_64 == TEST_I64);
assert (value_double == TEST_DOUBLE);
assert (strcmp (TEST_STRING, string) == 0);
assert (memcmp (comparator, binary, len) == 0);
g_assert (value_boolean == TEST_BOOL);
g_assert (value_byte == TEST_BYTE);
g_assert (value_16 == TEST_I16);
g_assert (value_32 == TEST_I32);
g_assert (value_64 == TEST_I64);
g_assert (value_double == TEST_DOUBLE);
g_assert (strcmp (TEST_STRING, string) == 0);
g_assert (strcmp ("", empty_string) == 0);
g_assert (memcmp (comparator, binary, len) == 0);
g_free (string);
g_free (empty_string);
g_free (binary);
g_assert (thrift_binary_protocol_read_binary (protocol, &binary,
&len, NULL) > 0);
g_assert (binary == NULL);
g_assert (len == 0);
g_free (binary);
thrift_transport_read_end (client, NULL);

View file

@ -17,7 +17,6 @@
* under the License.
*/
#include <assert.h>
#include <netdb.h>
#include <signal.h>
#include <sys/wait.h>
@ -32,6 +31,7 @@
#include "../src/thrift/c_glib/transport/thrift_buffered_transport.c"
static void thrift_server (const int port);
static void thrift_socket_server_open (const int port, int times);
/* test object creation and destruction */
static void
@ -43,10 +43,10 @@ test_create_and_destroy(void)
GObject *object = NULL;
object = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, NULL);
assert (object != NULL);
g_assert (object != NULL);
g_object_get (G_OBJECT (object), "transport", &transport,
"r_buf_size", &r_buf_size,
"w_buf_size", &w_buf_size, NULL);
"r_buf_size", &r_buf_size,
"w_buf_size", &w_buf_size, NULL);
g_object_unref (object);
}
@ -56,35 +56,52 @@ test_open_and_close(void)
ThriftSocket *tsocket = NULL;
ThriftTransport *transport = NULL;
GError *err = NULL;
pid_t pid;
int port = 51199;
int status;
/* create a ThriftSocket */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", 51188, NULL);
pid = fork ();
g_assert ( pid >= 0 );
/* create a BufferedTransport wrapper of the Socket */
transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket), NULL);
if ( pid == 0 )
{
/* child listens */
thrift_socket_server_open (port,1);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
/* create a ThriftSocket */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
/* this shouldn't work */
assert (thrift_buffered_transport_open (transport, NULL) == FALSE);
assert (thrift_buffered_transport_is_open (transport) == TRUE);
assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
g_object_unref (transport);
g_object_unref (tsocket);
/* create a BufferedTransport wrapper of the Socket */
transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket), NULL);
/* try and underlying socket failure */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
NULL);
/* this shouldn't work */
g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
g_assert (thrift_buffered_transport_is_open (transport) == TRUE);
g_assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
g_object_unref (transport);
g_object_unref (tsocket);
/* create a BufferedTransport wrapper of the Socket */
transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket), NULL);
/* try and underlying socket failure */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
NULL);
assert (thrift_buffered_transport_open (transport, &err) == FALSE);
g_object_unref (transport);
g_object_unref (tsocket);
g_error_free (err);
err = NULL;
/* create a BufferedTransport wrapper of the Socket */
transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket), NULL);
g_assert (thrift_buffered_transport_open (transport, &err) == FALSE);
g_object_unref (transport);
g_object_unref (tsocket);
g_error_free (err);
err = NULL;
g_assert ( wait (&status) == pid );
g_assert ( status == 0 );
}
}
static void
@ -98,60 +115,84 @@ test_read_and_write(void)
guchar buf[10] = TEST_DATA; /* a buffer */
pid = fork ();
assert ( pid >= 0 );
g_assert ( pid >= 0 );
if ( pid == 0 )
{
/* child listens */
thrift_server (port);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
{
/* child listens */
thrift_server (port);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket),
"w_buf_size", 4, NULL);
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket),
"w_buf_size", 4, NULL);
assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
assert (thrift_buffered_transport_is_open (transport));
g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
g_assert (thrift_buffered_transport_is_open (transport));
/* write 10 bytes */
thrift_buffered_transport_write (transport, buf, 10, NULL);
/* write 10 bytes */
thrift_buffered_transport_write (transport, buf, 10, NULL);
/* write 1 byte at a time */
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_write (transport, buf, 1, NULL);
/* write 1 byte at a time */
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_write (transport, buf, 1, NULL);
/* overflow the buffer */
thrift_buffered_transport_write (transport, buf, 2, NULL);
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_flush (transport, NULL);
/* overflow the buffer */
thrift_buffered_transport_write (transport, buf, 2, NULL);
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_flush (transport, NULL);
/* write 1 byte and flush */
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_flush (transport, NULL);
/* write 1 byte and flush */
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_flush (transport, NULL);
/* write and overflow buffer with 2 system calls */
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_write (transport, buf, 3, NULL);
/* write and overflow buffer with 2 system calls */
thrift_buffered_transport_write (transport, buf, 1, NULL);
thrift_buffered_transport_write (transport, buf, 3, NULL);
/* write 10 bytes */
thrift_buffered_transport_write (transport, buf, 10, NULL);
/* write 10 bytes */
thrift_buffered_transport_write (transport, buf, 10, NULL);
thrift_buffered_transport_write_end (transport, NULL);
thrift_buffered_transport_flush (transport, NULL);
thrift_buffered_transport_close (transport, NULL);
thrift_buffered_transport_write_end (transport, NULL);
thrift_buffered_transport_flush (transport, NULL);
thrift_buffered_transport_close (transport, NULL);
g_object_unref (transport);
g_object_unref (tsocket);
g_object_unref (transport);
g_object_unref (tsocket);
assert ( wait (&status) == pid );
assert ( status == 0 );
g_assert ( wait (&status) == pid );
g_assert ( status == 0 );
}
}
static void
thrift_socket_server_open (const int port, int times)
{
int bytes = 0;
ThriftServerTransport *transport = NULL;
ThriftTransport *client = NULL;
guchar buf[10]; /* a buffer */
guchar match[10] = TEST_DATA;
int i;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
for(i=0;i<times;i++){
client = thrift_server_transport_accept (transport, NULL);
g_assert (client != NULL);
thrift_socket_close (client, NULL);
g_object_unref (client);
}
g_object_unref (tsocket);
}
static void
@ -164,21 +205,21 @@ thrift_server (const int port)
guchar match[10] = TEST_DATA;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port, NULL);
"port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
/* wrap the client in a BufferedTransport */
client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
thrift_server_transport_accept (transport, NULL),
"r_buf_size", 5, NULL);
assert (client != NULL);
thrift_server_transport_accept (transport, NULL),
"r_buf_size", 5, NULL);
g_assert (client != NULL);
/* read 10 bytes */
bytes = thrift_buffered_transport_read (client, buf, 10, NULL);
assert (bytes == 10); /* make sure we've read 10 bytes */
assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
g_assert (bytes == 10); /* make sure we've read 10 bytes */
g_assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
/* read 1 byte */
bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
@ -207,65 +248,65 @@ test_write_fail(void)
signal(SIGPIPE, SIG_IGN);
pid = fork ();
assert ( pid >= 0 );
g_assert ( pid >= 0 );
if ( pid == 0 )
{
/* child listens */
ThriftServerTransport *transport = NULL;
ThriftTransport *client = NULL;
{
/* child listens */
ThriftServerTransport *transport = NULL;
ThriftTransport *client = NULL;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port, NULL);
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
/* wrap the client in a BufferedTransport */
client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
thrift_server_transport_accept (transport, NULL),
"r_buf_size", 5, NULL);
assert (client != NULL);
/* wrap the client in a BufferedTransport */
client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
thrift_server_transport_accept (transport, NULL),
"r_buf_size", 5, NULL);
g_assert (client != NULL);
/* just close socket */
thrift_buffered_transport_close (client, NULL);
g_object_unref (client);
g_object_unref (tsocket);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
/* just close socket */
thrift_buffered_transport_close (client, NULL);
g_object_unref (client);
g_object_unref (tsocket);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket),
"w_buf_size", 4, NULL);
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket),
"w_buf_size", 4, NULL);
assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
assert (thrift_buffered_transport_is_open (transport));
g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
g_assert (thrift_buffered_transport_is_open (transport));
/* recognize disconnection */
sleep(1);
assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
/* recognize disconnection */
sleep(1);
g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
/* write and overflow buffer */
assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
/* write and overflow buffer */
g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
/* write 1 and flush */
assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
assert (thrift_buffered_transport_flush (transport, NULL) == FALSE);
/* write 1 and flush */
g_assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
g_assert (thrift_buffered_transport_flush (transport, NULL) == FALSE);
thrift_buffered_transport_close (transport, NULL);
thrift_buffered_transport_close (transport, NULL);
g_object_unref (transport);
g_object_unref (tsocket);
g_object_unref (transport);
g_object_unref (tsocket);
assert ( wait (&status) == pid );
assert ( status == 0 );
}
g_assert ( wait (&status) == pid );
g_assert ( status == 0 );
}
}
int

File diff suppressed because it is too large Load diff

View file

@ -512,13 +512,12 @@ main(int argc, char *argv[])
THRIFT_SERVER_SOCKET_ERROR_ACCEPT));
/* Free our resources */
g_object_unref (server);
g_object_unref (transport_factory);
g_object_unref (protocol_factory);
g_object_unref (server_transport);
g_object_unref (processor);
g_object_unref (handler);
g_clear_object (&server);
g_clear_object (&protocol_factory);
g_clear_object (&transport_factory);
g_clear_object (&server_transport);
g_clear_object (&processor);
g_clear_object (&handler);
/* Wait for the child process to complete and return its exit status */
g_assert (wait (&status) == pid);
@ -527,4 +526,4 @@ main(int argc, char *argv[])
return exit_status;
}
}
}

View file

@ -17,7 +17,6 @@
* under the License.
*/
#include <assert.h>
#include <math.h>
#include <string.h>
#include <glib-object.h>
@ -838,11 +837,11 @@ test_services_inherited (void)
NULL);
/* TTestInheritedClient inherits from TTestSrvClient */
assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
g_assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
T_TEST_TYPE_SRV_CLIENT));
/* TTestInheritedClient implements TTestSrvClient's interface */
assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
g_assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
T_TEST_TYPE_SRV_IF));
/* TTestInheritedClient's inherited properties can be set and retrieved */
@ -856,8 +855,8 @@ test_services_inherited (void)
"output_protocol", &output_protocol,
NULL);
assert (input_protocol == G_OBJECT(protocol));
assert (output_protocol == G_OBJECT(protocol));
g_assert (input_protocol == G_OBJECT(protocol));
g_assert (output_protocol == G_OBJECT(protocol));
g_object_unref (output_protocol);
g_object_unref (input_protocol);

View file

@ -17,7 +17,6 @@
* under the License.
*/
#include <assert.h>
#include <string.h>
#include <fcntl.h>
@ -37,7 +36,7 @@ test_create_and_destroy (void)
{
GObject *object;
object = g_object_new (THRIFT_TYPE_FD_TRANSPORT, "fd", -1, NULL);
assert (object != NULL);
g_assert (object != NULL);
g_object_unref (object);
}
@ -54,7 +53,7 @@ test_open_and_close (void)
filename = NULL;
fd = g_file_open_tmp (NULL, &filename, &error);
assert (fd >= 0);
g_assert (fd >= 0);
transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT,
"fd", fd,
@ -62,20 +61,20 @@ test_open_and_close (void)
klass = THRIFT_TRANSPORT_GET_CLASS (transport);
/* open is no-op */
assert (klass->is_open (transport));
assert (klass->peek (transport, &error));
assert (klass->open (transport, &error));
assert (klass->is_open (transport));
assert (klass->peek (transport, &error));
g_assert (klass->is_open (transport));
g_assert (klass->peek (transport, &error));
g_assert (klass->open (transport, &error));
g_assert (klass->is_open (transport));
g_assert (klass->peek (transport, &error));
assert (klass->close (transport, &error));
assert (! klass->open (transport, &error));
assert (! klass->is_open (transport));
assert (! klass->peek (transport, &error));
g_assert (klass->close (transport, &error));
g_assert (! klass->open (transport, &error));
g_assert (! klass->is_open (transport));
g_assert (! klass->peek (transport, &error));
/* already closed */
assert (close (fd) != 0);
assert (errno == EBADF);
g_assert (close (fd) != 0);
g_assert (errno == EBADF);
g_object_unref (transport);
@ -88,13 +87,13 @@ test_open_and_close (void)
NULL));
klass = THRIFT_TRANSPORT_GET_CLASS (transport);
assert (! klass->is_open (transport));
g_assert (! klass->is_open (transport));
error = NULL;
assert (! klass->peek (transport, &error));
g_assert (! klass->peek (transport, &error));
error = NULL;
assert (! klass->open (transport, &error));
g_assert (! klass->open (transport, &error));
error = NULL;
assert (! klass->close (transport, &error));
g_assert (! klass->close (transport, &error));
g_object_unref (transport);
}
@ -115,22 +114,22 @@ test_read_and_write (void)
filename = NULL;
fd = g_file_open_tmp (NULL, &filename, &error);
assert (fd >= 0);
g_assert (fd >= 0);
/* write */
transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT,
"fd", fd,
NULL));
klass = THRIFT_TRANSPORT_GET_CLASS (transport);
assert (klass->is_open (transport));
assert (klass->write (transport, (gpointer) TEST_DATA, 11, &error));
assert (klass->flush (transport, &error));
assert (klass->close (transport, &error));
g_assert (klass->is_open (transport));
g_assert (klass->write (transport, (gpointer) TEST_DATA, 11, &error));
g_assert (klass->flush (transport, &error));
g_assert (klass->close (transport, &error));
g_object_unref (transport);
/* read */
fd = open(filename, O_RDONLY, S_IRUSR | S_IWUSR);
assert (fd >= 0);
g_assert (fd >= 0);
transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT,
"fd", fd,
@ -142,24 +141,24 @@ test_read_and_write (void)
want = 7;
while (want > 0) {
got = klass->read (transport, (gpointer) b, want, &error);
assert (got > 0 && got <= want);
g_assert (got > 0 && got <= want);
b += got;
want -= got;
}
assert (memcmp (out_buf, TEST_DATA, 7) == 0);
g_assert (memcmp (out_buf, TEST_DATA, 7) == 0);
memset(out_buf, 0, 8);
b = out_buf;
want = 4;
while (want > 0) {
got = klass->read (transport, (gpointer) b, want, &error);
assert (got > 0 && got <= want);
g_assert (got > 0 && got <= want);
b += got;
want -= got;
}
assert (memcmp (out_buf, TEST_DATA + 7, 4) == 0);
g_assert (memcmp (out_buf, TEST_DATA + 7, 4) == 0);
assert (klass->close (transport, &error));
g_assert (klass->close (transport, &error));
g_object_unref (transport);
/* clean up */

View file

@ -17,7 +17,6 @@
* under the License.
*/
#include <assert.h>
#include <netdb.h>
#include <sys/wait.h>
@ -31,6 +30,7 @@
#include "../src/thrift/c_glib/transport/thrift_framed_transport.c"
static void thrift_server (const int port);
static void thrift_socket_server_open (const int port, int times);
/* test object creation and destruction */
static void
@ -42,10 +42,10 @@ test_create_and_destroy(void)
GObject *object = NULL;
object = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, NULL);
assert (object != NULL);
g_assert (object != NULL);
g_object_get (G_OBJECT (object), "transport", &transport,
"r_buf_size", &r_buf_size,
"w_buf_size", &w_buf_size, NULL);
"r_buf_size", &r_buf_size,
"w_buf_size", &w_buf_size, NULL);
g_object_unref (object);
}
@ -55,35 +55,53 @@ test_open_and_close(void)
ThriftSocket *tsocket = NULL;
ThriftTransport *transport = NULL;
GError *err = NULL;
pid_t pid;
int port = 51199;
int status;
/* create a ThriftSocket */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", 51188, NULL);
pid = fork ();
g_assert ( pid >= 0 );
/* create a BufferedTransport wrapper of the Socket */
transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket), NULL);
if ( pid == 0 )
{
/* child listens */
thrift_socket_server_open (port,1);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
/* create a ThriftSocket */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
/* this shouldn't work */
assert (thrift_framed_transport_open (transport, NULL) == FALSE);
assert (thrift_framed_transport_is_open (transport) == TRUE);
assert (thrift_framed_transport_close (transport, NULL) == TRUE);
g_object_unref (transport);
g_object_unref (tsocket);
/* create a BufferedTransport wrapper of the Socket */
transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket), NULL);
/* try and underlying socket failure */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
NULL);
/* this shouldn't work */
g_assert (thrift_framed_transport_open (transport, NULL) == TRUE);
g_assert (thrift_framed_transport_is_open (transport) == TRUE);
g_assert (thrift_framed_transport_close (transport, NULL) == TRUE);
g_object_unref (transport);
g_object_unref (tsocket);
/* create a BufferedTransport wrapper of the Socket */
transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket), NULL);
/* try and underlying socket failure */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
NULL);
assert (thrift_framed_transport_open (transport, &err) == FALSE);
g_object_unref (transport);
g_object_unref (tsocket);
g_error_free (err);
err = NULL;
/* create a BufferedTransport wrapper of the Socket */
transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket), NULL);
g_assert (thrift_framed_transport_open (transport, &err) == FALSE);
g_object_unref (transport);
g_object_unref (tsocket);
g_error_free (err);
err = NULL;
g_assert ( wait (&status) == pid );
g_assert ( status == 0 );
}
}
static void
@ -97,49 +115,49 @@ test_read_and_write(void)
guchar buf[10] = TEST_DATA; /* a buffer */
pid = fork ();
assert ( pid >= 0 );
g_assert ( pid >= 0 );
if ( pid == 0 )
{
/* child listens */
thrift_server (port);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
{
/* child listens */
thrift_server (port);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket),
"w_buf_size", 4, NULL);
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket),
"w_buf_size", 4, NULL);
assert (thrift_framed_transport_open (transport, NULL) == TRUE);
assert (thrift_framed_transport_is_open (transport));
g_assert (thrift_framed_transport_open (transport, NULL) == TRUE);
g_assert (thrift_framed_transport_is_open (transport));
/* write 10 bytes */
thrift_framed_transport_write (transport, buf, 10, NULL);
thrift_framed_transport_flush (transport, NULL);
/* write 10 bytes */
thrift_framed_transport_write (transport, buf, 10, NULL);
thrift_framed_transport_flush (transport, NULL);
thrift_framed_transport_write (transport, buf, 1, NULL);
thrift_framed_transport_flush (transport, NULL);
thrift_framed_transport_write (transport, buf, 1, NULL);
thrift_framed_transport_flush (transport, NULL);
thrift_framed_transport_write (transport, buf, 10, NULL);
thrift_framed_transport_flush (transport, NULL);
thrift_framed_transport_write (transport, buf, 10, NULL);
thrift_framed_transport_flush (transport, NULL);
thrift_framed_transport_write (transport, buf, 10, NULL);
thrift_framed_transport_flush (transport, NULL);
thrift_framed_transport_write (transport, buf, 10, NULL);
thrift_framed_transport_flush (transport, NULL);
thrift_framed_transport_write_end (transport, NULL);
thrift_framed_transport_flush (transport, NULL);
thrift_framed_transport_close (transport, NULL);
thrift_framed_transport_write_end (transport, NULL);
thrift_framed_transport_flush (transport, NULL);
thrift_framed_transport_close (transport, NULL);
g_object_unref (transport);
g_object_unref (tsocket);
g_object_unref (transport);
g_object_unref (tsocket);
assert ( wait (&status) == pid );
assert ( status == 0 );
}
g_assert ( wait (&status) == pid );
g_assert ( status == 0 );
}
}
/* test reading from the transport after the peer has unexpectedly
@ -156,76 +174,100 @@ test_read_after_peer_close(void)
g_assert (pid >= 0);
if (pid == 0)
{
ThriftServerTransport *server_transport = NULL;
ThriftTransport *client_transport = NULL;
{
ThriftServerTransport *server_transport = NULL;
ThriftTransport *client_transport = NULL;
/* child listens */
server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port,
NULL);
g_assert (server_transport != NULL);
/* child listens */
server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port,
NULL);
g_assert (server_transport != NULL);
thrift_server_transport_listen (server_transport, &err);
g_assert (err == NULL);
thrift_server_transport_listen (server_transport, &err);
g_assert (err == NULL);
/* wrap the client transport in a ThriftFramedTransport */
client_transport = g_object_new
(THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", thrift_server_transport_accept (server_transport, &err),
"r_buf_size", 0,
NULL);
g_assert (err == NULL);
g_assert (client_transport != NULL);
/* wrap the client transport in a ThriftFramedTransport */
client_transport = g_object_new
(THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", thrift_server_transport_accept (server_transport, &err),
"r_buf_size", 0,
NULL);
g_assert (err == NULL);
g_assert (client_transport != NULL);
/* close the connection immediately after the client connects */
thrift_transport_close (client_transport, NULL);
/* close the connection immediately after the client connects */
thrift_transport_close (client_transport, NULL);
g_object_unref (client_transport);
g_object_unref (server_transport);
g_object_unref (client_transport);
g_object_unref (server_transport);
exit (0);
} else {
ThriftSocket *tsocket = NULL;
ThriftTransport *transport = NULL;
guchar buf[10]; /* a buffer */
exit (0);
} else {
ThriftSocket *tsocket = NULL;
ThriftTransport *transport = NULL;
guchar buf[10]; /* a buffer */
/* parent connects, wait a bit for the socket to be created */
sleep (1);
/* parent connects, wait a bit for the socket to be created */
sleep (1);
tsocket = g_object_new (THRIFT_TYPE_SOCKET,
"hostname", "localhost",
"port", port,
NULL);
transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket),
"w_buf_size", 0,
NULL);
tsocket = g_object_new (THRIFT_TYPE_SOCKET,
"hostname", "localhost",
"port", port,
NULL);
transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
"transport", THRIFT_TRANSPORT (tsocket),
"w_buf_size", 0,
NULL);
g_assert (thrift_transport_open (transport, NULL) == TRUE);
g_assert (thrift_transport_is_open (transport));
g_assert (thrift_transport_open (transport, NULL) == TRUE);
g_assert (thrift_transport_is_open (transport));
/* attempting to read from the transport after the peer has closed
/* attempting to read from the transport after the peer has closed
the connection fails gracefully without generating a critical
warning or segmentation fault */
thrift_transport_read (transport, buf, 10, &err);
g_assert (err != NULL);
thrift_transport_read (transport, buf, 10, &err);
g_assert (err != NULL);
g_error_free (err);
err = NULL;
g_error_free (err);
err = NULL;
thrift_transport_read_end (transport, &err);
g_assert (err == NULL);
thrift_transport_read_end (transport, &err);
g_assert (err == NULL);
thrift_transport_close (transport, &err);
g_assert (err == NULL);
thrift_transport_close (transport, &err);
g_assert (err == NULL);
g_object_unref (transport);
g_object_unref (tsocket);
g_object_unref (transport);
g_object_unref (tsocket);
g_assert (wait (&status) == pid);
g_assert (status == 0);
g_assert (wait (&status) == pid);
g_assert (status == 0);
}
}
static void
thrift_socket_server_open (const int port, int times)
{
int bytes = 0;
ThriftServerTransport *transport = NULL;
ThriftTransport *client = NULL;
guchar buf[10]; /* a buffer */
guchar match[10] = TEST_DATA;
int i;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
for(i=0;i<times;i++){
client = thrift_server_transport_accept (transport, NULL);
g_assert (client != NULL);
thrift_socket_close (client, NULL);
g_object_unref (client);
}
g_object_unref (tsocket);
}
static void
@ -238,21 +280,21 @@ thrift_server (const int port)
guchar match[10] = TEST_DATA;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port, NULL);
"port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
/* wrap the client in a BufferedTransport */
client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
thrift_server_transport_accept (transport, NULL),
"r_buf_size", 5, NULL);
assert (client != NULL);
thrift_server_transport_accept (transport, NULL),
"r_buf_size", 5, NULL);
g_assert (client != NULL);
/* read 10 bytes */
bytes = thrift_framed_transport_read (client, buf, 10, NULL);
assert (bytes == 10); /* make sure we've read 10 bytes */
assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
g_assert (bytes == 10); /* make sure we've read 10 bytes */
g_assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
bytes = thrift_framed_transport_read (client, buf, 6, NULL);
bytes = thrift_framed_transport_read (client, buf, 5, NULL);

View file

@ -17,7 +17,6 @@
* under the License.
*/
#include <assert.h>
#include <netdb.h>
#include <thrift/c_glib/transport/thrift_transport.h>
@ -37,7 +36,7 @@ test_create_and_destroy (void)
object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
"buf_size", 10,
NULL);
assert (object != NULL);
g_assert (object != NULL);
g_object_unref (object);
}
@ -48,7 +47,7 @@ test_create_and_destroy_large (void)
object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
"buf_size", 10 * 1024 * 1024,
NULL);
assert (object != NULL);
g_assert (object != NULL);
g_object_unref (object);
}
@ -57,7 +56,7 @@ test_create_and_destroy_default (void)
{
GObject *object = NULL;
object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
assert (object != NULL);
g_assert (object != NULL);
g_object_unref (object);
}
@ -66,11 +65,11 @@ test_create_and_destroy_external (void)
{
GObject *object = NULL;
GByteArray *buf = g_byte_array_new ();
assert (buf != NULL);
g_assert (buf != NULL);
object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
"buf", buf,
NULL);
assert (object != NULL);
g_assert (object != NULL);
g_object_unref (object);
}
@ -84,12 +83,12 @@ test_create_and_destroy_unowned (void)
object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER,
"owner", FALSE,
NULL);
assert (object != NULL);
g_assert (object != NULL);
g_value_init (&val, G_TYPE_POINTER);
g_object_get_property (object, "buf", &val);
buf = (GByteArray*) g_value_get_pointer (&val);
assert (buf != NULL);
g_assert (buf != NULL);
g_byte_array_unref (buf);
g_value_unset (&val);
@ -105,9 +104,9 @@ test_open_and_close (void)
tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
/* no-ops */
assert (thrift_memory_buffer_open (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
assert (thrift_memory_buffer_is_open (THRIFT_TRANSPORT (tbuffer)) == TRUE);
assert (thrift_memory_buffer_close (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
g_assert (thrift_memory_buffer_open (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
g_assert (thrift_memory_buffer_is_open (THRIFT_TRANSPORT (tbuffer)) == TRUE);
g_assert (thrift_memory_buffer_close (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
g_object_unref (tbuffer);
}
@ -122,18 +121,18 @@ test_read_and_write (void)
GError *error = NULL;
tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL);
assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
(gpointer) TEST_DATA,
10, &error) == FALSE);
assert (error != NULL);
g_assert (error != NULL);
g_error_free (error);
error = NULL;
g_object_unref (tbuffer);
tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, NULL);
assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
(gpointer) TEST_DATA, 10, &error) == TRUE);
assert (error == NULL);
g_assert (error == NULL);
memset(read, 0, 10);
b = read;
@ -141,12 +140,12 @@ test_read_and_write (void)
while (want > 0) {
got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
(gpointer) b, want, &error);
assert (got > 0 && got <= want);
assert (error == NULL);
g_assert (got > 0 && got <= want);
g_assert (error == NULL);
b += got;
want -= got;
}
assert (memcmp (read, TEST_DATA, 10) == 0);
g_assert (memcmp (read, TEST_DATA, 10) == 0);
g_object_unref (tbuffer);
}
@ -161,9 +160,9 @@ test_read_and_write_default (void)
tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
for (i = 0; i < 100; ++i) {
assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
(gpointer) TEST_DATA, 10, &error) == TRUE);
assert (error == NULL);
g_assert (error == NULL);
}
for (i = 0; i < 100; ++i) {
@ -173,12 +172,12 @@ test_read_and_write_default (void)
while (want > 0) {
got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
(gpointer) b, want, &error);
assert (got > 0 && got <= want);
assert (error == NULL);
g_assert (got > 0 && got <= want);
g_assert (error == NULL);
b += got;
want -= got;
}
assert (memcmp (read, TEST_DATA, 10) == 0);
g_assert (memcmp (read, TEST_DATA, 10) == 0);
}
g_object_unref (tbuffer);
}
@ -190,14 +189,14 @@ test_read_and_write_external (void)
gchar *b;
GError *error = NULL;
GByteArray *buf = g_byte_array_new ();
assert (buf != NULL);
g_assert (buf != NULL);
tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf", buf, NULL);
assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
(gpointer) TEST_DATA, 10, &error) == TRUE);
assert (error == NULL);
g_assert (error == NULL);
assert (memcmp (buf->data, TEST_DATA, 10) == 0);
g_assert (memcmp (buf->data, TEST_DATA, 10) == 0);
g_object_unref (tbuffer);
}

View file

@ -17,7 +17,6 @@
* under the License.
*/
#include <assert.h>
#include <glib.h>
#include <thrift/c_glib/thrift_struct.h>
@ -76,18 +75,18 @@ test_simple (void)
/* write-to-read with optional fields */
s1->im_optional = 10;
assert (s1->__isset_im_default == FALSE);
assert (s1->__isset_im_optional == FALSE);
g_assert (s1->__isset_im_default == FALSE);
g_assert (s1->__isset_im_optional == FALSE);
write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s2), NULL, NULL);
assert (s2->__isset_im_default == TRUE);
assert (s2->__isset_im_optional == FALSE);
assert (s2->im_optional == 0);
g_assert (s2->__isset_im_default == TRUE);
g_assert (s2->__isset_im_optional == FALSE);
g_assert (s2->im_optional == 0);
s1->__isset_im_optional = TRUE;
write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s3), NULL, NULL);
assert (s3->__isset_im_default == TRUE);
assert (s3->__isset_im_optional == TRUE);
assert (s3->im_optional == 10);
g_assert (s3->__isset_im_default == TRUE);
g_assert (s3->__isset_im_optional == TRUE);
g_assert (s3->im_optional == 10);
g_object_unref (s1);
g_object_unref (s2);
@ -109,10 +108,10 @@ test_tricky1 (void)
write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t1), NULL, NULL);
write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t2), NULL, NULL);
assert (t1->__isset_im_default == FALSE);
assert (t2->__isset_im_optional == TRUE);
assert (t1->im_default == t2->im_optional);
assert (t1->im_default == 0);
g_assert (t1->__isset_im_default == FALSE);
g_assert (t2->__isset_im_optional == TRUE);
g_assert (t1->im_default == t2->im_optional);
g_assert (t1->im_default == 0);
g_object_unref (t1);
g_object_unref (t2);
@ -133,7 +132,7 @@ test_tricky2 (void)
write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t3), NULL, NULL);
write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t1), NULL, NULL);
assert (t1->__isset_im_default == TRUE);
g_assert (t1->__isset_im_default == TRUE);
g_object_unref (t1);
g_object_unref (t3);
@ -176,12 +175,12 @@ test_tricky4 (void)
/* throws protocol exception */
write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, &read_error);
assert (read_error != NULL);
g_assert (read_error != NULL);
g_error_free (read_error);
write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL);
assert (t2->__isset_im_optional);
g_assert (t2->__isset_im_optional);
g_object_unref (t2);
g_object_unref (t3);

View file

@ -17,7 +17,6 @@
* under the License.
*/
#include <assert.h>
#include <glib.h>
#include <stdlib.h>
#include <unistd.h>
@ -89,7 +88,7 @@ test_server (void)
/* run the server in a child process */
pid = fork ();
assert (pid >= 0);
g_assert (pid >= 0);
if (pid == 0)
{
@ -103,8 +102,8 @@ test_server (void)
g_object_unref (ss);
g_object_unref (tss);
g_object_unref (p);
assert (wait (&status) == pid);
assert (status == SIGINT);
g_assert (wait (&status) == pid);
g_assert (status == SIGINT);
}
}

View file

@ -17,7 +17,6 @@
* under the License.
*/
#include <assert.h>
#include <glib-object.h>
#include "../src/thrift/c_glib/thrift_struct.c"
@ -89,7 +88,7 @@ test_initialize_object (void)
ThriftTestStruct *t = NULL;
t = g_object_new (THRIFT_TYPE_TEST_STRUCT, NULL);
assert ( THRIFT_IS_STRUCT (t));
g_assert ( THRIFT_IS_STRUCT (t));
thrift_struct_read (THRIFT_STRUCT (t), NULL, NULL);
thrift_struct_write (THRIFT_STRUCT (t), NULL, NULL);
thrift_test_struct_read (THRIFT_STRUCT (t), NULL, NULL);

View file

@ -1,4 +1,3 @@
#include <assert.h>
#include <netdb.h>
#include <thrift/c_glib/thrift.h>
@ -87,14 +86,14 @@ test_thrift_handler (void)
g_object_weak_ref (G_OBJECT (argument), set_indicator, (gpointer) &indicator);
assert (thrift_test_handler_test_insanity (NULL, &_return, argument, &error));
assert (! indicator);
g_assert (thrift_test_handler_test_insanity (NULL, &_return, argument, &error));
g_assert (! indicator);
g_hash_table_unref (_return);
assert (! indicator);
g_assert (! indicator);
g_object_unref (argument);
assert (indicator);
g_assert (indicator);
}
int

View file

@ -17,7 +17,7 @@
* under the License.
*/
/* test a C client with a C++ server */
/* test a C client with a C++ server (that makes sense...) */
#include <signal.h>
#include <sys/types.h>
@ -25,23 +25,34 @@
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/protocol/TDebugProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/stdcxx.h>
#include <thrift/transport/TServerSocket.h>
#include "ThriftTest.h"
#include "ThriftTest_types.h"
#include <iostream>
using namespace std;
using namespace boost;
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace apache::thrift;
using namespace apache::thrift::concurrency;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::thrift::server;
using namespace apache::thrift::transport;
using namespace thrift::test;
using std::cout;
using std::endl;
using std::fixed;
using std::make_pair;
using std::map;
using std::set;
using std::string;
using std::vector;
#define TEST_PORT 9980
// Extra functions required for ThriftTest_types to work
@ -347,7 +358,7 @@ test_thrift_client (void)
gchar *string = NULL;
gint8 byte = 0;
gint16 i16 = 0;
gint32 i32 = 0, another_i32 = 56789;
gint32 i32 = 0, another_i32 = 56789;
gint64 i64 = 0;
double dbl = 0.0;
TTestXtruct *xtruct_in, *xtruct_out;
@ -356,7 +367,7 @@ test_thrift_client (void)
GHashTable *set_in = NULL, *set_out = NULL;
GArray *list_in = NULL, *list_out = NULL;
TTestNumberz enum_in, enum_out;
TTestUserId user_id_in, user_id_out;
TTestUserId user_id_in, user_id_out;
GHashTable *insanity_in = NULL;
TTestXtruct *xtruct1, *xtruct2;
TTestInsanity *insanity_out = NULL;
@ -371,7 +382,7 @@ test_thrift_client (void)
#endif
// create a C client
tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
"hostname", "localhost",
"port", TEST_PORT, NULL);
protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
@ -607,11 +618,11 @@ main (void)
if (pid == 0) /* child */
{
boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
boost::shared_ptr<TestHandler> testHandler(new TestHandler());
boost::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
boost::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
stdcxx::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
stdcxx::shared_ptr<TestHandler> testHandler(new TestHandler());
stdcxx::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
stdcxx::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
stdcxx::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory);
signal (SIGALRM, bailout);
alarm (60);

View file

@ -17,7 +17,6 @@
* under the License.
*/
#include <assert.h>
#include <netdb.h>
#include <sys/wait.h>
@ -34,9 +33,9 @@ int
my_socket(int domain, int type, int protocol)
{
if (socket_error == 0)
{
return socket (domain, type, protocol);
}
{
return socket (domain, type, protocol);
}
return -1;
}
@ -45,9 +44,9 @@ ssize_t
my_recv(int socket, void *buffer, size_t length, int flags)
{
if (recv_error == 0)
{
return recv (socket, buffer, length, flags);
}
{
return recv (socket, buffer, length, flags);
}
return -1;
}
@ -56,9 +55,9 @@ ssize_t
my_send(int socket, const void *buffer, size_t length, int flags)
{
if (send_error == 0)
{
return send (socket, buffer, length, flags);
}
{
return send (socket, buffer, length, flags);
}
return -1;
}
@ -71,7 +70,7 @@ my_send(int socket, const void *buffer, size_t length, int flags)
#undef send
static void thrift_socket_server (const int port);
static void thrift_socket_server_open (const int port, int times);
/* test object creation and destruction */
static void
test_create_and_destroy(void)
@ -81,7 +80,7 @@ test_create_and_destroy(void)
GObject *object = NULL;
object = g_object_new (THRIFT_TYPE_SOCKET, NULL);
assert (object != NULL);
g_assert (object != NULL);
g_object_get (G_OBJECT(object), "hostname", &hostname, "port", &port, NULL);
g_free (hostname);
@ -94,82 +93,100 @@ test_open_and_close(void)
ThriftSocket *tsocket = NULL;
ThriftTransport *transport = NULL;
GError *err = NULL;
int port = 51199;
pid_t pid;
int status;
/* open a connection and close it */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", 51188, NULL);
transport = THRIFT_TRANSPORT (tsocket);
thrift_socket_open (transport, NULL);
assert (thrift_socket_is_open (transport) == TRUE);
thrift_socket_close (transport, NULL);
assert (thrift_socket_is_open (transport) == FALSE);
pid = fork ();
g_assert ( pid >= 0 );
/* test close failure */
tsocket->sd = -1;
thrift_socket_close (transport, NULL);
g_object_unref (tsocket);
if ( pid == 0 )
{
/* child listens */
thrift_socket_server_open (port, 1);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
/* try a hostname lookup failure */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
NULL);
transport = THRIFT_TRANSPORT (tsocket);
assert (thrift_socket_open (transport, &err) == FALSE);
g_object_unref (tsocket);
g_error_free (err);
err = NULL;
/* open a connection and close it */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
transport = THRIFT_TRANSPORT (tsocket);
thrift_socket_open (transport, NULL);
g_assert (thrift_socket_is_open (transport) == TRUE);
thrift_socket_close (transport, NULL);
g_assert (thrift_socket_is_open (transport) == FALSE);
/* try an error call to socket() */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", NULL);
transport = THRIFT_TRANSPORT (tsocket);
socket_error = 1;
assert (thrift_socket_open (transport, &err) == FALSE);
socket_error = 0;
g_object_unref (tsocket);
g_error_free (err);
/* test close failure */
tsocket->sd = -1;
thrift_socket_close (transport, NULL);
g_object_unref (tsocket);
/* try a hostname lookup failure */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
NULL);
transport = THRIFT_TRANSPORT (tsocket);
g_assert (thrift_socket_open (transport, &err) == FALSE);
g_object_unref (tsocket);
g_error_free (err);
err = NULL;
/* try an error call to socket() */
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", NULL);
transport = THRIFT_TRANSPORT (tsocket);
socket_error = 1;
g_assert (thrift_socket_open (transport, &err) == FALSE);
socket_error = 0;
g_object_unref (tsocket);
g_error_free (err);
g_assert ( wait (&status) == pid );
g_assert ( status == 0 );
}
}
static void
test_read_and_write(void)
{
int status;
pid_t pid;
ThriftSocket *tsocket = NULL;
ThriftTransport *transport = NULL;
pid_t pid;
int port = 51199;
int status;
guchar buf[10] = TEST_DATA; /* a buffer */
pid = fork ();
assert ( pid >= 0 );
g_assert ( pid >= 0 );
if ( pid == 0 )
{
/* child listens */
thrift_socket_server (port);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
{
/* child listens */
thrift_socket_server (port);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
transport = THRIFT_TRANSPORT (tsocket);
assert (thrift_socket_open (transport, NULL) == TRUE);
assert (thrift_socket_is_open (transport));
thrift_socket_write (transport, buf, 10, NULL);
tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
"port", port, NULL);
transport = THRIFT_TRANSPORT (tsocket);
g_assert (thrift_socket_open (transport, NULL) == TRUE);
g_assert (thrift_socket_is_open (transport));
thrift_socket_write (transport, buf, 10, NULL);
/* write fail */
send_error = 1;
thrift_socket_write (transport, buf, 1, NULL);
send_error = 0;
/* write fail */
send_error = 1;
thrift_socket_write (transport, buf, 1, NULL);
send_error = 0;
thrift_socket_write_end (transport, NULL);
thrift_socket_flush (transport, NULL);
thrift_socket_close (transport, NULL);
g_object_unref (tsocket);
thrift_socket_write_end (transport, NULL);
thrift_socket_flush (transport, NULL);
thrift_socket_close (transport, NULL);
g_object_unref (tsocket);
assert ( wait (&status) == pid );
assert ( status == 0 );
}
g_assert ( wait (&status) == pid );
g_assert ( status == 0 );
}
}
/* test ThriftSocket's peek() implementation */
@ -184,9 +201,9 @@ test_peek(void)
GError *error = NULL;
client_transport = g_object_new (THRIFT_TYPE_SOCKET,
"hostname", "localhost",
"port", port,
NULL);
"hostname", "localhost",
"port", port,
NULL);
/* thrift_transport_peek returns FALSE when the socket is closed */
g_assert (thrift_transport_is_open (client_transport) == FALSE);
@ -197,80 +214,104 @@ test_peek(void)
g_assert (pid >= 0);
if (pid == 0)
{
ThriftServerTransport *server_transport = NULL;
{
ThriftServerTransport *server_transport = NULL;
g_object_unref (client_transport);
g_object_unref (client_transport);
/* child listens */
server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port,
NULL);
g_assert (server_transport != NULL);
/* child listens */
server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port,
NULL);
g_assert (server_transport != NULL);
thrift_server_transport_listen (server_transport, &error);
g_assert (error == NULL);
thrift_server_transport_listen (server_transport, &error);
g_assert (error == NULL);
client_transport = g_object_new
(THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", thrift_server_transport_accept (server_transport, &error),
"r_buf_size", 0,
"w_buf_size", sizeof data,
NULL);
g_assert (error == NULL);
g_assert (client_transport != NULL);
client_transport = g_object_new
(THRIFT_TYPE_BUFFERED_TRANSPORT,
"transport", thrift_server_transport_accept (server_transport, &error),
"r_buf_size", 0,
"w_buf_size", sizeof data,
NULL);
g_assert (error == NULL);
g_assert (client_transport != NULL);
/* write exactly one character to the client */
g_assert (thrift_transport_write (client_transport,
&data,
sizeof data,
&error) == TRUE);
/* write exactly one character to the client */
g_assert (thrift_transport_write (client_transport,
&data,
sizeof data,
&error) == TRUE);
thrift_transport_flush (client_transport, &error);
thrift_transport_write_end (client_transport, &error);
thrift_transport_close (client_transport, &error);
thrift_transport_flush (client_transport, &error);
thrift_transport_write_end (client_transport, &error);
thrift_transport_close (client_transport, &error);
g_object_unref (client_transport);
g_object_unref (server_transport);
g_object_unref (client_transport);
g_object_unref (server_transport);
exit (0);
}
exit (0);
}
else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
/* parent connects, wait a bit for the socket to be created */
sleep (1);
/* connect to the child */
thrift_transport_open (client_transport, &error);
g_assert (error == NULL);
g_assert (thrift_transport_is_open (client_transport) == TRUE);
/* connect to the child */
thrift_transport_open (client_transport, &error);
g_assert (error == NULL);
g_assert (thrift_transport_is_open (client_transport) == TRUE);
/* thrift_transport_peek returns TRUE when the socket is open and there is
/* thrift_transport_peek returns TRUE when the socket is open and there is
data available to be read */
g_assert (thrift_transport_peek (client_transport, &error) == TRUE);
g_assert (error == NULL);
g_assert (thrift_transport_peek (client_transport, &error) == TRUE);
g_assert (error == NULL);
/* read exactly one character from the server */
g_assert_cmpint (thrift_transport_read (client_transport,
&data,
sizeof data,
&error), ==, sizeof data);
/* read exactly one character from the server */
g_assert_cmpint (thrift_transport_read (client_transport,
&data,
sizeof data,
&error), ==, sizeof data);
/* thrift_transport_peek returns FALSE when the socket is open but there is
/* thrift_transport_peek returns FALSE when the socket is open but there is
no (more) data available to be read */
g_assert (thrift_transport_is_open (client_transport) == TRUE);
g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
g_assert (error == NULL);
g_assert (thrift_transport_is_open (client_transport) == TRUE);
g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
g_assert (error == NULL);
thrift_transport_read_end (client_transport, &error);
thrift_transport_close (client_transport, &error);
thrift_transport_read_end (client_transport, &error);
thrift_transport_close (client_transport, &error);
g_object_unref (client_transport);
g_object_unref (client_transport);
g_assert (wait (&status) == pid);
g_assert (status == 0);
g_assert (wait (&status) == pid);
g_assert (status == 0);
}
}
static void
thrift_socket_server_open (const int port, int times)
{
int bytes = 0;
ThriftServerTransport *transport = NULL;
ThriftTransport *client = NULL;
guchar buf[10]; /* a buffer */
guchar match[10] = TEST_DATA;
int i;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
for(i=0;i<times;i++){
client = thrift_server_transport_accept (transport, NULL);
g_assert (client != NULL);
thrift_socket_close (client, NULL);
g_object_unref (client);
}
g_object_unref (tsocket);
}
static void
thrift_socket_server (const int port)
{
@ -281,17 +322,17 @@ thrift_socket_server (const int port)
guchar match[10] = TEST_DATA;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port, NULL);
"port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
client = thrift_server_transport_accept (transport, NULL);
assert (client != NULL);
g_assert (client != NULL);
/* read 10 bytes */
bytes = thrift_socket_read (client, buf, 10, NULL);
assert (bytes == 10); /* make sure we've read 10 bytes */
assert ( memcmp(buf, match, 10) == 0 ); /* make sure what we got matches */
g_assert (bytes == 10); /* make sure we've read 10 bytes */
g_assert ( memcmp(buf, match, 10) == 0 ); /* make sure what we got matches */
/* failed read */
recv_error = 1;

View file

@ -0,0 +1,542 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#define _POSIX_C_SOURCE 200112L /* https://stackoverflow.com/questions/37541985/storage-size-of-addrinfo-isnt-known */
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <thrift/c_glib/transport/thrift_transport.h>
#include <thrift/c_glib/transport/thrift_buffered_transport.h>
#include <thrift/c_glib/transport/thrift_server_transport.h>
#include <thrift/c_glib/transport/thrift_server_socket.h>
#include <thrift/c_glib/transport/thrift_ssl_socket.h>
/* #define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' } */
#define TEST_DATA { "GET / HTTP/1.1\n\n" }
/* substituted functions to test failures of system and library calls */
static int socket_error = 0;
int
my_socket(int domain, int type, int protocol)
{
if (socket_error == 0)
{
return socket (domain, type, protocol);
}
return -1;
}
static int recv_error = 0;
ssize_t
my_recv(int socket, void *buffer, size_t length, int flags)
{
if (recv_error == 0)
{
return recv (socket, buffer, length, flags);
}
return -1;
}
static int send_error = 0;
ssize_t
my_send(int socket, const void *buffer, size_t length, int flags)
{
if (send_error == 0)
{
return send (socket, buffer, length, flags);
}
return -1;
}
#define socket my_socket
#define recv my_recv
#define send my_send
#include "../src/thrift/c_glib/transport/thrift_ssl_socket.c"
#undef socket
#undef recv
#undef send
static void thrift_socket_server (const int port);
/* test object creation and destruction */
static void
test_ssl_create_and_destroy(void)
{
gchar *hostname = NULL;
guint port = 0;
GObject *object = NULL;
object = g_object_new (THRIFT_TYPE_SSL_SOCKET, NULL);
g_assert (object != NULL);
g_object_get (G_OBJECT(object), "hostname", &hostname, "port", &port, NULL);
g_free (hostname);
g_object_unref (object);
}
static void
test_ssl_create_and_set_properties(void)
{
gchar *hostname = NULL;
guint port = 0;
SSL_CTX* ssl_ctx= NULL;
GError *error=NULL;
GObject *object = NULL;
object = thrift_ssl_socket_new(SSLTLS, &error);
g_object_get (G_OBJECT(object), "hostname", &hostname, "port", &port, "ssl_context", &ssl_ctx, NULL);
g_assert (ssl_ctx!=NULL);
g_free (hostname);
g_object_unref (object);
}
static void
test_ssl_open_and_close_non_ssl_server(void)
{
ThriftSSLSocket *tSSLSocket = NULL;
ThriftTransport *transport = NULL;
GError *error=NULL;
pid_t pid;
int non_ssl_port = 51198;
char errormsg[255];
pid = fork ();
g_assert ( pid >= 0 );
if ( pid == 0 )
{
/* child listens */
/* This is a non SSL server */
thrift_socket_server (non_ssl_port);
exit (0);
} else {
/* parent connects, wait a bit for the socket to be created */
sleep (1);
/* open a connection and close it */
tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", non_ssl_port, &error);
transport = THRIFT_TRANSPORT (tSSLSocket);
g_assert (thrift_ssl_socket_open (transport, &error) == FALSE);
g_assert_cmpstr(error->message, == ,"Error while connect/bind: 68 -> Connection reset by peer");
g_clear_error (&error);
g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
thrift_ssl_socket_close (transport, NULL);
g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
/* test close failure */
THRIFT_SOCKET(tSSLSocket)->sd = -1;
thrift_ssl_socket_close (transport, NULL);
g_object_unref (tSSLSocket);
/* try a hostname lookup failure */
tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost.broken", non_ssl_port, &error);
transport = THRIFT_TRANSPORT (tSSLSocket);
g_assert (thrift_ssl_socket_open (transport, &error) == FALSE);
snprintf(errormsg, 255, "host lookup failed for localhost.broken:%d - Unknown host", non_ssl_port);
g_assert_cmpstr(error->message, ==, errormsg);
g_clear_error (&error);
g_object_unref (tSSLSocket);
error = NULL;
/* try an error call to socket() */
/*
tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", port, &error);
transport = THRIFT_TRANSPORT (tSSLSocket);
socket_error = 1;
assert (thrift_ssl_socket_open (transport, &error) == FALSE);
socket_error = 0;
g_object_unref (tSSLSocket);
g_error_free (error);
*/
}
}
static void
test_ssl_write_invalid_socket(void)
{
ThriftSSLSocket *tSSLSocket = NULL;
ThriftTransport *transport = NULL;
GError *error=NULL;
char buffer[] = "this must not break";
/* open a connection and close it */
tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", 51188+1, &error);
transport = THRIFT_TRANSPORT (tSSLSocket);
g_assert (thrift_ssl_socket_open (transport, NULL) == FALSE);
g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
/* FIXME This must be tested but since the assertion inside thrift_ssl_socket_write breaks the test unit
it's disabled. They idea is to disable trap/coredump during this test
g_assert (thrift_ssl_socket_write(transport, buffer, sizeof(buffer), &error) == FALSE);
g_message ("write_failed_with_error: %s",
error != NULL ? error->message : "No");
g_clear_error (&error);
*/
thrift_ssl_socket_close (transport, NULL);
g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
/* test close failure */
THRIFT_SOCKET(tSSLSocket)->sd = -1;
thrift_ssl_socket_close (transport, NULL);
g_object_unref (tSSLSocket);
}
/**
* Print the common name of certificate
*/
unsigned char * get_cn_name(X509_NAME* const name)
{
int idx = -1;
unsigned char *utf8 = NULL;
do
{
if(!name) break; /* failed */
idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
if(!(idx > -1)) break; /* failed */
X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, idx);
if(!entry) break; /* failed */
ASN1_STRING* data = X509_NAME_ENTRY_get_data(entry);
if(!data) break; /* failed */
int length = ASN1_STRING_to_UTF8(&utf8, data);
if(!utf8 || !(length > 0)) break; /* failed */
} while (0);
return utf8;
}
/*
* Handle IPV4 and IPV6 addr
*/
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET)
return &(((struct sockaddr_in*)sa)->sin_addr);
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int verify_ip(char * hostname, struct sockaddr_storage *addr)
{
struct addrinfo *addr_info,*p;
struct addrinfo hints;
int res;
int retval = 0;
memset(&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_UNSPEC; /* use AF_INET6 to force IPv6 */
hints.ai_socktype = SOCK_STREAM;
if ( (res = getaddrinfo(hostname, NULL, &hints, &addr_info) ) != 0)
{
/* get the host info */
g_error("Cannot get the host address");
return retval;
}
/* loop through all the results and connect to the first we can */
char dnshost[INET6_ADDRSTRLEN]; /* bigger addr supported IPV6 */
char socket_ip[INET6_ADDRSTRLEN];
if(inet_ntop(addr->ss_family, get_in_addr(addr), socket_ip, INET6_ADDRSTRLEN)==socket_ip){
g_debug("We are connected to host %s checking against certificate...", socket_ip);
int sizeip = socket_ip!=NULL ? strlen(socket_ip) : 0;
for(p = addr_info; p != NULL; p = p->ai_next) {
if(inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), dnshost, INET6_ADDRSTRLEN)==dnshost){
if(dnshost!=NULL){
g_info("DNS address [%i -> %s]", p->ai_addr, dnshost);
if(!strncmp(dnshost, socket_ip, sizeip)){
retval=1;
break; /* if we get here, we must have connected successfully */
}
}
}
}
}
if(addr_info)
freeaddrinfo(addr_info);
return retval;
}
static void
read_from_file(char *buffer, long size, const char *file_name)
{
char ch;
long index=0;
FILE *fp;
fp = fopen(file_name,"r"); /* read mode */
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
while(index<size && ( ch = fgetc(fp) ) != EOF ){
buffer[index++] = ch;
}
fclose(fp);
}
#define ISSUER_CN_PINNING "The Apache Software Foundation"
#define SUBJECT_CN_PINNING "localhost"
#define CERT_SERIAL_NUMBER "1"
gboolean verify_certificate_sn(X509 *cert, const unsigned char *serial_number)
{
gboolean retval = FALSE;
ASN1_INTEGER *serial = X509_get_serialNumber(cert);
BIGNUM *bn = ASN1_INTEGER_to_BN(serial, NULL);
if (!bn) {
fprintf(stderr, "unable to convert ASN1INTEGER to BN\n");
return EXIT_FAILURE;
}
char *tmp = BN_bn2dec(bn);
if (!tmp) {
g_warning(stderr, "unable to convert BN to decimal string.\n");
BN_free(bn);
return EXIT_FAILURE;
}
/*
if (strlen(tmp) >= len) {
g_warn(stderr, "buffer length shorter than serial number\n");
BN_free(bn);
OPENSSL_free(tmp);
return EXIT_FAILURE;
}
*/
if(!strncmp(serial_number, tmp, strlen(serial_number))){
retval=TRUE;
}else{
g_warning("Serial number is not valid");
}
BN_free(bn);
OPENSSL_free(tmp);
return retval;
}
gboolean my_access_manager(ThriftTransport * transport, X509 *cert, struct sockaddr_storage *addr, GError **error)
{
ThriftSSLSocket *sslSocket = THRIFT_SSL_SOCKET (transport);
g_info("Processing access to the server");
X509_NAME* iname = cert ? X509_get_issuer_name(cert) : NULL;
X509_NAME* sname = cert ? X509_get_subject_name(cert) : NULL;
/* Issuer is the authority we trust that warrants nothing useful */
const unsigned char * issuer = get_cn_name(iname);
if(issuer){
gboolean valid = TRUE;
g_info("Issuer (cn) %s", issuer);
/* Issuer pinning */
if(strncmp(ISSUER_CN_PINNING, issuer, strlen(ISSUER_CN_PINNING))){
g_warning("The Issuer of the certificate is not valid");
valid=FALSE;
}
OPENSSL_free(issuer);
if(!valid)
return valid;
}
/* Subject is who the certificate is issued to by the authority */
const unsigned char * subject = get_cn_name(sname);
if(subject){
g_info("Subject (cn) %s", subject);
gboolean valid = TRUE;
/* Subject pinning */
if(strncmp(SUBJECT_CN_PINNING, subject, strlen(SUBJECT_CN_PINNING))){
g_warning("The subject of the certificate is not valid");
valid=FALSE;
}
if(!valid)
return valid;
/* Host pinning */
if(verify_ip(subject, addr)){
g_info("Verified subject");
}else{
g_info("Cannot verify subject");
valid=FALSE;
}
OPENSSL_free(subject);
if(!valid)
return valid;
}
if(!verify_certificate_sn(cert, CERT_SERIAL_NUMBER)){
return FALSE;
}else{
g_info("Verified serial number");
}
return TRUE;
}
#ifdef BUILD_SERVER
static void
test_ssl_authorization_manager(void)
{
int status=0;
pid_t pid;
ThriftSSLSocket *tSSLsocket = NULL;
ThriftTransport *transport = NULL;
/* int port = 51199; */
int port = 443;
GError *error=NULL;
guchar buf[17] = TEST_DATA; /* a buffer */
/*
pid = fork ();
g_assert ( pid >= 0 );
if ( pid == 0 )
{
thrift_ssl_socket_server (port);
exit (0);
} else {
*/
/* parent connects, wait a bit for the socket to be created */
sleep (1);
/* Test against level2 owncloud certificate */
tSSLsocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", port, &error);
thrift_ssl_socket_set_manager(tSSLsocket, my_access_manager); /* Install pinning manager */
/* thrift_ssl_load_cert_from_file(tSSLsocket, "./owncloud.level2crm.pem"); */
unsigned char cert_buffer[65534];
read_from_file(cert_buffer, 65534, "../../keys/client.pem");
if(!thrift_ssl_load_cert_from_buffer(tSSLsocket, cert_buffer)){
g_warning("Certificates cannot be loaded!");
}
transport = THRIFT_TRANSPORT (tSSLsocket);
g_assert (thrift_ssl_socket_open (transport, NULL) == TRUE);
g_assert (thrift_ssl_socket_is_open (transport));
thrift_ssl_socket_write (transport, buf, 17, NULL);
/* write fail */
send_error = 1;
/*
thrift_ssl_socket_write (transport, buf, 1, NULL);
send_error = 0;
thrift_ssl_socket_write_end (transport, NULL);
thrift_ssl_socket_flush (transport, NULL);
*/
thrift_ssl_socket_close (transport, NULL);
g_object_unref (tSSLsocket);
/* g_assert ( wait (&status) == pid ); */
g_assert ( status == 0 );
/* } */
}
#endif
static void
thrift_socket_server (const int port)
{
int bytes = 0;
ThriftServerTransport *transport = NULL;
ThriftTransport *client = NULL;
guchar buf[10]; /* a buffer */
guchar match[10] = TEST_DATA;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
"port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
client = thrift_server_transport_accept (transport, NULL);
g_assert (client != NULL);
/* read 10 bytes */
bytes = thrift_ssl_socket_read (client, buf, 10, NULL);
g_assert (bytes == 10); /* make sure we've read 10 bytes */
g_assert ( memcmp(buf, match, 10) == 0 ); /* make sure what we got matches */
/* failed read */
recv_error = 1;
thrift_ssl_socket_read (client, buf, 1, NULL);
recv_error = 0;
thrift_ssl_socket_read_end (client, NULL);
thrift_ssl_socket_close (client, NULL);
g_object_unref (tsocket);
g_object_unref (client);
}
int
main(int argc, char *argv[])
{
int retval;
#if (!GLIB_CHECK_VERSION (2, 36, 0))
g_type_init();
#endif
g_test_init (&argc, &argv, NULL);
thrift_ssl_socket_initialize_openssl();
g_test_add_func ("/testtransportsslsocket/CreateAndDestroy", test_ssl_create_and_destroy);
g_test_add_func ("/testtransportsslsocket/CreateAndSetProperties", test_ssl_create_and_set_properties);
g_test_add_func ("/testtransportsslsocket/OpenAndCloseNonSSLServer", test_ssl_open_and_close_non_ssl_server);
g_test_add_func ("/testtransportsslsocket/OpenAndWriteInvalidSocket", test_ssl_write_invalid_socket);
retval = g_test_run ();
thrift_ssl_socket_finalize_openssl();
return retval;
}