Example #1
0
static void
test_read_and_write_default (void)
{
  ThriftMemoryBuffer *tbuffer = NULL;
  gint got, want, i;
  gchar read[10];
  gchar *b;
  GError *error = NULL;

  tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
  for (i = 0; i < 100; ++i) {
    g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
                                        (gpointer) TEST_DATA, 10, &error) == TRUE);
    g_assert (error == NULL);
  }

  for (i = 0; i < 100; ++i) {
    memset(read, 0, 10);
    b = read;
    want = 10;
    while (want > 0) {
      got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
                                       (gpointer) b, want, &error);
      g_assert (got > 0 && got <= want);
      g_assert (error == NULL);
      b += got;
      want -= got;
    }
    g_assert (memcmp (read, TEST_DATA, 10) == 0);
  }
  g_object_unref (tbuffer);
}
Example #2
0
static void
test_read_and_write(void)
{
  ThriftMemoryBuffer *tbuffer = NULL;
  guchar buf[10] = TEST_DATA;
  guchar read[10];
  GError *error = NULL;

  tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL);
  assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
                                      (gpointer) buf,
                                      10, &error) == FALSE);
  assert (error != NULL);
  g_error_free (error);
  error = NULL;
  g_object_unref (tbuffer);

  tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, NULL);
  assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
                                      (gpointer) buf, 10, &error) == TRUE);
  assert (error == NULL);

  assert (thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
                                     &read, 10, &error) > 0);
  assert (error == NULL);
}
Example #3
0
static void
test_open_and_close (void)
{
  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);

  transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT,
                                              "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_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);
}
Example #4
0
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 );
    }
}
Example #5
0
static void
test_open_and_close(void)
{
  ThriftMemoryBuffer *tbuffer = NULL;

  /* create a ThriftMemoryBuffer */
  tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);

  /* this shouldn't work */
  assert (thrift_memory_buffer_open (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
  assert (thrift_memory_buffer_is_open (THRIFT_TRANSPORT (tbuffer)) == TRUE);
  assert (thrift_memory_buffer_close (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
  g_object_unref (tbuffer);
}
/* Wraps a transport with a ThriftFramedTransport. */
ThriftTransport *
thrift_framed_transport_factory_get_transport (ThriftTransportFactory *factory,
                                               ThriftTransport *transport)
{
  THRIFT_UNUSED_VAR (factory);

  return THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
                                         "transport", transport,
                                         NULL));
}
Example #7
0
int main(int argc, char **argv) {
#else
int log(){
#endif /* NOMAIN */

    g_type_init();

    ThriftTransport *transport = NULL;
    ThriftSocket *tsocket = NULL;
    ThriftBinaryProtocol *protocol = NULL;
    scribeClient *client = NULL;
    scribeIf *iface = NULL;

    ResultCode result;
    LogEntry *log = NULL;
    GPtrArray * messages = NULL;

    GError *error = NULL;

    tsocket = g_object_new(THRIFT_TYPE_SOCKET, "hostname", "localhost",
        "port", 1463, NULL);
    transport = g_object_new(THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
        THRIFT_TRANSPORT(tsocket), NULL);
    protocol = g_object_new(THRIFT_TYPE_BINARY_PROTOCOL, "transport",
        THRIFT_TRANSPORT(transport), NULL);
    client = g_object_new(TYPE_SCRIBE_CLIENT,
        "input_protocol", THRIFT_PROTOCOL(protocol),
        "output_protocol", THRIFT_PROTOCOL(protocol), NULL);
    iface = SCRIBE_IF(client);

    thrift_transport_open(THRIFT_TRANSPORT(tsocket), NULL);

    messages = g_ptr_array_new();

    log = g_object_new(TYPE_LOG_ENTRY, NULL);
    log->category = "TEST";
    log->message = "This is a test message\n\0";

    g_ptr_array_add(messages, log);
    scribe_client_log(iface, &result, messages, &error);
    return 0;

}
Example #8
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 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, 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);

	g_object_unref (transport);
	g_object_unref (tsocket);

	g_assert ( wait (&status) == pid );
	g_assert ( status == 0 );
    }
}
Example #9
0
static void
test_read_and_write (void)
{
  ThriftMemoryBuffer *tbuffer = NULL;
  gint got, want;
  gchar read[10];
  gchar *b;
  GError *error = NULL;

  tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL);
  g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
                                      (gpointer) TEST_DATA,
                                      10, &error) == FALSE);
  g_assert (error != NULL);
  g_error_free (error);
  error = NULL;
  g_object_unref (tbuffer);

  tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, NULL);
  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;
  while (want > 0) {
    got = thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
                                     (gpointer) b, want, &error);
    g_assert (got > 0 && got <= want);
    g_assert (error == NULL);
    b += got;
    want -= got;
  }
  g_assert (memcmp (read, TEST_DATA, 10) == 0);
  g_object_unref (tbuffer);
}
Example #10
0
static void
test_open_and_close(void)
{
    ThriftSocket *tsocket = NULL;
    ThriftTransport *transport = NULL;
    GError *err = NULL;

    /* create a ThriftSocket */
    tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
                            "port", 51188, 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 */
    assert (thrift_buffered_transport_open (transport, NULL) == FALSE);
    assert (thrift_buffered_transport_is_open (transport) == TRUE);
    assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
    g_object_unref (transport);
    g_object_unref (tsocket);

    /* 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);

    assert (thrift_buffered_transport_open (transport, &err) == FALSE);
    g_object_unref (transport);
    g_object_unref (tsocket);
    g_error_free (err);
    err = NULL;
}
Example #11
0
static void
test_read_and_write_external (void)
{
  ThriftMemoryBuffer *tbuffer = NULL;
  gchar *b;
  GError *error = NULL;
  GByteArray *buf = g_byte_array_new ();
  g_assert (buf != NULL);

  tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf", buf, NULL);
  g_assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
                                      (gpointer) TEST_DATA, 10, &error) == TRUE);
  g_assert (error == NULL);

  g_assert (memcmp (buf->data, TEST_DATA, 10) == 0);
  g_object_unref (tbuffer);
}
Example #12
0
ThriftTransport *
thrift_server_socket_accept (ThriftServerTransport *transport, GError **error)
{
    int sd = THRIFT_INVALID_SOCKET;
    guint addrlen = 0;
    struct sockaddr_in address;
    ThriftSocket *socket = NULL;

    ThriftServerSocket *tsocket = THRIFT_SERVER_SOCKET (transport);

    if ((sd = accept(tsocket->sd, (struct sockaddr *) &address, &addrlen)) == -1)
    {
        g_set_error (error, THRIFT_SERVER_SOCKET_ERROR,
                     THRIFT_SERVER_SOCKET_ERROR_ACCEPT,
                     "failed to accept connection - %s",
                     strerror(errno));
        return FALSE;
    }

    socket = g_object_new (THRIFT_TYPE_SOCKET, NULL);
    socket->sd = sd;

    return THRIFT_TRANSPORT(socket);
}
Example #13
0
static void
test_read_and_write_complex_types (void)
{
  int status;
  pid_t pid;
  ThriftSocket *tsocket = NULL;
  ThriftTransport *transport = NULL;
  ThriftBinaryProtocol *tb = NULL;
  ThriftProtocol *protocol = NULL;
  int port = TEST_PORT;

  /* fork a server from the client */
  pid = fork ();
  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);
    assert (thrift_transport_is_open (transport));

    /* create a ThriftBinaryTransport */
    tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
                       tsocket, NULL);
    protocol = THRIFT_PROTOCOL (tb);
    assert (protocol != NULL);

    /* test structures */
    assert (thrift_binary_protocol_write_struct_begin (protocol, 
                                                       NULL, NULL) == 0);
    assert (thrift_binary_protocol_write_struct_end (protocol, NULL) == 0);

    assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
                                                      1, NULL) > 0);
    assert (thrift_binary_protocol_write_field_end (protocol, NULL) == 0);

    /* test write error */
    transport_write_error = 1;
    assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID, 
                                                      1, NULL) == -1);
    transport_write_error = 0;

    /* test 2nd write error */
    transport_write_count = 0;
    transport_write_error_at = 1;
    assert (thrift_binary_protocol_write_field_begin (protocol, "test", T_VOID,
                                                      1, NULL) == -1);
    transport_write_error_at = -1;

    /* test 2nd read failure on a field */
    thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);

    /* test write_field_stop */
    assert (thrift_binary_protocol_write_field_stop (protocol, NULL) > 0);

    /* write a map */
    assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
                                                    1, NULL) > 0);
    assert (thrift_binary_protocol_write_map_end (protocol, NULL) == 0);

    /* test 2nd read failure on a map */
    thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);

    /* test 3rd read failure on a map */
    thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
    thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);

    /* test 1st write failure on a map */
    transport_write_error = 1;
    assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
                                                    1, NULL) == -1);
    transport_write_error = 0;

    /* test 2nd write failure on a map */
    transport_write_count = 0;
    transport_write_error_at = 1;
    assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
                                                    1, NULL) == -1);
    transport_write_error_at = -1;

    /* test 3rd write failure on a map */
    transport_write_count = 0;
    transport_write_error_at = 2;
    assert (thrift_binary_protocol_write_map_begin (protocol, T_VOID, T_VOID,
                                                    1, NULL) == -1);
    transport_write_error_at = -1;

    /* test negative map size */
    thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
    thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
    thrift_binary_protocol_write_i32 (protocol, -10, NULL);

    /* test list operations */
    assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
                                                     1, NULL) > 0);
    assert (thrift_binary_protocol_write_list_end (protocol, NULL) == 0);

    /* test 2nd read failure on a list */
    thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);

    /* test negative list size */
    thrift_binary_protocol_write_byte (protocol, T_VOID, NULL);
    thrift_binary_protocol_write_i32 (protocol, -10, NULL);

    /* test first write error on a list */
    transport_write_error = 1;
    assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
                                                     1, NULL) == -1);
    transport_write_error = 0;

    /* test 2nd write error on a list */
    transport_write_count = 0;
    transport_write_error_at = 1;
    assert (thrift_binary_protocol_write_list_begin (protocol, T_VOID,
                                                     1, NULL) == -1);
    transport_write_error_at = -1;

    /* test set operation s*/
    assert (thrift_binary_protocol_write_set_begin (protocol, T_VOID,
                                                    1, NULL) > 0);
    assert (thrift_binary_protocol_write_set_end (protocol, NULL) == 0);

    /* invalid version */
    assert (thrift_binary_protocol_write_i32 (protocol, -1, NULL) > 0);

    /* sz > 0 for a message */
    assert (thrift_binary_protocol_write_i32 (protocol, 1, NULL) > 0);

    /* send a valid message */
    thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
    thrift_binary_protocol_write_string (protocol, "test", NULL);
    thrift_binary_protocol_write_i32 (protocol, 1, NULL);

    /* broken 2nd read */
    thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);

    /* send a broken 3rd read */
    thrift_binary_protocol_write_i32 (protocol, 0x80010000, NULL);
    thrift_binary_protocol_write_string (protocol, "test", NULL);

    /* send a valid message */
    assert (thrift_binary_protocol_write_message_begin (protocol, "test",
                                                        T_CALL, 1, NULL) > 0);

    assert (thrift_binary_protocol_write_message_end (protocol, NULL) == 0);

    /* send broken writes */
    transport_write_error = 1;
    assert (thrift_binary_protocol_write_message_begin (protocol, "test",
                                                        T_CALL, 1, NULL) == -1);
    transport_write_error = 0;

    transport_write_count = 0;
    transport_write_error_at = 2;
    assert (thrift_binary_protocol_write_message_begin (protocol, "test",
                                                        T_CALL, 1, NULL) == -1);
    transport_write_error_at = -1;

    transport_write_count = 0;
    transport_write_error_at = 3;
    assert (thrift_binary_protocol_write_message_begin (protocol, "test",
                                                        T_CALL, 1, NULL) == -1);
    transport_write_error_at = -1;

    /* clean up */
    thrift_transport_close (transport, NULL);
    g_object_unref (tsocket);
    g_object_unref (protocol);
    assert (wait (&status) == pid);
    assert (status == 0);
  }
}
Example #14
0
static void
test_read_and_write_primitives(void)
{
  int status;
  pid_t pid;
  ThriftSocket *tsocket = NULL;
  ThriftTransport *transport = NULL;
  ThriftBinaryProtocol *tb = NULL;
  ThriftProtocol *protocol = NULL;
  gpointer binary = (gpointer *) TEST_STRING;
  guint32 len = strlen (TEST_STRING);
  int port = TEST_PORT;

  /* fork a server from the client */
  pid = fork ();
  assert (pid >= 0);

  if (pid == 0)
  {
    /* child listens */
    thrift_server_primitives (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);
    assert (thrift_transport_is_open (transport));

    /* create a ThriftBinaryTransport */
    tb = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
                       tsocket, NULL);
    protocol = THRIFT_PROTOCOL (tb);
    assert (protocol != NULL);

    /* write a bunch of primitives */
    assert (thrift_binary_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
    assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, NULL) > 0);
    assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) > 0);
    assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) > 0);
    assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) > 0);
    assert (thrift_binary_protocol_write_double (protocol, 
                                                 TEST_DOUBLE, NULL) > 0);
    assert (thrift_binary_protocol_write_string (protocol,
                                                 TEST_STRING, NULL) > 0);
    assert (thrift_binary_protocol_write_binary (protocol, binary, 
                                                 len, NULL) > 0);
    assert (thrift_binary_protocol_write_binary (protocol, NULL, 0, NULL) > 0);
    assert (thrift_binary_protocol_write_binary (protocol, binary,
                                                 len, NULL) > 0);

    /* test write errors */
    transport_write_error = 1;
    assert (thrift_binary_protocol_write_byte (protocol, TEST_BYTE, 
                                               NULL) == -1);
    assert (thrift_binary_protocol_write_i16 (protocol, TEST_I16, NULL) == -1);
    assert (thrift_binary_protocol_write_i32 (protocol, TEST_I32, NULL) == -1);
    assert (thrift_binary_protocol_write_i64 (protocol, TEST_I64, NULL) == -1);
    assert (thrift_binary_protocol_write_double (protocol, TEST_DOUBLE,
                                                 NULL) == -1);
    assert (thrift_binary_protocol_write_binary (protocol, binary, len,
                                                 NULL) == -1);
    transport_write_error = 0;

    /* test binary partial failure */
    transport_write_count = 0;
    transport_write_error_at = 1;
    assert (thrift_binary_protocol_write_binary (protocol, binary,
                                                 len, NULL) == -1);
    transport_write_error_at = -1;

    /* clean up */
    thrift_transport_close (transport, NULL);
    g_object_unref (tsocket);
    g_object_unref (protocol);
    assert (wait (&status) == pid);
    assert (status == 0);
  }
}
Example #15
0
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 ();
  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);
    assert (thrift_transport_is_open (transport));

    /* create a ThriftCompactTransport */
    tc = g_object_new (THRIFT_TYPE_COMPACT_PROTOCOL, "transport",
                       tsocket, NULL);
    protocol = THRIFT_PROTOCOL (tc);
    assert (protocol != NULL);

    /* test structures */
    assert (thrift_compact_protocol_write_struct_begin (protocol,
                                                       NULL, NULL) == 0);
    assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0);

    /* test field state w.r.t. deltas */

    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE, 1, NULL) == 1);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       16, NULL) == 1);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       17, NULL) == 1);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       15, NULL) > 1);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       30, NULL) == 1);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       46, NULL) > 1);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       47, NULL) == 1);

    /* test fields */
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       1, NULL) > 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);

    /* test field state w.r.t. structs */

    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       1, NULL) > 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       16, NULL) == 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);

    assert (thrift_compact_protocol_write_struct_begin (protocol,
                                                       NULL, NULL) == 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       17, NULL) > 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);

    assert (thrift_compact_protocol_write_struct_begin (protocol,
                                                       NULL, NULL) == 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       18, NULL) > 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       19, NULL) == 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0);

    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       18, NULL) == 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       25, NULL) == 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_struct_end (protocol, NULL) == 0);

    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       17, NULL) == 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);

    /* test field state w.r.t. bools */

    /* deltas */
    /* non-bool field -> bool field -> non-bool field */
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       18, NULL) == 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
                                                       19, NULL) == 0);
    assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL,
                                                NULL) == 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       20, NULL) == 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    /* bool -> bool field -> bool */
    assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
                                                       21, NULL) == 0);
    assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL,
                                                NULL) == 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);

    /* no deltas */
    /* non-bool field -> bool field -> non-bool field */
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       1, NULL) > 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
                                                      1, NULL) == 0);
    assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       1, NULL) > 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    /* bool -> bool field -> bool */
    assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);
    assert (thrift_compact_protocol_write_field_begin (protocol, "test", T_BOOL,
                                                      1, NULL) == 0);
    assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 1);
    assert (thrift_compact_protocol_write_field_end (protocol, NULL) == 0);
    assert (thrift_compact_protocol_write_bool (protocol, TEST_BOOL, NULL) > 0);

    /* test write error */
    transport_write_error = 1;
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       1, NULL) == -1);
    transport_write_error = 0;

    /* test 2nd write error */
    transport_write_count = 0;
    transport_write_error_at = 1;
    assert (thrift_compact_protocol_write_field_begin (protocol, "test",
                                                       T_DOUBLE,
                                                       1, NULL) == -1);
    transport_write_error_at = -1;

    /* test 2nd read failure on a field */
    thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL);

    /* test write_field_stop */
    assert (thrift_compact_protocol_write_field_stop (protocol, NULL) > 0);

    /* write a map */
    assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE,
                                                     T_DOUBLE,
                                                     1, NULL) > 0);
    assert (thrift_compact_protocol_write_map_end (protocol, NULL) == 0);

    /* test 1st read failure on map---nothing to do on our side */

    /* test 2nd read failure on a map */
    thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL);

    /* test 1st write failure on a map */
    transport_write_error = 1;
    assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE,
                                                     T_DOUBLE,
                                                     1, NULL) == -1);
    transport_write_error = 0;

    /* test 2nd write failure on a map */
    transport_write_count = 0;
    transport_write_error_at = 1;
    assert (thrift_compact_protocol_write_map_begin (protocol, T_DOUBLE,
                                                     T_DOUBLE,
                                                     1, NULL) == -1);
    transport_write_error_at = -1;

    /* test negative map size */
    thrift_compact_protocol_write_varint32 (tc, -10, NULL);
    thrift_compact_protocol_write_byte (protocol, T_DOUBLE, NULL);

    /* test list operations */
    assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
                                                     15, NULL) > 0);
    assert (thrift_compact_protocol_write_list_end (protocol, NULL) == 0);

    /* test 1st read failure on a small list---nothing to do on our end */

    /* test 1st read failure on a big list---nothing to do on our end */

    /* test 2nd read failure on a big list */
    thrift_compact_protocol_write_byte (protocol, (gint8) 0xf0, NULL);

    /* test negative list size */
    thrift_compact_protocol_write_byte (protocol, (gint8) 0xf0, NULL);
    thrift_compact_protocol_write_varint32 (tc, -10, NULL);

    /* test first write error on a small list */
    transport_write_error = 1;
    assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
                                                     14, NULL) == -1);
    transport_write_error = 0;

    /* test first write error on a big list */
    transport_write_error = 1;
    assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
                                                     15, NULL) == -1);
    transport_write_error = 0;

    /* test 2nd write error on a big list */
    transport_write_count = 0;
    transport_write_error_at = 1;
    assert (thrift_compact_protocol_write_list_begin (protocol, T_DOUBLE,
                                                     15, NULL) == -1);
    transport_write_error_at = -1;

    /* test set operation s*/
    assert (thrift_compact_protocol_write_set_begin (protocol, T_DOUBLE,
                                                    1, NULL) > 0);
    assert (thrift_compact_protocol_write_set_end (protocol, NULL) == 0);

    /* invalid protocol */
    assert (thrift_compact_protocol_write_byte (protocol, 0, NULL) > 0);

    /* invalid version */
    assert (thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u,
                                                NULL) > 0);
    assert (thrift_compact_protocol_write_byte (protocol, 0, NULL) > 0);

    /* send a valid message */
    assert (thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u,
                                                NULL) > 0);
    assert (thrift_compact_protocol_write_byte (protocol, 0x01u, NULL) > 0);
    thrift_compact_protocol_write_varint32 (tc, 1, NULL);
    thrift_compact_protocol_write_string (protocol, "test", NULL);

    /* broken 2nd read */
    thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL);

    /* send a broken 3rd read */
    thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL);
    thrift_compact_protocol_write_byte (protocol, 0x01u, NULL);

    /* send a broken 4th read */
    thrift_compact_protocol_write_byte (protocol, (gint8) 0x82u, NULL);
    thrift_compact_protocol_write_byte (protocol, 0x01u, NULL);
    thrift_compact_protocol_write_varint32 (tc, 1, NULL);

    /* send a valid message */
    assert (thrift_compact_protocol_write_message_begin (protocol, "test",
                                                        T_CALL, 1, NULL) > 0);

    assert (thrift_compact_protocol_write_message_end (protocol, NULL) == 0);

    /* send broken writes */
    transport_write_error = 1;
    assert (thrift_compact_protocol_write_message_begin (protocol, "test",
                                                        T_CALL, 1, NULL) == -1);
    transport_write_error = 0;

    transport_write_count = 0;
    transport_write_error_at = 1;
    assert (thrift_compact_protocol_write_message_begin (protocol, "test",
                                                        T_CALL, 1, NULL) == -1);
    transport_write_error_at = -1;

    transport_write_count = 0;
    transport_write_error_at = 2;
    assert (thrift_compact_protocol_write_message_begin (protocol, "test",
                                                        T_CALL, 1, NULL) == -1);
    transport_write_error_at = -1;

    transport_write_count = 0;
    transport_write_error_at = 3;
    assert (thrift_compact_protocol_write_message_begin (protocol, "test",
                                                        T_CALL, 1, NULL) == -1);
    transport_write_error_at = -1;

    /* clean up */
    thrift_transport_close (transport, NULL);
    g_object_unref (tsocket);
    g_object_unref (protocol);
    assert (wait (&status) == pid);
    assert (status == 0);
  }
}
Example #16
0
/* test reading from the transport after the peer has unexpectedly
   closed the connection */
static void
test_read_after_peer_close(void)
{
  int status;
  pid_t pid;
  int port = 51199;
  GError *err = NULL;

  pid = fork ();
  g_assert (pid >= 0);

  if (pid == 0)
    {
      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);

      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);

      /* close the connection immediately after the client connects */
      thrift_transport_close (client_transport, NULL);

      g_object_unref (client_transport);
      g_object_unref (server_transport);

      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);

	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));

	/* 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);

	g_error_free (err);
	err = NULL;

	thrift_transport_read_end (transport, &err);
	g_assert (err == NULL);

	thrift_transport_close (transport, &err);
	g_assert (err == NULL);

	g_object_unref (transport);
	g_object_unref (tsocket);

	g_assert (wait (&status) == pid);
	g_assert (status == 0);
    }
}
Example #17
0
static void
test_read_and_write (void)
{
  gchar out_buf[8];
  gchar *b;
  gint want, got;
  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);

  /* write */
  transport = THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FD_TRANSPORT,
                                              "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,
                                              "fd", fd,
                                              NULL));
  klass = THRIFT_TRANSPORT_GET_CLASS (transport);

  memset(out_buf, 0, 8);
  b = out_buf;
  want = 7;
  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, 7) == 0);

  memset(out_buf, 0, 8);
  b = out_buf;
  want = 4;
  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 + 7, 4) == 0);

  g_assert (klass->close (transport, &error));
  g_object_unref (transport);

  /* clean up */

  g_remove (filename);
  g_free (filename);
}
Example #18
0
static void
test_write_fail(void)
{
    int status;
    pid_t pid;
    ThriftSocket *tsocket = NULL;
    ThriftTransport *transport = NULL;
    int port = 51198;
    guchar buf[10] = TEST_DATA; /* a buffer */

    /* SIGPIPE when send to disconnected socket */
    signal(SIGPIPE, SIG_IGN);

    pid = fork ();
    assert ( pid >= 0 );

    if ( pid == 0 )
    {
        /* child listens */
        ThriftServerTransport *transport = NULL;
        ThriftTransport *client = 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_BUFFERED_TRANSPORT, "transport",
                               thrift_server_transport_accept (transport, NULL),
                               "r_buf_size", 5, NULL);
        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);

        tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
                                "port", port, NULL);
        transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
                                  "transport", THRIFT_TRANSPORT (tsocket),
                                  "w_buf_size", 4, NULL);


        assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
        assert (thrift_buffered_transport_is_open (transport));

        /* recognize disconnection */
        sleep(1);
        assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
        assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);

        /* write and overflow buffer */
        assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);

        /* write 1 and flush */
        assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
        assert (thrift_buffered_transport_flush (transport, NULL) == FALSE);

        thrift_buffered_transport_close (transport, NULL);

        g_object_unref (transport);
        g_object_unref (tsocket);

        assert ( wait (&status) == pid );
        assert ( status == 0 );
    }
}