Add non-blocking mock TCP callbacks to SSL tests

This commit is contained in:
Janos Follath 2019-11-27 13:31:42 +00:00
parent 031827feba
commit 3766ba50de
2 changed files with 174 additions and 31 deletions

View file

@ -31,8 +31,20 @@ test_callback_buffer:50:50:50:0:0:10:0:60:50
Callback buffer test: Reading from empty buffer Callback buffer test: Reading from empty buffer
test_callback_buffer:50:0:0:10:0:0:0:0:0 test_callback_buffer:50:0:0:10:0:0:0:0:0
Test mock TCP connection Test mock blocking TCP connection
ssl_mock_tcp: ssl_mock_tcp:1:0:0
Test mock non-blocking TCP connection: would not block
ssl_mock_tcp:0:0:0
Test mock non-blocking TCP connection: client would block
ssl_mock_tcp:0:0xB509:0
Test mock non-blocking TCP connection: server would block
ssl_mock_tcp:0:0x0FB1:0
Test mock non-blocking TCP connection: both peers would block
ssl_mock_tcp:0:0x1111:0xEEEE
SSL DTLS replay: initial state, seqnum 0 SSL DTLS replay: initial state, seqnum 0
ssl_dtls_replay:"":"000000000000":0 ssl_dtls_replay:"":"000000000000":0

View file

@ -146,6 +146,7 @@ int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
typedef struct mbedtls_mock_socket typedef struct mbedtls_mock_socket
{ {
int status; int status;
uint32_t blocking_pattern;
mbedtls_test_buffer *input; mbedtls_test_buffer *input;
mbedtls_test_buffer *output; mbedtls_test_buffer *output;
struct mbedtls_mock_socket *peer; struct mbedtls_mock_socket *peer;
@ -254,6 +255,26 @@ exit:
return ret; return ret;
} }
/*
* Set the blocking pattern for the socket.
*
* For every bit of \p blocking_pattern set to one the socket will simulate a
* "would block" event. The bits are processed starting with the least
* significant bit and every call to a non-blocking I/O function consumes one.
*
* The behaviour of blocking I/O functions remains unchanged.
*/
int mbedtls_mock_socket_set_block( mbedtls_mock_socket* socket,
uint32_t blocking_pattern )
{
if( socket == NULL )
return -1;
socket->blocking_pattern = blocking_pattern;
return 0;
}
/* /*
* Callbacks for simulating blocking I/O over connection-oriented transport. * Callbacks for simulating blocking I/O over connection-oriented transport.
*/ */
@ -278,6 +299,46 @@ int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
return mbedtls_test_buffer_get( socket->input, buf, len ); return mbedtls_test_buffer_get( socket->input, buf, len );
} }
/*
* Callbacks for simulating non-blocking I/O over connection-oriented transport.
*/
int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
{
mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
return -1;
if( socket->blocking_pattern & 1 )
{
socket->blocking_pattern >>= 1;
return MBEDTLS_ERR_SSL_WANT_WRITE;
}
socket->blocking_pattern >>= 1;
return mbedtls_test_buffer_put( socket->output, buf, len );
}
int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
{
mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
return -1;
if( socket->blocking_pattern & 1 )
{
socket->blocking_pattern >>= 1;
return MBEDTLS_ERR_SSL_WANT_READ;
}
socket->blocking_pattern >>= 1;
return mbedtls_test_buffer_get( socket->input, buf, len );
}
/* /*
* Helper function setting up inverse record transformations * Helper function setting up inverse record transformations
* using given cipher, hash, EtM mode, authentication tag length, * using given cipher, hash, EtM mode, authentication tag length,
@ -811,7 +872,7 @@ exit:
*/ */
/* BEGIN_CASE */ /* BEGIN_CASE */
void ssl_mock_tcp() void ssl_mock_tcp( int blocking, int client_pattern, int server_pattern )
{ {
enum { ROUNDS = 2 }; enum { ROUNDS = 2 };
enum { MSGLEN = 105 }; enum { MSGLEN = 105 };
@ -824,6 +885,21 @@ void ssl_mock_tcp()
int send_ret[ROUNDS]; int send_ret[ROUNDS];
int recv_ret[ROUNDS]; int recv_ret[ROUNDS];
unsigned i, j, progress; unsigned i, j, progress;
mbedtls_ssl_send_t *send;
mbedtls_ssl_recv_t *recv;
uint32_t client_block = client_pattern;
uint32_t server_block = server_pattern;
if( blocking == 0 )
{
send = mbedtls_mock_tcp_send_nb;
recv = mbedtls_mock_tcp_recv_nb;
}
else
{
send = mbedtls_mock_tcp_send_b;
recv = mbedtls_mock_tcp_recv_b;
}
mbedtls_mock_socket_init( &client ); mbedtls_mock_socket_init( &client );
mbedtls_mock_socket_init( &server ); mbedtls_mock_socket_init( &server );
@ -839,29 +915,46 @@ void ssl_mock_tcp()
} }
/* Try sending or receiving on an unconnected socket */ /* Try sending or receiving on an unconnected socket */
TEST_ASSERT( mbedtls_mock_tcp_send_b( &client, message[0], MSGLEN ) < 0 ); TEST_ASSERT( send( &client, message[0], MSGLEN ) < 0 );
TEST_ASSERT( mbedtls_mock_tcp_recv_b( &client, received[0], MSGLEN ) < 0 ); TEST_ASSERT( recv( &client, received[0], MSGLEN ) < 0 );
/* Make sure that sending a message takes a few iterations. */ /* Make sure that sending a message takes a few iterations. */
TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
MSGLEN / 5 ) ); MSGLEN / 5 ) );
TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &client, client_block ) );
TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &server, server_block ) );
/* Send the message to the server */ /* Send the message to the server */
send_ret[0] = recv_ret[0] = 1; send_ret[0] = recv_ret[0] = 1;
written[0] = read[0] = 0; written[0] = read[0] = 0;
while( send_ret[0] != 0 || recv_ret[0] != 0 ) while( send_ret[0] != 0 || recv_ret[0] != 0 )
{ {
send_ret[0] = mbedtls_mock_tcp_send_b( &client, send_ret[0] = send( &client, message[0] + written[0],
message[0] + written[0],
MSGLEN - written[0] ); MSGLEN - written[0] );
TEST_ASSERT( send_ret[0] >= 0 );
written[0] += send_ret[0];
recv_ret[0] = mbedtls_mock_tcp_recv_b( &server, if( ( blocking == 0 ) && ( client_block & 1 ) )
received[0] + read[0], {
TEST_ASSERT( send_ret[0] == MBEDTLS_ERR_SSL_WANT_WRITE );
}
else
{
TEST_ASSERT( send_ret[0] >= 0 );
written[0] += send_ret[0];
}
client_block >>= 1;
recv_ret[0] = recv( &server, received[0] + read[0],
MSGLEN - read[0] ); MSGLEN - read[0] );
TEST_ASSERT( recv_ret[0] >= 0 ); if( ( blocking == 0 ) && ( server_block & 1 ) )
read[0] += recv_ret[0]; {
TEST_ASSERT( recv_ret[0] == MBEDTLS_ERR_SSL_WANT_READ );
}
else
{
TEST_ASSERT( recv_ret[0] >= 0 );
read[0] += recv_ret[0];
}
server_block >>= 1;
} }
TEST_ASSERT( memcmp( message[0], received[0], MSGLEN ) == 0 ); TEST_ASSERT( memcmp( message[0], received[0], MSGLEN ) == 0 );
@ -873,6 +966,10 @@ void ssl_mock_tcp()
/* Make sure that sending a message takes a few iterations. */ /* Make sure that sending a message takes a few iterations. */
TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
MSGLEN / 5 ) ); MSGLEN / 5 ) );
client_block = client_pattern;
server_block = server_pattern;
TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &client, client_block ) );
TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &server, server_block ) );
/* Send the message from both sides, interleaving. */ /* Send the message from both sides, interleaving. */
progress = 1; progress = 1;
@ -885,38 +982,72 @@ void ssl_mock_tcp()
* of at least one byte on either side. */ * of at least one byte on either side. */
while( progress != 0 ) while( progress != 0 )
{ {
send_ret[0] = mbedtls_mock_tcp_send_b( &client, send_ret[0] = send( &client, message[0] + written[0],
message[0] + written[0],
MSGLEN - written[0] ); MSGLEN - written[0] );
TEST_ASSERT( send_ret[0] >= 0 ); if( ( blocking == 0 ) && ( client_block & 1 ) )
written[0] += send_ret[0]; {
TEST_ASSERT( send_ret[0] == MBEDTLS_ERR_SSL_WANT_WRITE );
}
else
{
TEST_ASSERT( send_ret[0] >= 0 );
written[0] += send_ret[0];
}
client_block >>= 1;
send_ret[1] = mbedtls_mock_tcp_send_b( &server, send_ret[1] = send( &server, message[1] + written[1],
message[1] + written[1],
MSGLEN - written[1] ); MSGLEN - written[1] );
TEST_ASSERT( send_ret[1] >= 0 ); if( ( blocking == 0 ) && ( server_block & 1 ) )
written[1] += send_ret[1]; {
TEST_ASSERT( send_ret[1] == MBEDTLS_ERR_SSL_WANT_WRITE );
}
else
{
TEST_ASSERT( send_ret[1] >= 0 );
written[1] += send_ret[1];
}
server_block >>= 1;
recv_ret[0] = mbedtls_mock_tcp_recv_b( &server, recv_ret[0] = recv( &server, received[0] + read[0],
received[0] + read[0],
MSGLEN - read[0] ); MSGLEN - read[0] );
TEST_ASSERT( recv_ret[0] >= 0 ); if( ( blocking == 0 ) && ( server_block & 1 ) )
read[0] += recv_ret[0]; {
TEST_ASSERT( recv_ret[0] == MBEDTLS_ERR_SSL_WANT_READ );
}
else
{
TEST_ASSERT( recv_ret[0] >= 0 );
read[0] += recv_ret[0];
}
server_block >>= 1;
recv_ret[1] = mbedtls_mock_tcp_recv_b( &client, recv_ret[1] = recv( &client, received[1] + read[1],
received[1] + read[1],
MSGLEN - read[1] ); MSGLEN - read[1] );
TEST_ASSERT( recv_ret[1] >= 0 ); if( ( blocking == 0 ) && ( client_block & 1 ) )
read[1] += recv_ret[1]; {
TEST_ASSERT( recv_ret[1] == MBEDTLS_ERR_SSL_WANT_READ );
}
else
{
TEST_ASSERT( recv_ret[1] >= 0 );
read[1] += recv_ret[1];
}
client_block >>= 1;
progress = 0; progress = 0;
for( i = 0; i < ROUNDS; i++ ) for( i = 0; i < ROUNDS; i++ )
{ {
if( send_ret[i] > 0 ) if( ( send_ret[i] > 0 ) ||
( send_ret[i] == MBEDTLS_ERR_SSL_WANT_WRITE ) )
{
progress++; progress++;
}
if( recv_ret[i] > 0 ) if( ( recv_ret[i] > 0 ) ||
( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ) )
{
progress++; progress++;
}
} }
} }