예제 #1
0
static void* producer_thread( object_t thread, void* arg )
{
	uint8_t buffer[256] = {0};
	producer_thread_arg_t* args = arg;
	unsigned int produced = 0;
	tick_t timestamp = 0;

	do
	{
		if( args->sleep_time )
			thread_sleep( (int)args->sleep_time );
		else
			thread_yield();
		timestamp = args->max_delay ? time_current() + random64_range( 0, args->max_delay ) : 0;
		memcpy( buffer, &timestamp, sizeof( tick_t ) );
		event_post( args->stream, random32_range( 1, 65535 ), random32_range( timestamp ? 8 : 0, 256 ), args->id, buffer, timestamp );
		++produced;
	} while( !thread_should_terminate( thread ) && ( time_current() < args->end_time ) );

	return (void*)((uintptr_t)produced);
}
예제 #2
0
DECLARE_TEST(blowfish, random_data) {
  uint64_t plaintext[2][1024];
  uint64_t keytext[32];
  unsigned int i, j;
  blowfish_t* blowfish;
  uint64_t init_vector;

  blowfish = blowfish_allocate();

  for (i = 0; i < 1024; ++i) {
    for (j = 0; j < 32; ++j)
      keytext[j] = random64();

    for (j = 0; j < 1024; ++j) {
      plaintext[0][j] = random64();
      plaintext[1][j] = plaintext[0][j];
    }

    init_vector = random64();

    blowfish_initialize(blowfish, keytext, random32_range(1, 32 * 8));

    blowfish_encrypt(blowfish, plaintext[0], 1024 * 8, BLOCKCIPHER_ECB, init_vector);
    blowfish_decrypt(blowfish, plaintext[0], 1024 * 8, BLOCKCIPHER_ECB, init_vector);
    EXPECT_EQ(memcmp(plaintext[0], plaintext[1], 1024 * 8), 0);

    blowfish_encrypt(blowfish, plaintext[0], 1024 * 8, BLOCKCIPHER_CBC, init_vector);
    blowfish_decrypt(blowfish, plaintext[0], 1024 * 8, BLOCKCIPHER_CBC, init_vector);
    EXPECT_EQ(memcmp(plaintext[0], plaintext[1], 1024 * 8), 0);

    blowfish_encrypt(blowfish, plaintext[0], 1024 * 8, BLOCKCIPHER_CFB, init_vector);
    blowfish_decrypt(blowfish, plaintext[0], 1024 * 8, BLOCKCIPHER_CFB, init_vector);
    EXPECT_EQ(memcmp(plaintext[0], plaintext[1], 1024 * 8), 0);

    blowfish_encrypt(blowfish, plaintext[0], 1024 * 8, BLOCKCIPHER_OFB, init_vector);
    blowfish_decrypt(blowfish, plaintext[0], 1024 * 8, BLOCKCIPHER_OFB, init_vector);
    EXPECT_EQ(memcmp(plaintext[0], plaintext[1], 1024 * 8), 0);
  }

  blowfish_deallocate(blowfish);

  return 0;
}
예제 #3
0
DECLARE_TEST(udp, datagram_ipv6) {
	network_address_t** address_local = 0;
	network_address_t* address = 0;
	network_address_t* address_server = 0;
	test_datagram_arg_t client_arg[4];

	int server_port;
	int state, iaddr, asize;
	thread_t threads[5];

	socket_t* sock_server;
	socket_t* sock_client[4];

	if (!network_supports_ipv6())
		return 0;

	sock_server = udp_socket_allocate();
	sock_client[0] = udp_socket_allocate();
	sock_client[1] = udp_socket_allocate();
	sock_client[2] = udp_socket_allocate();
	sock_client[3] = udp_socket_allocate();

	address_local = network_address_local();
	for (iaddr = 0, asize = array_size(address_local); iaddr < asize; ++iaddr) {
		if (network_address_family(address_local[iaddr]) == NETWORK_ADDRESSFAMILY_IPV6) {
			address = address_local[iaddr];
			break;
		}
	}
	EXPECT_NE(address, 0);

	do {
		server_port = random32_range(1024, 35535);
		network_address_ip_set_port(address, server_port);
		if (socket_bind(sock_server, address))
			break;
	}
	while (true);

	address_server = network_address_clone(address);
	network_address_ip_set_port(address_server, server_port);

	network_address_array_deallocate(address_local);

	state = socket_state(sock_server);
	EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED);

	state = socket_state(sock_client[0]);
	EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED);

	state = socket_state(sock_client[1]);
	EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED);

	state = socket_state(sock_client[2]);
	EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED);

	state = socket_state(sock_client[3]);
	EXPECT_TRUE(state == SOCKETSTATE_NOTCONNECTED);

	socket_set_blocking(sock_server, true);
	socket_set_blocking(sock_client[0], true);
	socket_set_blocking(sock_client[1], true);
	socket_set_blocking(sock_client[2], true);
	socket_set_blocking(sock_client[3], true);

	client_arg[0].sock = sock_client[0]; client_arg[0].target = address_server;
	client_arg[1].sock = sock_client[1]; client_arg[1].target = address_server;
	client_arg[2].sock = sock_client[2]; client_arg[2].target = address_server;
	client_arg[3].sock = sock_client[3]; client_arg[3].target = address_server;

	thread_initialize(&threads[0], datagram_server_blocking_thread, sock_server,
	                  STRING_CONST("server_thread"), THREAD_PRIORITY_NORMAL, 0);
	thread_initialize(&threads[1], datagram_client_blocking_thread, &client_arg[0],
	                  STRING_CONST("client_thread"), THREAD_PRIORITY_NORMAL, 0);
	thread_initialize(&threads[2], datagram_client_blocking_thread, &client_arg[1],
	                  STRING_CONST("client_thread"), THREAD_PRIORITY_NORMAL, 0);
	thread_initialize(&threads[3], datagram_client_blocking_thread, &client_arg[2],
	                  STRING_CONST("client_thread"), THREAD_PRIORITY_NORMAL, 0);
	thread_initialize(&threads[4], datagram_client_blocking_thread, &client_arg[3],
	                  STRING_CONST("client_thread"), THREAD_PRIORITY_NORMAL, 0);

	thread_start(&threads[0]);
	thread_start(&threads[1]);
	thread_start(&threads[2]);
	thread_start(&threads[3]);
	thread_start(&threads[4]);

	test_wait_for_threads_startup(threads, 5);

	thread_finalize(&threads[0]);
	thread_finalize(&threads[1]);
	thread_finalize(&threads[2]);
	thread_finalize(&threads[3]);
	thread_finalize(&threads[4]);

	socket_deallocate(sock_server);
	socket_deallocate(sock_client[0]);
	socket_deallocate(sock_client[1]);
	socket_deallocate(sock_client[2]);
	socket_deallocate(sock_client[3]);

	memory_deallocate(address_server);

	return 0;
}
예제 #4
0
DECLARE_TEST(udp, stream_ipv6) {
	network_address_t** address_local = 0;
	network_address_t* address = 0;

	int server_port, client_port;
	int state, iaddr, asize;
	thread_t threads[2];

	socket_t* sock_server;
	socket_t* sock_client;

	if (!network_supports_ipv6())
		return 0;

	sock_server = udp_socket_allocate();
	sock_client = udp_socket_allocate();

	address_local = network_address_local();
	for (iaddr = 0, asize = array_size(address_local); iaddr < asize; ++iaddr) {
		if (network_address_family(address_local[iaddr]) == NETWORK_ADDRESSFAMILY_IPV6) {
			address = address_local[iaddr];
			break;
		}
	}
	EXPECT_NE(address, 0);

	do {
		server_port = random32_range(1024, 35535);
		network_address_ip_set_port(address, server_port);
		if (socket_bind(sock_server, address))
			break;
	}
	while (true);

	do {
		client_port = random32_range(1024, 35535);
		network_address_ip_set_port(address, client_port);
		if (socket_bind(sock_client, address))
			break;
	}
	while (true);

	socket_set_blocking(sock_server, false);
	socket_set_blocking(sock_client, false);

	network_address_ip_set_port(address, client_port);
	socket_connect(sock_server, address, 0);

	network_address_ip_set_port(address, server_port);
	socket_connect(sock_client, address, 0);

	network_address_array_deallocate(address_local);

	state = socket_state(sock_server);
	EXPECT_TRUE(state == SOCKETSTATE_CONNECTED);

	state = socket_state(sock_client);
	EXPECT_TRUE(state == SOCKETSTATE_CONNECTED);

	socket_set_blocking(sock_server, true);
	socket_set_blocking(sock_client, true);

	thread_initialize(&threads[0], stream_blocking_thread, sock_server, STRING_CONST("io_thread"),
	                  THREAD_PRIORITY_NORMAL, 0);
	thread_initialize(&threads[1], stream_blocking_thread, sock_client, STRING_CONST("io_thread"),
	                  THREAD_PRIORITY_NORMAL, 0);

	thread_start(&threads[0]);
	thread_start(&threads[1]);

	test_wait_for_threads_startup(threads, 2);

	thread_finalize(&threads[0]);
	thread_finalize(&threads[1]);

	socket_deallocate(sock_server);
	socket_deallocate(sock_client);

	return 0;
}
예제 #5
0
DECLARE_TEST( hash, stability )
{
	//TODO: Implement a proper test instead of this crap
	int i, j, k, len;
	hash_t lhash, rhash, rhashref;

	for( i = 0; i < 128; ++i )
	{
		uint32_t lhs[129], rhs[129];
		len = i + 1;

		for( k = 0; k < len; ++k )
			lhs[k] = random32();

		lhash = hash( lhs, len * sizeof( uint32_t ) );
		EXPECT_NE( lhash, 0U );

		for( j = 0; j < 64000; ++j )
		{
			for( k = 0; k < len; ++k )
				rhs[k] = random32();

			rhashref = hash( rhs, len * sizeof( uint32_t ) );
			rhash = hash( rhs, len * sizeof( uint32_t ) );

			EXPECT_EQ( rhashref, rhash );
			if( memcmp( lhs, rhs, len * sizeof( uint32_t ) ) )
				EXPECT_NE( lhash, rhash );
			EXPECT_NE( rhash, 0U );
		}
	}

	for( i = 4; i < 128; ++i )
	{
		char lhs[130], rhs[130];
		len = i + 1;

		lhs[0] = 'f'; lhs[1] = 'n'; lhs[2] = 'd'; lhs[3] = '_';
		rhs[0] = 'f'; rhs[1] = 'n'; rhs[2] = 'd'; rhs[3] = '_';

		for( k = 4; k < len; ++k )
			lhs[k] = random32_range( 32, 128 );
		lhs[len] = 0;

		lhash = hash( lhs, len );
		EXPECT_NE( lhash, 0U );

		for( j = 0; j < 128000; ++j )
		{
			for( k = 4; k < len; ++k )
				rhs[k] = random32_range( 32, 128 );
			rhs[len] = 0;

			rhashref = hash( rhs, len );
			rhash = hash( rhs, len );

			EXPECT_EQ( rhashref, rhash );
			if( !string_equal_substr( lhs, rhs, len ) )
				EXPECT_NE( lhash, rhash );
			EXPECT_NE( rhash, 0U );
		}
	}
	return 0;
}