THRIFT-4329: multiplexed processor, client and server for c_glib
Client: c_glib

This closes #1361
diff --git a/lib/c_glib/test/CMakeLists.txt b/lib/c_glib/test/CMakeLists.txt
index 2c87dbc..fb3e41c 100644
--- a/lib/c_glib/test/CMakeLists.txt
+++ b/lib/c_glib/test/CMakeLists.txt
@@ -111,6 +111,8 @@
 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)
diff --git a/lib/c_glib/test/testbufferedtransport.c b/lib/c_glib/test/testbufferedtransport.c
index 1c15ef2..c6e6b58 100755
--- a/lib/c_glib/test/testbufferedtransport.c
+++ b/lib/c_glib/test/testbufferedtransport.c
@@ -31,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
@@ -44,8 +45,8 @@
   object = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, 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 +56,52 @@
   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 */
-  g_assert (thrift_buffered_transport_open (transport, NULL) == FALSE);
-  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);
+	/* 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);
 
-  g_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
@@ -100,57 +118,81 @@
   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);
 
-    g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
-    g_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);
 
-    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
@@ -163,15 +205,15 @@
   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);
+			 thrift_server_transport_accept (transport, NULL),
+			 "r_buf_size", 5, NULL);
   g_assert (client != NULL);
 
   /* read 10 bytes */
@@ -209,62 +251,62 @@
   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);
-    g_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);
 
 
-    g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
-    g_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);
-    g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
-    g_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 */
-    g_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 */
-    g_assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
-    g_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);
 
-    g_assert ( wait (&status) == pid );
-    g_assert ( status == 0 );
-  }
+	g_assert ( wait (&status) == pid );
+	g_assert ( status == 0 );
+    }
 }
 
 int
diff --git a/lib/c_glib/test/testframedtransport.c b/lib/c_glib/test/testframedtransport.c
index 0328737..45397ce 100755
--- a/lib/c_glib/test/testframedtransport.c
+++ b/lib/c_glib/test/testframedtransport.c
@@ -30,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
@@ -43,8 +44,8 @@
   object = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, 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);
 }
 
@@ -54,35 +55,53 @@
   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 */
-  g_assert (thrift_framed_transport_open (transport, NULL) == FALSE);
-  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);
+	/* 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);
 
-  g_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
@@ -99,46 +118,46 @@
   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);
 
-    g_assert (thrift_framed_transport_open (transport, NULL) == TRUE);
-    g_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);
 
-    g_assert ( wait (&status) == pid );
-    g_assert ( status == 0 );
-  }
+	g_assert ( wait (&status) == pid );
+	g_assert ( status == 0 );
+    }
 }
 
 /* test reading from the transport after the peer has unexpectedly
@@ -155,76 +174,100 @@
   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
@@ -237,15 +280,15 @@
   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);
+			 thrift_server_transport_accept (transport, NULL),
+			 "r_buf_size", 5, NULL);
   g_assert (client != NULL);
 
   /* read 10 bytes */
diff --git a/lib/c_glib/test/testtransportsocket.c b/lib/c_glib/test/testtransportsocket.c
index 8e96375..fedbad6 100755
--- a/lib/c_glib/test/testtransportsocket.c
+++ b/lib/c_glib/test/testtransportsocket.c
@@ -33,9 +33,9 @@
 my_socket(int domain, int type, int protocol)
 {
   if (socket_error == 0)
-  {
-    return socket (domain, type, protocol);
-  }
+    {
+      return socket (domain, type, protocol);
+    }
   return -1;
 }
 
@@ -44,9 +44,9 @@
 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;
 }
 
@@ -55,9 +55,9 @@
 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;
 }
 
@@ -70,7 +70,7 @@
 #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)
@@ -93,82 +93,100 @@
   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);
-  g_assert (thrift_socket_is_open (transport) == TRUE);
-  thrift_socket_close (transport, NULL);
-  g_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);
-  g_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;
-  g_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 ();
   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);
-    g_assert (thrift_socket_open (transport, NULL) == TRUE);
-    g_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);
 
-    g_assert ( wait (&status) == pid );
-    g_assert ( status == 0 );
-  }
+	g_assert ( wait (&status) == pid );
+	g_assert ( status == 0 );
+    }
 }
 
 /* test ThriftSocket's peek() implementation */
@@ -183,9 +201,9 @@
   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);
@@ -196,81 +214,105 @@
   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)
 {
   int bytes = 0;
@@ -280,7 +322,7 @@
   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);
diff --git a/lib/c_glib/test/testtransportsslsocket.c b/lib/c_glib/test/testtransportsslsocket.c
index f2f56f8..3c2644d 100644
--- a/lib/c_glib/test/testtransportsslsocket.c
+++ b/lib/c_glib/test/testtransportsslsocket.c
@@ -16,13 +16,14 @@
  * 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 <netdb.h>
+
 #include <sys/wait.h>
+#include <arpa/inet.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netdb.h>
-#include <arpa/inet.h>
 
 #include <thrift/c_glib/transport/thrift_transport.h>
 #include <thrift/c_glib/transport/thrift_buffered_transport.h>
@@ -30,7 +31,7 @@
 #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 { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' } */
 #define TEST_DATA { "GET / HTTP/1.1\n\n" }
 
 
@@ -40,9 +41,9 @@
 my_socket(int domain, int type, int protocol)
 {
   if (socket_error == 0)
-  {
-    return socket (domain, type, protocol);
-  }
+    {
+      return socket (domain, type, protocol);
+    }
   return -1;
 }
 
@@ -51,9 +52,9 @@
 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;
 }
 
@@ -62,9 +63,9 @@
 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;
 }
 
@@ -76,7 +77,7 @@
 #undef recv
 #undef send
 
-static void thrift_ssl_socket_server (const int port);
+static void thrift_socket_server (const int port);
 
 /* test object creation and destruction */
 static void
@@ -111,18 +112,90 @@
 }
 
 static void
-test_ssl_open_and_close(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, &error);
+  tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", 51188+1, &error);
 
   transport = THRIFT_TRANSPORT (tSSLSocket);
-  thrift_ssl_socket_open (transport, NULL);
-  g_assert (thrift_ssl_socket_is_open (transport) == TRUE);
+  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);
 
@@ -130,23 +203,6 @@
   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", 51188, &error);
-  transport = THRIFT_TRANSPORT (tSSLSocket);
-  g_assert (thrift_ssl_socket_open (transport, &error) == FALSE);
-  g_object_unref (tSSLSocket);
-  g_error_free (error);
-  error = NULL;
-
-  /* try an error call to socket() */
-  tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", 51188, &error);
-  transport = THRIFT_TRANSPORT (tSSLSocket);
-  socket_error = 1;
-  g_assert (thrift_ssl_socket_open (transport, &error) == FALSE);
-  socket_error = 0;
-  g_object_unref (tSSLSocket);
-  g_error_free (error);
 }
 
 
@@ -160,22 +216,22 @@
   unsigned char *utf8 = NULL;
 
   do
-  {
-    if(!name) break; /* failed */
+    {
+      if(!name) break; /* failed */
 
-    idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
-    if(!(idx > -1))  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 */
+      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 */
+      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 */
+      int length = ASN1_STRING_to_UTF8(&utf8, data);
+      if(!utf8 || !(length > 0))  break; /* failed */
 
-  } while (0);
+    } while (0);
   return utf8;
 }
 
@@ -197,34 +253,34 @@
   int retval = 0;
 
 
-  memset(&hints, 0, sizeof hints);
-  hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
+  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
+    {
+      /* 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
-          }
-        }
+      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)
@@ -236,25 +292,25 @@
 static void
 read_from_file(char *buffer, long size, const char *file_name)
 {
-     char ch;
-     long index=0;
-     FILE *fp;
+  char ch;
+  long index=0;
+  FILE *fp;
 
-     fp = fopen(file_name,"r"); // read mode
+  fp = fopen(file_name,"r"); /* read mode */
 
-     if( fp == NULL )
-     {
-        perror("Error while opening the file.\n");
-        exit(EXIT_FAILURE);
-     }
+  if( fp == NULL )
+    {
+      perror("Error while opening the file.\n");
+      exit(EXIT_FAILURE);
+    }
 
-     printf("The contents of %s file are :\n", file_name);
+  printf("The contents of %s file are :\n", file_name);
 
-     while(index<size && ( ch = fgetc(fp) ) != EOF ){
-       buffer[index++] = ch;
-     }
+  while(index<size && ( ch = fgetc(fp) ) != EOF ){
+      buffer[index++] = ch;
+  }
 
-     fclose(fp);
+  fclose(fp);
 }
 
 #define ISSUER_CN_PINNING "The Apache Software Foundation"
@@ -269,25 +325,27 @@
 
   BIGNUM *bn = ASN1_INTEGER_to_BN(serial, NULL);
   if (!bn) {
-    fprintf(stderr, "unable to convert ASN1INTEGER to BN\n");
-    return EXIT_FAILURE;
+      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;
+      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 (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;
+      retval=TRUE;
   }else{
-    g_warning("Serial number is not valid");
+      g_warning("Serial number is not valid");
   }
 
   BN_free(bn);
@@ -306,52 +364,52 @@
   /* 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);
+      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;
+      /* 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;
+      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;
-    }
+      /* 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;
+      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);
+      /* 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(!valid)
+	return valid;
   }
 
   if(!verify_certificate_sn(cert, CERT_SERIAL_NUMBER)){
-    return FALSE;
+      return FALSE;
   }else{
-    g_info("Verified serial number");
+      g_info("Verified serial number");
   }
 
   return TRUE;
@@ -369,32 +427,33 @@
   pid_t pid;
   ThriftSSLSocket *tSSLsocket = NULL;
   ThriftTransport *transport = NULL;
-  //  int port = 51199;
+  /*  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 )
-  //  {
-  //    /* child listens */
-  //    thrift_ssl_socket_server (port);
-  //    exit (0);
-  //  } else {
+/*
+  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
+  /* 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");
+  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!");
+      g_warning("Certificates cannot be loaded!");
   }
 
   transport = THRIFT_TRANSPORT (tSSLsocket);
@@ -405,122 +464,24 @@
 
   /* 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_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 ( wait (&status) == pid ); */
   g_assert ( status == 0 );
-  //  }
+  /*  } */
 }
 #endif
 
 
-/* test ThriftSocket's peek() implementation */
-//static void
-//test_ssl_peek(void)
-//{
-//  gint status;
-//  pid_t pid;
-//  guint port = 51199;
-//  gchar data = 'A';
-//  ThriftTransport *client_transport;
-//  GError *error = NULL;
-//
-//  client_transport = g_object_new (THRIFT_TYPE_SSL_SOCKET,
-//                                   "hostname", "localhost",
-//                                   "port",     port,
-//                                   NULL);
-//
-//  /* thrift_transport_peek returns FALSE when the socket is closed */
-//  g_assert (thrift_transport_is_open (client_transport) == FALSE);
-//  g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
-//  g_assert (error == NULL);
-//
-//  pid = fork ();
-//  g_assert (pid >= 0);
-//
-//  if (pid == 0)
-//  {
-//    ThriftServerTransport *server_transport = NULL;
-//
-//    g_object_unref (client_transport);
-//
-//    /* 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);
-//
-//    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);
-//
-//    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);
-//
-//    exit (0);
-//  }
-//  else {
-//    /* 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);
-//
-//    /* 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);
-//
-//    /* 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
-//       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);
-//
-//    thrift_transport_read_end (client_transport, &error);
-//    thrift_transport_close (client_transport, &error);
-//
-//    g_object_unref (client_transport);
-//
-//    g_assert (wait (&status) == pid);
-//    g_assert (status == 0);
-//  }
-//}
-
 static void
-thrift_ssl_socket_server (const int port)
+thrift_socket_server (const int port)
 {
   int bytes = 0;
   ThriftServerTransport *transport = NULL;
@@ -529,7 +490,7 @@
   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);
@@ -566,10 +527,11 @@
 
   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/OpenAndClose", test_ssl_open_and_close);
-  // This test is disabled because server is not ready
-  // g_test_add_func ("/testtransportsslsocket/AuthorizationManagerPinning", test_ssl_authorization_manager);
-  //  g_test_add_func ("/testtransportsslsocket/Peek", test_ssl_peek);
+  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 ();