Skip to content

Commit e9f3bd5

Browse files
committed
dtls: test precise header headroom computation
1 parent 5512c2d commit e9f3bd5

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

tests/api/test_dtls.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,119 @@ int test_records_span_network_boundaries(void)
14871487
#endif /* defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
14881488
!defined(WOLFSSL_NO_TLS12) */
14891489

1490+
int test_dtls_mtu_fragment_headroom(void)
1491+
{
1492+
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS_MTU) && \
1493+
defined(HAVE_AESGCM) && defined(HAVE_ECC)
1494+
EXPECT_DECLS;
1495+
struct {
1496+
method_provider client_meth;
1497+
method_provider server_meth;
1498+
const char* cipher;
1499+
int use_cid;
1500+
} params[] = {
1501+
#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_TLS13)
1502+
{ wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
1503+
"TLS13-AES128-GCM-SHA256", 0 },
1504+
#ifdef WOLFSSL_DTLS_CID
1505+
{ wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
1506+
"TLS13-AES128-GCM-SHA256", 1 },
1507+
#endif
1508+
#endif
1509+
#if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
1510+
{ wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
1511+
"ECDHE-RSA-AES128-GCM-SHA256", 0 },
1512+
#ifdef WOLFSSL_DTLS_CID
1513+
{ wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
1514+
"ECDHE-RSA-AES128-GCM-SHA256", 1 },
1515+
#endif
1516+
#if !defined(WOLFSSL_AEAD_ONLY) && !defined(NO_AES) && !defined(NO_SHA)
1517+
{ wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
1518+
"ECDHE-RSA-AES128-SHA", 0 },
1519+
#endif
1520+
#endif
1521+
};
1522+
size_t i;
1523+
1524+
for (i = 0; i < XELEM_CNT(params) && EXPECT_SUCCESS(); i++) {
1525+
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
1526+
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
1527+
struct test_memio_ctx test_ctx;
1528+
unsigned char payload[33];
1529+
word16 mtu;
1530+
int recordLen;
1531+
int overhead;
1532+
int ret;
1533+
1534+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
1535+
1536+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
1537+
params[i].client_meth, params[i].server_meth),
1538+
0);
1539+
1540+
ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, params[i].cipher), 1);
1541+
ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, params[i].cipher), 1);
1542+
1543+
#ifdef WOLFSSL_DTLS_CID
1544+
if (params[i].use_cid) {
1545+
unsigned char cid_c[] = { 0,1,2,3 };
1546+
unsigned char cid_s[] = { 4,5,6,7,8,9 };
1547+
ExpectIntEQ(wolfSSL_dtls_cid_use(ssl_c), 1);
1548+
ExpectIntEQ(wolfSSL_dtls_cid_use(ssl_s), 1);
1549+
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_c, cid_s, (int)sizeof(cid_s)),
1550+
1);
1551+
ExpectIntEQ(wolfSSL_dtls_cid_set(ssl_s, cid_c, (int)sizeof(cid_c)),
1552+
1);
1553+
}
1554+
#endif
1555+
1556+
/* Complete handshake and clear any leftover records. */
1557+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
1558+
test_memio_clear_buffer(&test_ctx, 1);
1559+
test_memio_clear_buffer(&test_ctx, 0);
1560+
1561+
/* Measure application-data record overhead. */
1562+
ExpectIntEQ(wolfSSL_write(ssl_c, "A", 1), 1);
1563+
ExpectIntEQ(test_ctx.s_msg_count, 1);
1564+
recordLen = test_ctx.s_len;
1565+
ExpectIntGT(recordLen, 1);
1566+
overhead = recordLen - 1;
1567+
1568+
/* Reset buffers before MTU-limited send. */
1569+
test_memio_clear_buffer(&test_ctx, 0);
1570+
test_memio_clear_buffer(&test_ctx, 1);
1571+
1572+
/* Set MTU to overhead + 32 bytes of payload. */
1573+
mtu = (word16)(overhead + 32);
1574+
ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, mtu), WOLFSSL_SUCCESS);
1575+
ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, mtu), WOLFSSL_SUCCESS);
1576+
1577+
/* With the tightened MTU, we should still be able to send 32 bytes. */
1578+
ExpectIntEQ(wolfSSL_write(ssl_c, payload, 32), 32);
1579+
ExpectIntEQ(test_ctx.s_msg_count, 1);
1580+
recordLen = test_ctx.s_len;
1581+
ExpectIntEQ(recordLen, overhead + 32);
1582+
ExpectIntLE(recordLen, mtu);
1583+
1584+
/* Underestimation: drop MTU by 1 and expect DTLS_SIZE_ERROR. */
1585+
test_memio_clear_buffer(&test_ctx, 0);
1586+
test_memio_clear_buffer(&test_ctx, 1);
1587+
ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, mtu - 1), WOLFSSL_SUCCESS);
1588+
ret = wolfSSL_write(ssl_c, payload, 32);
1589+
ExpectIntNE(ret, 32);
1590+
ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), DTLS_SIZE_ERROR);
1591+
1592+
wolfSSL_free(ssl_c);
1593+
wolfSSL_CTX_free(ctx_c);
1594+
wolfSSL_free(ssl_s);
1595+
wolfSSL_CTX_free(ctx_s);
1596+
}
1597+
return EXPECT_RESULT();
1598+
#else
1599+
return TEST_SKIPPED;
1600+
#endif
1601+
}
1602+
14901603
int test_dtls_rtx_across_epoch_change(void)
14911604
{
14921605
EXPECT_DECLS;

tests/api/test_dtls.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ int test_dtls_timeout(void);
4747
int test_dtls_certreq_order(void);
4848
int test_dtls_memio_wolfio(void);
4949
int test_dtls_memio_wolfio_stateless(void);
50+
int test_dtls_mtu_fragment_headroom(void);
5051

5152
#define TEST_DTLS_DECLS \
5253
TEST_DECL_GROUP("dtls", test_dtls12_basic_connection_id), \
@@ -73,5 +74,6 @@ int test_dtls_memio_wolfio_stateless(void);
7374
TEST_DECL_GROUP("dtls", test_dtls_certreq_order), \
7475
TEST_DECL_GROUP("dtls", test_dtls_timeout), \
7576
TEST_DECL_GROUP("dtls", test_dtls_memio_wolfio), \
77+
TEST_DECL_GROUP("dtls", test_dtls_mtu_fragment_headroom), \
7678
TEST_DECL_GROUP("dtls", test_dtls_memio_wolfio_stateless)
7779
#endif /* TESTS_API_DTLS_H */

0 commit comments

Comments
 (0)