Add non-blocking mock TCP callbacks to SSL tests
diff --git a/tests/suites/test_suite_ssl.function b/tests/suites/test_suite_ssl.function
index 67ddd06..0ee6652 100644
--- a/tests/suites/test_suite_ssl.function
+++ b/tests/suites/test_suite_ssl.function
@@ -146,6 +146,7 @@
typedef struct mbedtls_mock_socket
{
int status;
+ uint32_t blocking_pattern;
mbedtls_test_buffer *input;
mbedtls_test_buffer *output;
struct mbedtls_mock_socket *peer;
@@ -255,6 +256,26 @@
}
/*
+ * 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.
*/
@@ -279,6 +300,46 @@
}
/*
+ * 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
* using given cipher, hash, EtM mode, authentication tag length,
* and version.
@@ -811,7 +872,7 @@
*/
/* BEGIN_CASE */
-void ssl_mock_tcp()
+void ssl_mock_tcp( int blocking, int client_pattern, int server_pattern )
{
enum { ROUNDS = 2 };
enum { MSGLEN = 105 };
@@ -824,6 +885,21 @@
int send_ret[ROUNDS];
int recv_ret[ROUNDS];
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( &server );
@@ -839,29 +915,46 @@
}
/* Try sending or receiving on an unconnected socket */
- TEST_ASSERT( mbedtls_mock_tcp_send_b( &client, message[0], MSGLEN ) < 0 );
- TEST_ASSERT( mbedtls_mock_tcp_recv_b( &client, received[0], MSGLEN ) < 0 );
+ TEST_ASSERT( send( &client, message[0], MSGLEN ) < 0 );
+ TEST_ASSERT( recv( &client, received[0], MSGLEN ) < 0 );
/* Make sure that sending a message takes a few iterations. */
TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
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_ret[0] = recv_ret[0] = 1;
written[0] = read[0] = 0;
while( send_ret[0] != 0 || recv_ret[0] != 0 )
{
- send_ret[0] = mbedtls_mock_tcp_send_b( &client,
- message[0] + written[0],
+ send_ret[0] = send( &client, message[0] + 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,
- received[0] + read[0],
+ if( ( blocking == 0 ) && ( client_block & 1 ) )
+ {
+ 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] );
- TEST_ASSERT( recv_ret[0] >= 0 );
- read[0] += recv_ret[0];
+ if( ( blocking == 0 ) && ( server_block & 1 ) )
+ {
+ 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 );
@@ -873,6 +966,10 @@
/* Make sure that sending a message takes a few iterations. */
TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
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. */
progress = 1;
@@ -885,38 +982,72 @@
* of at least one byte on either side. */
while( progress != 0 )
{
- send_ret[0] = mbedtls_mock_tcp_send_b( &client,
- message[0] + written[0],
+ send_ret[0] = send( &client, message[0] + written[0],
MSGLEN - written[0] );
- TEST_ASSERT( send_ret[0] >= 0 );
- written[0] += send_ret[0];
+ if( ( blocking == 0 ) && ( client_block & 1 ) )
+ {
+ 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,
- message[1] + written[1],
+ send_ret[1] = send( &server, message[1] + written[1],
MSGLEN - written[1] );
- TEST_ASSERT( send_ret[1] >= 0 );
- written[1] += send_ret[1];
+ if( ( blocking == 0 ) && ( server_block & 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,
- received[0] + read[0],
+ recv_ret[0] = recv( &server, received[0] + read[0],
MSGLEN - read[0] );
- TEST_ASSERT( recv_ret[0] >= 0 );
- read[0] += recv_ret[0];
+ if( ( blocking == 0 ) && ( server_block & 1 ) )
+ {
+ 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,
- received[1] + read[1],
+ recv_ret[1] = recv( &client, received[1] + read[1],
MSGLEN - read[1] );
- TEST_ASSERT( recv_ret[1] >= 0 );
- read[1] += recv_ret[1];
+ if( ( blocking == 0 ) && ( client_block & 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;
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++;
+ }
- if( recv_ret[i] > 0 )
+ if( ( recv_ret[i] > 0 ) ||
+ ( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ) )
+ {
progress++;
+ }
}
}