Skip to content

Commit 1bb45f5

Browse files
committed
test: dtls network boundaires: additional test case
1 parent 6221ade commit 1bb45f5

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

tests/api.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67857,6 +67857,8 @@ TEST_CASE testCases[] = {
6785767857
TEST_DECL(test_dtls12_short_read),
6785867858
TEST_DECL(test_dtls13_longer_length),
6785967859
TEST_DECL(test_dtls13_short_read),
67860+
TEST_DECL(test_records_span_network_boundaries),
67861+
TEST_DECL(test_dtls_record_cross_boundaries),
6786067862
TEST_DECL(test_wolfSSL_SCR_after_resumption),
6786167863
TEST_DECL(test_dtls_no_extensions),
6786267864
TEST_DECL(test_tls_alert_no_server_hello),

tests/api/test_dtls.c

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,11 +994,78 @@ int test_dtls12_record_length_mismatch(void)
994994

995995
return EXPECT_RESULT();
996996
}
997+
998+
int test_dtls_record_cross_boundaries(void)
999+
{
1000+
EXPECT_DECLS;
1001+
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
1002+
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
1003+
struct test_memio_ctx test_ctx;
1004+
unsigned char readBuf[100];
1005+
int rec0_len, rec1_len;
1006+
1007+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
1008+
1009+
/* Setup DTLS contexts */
1010+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
1011+
wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method),
1012+
0);
1013+
1014+
/* Complete handshake */
1015+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
1016+
1017+
/* create a first record in the buffer */
1018+
wolfSSL_SetLoggingPrefix("client");
1019+
ExpectIntEQ(wolfSSL_write(ssl_c, "test0", 5), 5);
1020+
rec0_len = test_ctx.s_msg_sizes[0];
1021+
1022+
/* create a second record in the buffer */
1023+
ExpectIntEQ(wolfSSL_write(ssl_c, "test1", 5), 5);
1024+
rec1_len = test_ctx.s_msg_sizes[1];
1025+
1026+
ExpectIntLE(rec0_len + rec1_len, sizeof(readBuf));
1027+
XMEMCPY(readBuf, test_ctx.s_buff, rec0_len + rec1_len);
1028+
1029+
/* clear buffer */
1030+
test_memio_clear_buffer(&test_ctx, 0);
1031+
1032+
/* inject first record + 1 bytes of second record */
1033+
ExpectIntEQ(test_memio_inject_message(&test_ctx, 0, (const char*)readBuf,
1034+
rec0_len + 1),
1035+
0);
1036+
1037+
/* inject second record */
1038+
ExpectIntEQ(test_memio_inject_message(&test_ctx, 0,
1039+
(const char*)readBuf + rec0_len + 1, rec1_len - 1),
1040+
0);
1041+
ExpectIntEQ(test_ctx.s_len, rec0_len + rec1_len);
1042+
1043+
/* reading the record should return just the first message*/
1044+
wolfSSL_SetLoggingPrefix("server");
1045+
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), 5);
1046+
ExpectBufEQ(readBuf, "test0", 5);
1047+
1048+
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)),
1049+
WOLFSSL_FATAL_ERROR);
1050+
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
1051+
1052+
/* cleanup */
1053+
wolfSSL_free(ssl_s);
1054+
wolfSSL_free(ssl_c);
1055+
wolfSSL_CTX_free(ctx_s);
1056+
wolfSSL_CTX_free(ctx_c);
1057+
1058+
return EXPECT_RESULT();
1059+
}
9971060
#else
9981061
int test_dtls12_record_length_mismatch(void)
9991062
{
10001063
return TEST_SKIPPED;
10011064
}
1065+
int test_dtls_record_cross_boundaries(void)
1066+
{
1067+
return TEST_SKIPPED;
1068+
}
10021069
#endif /* !defined(WOLFSSL_DTLS_RECORDS_CAN_SPAN_DATAGRAMS) */
10031070

10041071
int test_dtls_short_ciphertext(void)
@@ -1065,4 +1132,84 @@ int test_dtls13_longer_length(void)
10651132
{
10661133
return TEST_SKIPPED;
10671134
}
1135+
int test_dtls_record_cross_boundaries(void)
1136+
{
1137+
return TEST_SKIPPED;
1138+
}
10681139
#endif /* defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) */
1140+
1141+
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
1142+
/* This test that the DTLS record boundary check doesn't interfere with TLS
1143+
* records processing */
1144+
int test_records_span_network_boundaries(void)
1145+
{
1146+
EXPECT_DECLS;
1147+
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
1148+
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
1149+
struct test_memio_ctx test_ctx;
1150+
unsigned char readBuf[50];
1151+
int record_len;
1152+
1153+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
1154+
1155+
/* Setup DTLS contexts */
1156+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
1157+
wolfTLSv1_2_client_method, wolfTLSv1_2_server_method),
1158+
0);
1159+
1160+
/* Complete handshake */
1161+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
1162+
1163+
/* create a good record in the buffer */
1164+
wolfSSL_SetLoggingPrefix("client");
1165+
ExpectIntEQ(wolfSSL_write(ssl_c, "test", 4), 4);
1166+
ExpectIntLE(test_ctx.s_len, 50);
1167+
ExpectIntGT(test_ctx.s_len, 10);
1168+
record_len = test_ctx.s_len;
1169+
XMEMCPY(readBuf, test_ctx.s_buff, record_len);
1170+
1171+
/* drop record and simulate a split write */
1172+
ExpectIntEQ(test_memio_drop_message(&test_ctx, 0, 0), 0);
1173+
ExpectIntEQ(test_ctx.s_msg_count, 0);
1174+
1175+
/* inject first record header */
1176+
ExpectIntEQ(
1177+
test_memio_inject_message(&test_ctx, 0, (const char*)readBuf, 5), 0);
1178+
ExpectIntEQ(test_ctx.s_msg_count, 1);
1179+
ExpectIntEQ(test_ctx.s_msg_sizes[0], 5);
1180+
1181+
/* inject another 5 bytes of the record */
1182+
ExpectIntEQ(
1183+
test_memio_inject_message(&test_ctx, 0, (const char*)readBuf + 5, 5),
1184+
0);
1185+
ExpectIntEQ(test_ctx.s_msg_count, 2);
1186+
ExpectIntEQ(test_ctx.s_msg_sizes[1], 5);
1187+
1188+
/* inject the rest of the record */
1189+
ExpectIntEQ(test_memio_inject_message(&test_ctx, 0,
1190+
(const char*)readBuf + 10, record_len - 10),
1191+
0);
1192+
ExpectIntEQ(test_ctx.s_msg_count, 3);
1193+
ExpectIntEQ(test_ctx.s_msg_sizes[2], record_len - 10);
1194+
1195+
/* read the record */
1196+
wolfSSL_SetLoggingPrefix("server");
1197+
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), 4);
1198+
ExpectIntEQ(test_ctx.s_len, 0);
1199+
1200+
ExpectBufEQ(readBuf, "test", 4);
1201+
1202+
wolfSSL_free(ssl_s);
1203+
wolfSSL_free(ssl_c);
1204+
wolfSSL_CTX_free(ctx_s);
1205+
wolfSSL_CTX_free(ctx_c);
1206+
1207+
return EXPECT_RESULT();
1208+
}
1209+
#else
1210+
int test_records_span_network_boundaries(void)
1211+
{
1212+
return TEST_SKIPPED;
1213+
}
1214+
#endif /* defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
1215+
!defined(WOLFSSL_NO_TLS12) */

tests/api/test_dtls.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ int test_dtls12_record_length_mismatch(void);
3232
int test_dtls12_short_read(void);
3333
int test_dtls13_longer_length(void);
3434
int test_dtls13_short_read(void);
35+
int test_records_span_network_boundaries(void);
36+
int test_dtls_record_cross_boundaries(void);
3537
#endif /* TESTS_API_DTLS_H */

0 commit comments

Comments
 (0)