Skip to content

Commit ccfc958

Browse files
committed
test: dtls13: add ack test
1 parent 1c83e24 commit ccfc958

3 files changed

Lines changed: 123 additions & 1 deletion

File tree

tests/api/test_dtls.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,124 @@ int test_dtls13_ack_order(void)
939939
return EXPECT_RESULT();
940940
}
941941

942+
int test_dtls13_ack_overflow(void)
943+
{
944+
EXPECT_DECLS;
945+
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
946+
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
947+
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
948+
struct test_memio_ctx test_ctx;
949+
unsigned char readBuf[50];
950+
word32 length = 0;
951+
int i;
952+
953+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
954+
955+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
956+
wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
957+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
958+
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)), -1);
959+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
960+
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), -1);
961+
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
962+
963+
/* Edge case 1: one below limit - all inserts must succeed */
964+
for (i = 0; i < DTLS13_ACK_MAX_RECORDS - 1; i++) {
965+
ExpectIntEQ(Dtls13RtxAddAck(ssl_c, w64From32(0, 0),
966+
w64From32(0, (word32)i)), 0);
967+
}
968+
ExpectIntEQ(ssl_c->dtls13Rtx.seenRecordsCount, DTLS13_ACK_MAX_RECORDS - 1);
969+
970+
/* Edge case 2: insert the last allowed record - must succeed */
971+
ExpectIntEQ(Dtls13RtxAddAck(ssl_c, w64From32(0, 0),
972+
w64From32(0, (word32)(DTLS13_ACK_MAX_RECORDS - 1))), 0);
973+
ExpectIntEQ(ssl_c->dtls13Rtx.seenRecordsCount, DTLS13_ACK_MAX_RECORDS);
974+
975+
/* Writing a full-but-valid list must succeed */
976+
ExpectIntEQ(Dtls13WriteAckMessage(ssl_c, ssl_c->dtls13Rtx.seenRecords,
977+
ssl_c->dtls13Rtx.seenRecordsCount, &length), 0);
978+
979+
/* Edge case 3: one over limit - must be silently dropped */
980+
ExpectIntEQ(Dtls13RtxAddAck(ssl_c, w64From32(0, 0),
981+
w64From32(0, (word32)DTLS13_ACK_MAX_RECORDS)), 0);
982+
ExpectIntEQ(ssl_c->dtls13Rtx.seenRecordsCount, DTLS13_ACK_MAX_RECORDS);
983+
984+
/* Bypass the insert guard to force the list one element over the limit,
985+
* then verify Dtls13WriteAckMessage errors out instead of overflowing */
986+
ssl_c->dtls13Rtx.seenRecordsCount = 0;
987+
ExpectIntEQ(Dtls13RtxAddAck(ssl_c, w64From32(0, 1),
988+
w64From32(0, (word32)DTLS13_ACK_MAX_RECORDS)), 0);
989+
ssl_c->dtls13Rtx.seenRecordsCount = (word16)(DTLS13_ACK_MAX_RECORDS + 1);
990+
ExpectIntEQ(Dtls13WriteAckMessage(ssl_c, ssl_c->dtls13Rtx.seenRecords,
991+
ssl_c->dtls13Rtx.seenRecordsCount, &length), BUFFER_E);
992+
993+
wolfSSL_free(ssl_c);
994+
wolfSSL_CTX_free(ctx_c);
995+
wolfSSL_free(ssl_s);
996+
wolfSSL_CTX_free(ctx_s);
997+
#endif
998+
return EXPECT_RESULT();
999+
}
1000+
1001+
int test_dtls13_ack_dup_write_counter(void)
1002+
{
1003+
EXPECT_DECLS;
1004+
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
1005+
&& defined(HAVE_WRITE_DUP)
1006+
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
1007+
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
1008+
WOLFSSL *ssl_c2 = NULL;
1009+
struct test_memio_ctx test_ctx;
1010+
unsigned char readBuf[50];
1011+
int i;
1012+
1013+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
1014+
1015+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
1016+
wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
1017+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
1018+
/* Drain any post-handshake messages */
1019+
ExpectIntEQ(wolfSSL_read(ssl_c, readBuf, sizeof(readBuf)), -1);
1020+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
1021+
ExpectIntEQ(wolfSSL_read(ssl_s, readBuf, sizeof(readBuf)), -1);
1022+
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
1023+
1024+
/* Split ssl_c: ssl_c becomes READ_DUP_SIDE, ssl_c2 becomes WRITE_DUP_SIDE */
1025+
ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
1026+
1027+
/* Cycle 1: add records, trigger handoff, verify counter is reset to 0 */
1028+
for (i = 0; i < 5; i++)
1029+
ExpectIntEQ(Dtls13RtxAddAck(ssl_c, w64From32(0, 3),
1030+
w64From32(0, (word32)i)), 0);
1031+
ExpectIntEQ(ssl_c->dtls13Rtx.seenRecordsCount, 5);
1032+
ssl_c->dtls13Rtx.sendAcks = 1;
1033+
ExpectIntEQ(Dtls13DoScheduledWork(ssl_c), 0);
1034+
/* seenRecords ownership was transferred to dupWrite->sendAckList;
1035+
* seenRecordsCount must be reset to 0 — not left at 5. */
1036+
ExpectNull(ssl_c->dtls13Rtx.seenRecords);
1037+
ExpectIntEQ(ssl_c->dtls13Rtx.seenRecordsCount, 0);
1038+
1039+
/* Cycle 2 (different epoch to avoid the dup-filter): verify the counter
1040+
* did not accumulate across the previous transfer. Without the fix,
1041+
* seenRecordsCount would now be 10 after this second batch. */
1042+
for (i = 0; i < 5; i++)
1043+
ExpectIntEQ(Dtls13RtxAddAck(ssl_c, w64From32(0, 4),
1044+
w64From32(0, (word32)i)), 0);
1045+
ExpectIntEQ(ssl_c->dtls13Rtx.seenRecordsCount, 5);
1046+
ssl_c->dtls13Rtx.sendAcks = 1;
1047+
ExpectIntEQ(Dtls13DoScheduledWork(ssl_c), 0);
1048+
ExpectNull(ssl_c->dtls13Rtx.seenRecords);
1049+
ExpectIntEQ(ssl_c->dtls13Rtx.seenRecordsCount, 0);
1050+
1051+
wolfSSL_free(ssl_c);
1052+
wolfSSL_free(ssl_c2);
1053+
wolfSSL_CTX_free(ctx_c);
1054+
wolfSSL_free(ssl_s);
1055+
wolfSSL_CTX_free(ctx_s);
1056+
#endif
1057+
return EXPECT_RESULT();
1058+
}
1059+
9421060
int test_dtls_version_checking(void)
9431061
{
9441062
EXPECT_DECLS;

tests/api/test_dtls.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ int test_wolfSSL_dtls_cid_parse(void);
3030
int test_wolfSSL_dtls_set_pending_peer(void);
3131
int test_dtls13_epochs(void);
3232
int test_dtls13_ack_order(void);
33+
int test_dtls13_ack_overflow(void);
34+
int test_dtls13_ack_dup_write_counter(void);
3335
int test_dtls_version_checking(void);
3436
int test_dtls_short_ciphertext(void);
3537
int test_dtls12_record_length_mismatch(void);
@@ -60,6 +62,8 @@ int test_dtls13_min_rtx_interval(void);
6062
TEST_DECL_GROUP("dtls", test_wolfSSL_dtls_set_pending_peer), \
6163
TEST_DECL_GROUP("dtls", test_dtls13_epochs), \
6264
TEST_DECL_GROUP("dtls", test_dtls13_ack_order), \
65+
TEST_DECL_GROUP("dtls", test_dtls13_ack_overflow), \
66+
TEST_DECL_GROUP("dtls", test_dtls13_ack_dup_write_counter), \
6367
TEST_DECL_GROUP("dtls", test_dtls_version_checking), \
6468
TEST_DECL_GROUP("dtls", test_dtls_short_ciphertext), \
6569
TEST_DECL_GROUP("dtls", test_dtls12_record_length_mismatch), \

wolfssl/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7244,7 +7244,7 @@ WOLFSSL_LOCAL int Dtls13GetSeq(WOLFSSL* ssl, int order, word32* seq,
72447244
byte increment);
72457245
WOLFSSL_LOCAL void Dtls13RtxRemoveRecord(WOLFSSL* ssl, w64wrapper epoch,
72467246
w64wrapper seq);
7247-
WOLFSSL_LOCAL int Dtls13DoScheduledWork(WOLFSSL* ssl);
7247+
WOLFSSL_TEST_VIS int Dtls13DoScheduledWork(WOLFSSL* ssl);
72487248
WOLFSSL_LOCAL int Dtls13DeriveSnKeys(WOLFSSL* ssl, int provision);
72497249
WOLFSSL_LOCAL int Dtls13SetRecordNumberKeys(WOLFSSL* ssl,
72507250
enum encrypt_side side);

0 commit comments

Comments
 (0)