THRIFT-5237 Implement MAX_MESSAGE_SIZE and consolidate limits into a TConfiguration class
Client: c_glib
Patch: Zezeng Wang
This closes #2208
diff --git a/lib/c_glib/test/Makefile.am b/lib/c_glib/test/Makefile.am
index c99e0da..3ff48a3 100755
--- a/lib/c_glib/test/Makefile.am
+++ b/lib/c_glib/test/Makefile.am
@@ -58,7 +58,13 @@
testsimpleserver \
testdebugproto \
testoptionalrequired \
- testthrifttest
+ testthrifttest \
+ testthriftbinaryreadcheck \
+ testthriftcompactreadcheck \
+ testthriftbufferedreadcheck \
+ testthriftfdreadcheck \
+ testthriftframedreadcheck \
+ testthriftmemorybufferreadcheck
if WITH_CPP
BUILT_SOURCES += gen-cpp/ThriftTest_types.cpp
@@ -69,6 +75,7 @@
testserialization_LDADD = \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/protocol/libthrift_c_glib_la-thrift_protocol.o \
$(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/libthrift_c_glib_la-thrift_configuration.o \
libtestgenc.la
testapplicationexception_SOURCES = testapplicationexception.c
@@ -76,7 +83,8 @@
$(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_application_exception.o \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/protocol/libthrift_c_glib_la-thrift_protocol.o \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_struct.o \
- $(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_transport.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testcontainertest_SOURCES = testcontainertest.c
testcontainertest_LDADD = \
@@ -92,14 +100,16 @@
$(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 \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/server/libthrift_c_glib_la-thrift_server.o \
- libtestgenc.la
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o \
+ libtestgenc.la
testtransportsocket_SOURCES = testtransportsocket.c
testtransportsocket_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_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
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testtransportsslsocket_SOURCES = testtransportsslsocket.c
@@ -108,7 +118,8 @@
$(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
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testbinaryprotocol_SOURCES = testbinaryprotocol.c
@@ -118,7 +129,8 @@
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_framed_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_server_transport.o \
- $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testcompactprotocol_SOURCES = testcompactprotocol.c
testcompactprotocol_LDADD = \
@@ -127,35 +139,41 @@
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_framed_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_server_transport.o \
- $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testbufferedtransport_SOURCES = testbufferedtransport.c
testbufferedtransport_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_server_transport.o \
- $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testframedtransport_SOURCES = testframedtransport.c
testframedtransport_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_server_transport.o \
- $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testfdtransport_SOURCES = testfdtransport.c
testfdtransport_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_fd_transport.o
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_fd_transport.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testmemorybuffer_SOURCES = testmemorybuffer.c
testmemorybuffer_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_transport.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
teststruct_SOURCES = teststruct.c
teststruct_LDADD = \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/protocol/libthrift_c_glib_la-thrift_protocol.o \
- $(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_transport.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testsimpleserver_SOURCES = testsimpleserver.c
testsimpleserver_LDADD = \
@@ -169,7 +187,8 @@
$(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_server_transport.o \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
- $(top_builddir)/lib/c_glib/src/thrift/c_glib/server/libthrift_c_glib_la-thrift_server.o
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/server/libthrift_c_glib_la-thrift_server.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
testdebugproto_SOURCES = testdebugproto.c
testdebugproto_LDADD = libtestgenc.la
@@ -178,6 +197,7 @@
testoptionalrequired_LDADD = \
$(top_builddir)/lib/c_glib/src/thrift/c_glib/protocol/libthrift_c_glib_la-thrift_protocol.o \
$(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/libthrift_c_glib_la-thrift_configuration.o \
libtestgenc.la
testthrifttest_SOURCES = testthrifttest.c
@@ -185,6 +205,54 @@
$(top_builddir)/test/c_glib/src/thrift_test_handler.o
testthrifttest_CFLAGS = -I$(top_srcdir)/test/c_glib/src -I./gen-c_glib $(GLIB_CFLAGS)
+testthriftbinaryreadcheck_SOURCES = testthriftbinaryreadcheck.c
+testthriftbinaryreadcheck_LDADD = \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/protocol/libthrift_c_glib_la-thrift_protocol.o \
+ $(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_framed_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_server_transport.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
+
+testthriftcompactreadcheck_SOURCES = testthriftcompactreadcheck.c
+testthriftcompactreadcheck_LDADD = \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/protocol/libthrift_c_glib_la-thrift_protocol.o \
+ $(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_framed_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_server_transport.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
+
+testthriftbufferedreadcheck_SOURCES = testthriftbufferedreadcheck.c
+testthriftbufferedreadcheck_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_server_transport.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
+
+testthriftfdreadcheck_SOURCES = testthriftfdreadcheck.c
+testthriftfdreadcheck_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_fd_transport.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
+
+testthriftframedreadcheck_SOURCES = testthriftframedreadcheck.c
+testthriftframedreadcheck_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_server_transport.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \
+ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o
+
+testthriftmemorybufferreadcheck_SOURCES = testthriftmemorybufferreadcheck.c
+testthriftmemorybufferreadcheck_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/libthrift_c_glib_la-thrift_configuration.o
+
+
testthrifttestclient_SOURCES = testthrifttestclient.cpp
testthrifttestclient_CPPFLAGS = -I../../cpp/src $(BOOST_CPPFLAGS) -I./gen-cpp -I../src -I./gen-c_glib $(GLIB_CFLAGS)
testthrifttestclient_LDADD = ../../cpp/.libs/libthrift.la ../libthrift_c_glib.la libtestgenc.la libtestgencpp.la
diff --git a/lib/c_glib/test/testthriftbinaryreadcheck.c b/lib/c_glib/test/testthriftbinaryreadcheck.c
new file mode 100644
index 0000000..36454ca
--- /dev/null
+++ b/lib/c_glib/test/testthriftbinaryreadcheck.c
@@ -0,0 +1,281 @@
+/*
+ * 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.
+ */
+#ifdef __GLIBC__
+#include <features.h>
+#define __NO_STRING_INLINES 1
+#endif
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <netdb.h>
+#include <string.h>
+#include <sys/wait.h>
+
+#include <thrift/c_glib/protocol/thrift_protocol.h>
+#include <thrift/c_glib/transport/thrift_socket.h>
+#include <thrift/c_glib/transport/thrift_server_socket.h>
+#include <thrift/c_glib/transport/thrift_framed_transport.h>
+
+#define TEST_BOOL TRUE
+#define TEST_BYTE 123
+#define TEST_I16 12345
+#define TEST_I32 1234567890
+#define TEST_I64 G_GINT64_CONSTANT (123456789012345)
+#define TEST_DOUBLE 1234567890.123
+#define TEST_STRING "this is a test string 1234567890!@#$%^&*()"
+#define TEST_PORT 51199
+
+#define MAX_MESSAGE_SIZE 4
+
+static int transport_read_count = 0;
+static int transport_read_error = 0;
+static int transport_read_error_at = -1;
+gint32
+my_thrift_transport_read_all (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ if (transport_read_count != transport_read_error_at
+ && transport_read_error == 0)
+ {
+ transport_read_count++;
+ return thrift_transport_read_all (transport, buf, len, error);
+ }
+ return -1;
+}
+
+static int transport_write_count = 0;
+static int transport_write_error = 0;
+static int transport_write_error_at = -1;
+gboolean
+my_thrift_transport_write (ThriftTransport *transport, const gpointer buf,
+ const guint32 len, GError **error)
+{
+ if (transport_write_count != transport_write_error_at
+ && transport_write_error == 0)
+ {
+ transport_write_count++;
+ return thrift_transport_write (transport, buf, len, error);
+ }
+ return FALSE;
+}
+
+#define thrift_transport_read_all my_thrift_transport_read_all
+#define thrift_transport_write my_thrift_transport_write
+#include "../src/thrift/c_glib/protocol/thrift_binary_protocol.c"
+#undef thrift_transport_read_all
+#undef thrift_transport_write
+
+static void thrift_server_complex_types (const int port);
+
+static void
+test_create_and_destroy (void)
+{
+ GObject *object = NULL;
+
+ /* create an object and then destroy it */
+ object = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, NULL);
+ g_assert (object !=NULL);
+ g_object_unref (object);
+}
+
+static void
+test_initialize (void)
+{
+ ThriftConfiguration *tconfiguration = NULL;
+ ThriftSocket *tsocket = NULL;
+ ThriftBinaryProtocol *bprotocol = NULL;
+ ThriftSocket *temp = NULL;
+ ThriftConfiguration *tempconf = NULL;
+
+ glong tempsize = 0;
+
+ /* create a ThriftConfiguration */
+ tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+ g_assert (tconfiguration != NULL);
+ /* create a ThriftTransport */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", 51188, "path", NULL,
+ "configuration", tconfiguration,
+ "remainingmessagesize", tconfiguration->maxMessageSize_, NULL);
+ g_assert (tsocket != NULL);
+ /* fetch the properties */
+ g_object_get (G_OBJECT (tconfiguration), "max_message_size", &tempsize, NULL);
+ g_assert (tempsize == MAX_MESSAGE_SIZE);
+ /* fetch the properties */
+ g_object_get (G_OBJECT (tsocket), "remainingmessagesize", &tempsize, NULL);
+ g_assert (tempsize == MAX_MESSAGE_SIZE);
+ /* fetch the properties */
+ g_object_get (G_OBJECT (tsocket), "configuration", &tempconf, NULL);
+ g_object_unref (tempconf);
+ /* create a ThriftBinaryProtocol using Transport */
+ bprotocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport", tsocket, NULL);
+ g_assert (bprotocol != NULL);
+ /* fetch the properties */
+ g_object_get (G_OBJECT (bprotocol), "transport", &temp, NULL);
+ g_object_unref (temp);
+
+ /* clean up memory */
+ g_object_unref (bprotocol);
+ g_object_unref (tsocket);
+ g_object_unref (tconfiguration);
+}
+
+void
+test_read_and_wirte_complex_types (void)
+{
+ int status;
+ pid_t pid;
+ ThriftConfiguration *tconfiguration = NULL;
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ ThriftBinaryProtocol *tb = NULL;
+ ThriftProtocol *protocol = NULL;
+ int port = TEST_PORT;
+
+ /* fork a server from the client */
+ pid = fork ();
+ g_assert (pid >= 0);
+
+ if (pid == 0)
+ {
+ /* child listens */
+ thrift_server_complex_types (port);
+ exit (0);
+ } else {
+ /* parent. wait a bit for the socket to be created. */
+ sleep (1);
+
+ /* create a ThriftConfiguration */
+ tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+ g_assert (tconfiguration != NULL);
+
+ /* create a ThriftSocket */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", port, "path", NULL,
+ "configuration", tconfiguration, NULL);
+ transport = THRIFT_TRANSPORT (tsocket);
+ THRIFT_TRANSPORT_GET_CLASS (tsocket)->resetConsumedMessageSize(tsocket, -1, NULL);
+ thrift_transport_open (transport, NULL);
+ 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);
+ g_assert (protocol != NULL);
+
+ /* test 1st write failure on a map */
+ g_assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_BYTE,
+ 1, NULL) > 0);
+ g_assert (thrift_binary_protocol_write_map_end (protocol, NULL) == 0);
+
+ g_assert (thrift_binary_protocol_write_map_begin (protocol, T_I32, T_BYTE,
+ 1, NULL) > 0);
+ g_assert (thrift_binary_protocol_write_map_end (protocol, NULL) == 0);
+
+ /* test list operations */
+ g_assert (thrift_binary_protocol_write_list_begin (protocol, T_BYTE,
+ 1, NULL) > 0);
+ g_assert (thrift_binary_protocol_write_list_end (protocol, NULL) == 0);
+
+ g_assert (thrift_binary_protocol_write_list_begin (protocol, T_I32,
+ 10, NULL) > 0);
+ g_assert (thrift_binary_protocol_write_list_end (protocol, NULL) == 0);
+
+ /* clean up */
+ thrift_transport_close (transport, NULL);
+ g_object_unref (tsocket);
+ g_object_unref (protocol);
+ g_object_unref (tconfiguration);
+ g_assert (wait (&status) == pid);
+ g_assert (status == 0);
+ }
+}
+
+static void
+thrift_server_complex_types (const int port)
+{
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ ThriftBinaryProtocol *tbp = NULL;
+ ThriftProtocol *protocol = NULL;
+ ThriftType element_type = T_VOID,
+ key_type = T_VOID,
+ value_type = T_VOID;
+ gint32 ret = 0;
+ guint32 size = 0;
+ glong tempsize = 0;
+
+ ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION,
+ "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", port,
+ "configuration", tconfiguration, NULL);
+ transport = THRIFT_SERVER_TRANSPORT (tsocket);
+ THRIFT_SERVER_TRANSPORT_GET_CLASS (tsocket)->resetConsumedMessageSize(transport, -1, NULL);
+ thrift_server_transport_listen (transport, NULL);
+ client = thrift_server_transport_accept (transport, NULL);
+ g_assert (client != NULL);
+
+ tbp = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
+ client, NULL);
+ protocol = THRIFT_PROTOCOL(tbp);
+
+ g_assert (thrift_binary_protocol_read_map_begin (protocol, &key_type, &value_type,
+ &size, NULL) > 0);
+ g_assert (thrift_binary_protocol_read_map_end (protocol, NULL) == 0);
+
+ g_assert (thrift_binary_protocol_read_map_begin (protocol, &key_type, &value_type,
+ &size, NULL) == -1);
+ g_assert (thrift_binary_protocol_read_map_end (protocol, NULL) == 0);
+
+ /* test read failure */
+ g_assert (thrift_binary_protocol_read_list_begin (protocol, &element_type,
+ &size, NULL) > 0);
+ g_assert (thrift_binary_protocol_read_list_end(protocol, NULL) == 0);
+
+ g_assert (thrift_binary_protocol_read_list_begin (protocol, &element_type,
+ &size, NULL) == -1);
+ g_assert (thrift_binary_protocol_read_list_end(protocol, NULL) == 0);
+
+ g_object_unref (client);
+ /* TODO: investigate g_object_unref (tbp); */
+ g_object_unref (tsocket);
+ g_object_unref (tconfiguration);
+}
+
+int
+main (int argc, char *argv[])
+{
+#if (!GLIB_CHECK_VERSION (2, 36, 0))
+ g_type_init ();
+#endif
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/testthriftbinaryreadcheck/CreateAndDestroy", test_create_and_destroy);
+ g_test_add_func ("/testthriftbinaryreadcheck/Initialize", test_initialize);
+ g_test_add_func ("/testthriftbinaryreadcheck/test_read_and_write_complex_types", test_read_and_wirte_complex_types);
+
+ return g_test_run ();
+}
diff --git a/lib/c_glib/test/testthriftbufferedreadcheck.c b/lib/c_glib/test/testthriftbufferedreadcheck.c
new file mode 100755
index 0000000..1472072
--- /dev/null
+++ b/lib/c_glib/test/testthriftbufferedreadcheck.c
@@ -0,0 +1,224 @@
+/*
+ * 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.
+ */
+
+#include <netdb.h>
+#include <signal.h>
+#include <sys/wait.h>
+
+#include <thrift/c_glib/transport/thrift_transport.h>
+#include <thrift/c_glib/transport/thrift_socket.h>
+#include <thrift/c_glib/transport/thrift_server_transport.h>
+#include <thrift/c_glib/transport/thrift_server_socket.h>
+
+#define TEST_DATA { 'a', 'b', 'c' }
+
+#define MAX_MESSAGE_SIZE 3
+
+#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);
+
+static void
+test_open_and_close(void)
+{
+ ThriftConfiguration *tconfiguration = NULL;
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ GError *err = NULL;
+ pid_t pid;
+ int port = 51199;
+ int status;
+
+ pid = fork ();
+ g_assert ( pid >= 0 );
+
+ 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);
+
+ /* create a BufferedTransport wrapper of the Socket */
+ transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket),
+ 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);
+
+ /* try and underlying socket failure */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
+ 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
+test_read_and_write(void)
+{
+ int status;
+ pid_t pid;
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ int port = 51199;
+ guchar buf[3] = TEST_DATA; /* a buffer */
+
+ pid = fork ();
+ 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);
+
+ 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);
+
+ g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
+ g_assert (thrift_buffered_transport_is_open (transport));
+
+ /* write 3 bytes */
+ thrift_buffered_transport_write (transport, buf, 3, NULL);
+
+ /* write 4 bytes */
+ thrift_buffered_transport_write (transport, buf, 4, 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_assert ( wait (&status) == pid );
+ g_assert ( status == 0 );
+ }
+}
+
+
+static void
+thrift_socket_server_open (const int port, int times)
+{
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ int i;
+
+ ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", port,
+ "configuration", tconfiguration, NULL);
+
+ transport = THRIFT_SERVER_TRANSPORT (tsocket);
+ THRIFT_SERVER_TRANSPORT_GET_CLASS (tsocket)->resetConsumedMessageSize(transport, -1, NULL);
+ 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);
+ g_object_unref (tconfiguration);
+ g_assert(tconfiguration != NULL);
+}
+
+static void
+thrift_server (const int port)
+{
+ int bytes = 0;
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ guchar buf[3]; /* a buffer */
+ guchar match[3] = TEST_DATA;
+
+ ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
+ "port", port, "configuration", tconfiguration, 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);
+ g_assert (client != NULL);
+
+ /* read 3 bytes */
+ bytes = thrift_buffered_transport_read (client, buf, 3, NULL);
+ g_assert (bytes == 3); /* make sure we've read 10 bytes */
+ g_assert ( memcmp (buf, match, 3) == 0 ); /* make sure what we got matches */
+
+ bytes = thrift_buffered_transport_read (client, buf, 4, NULL);
+ g_assert (bytes == -1);
+
+ thrift_buffered_transport_read_end (client, NULL);
+ thrift_buffered_transport_close (client, NULL);
+ g_object_unref (client);
+ g_object_unref (tsocket);
+ g_object_unref (tconfiguration);
+}
+
+int
+main(int argc, char *argv[])
+{
+#if (!GLIB_CHECK_VERSION (2, 36, 0))
+ g_type_init();
+#endif
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/testthriftbufferedreadcheck/OpenAndClose", test_open_and_close);
+ g_test_add_func ("/testthriftbufferedreadcheck/ReadAndWrite", test_read_and_write);
+
+ return g_test_run ();
+}
+
diff --git a/lib/c_glib/test/testthriftcompactreadcheck.c b/lib/c_glib/test/testthriftcompactreadcheck.c
new file mode 100644
index 0000000..03466ae
--- /dev/null
+++ b/lib/c_glib/test/testthriftcompactreadcheck.c
@@ -0,0 +1,275 @@
+/*
+ * 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.
+ */
+
+/* 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 g_assert () */
+#if !defined(__APPLE__) && !defined(__FreeBSD__) && \
+ !defined(__OpenBSD__) && !defined(__NetBSD__)
+#include <features.h>
+#endif
+
+#ifdef __GLIBC__
+#define __NO_STRING_INLINES 1
+#endif
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <netdb.h>
+#include <string.h>
+#include <sys/wait.h>
+
+#include <thrift/c_glib/protocol/thrift_protocol.h>
+#include <thrift/c_glib/transport/thrift_socket.h>
+#include <thrift/c_glib/transport/thrift_server_socket.h>
+#include <thrift/c_glib/transport/thrift_framed_transport.h>
+
+#define TEST_BOOL TRUE
+#define TEST_BYTE 123
+#define TEST_I16 12345
+#define TEST_I32 1234567890
+#define TEST_I64 123456789012345
+#define TEST_NI16 (-12345)
+#define TEST_NI32 (-1234567890)
+#define TEST_NI64 (-123456789012345)
+#define TEST_DOUBLE 1234567890.123
+#define TEST_STRING "this is a test string 1234567890!@#$%^&*()"
+#define TEST_PORT 51199
+
+#define MAX_MESSAGE_SIZE 2
+
+static int transport_read_count = 0;
+static int transport_read_error = 0;
+static int transport_read_error_at = -1;
+gint32
+my_thrift_transport_read_all (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ if (transport_read_count != transport_read_error_at
+ && transport_read_error == 0)
+ {
+ transport_read_count++;
+ return thrift_transport_read_all (transport, buf, len, error);
+ }
+ return -1;
+}
+
+static int transport_write_count = 0;
+static int transport_write_error = 0;
+static int transport_write_error_at = -1;
+gboolean
+my_thrift_transport_write (ThriftTransport *transport, const gpointer buf,
+ const guint32 len, GError **error)
+{
+ if (transport_write_count != transport_write_error_at
+ && transport_write_error == 0)
+ {
+ transport_write_count++;
+ return thrift_transport_write (transport, buf, len, error);
+ }
+ return FALSE;
+}
+
+#define thrift_transport_read_all my_thrift_transport_read_all
+#define thrift_transport_write my_thrift_transport_write
+#include "../src/thrift/c_glib/protocol/thrift_compact_protocol.c"
+#undef thrift_transport_read_all
+#undef thrift_transport_write
+
+static void thrift_server_complex_types (const int port);
+
+static void
+test_create_and_destroy (void)
+{
+ GObject *object = NULL;
+
+ /* create an object and then destroy it */
+ object = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, NULL);
+ g_assert (object != NULL);
+ g_object_unref (object);
+}
+
+static void
+test_initialize (void)
+{
+ ThriftSocket *tsocket = NULL;
+ ThriftCompactProtocol *protocol = NULL;
+ ThriftSocket *temp = NULL;
+ ThriftConfiguration *tconfiguration = NULL;
+ ThriftConfiguration *tempconf = NULL;
+ glong tempsize = 0;
+
+ /* create a ThriftConfiguration */
+ tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+ /* create a ThriftTransport */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", 51188, "configuration", tconfiguration,
+ "remainingmessagesize", MAX_MESSAGE_SIZE, NULL);
+ g_assert (tsocket != NULL);
+ /* fetch the properties */
+ g_object_get (G_OBJECT (tconfiguration), "max_message_size", &tempsize, NULL);
+ g_assert (tempsize == MAX_MESSAGE_SIZE);
+ /* fetch the properties */
+ g_object_get (G_OBJECT (tsocket), "remainingmessagesize", &tempsize, NULL);
+ g_assert (tempsize == MAX_MESSAGE_SIZE);
+ /* fetch the properties */
+ g_object_get (G_OBJECT (tsocket), "configuration", &tempconf, NULL);
+ g_object_unref (tempconf);
+ /* create a ThriftCompactProtocol using the Transport */
+ protocol = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
+ tsocket, NULL);
+ g_assert (protocol != NULL);
+ /* fetch the properties */
+ g_object_get (G_OBJECT (protocol), "transport", &temp, NULL);
+ g_object_unref (temp);
+
+ /* clean up memory */
+ g_object_unref (protocol);
+ g_object_unref (tsocket);
+}
+
+
+static void
+test_read_and_write_complex_types (void)
+{
+ int status;
+ pid_t pid;
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ ThriftCompactProtocol *tc = NULL;
+ ThriftProtocol *protocol = NULL;
+ int port = TEST_PORT;
+
+ /* fork a server from the client */
+ pid = fork ();
+ g_assert (pid >= 0);
+
+ if (pid == 0)
+ {
+ /* child listens */
+ thrift_server_complex_types (port);
+ exit (0);
+ } else {
+ /* parent. 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);
+ transport = THRIFT_TRANSPORT (tsocket);
+ thrift_transport_open (transport, NULL);
+ g_assert (thrift_transport_is_open (transport));
+
+ /* create a ThriftCompactTransport */
+ tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
+ tsocket, NULL);
+ protocol = THRIFT_PROTOCOL (tc);
+ g_assert (protocol != NULL);
+
+ g_assert (thrift_compact_protocol_write_map_begin (protocol, T_VOID, T_BYTE,
+ 1, NULL) > 0);
+ g_assert (thrift_compact_protocol_write_map_end (protocol, NULL) == 0);
+
+ g_assert (thrift_compact_protocol_write_map_begin (protocol, T_VOID, T_BYTE,
+ 3, NULL) > 0);
+ g_assert (thrift_compact_protocol_write_map_end (protocol, NULL) == 0);
+
+ g_assert (thrift_compact_protocol_write_list_begin (protocol, T_BYTE,
+ 1, NULL) > 0);
+ g_assert (thrift_compact_protocol_write_list_end (protocol, NULL) == 0);
+
+ g_assert (thrift_compact_protocol_write_list_begin (protocol, T_I32,
+ 3, NULL) > 0);
+ g_assert (thrift_compact_protocol_write_list_end (protocol, NULL) == 0);
+
+ /* clean up */
+ thrift_transport_close (transport, NULL);
+ g_object_unref (tsocket);
+ g_object_unref (protocol);
+ g_assert (wait (&status) == pid);
+ g_assert (status == 0);
+ }
+}
+
+
+static void
+thrift_server_complex_types (const int port)
+{
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ ThriftCompactProtocol *tc = NULL;
+ ThriftProtocol *protocol = NULL;
+ ThriftType element_type, key_type, value_type, field_type;
+ guint32 size = 0;
+
+ ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
+ "port", port, "configuration", tconfiguration, NULL);
+ transport = THRIFT_SERVER_TRANSPORT (tsocket);
+ THRIFT_SERVER_TRANSPORT_GET_CLASS (tsocket)->resetConsumedMessageSize(transport, -1, NULL);
+ thrift_server_transport_listen (transport, NULL);
+ client = thrift_server_transport_accept (transport, NULL);
+ g_assert (client != NULL);
+
+ tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
+ client, NULL);
+ protocol = THRIFT_PROTOCOL (tc);
+
+ g_assert (thrift_compact_protocol_read_map_begin (protocol, &key_type, &value_type,
+ &size, NULL) > 0);
+ g_assert (thrift_compact_protocol_read_map_end (protocol, NULL) == 0);
+
+ g_assert (thrift_compact_protocol_read_map_begin (protocol, &key_type, &value_type,
+ &size, NULL) == -1);
+ g_assert (thrift_compact_protocol_read_map_end (protocol, NULL) == 0);
+
+ g_assert (thrift_compact_protocol_read_list_begin (protocol, &element_type,
+ &size, NULL) > 0);
+ g_assert (thrift_compact_protocol_read_list_end (protocol, NULL) == 0);
+
+ g_assert (thrift_compact_protocol_read_list_begin (protocol, &element_type,
+ &size, NULL) == -1);
+ g_assert (thrift_compact_protocol_read_list_end (protocol, NULL) == 0);
+
+ g_object_unref (client);
+ g_object_unref (tsocket);
+ g_object_unref (tconfiguration);
+}
+
+
+int
+main (int argc, char *argv[])
+{
+#if (!GLIB_CHECK_VERSION (2, 36, 0))
+ g_type_init ();
+#endif
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/testthriftcompactreadcheck/CreateAndDestroy",
+ test_create_and_destroy);
+ g_test_add_func ("/testthriftcompactreadcheck/Initialize", test_initialize);
+ g_test_add_func ("/testthriftcompactreadcheck/ReadAndWriteComplexTypes",
+ test_read_and_write_complex_types);
+
+ return g_test_run ();
+}
diff --git a/lib/c_glib/test/testthriftfdreadcheck.c b/lib/c_glib/test/testthriftfdreadcheck.c
new file mode 100755
index 0000000..986b70d
--- /dev/null
+++ b/lib/c_glib/test/testthriftfdreadcheck.c
@@ -0,0 +1,182 @@
+/*
+ * 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.
+ */
+
+
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include <thrift/c_glib/transport/thrift_transport.h>
+#include <thrift/c_glib/transport/thrift_fd_transport.h>
+
+#define MAX_MESSAGE_SIZE 2
+
+static const gchar TEST_DATA[12] = "abcde01234!";
+
+static void
+test_open_and_close (void)
+{
+ ThriftConfiguration *configuration;
+ ThriftTransport *transport;
+ ThriftTransportClass *klass;
+ GError *error;
+ gint fd;
+ gchar *filename;
+
+ error = NULL;
+ filename = NULL;
+
+ fd = g_file_open_tmp (NULL, &filename, &error);
+ g_assert (fd >= 0);
+
+ configuration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+
+ transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT,
+ "configuration", configuration, "fd", fd,
+ NULL));
+ klass = THRIFT_TRANSPORT_GET_CLASS (transport);
+
+ /* open is no-op */
+ 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));
+
+ 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 */
+ g_assert (close (fd) != 0);
+ g_assert (errno == EBADF);
+
+ g_object_unref (transport);
+ g_object_unref (configuration);
+
+ g_remove (filename);
+ g_free (filename);
+
+ /* test bad fd */
+ transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT,
+ "fd", -1,
+ NULL));
+ klass = THRIFT_TRANSPORT_GET_CLASS (transport);
+
+ g_assert (! klass->is_open (transport));
+ error = NULL;
+ g_assert (! klass->peek (transport, &error));
+ error = NULL;
+ g_assert (! klass->open (transport, &error));
+ error = NULL;
+ g_assert (! klass->close (transport, &error));
+
+ g_object_unref (transport);
+}
+
+static void
+test_read_and_write (void)
+{
+ gchar out_buf[8];
+ gchar *b;
+ gint want, got;
+ ThriftConfiguration *configuration;
+ ThriftTransport *transport;
+ ThriftTransportClass *klass;
+ GError *error;
+ gint fd;
+ gchar *filename;
+
+ error = NULL;
+ filename = NULL;
+
+ fd = g_file_open_tmp (NULL, &filename, &error);
+ g_assert (fd >= 0);
+
+ configuration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+ /* write */
+ transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT,
+ "configuration", configuration, "fd", fd,
+ NULL));
+ klass = THRIFT_TRANSPORT_GET_CLASS (transport);
+ 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);
+ g_assert (fd >= 0);
+
+ transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT,
+ "configuration", configuration,
+ "remainingmessagesize", MAX_MESSAGE_SIZE,
+ "fd", fd,
+ NULL));
+ klass = THRIFT_TRANSPORT_GET_CLASS (transport);
+
+ memset(out_buf, 0, 8);
+ b = out_buf;
+ want = 2;
+ while (want > 0) {
+ got = klass->read (transport, (gpointer) b, want, &error);
+ g_assert (got > 0 && got <= want);
+ b += got;
+ want -= got;
+ }
+ g_assert (memcmp (out_buf, TEST_DATA, 2) == 0);
+
+ memset(out_buf, 0, 8);
+ b = out_buf;
+ want = 4;
+ got = klass->read (transport, (gpointer) b, want, &error);
+ g_assert (got < 0);
+
+ g_assert (klass->close (transport, &error));
+ g_object_unref (transport);
+ g_object_unref (configuration);
+
+ /* clean up */
+
+ g_remove (filename);
+ g_free (filename);
+}
+
+int
+main (int argc, char *argv[])
+{
+#if (!GLIB_CHECK_VERSION (2, 36, 0))
+ g_type_init ();
+#endif
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/testfdtransport/OpenAndClose", test_open_and_close);
+ g_test_add_func ("/testfdtransport/ReadAndWrite", test_read_and_write);
+
+ return g_test_run ();
+}
diff --git a/lib/c_glib/test/testthriftframedreadcheck.c b/lib/c_glib/test/testthriftframedreadcheck.c
new file mode 100755
index 0000000..95e853f
--- /dev/null
+++ b/lib/c_glib/test/testthriftframedreadcheck.c
@@ -0,0 +1,222 @@
+/*
+ * 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.
+ */
+
+#include <netdb.h>
+#include <sys/wait.h>
+
+#include <thrift/c_glib/transport/thrift_transport.h>
+#include <thrift/c_glib/transport/thrift_socket.h>
+#include <thrift/c_glib/transport/thrift_server_transport.h>
+#include <thrift/c_glib/transport/thrift_server_socket.h>
+
+#define TEST_DATA { 'a', 'b' }
+#define MAX_MESSAGE_SIZE 2
+
+#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);
+
+static void
+test_open_and_close(void)
+{
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ GError *err = NULL;
+ pid_t pid;
+ int port = 51199;
+ int status;
+
+ pid = fork ();
+ g_assert ( pid >= 0 );
+
+ 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);
+
+ /* create a BufferedTransport wrapper of the Socket */
+ transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket), 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);
+
+ /* try and underlying socket failure */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
+ 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
+test_read_and_write(void)
+{
+ int status;
+ pid_t pid;
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ int port = 51199;
+ guchar buf[10] = TEST_DATA; /* a buffer */
+
+ pid = fork ();
+ 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);
+
+ 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);
+
+ g_assert (thrift_framed_transport_open (transport, NULL) == TRUE);
+ g_assert (thrift_framed_transport_is_open (transport));
+
+ /* write 2 bytes */
+ thrift_framed_transport_write (transport, buf, 2, NULL);
+ thrift_framed_transport_flush (transport, NULL);
+
+ thrift_framed_transport_write (transport, buf, 3, 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);
+
+ g_object_unref (transport);
+ g_object_unref (tsocket);
+
+ g_assert ( wait (&status) == pid );
+ g_assert ( status == 0 );
+ }
+}
+
+static void
+thrift_socket_server_open (const int port, int times)
+{
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ int i;
+
+ ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION,
+ "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE, NULL);
+
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
+ "port", port, "configuration", tconfiguration, 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);
+ g_object_unref (tconfiguration);
+}
+
+static void
+thrift_server (const int port)
+{
+ int bytes = 0;
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ guchar buf[12]; /* a buffer */
+ guchar match[10] = TEST_DATA;
+
+ ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION,
+ "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE,
+ NULL);
+
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
+ "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, "configuration", tconfiguration,
+ "remainingmessagesize", MAX_MESSAGE_SIZE, NULL);
+ g_assert (client != NULL);
+
+ /* read 2 bytes */
+ bytes = thrift_framed_transport_read (client, buf, 2, NULL);
+ g_assert (bytes == 2); /* make sure we've read 2 bytes */
+ g_assert ( memcmp (buf, match, 1) == 0 ); /* make sure what we got matches */
+
+ bytes = thrift_framed_transport_read (client, buf, 3, NULL);
+ g_assert (bytes == -1);
+
+ thrift_framed_transport_read_end (client, NULL);
+ thrift_framed_transport_close (client, NULL);
+ g_object_unref (client);
+ g_object_unref (tsocket);
+ g_object_unref (tconfiguration);
+}
+
+int
+main(int argc, char *argv[])
+{
+#if (!GLIB_CHECK_VERSION (2, 36, 0))
+ g_type_init();
+#endif
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/testframedtransport/OpenAndClose", test_open_and_close);
+ g_test_add_func ("/testframedtransport/ReadAndWrite", test_read_and_write);
+
+ return g_test_run ();
+}
diff --git a/lib/c_glib/test/testthriftmemorybufferreadcheck.c b/lib/c_glib/test/testthriftmemorybufferreadcheck.c
new file mode 100755
index 0000000..658b86a
--- /dev/null
+++ b/lib/c_glib/test/testthriftmemorybufferreadcheck.c
@@ -0,0 +1,96 @@
+/*
+ * 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.
+ */
+
+#include <netdb.h>
+
+#include <thrift/c_glib/transport/thrift_transport.h>
+#include <thrift/c_glib/transport/thrift_socket.h>
+#include <thrift/c_glib/transport/thrift_server_transport.h>
+#include <thrift/c_glib/transport/thrift_server_socket.h>
+
+static const gchar TEST_DATA[11] = "abcdefghij";
+
+#include "../src/thrift/c_glib/transport/thrift_memory_buffer.c"
+
+#define MAX_MESSAGE_SIZE 2
+
+static void
+test_open_and_close (void)
+{
+ ThriftMemoryBuffer *tbuffer = NULL;
+ ThriftConfiguration *tconfiguration = NULL;
+
+ /* create a ThriftConfiguration */
+ tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION,
+ "max_message_size", MAX_MESSAGE_SIZE,
+ "max_frame_size", MAX_MESSAGE_SIZE,
+ NULL);
+ /* create a ThriftMemoryBuffer */
+ tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "configuration", tconfiguration, NULL);
+
+ /* no-ops */
+ 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);
+ g_object_unref (tconfiguration);
+}
+
+static void
+test_read_and_write (void)
+{
+ ThriftConfiguration *tconfiguration = NULL;
+ ThriftMemoryBuffer *tbuffer = NULL;
+ gint got, want;
+ gchar read[10];
+ gchar *b;
+ GError *error = NULL;
+
+ tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE, NULL);
+ tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, "configuration", tconfiguration, NULL);
+ THRIFT_TRANSPORT_GET_CLASS (tbuffer)->resetConsumedMessageSize(THRIFT_TRANSPORT(tbuffer), -1, NULL);
+ g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
+ (gpointer) TEST_DATA, 10, &error) == TRUE);
+ g_assert (error == NULL);
+
+ memset(read, 0, 10);
+ b = read;
+ want = 10;
+ got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
+ (gpointer) b, want, &error);
+ g_assert (got < 0);
+ g_object_unref (tbuffer);
+ g_object_unref (tconfiguration);
+}
+
+int
+main(int argc, char *argv[])
+{
+#if (!GLIB_CHECK_VERSION (2, 36, 0))
+ g_type_init ();
+#endif
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/testthriftmemorybufferreadcheck/OpenAndClose", test_open_and_close);
+ g_test_add_func ("/testthriftmemorybufferreadcheck/ReadAndWrite", test_read_and_write);
+
+ return g_test_run ();
+}