Skip to content

Commit 5f250bf

Browse files
committed
test: tls13_early_data: test WANT_WRITE in early data
1 parent 0d06061 commit 5f250bf

File tree

1 file changed

+162
-29
lines changed

1 file changed

+162
-29
lines changed

tests/api/test_tls13.c

Lines changed: 162 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,101 @@ int test_tls13_pq_groups(void)
19921992
return EXPECT_RESULT();
19931993
}
19941994

1995+
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
1996+
defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
1997+
static int test_tls13_read_until_write_ok(WOLFSSL* ssl, void* buf, int bufLen)
1998+
{
1999+
int ret, err;
2000+
int tries = 5;
2001+
2002+
err = 0;
2003+
do {
2004+
ret = wolfSSL_read(ssl, buf, bufLen);
2005+
if (ret == WOLFSSL_FATAL_ERROR) {
2006+
err = wolfSSL_get_error(ssl, ret);
2007+
}
2008+
} while (tries-- && ret == WOLFSSL_FATAL_ERROR &&
2009+
err == WOLFSSL_ERROR_WANT_WRITE);
2010+
return ret;
2011+
}
2012+
static int test_tls13_connect_until_write_ok(WOLFSSL* ssl)
2013+
{
2014+
int ret, err;
2015+
int tries = 5;
2016+
2017+
err = 0;
2018+
do {
2019+
ret = wolfSSL_connect(ssl);
2020+
if (ret == WOLFSSL_FATAL_ERROR) {
2021+
err = wolfSSL_get_error(ssl, ret);
2022+
}
2023+
} while (tries-- && ret == WOLFSSL_FATAL_ERROR &&
2024+
err == WOLFSSL_ERROR_WANT_WRITE);
2025+
return ret;
2026+
}
2027+
static int test_tls13_write_until_write_ok(WOLFSSL* ssl, const void* msg,
2028+
int msgLen)
2029+
{
2030+
int ret, err;
2031+
int tries = 5;
2032+
2033+
err = 0;
2034+
do {
2035+
ret = wolfSSL_write(ssl, msg, msgLen);
2036+
if (ret == WOLFSSL_FATAL_ERROR) {
2037+
err = wolfSSL_get_error(ssl, ret);
2038+
}
2039+
} while (tries-- && ret == WOLFSSL_FATAL_ERROR &&
2040+
err == WOLFSSL_ERROR_WANT_WRITE);
2041+
return ret;
2042+
}
2043+
static int test_tls13_early_data_read_until_write_ok(WOLFSSL* ssl, void* buf,
2044+
int bufLen, int* read)
2045+
{
2046+
int ret, err;
2047+
int tries = 5;
2048+
2049+
err = 0;
2050+
do {
2051+
ret = wolfSSL_read_early_data(ssl, buf, bufLen, read);
2052+
if (ret == WOLFSSL_FATAL_ERROR) {
2053+
err = wolfSSL_get_error(ssl, ret);
2054+
}
2055+
} while (tries-- && ret == WOLFSSL_FATAL_ERROR &&
2056+
err == WOLFSSL_ERROR_WANT_WRITE);
2057+
return ret;
2058+
}
2059+
static int test_tls13_early_data_write_until_write_ok(WOLFSSL* ssl,
2060+
const void* msg, int msgLen, int* written)
2061+
{
2062+
int ret, err;
2063+
int tries = 5;
2064+
2065+
err = 0;
2066+
do {
2067+
ret = wolfSSL_write_early_data(ssl, msg, msgLen, written);
2068+
if (ret == WOLFSSL_FATAL_ERROR) {
2069+
err = wolfSSL_get_error(ssl, ret);
2070+
}
2071+
} while (tries-- && ret == WOLFSSL_FATAL_ERROR &&
2072+
err == WOLFSSL_ERROR_WANT_WRITE);
2073+
return ret;
2074+
}
2075+
struct test_tls13_wwrite_ctx {
2076+
int want_write;
2077+
struct test_memio_ctx *text_ctx;
2078+
};
2079+
static int test_tls13_mock_wantwrite_cb(WOLFSSL* ssl, char* data, int sz,
2080+
void* ctx)
2081+
{
2082+
struct test_tls13_wwrite_ctx *wwctx = (struct test_tls13_wwrite_ctx *)ctx;
2083+
wwctx->want_write = !wwctx->want_write;
2084+
if (wwctx->want_write) {
2085+
return WOLFSSL_CBIO_ERR_WANT_WRITE;
2086+
}
2087+
return test_memio_write_cb(ssl, data, sz, wwctx->text_ctx);
2088+
}
2089+
#endif /* HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_EARLY_DATA */
19952090
int test_tls13_early_data(void)
19962091
{
19972092
EXPECT_DECLS;
@@ -2000,7 +2095,6 @@ int test_tls13_early_data(void)
20002095
int written = 0;
20012096
int read = 0;
20022097
size_t i;
2003-
int splitEarlyData;
20042098
char msg[] = "This is early data";
20052099
char msg2[] = "This is client data";
20062100
char msg3[] = "This is server data";
@@ -2012,18 +2106,27 @@ int test_tls13_early_data(void)
20122106
const char* tls_version;
20132107
int isUdp;
20142108
int splitEarlyData;
2109+
int everyWriteWantWrite;
20152110
} params[] = {
20162111
#ifdef WOLFSSL_TLS13
20172112
{ wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
2018-
"TLS 1.3", 0, 0 },
2113+
"TLS 1.3", 0, 0, 0 },
2114+
{ wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
2115+
"TLS 1.3", 0, 1, 0 },
2116+
{ wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
2117+
"TLS 1.3", 0, 0, 1 },
20192118
{ wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
2020-
"TLS 1.3", 0, 1 },
2119+
"TLS 1.3", 0, 1, 1 },
20212120
#endif
20222121
#ifdef WOLFSSL_DTLS13
20232122
{ wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
2024-
"DTLS 1.3", 1, 0 },
2123+
"DTLS 1.3", 1, 0, 0 },
20252124
{ wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
2026-
"DTLS 1.3", 1, 1 },
2125+
"DTLS 1.3", 1, 1, 0 },
2126+
{ wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
2127+
"DTLS 1.3", 1, 0, 1 },
2128+
{ wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
2129+
"DTLS 1.3", 1, 1, 1 },
20272130
#endif
20282131
};
20292132

@@ -2033,10 +2136,14 @@ int test_tls13_early_data(void)
20332136
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
20342137
WOLFSSL_SESSION *sess = NULL;
20352138
int splitEarlyData = params[i].splitEarlyData;
2139+
int everyWriteWantWrite = params[i].everyWriteWantWrite;
2140+
struct test_tls13_wwrite_ctx wwrite_ctx_s, wwrite_ctx_c;
20362141

20372142
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
20382143

2039-
fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
2144+
fprintf(stderr, "\tEarly data with %s%s%s\n", params[i].tls_version,
2145+
splitEarlyData ? " (split early data)" : "",
2146+
everyWriteWantWrite ? " (every write WANT_WRITE)" : "");
20402147

20412148
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
20422149
&ssl_s, params[i].client_meth, params[i].server_meth), 0);
@@ -2071,49 +2178,66 @@ int test_tls13_early_data(void)
20712178
}
20722179
#endif
20732180

2181+
if (everyWriteWantWrite) {
2182+
XMEMSET(&wwrite_ctx_c, 0, sizeof(wwrite_ctx_c));
2183+
XMEMSET(&wwrite_ctx_s, 0, sizeof(wwrite_ctx_s));
2184+
wwrite_ctx_c.text_ctx = &test_ctx;
2185+
wwrite_ctx_s.text_ctx = &test_ctx;
2186+
wolfSSL_SetIOWriteCtx(ssl_c, &wwrite_ctx_c);
2187+
wolfSSL_SSLSetIOSend(ssl_c, test_tls13_mock_wantwrite_cb);
2188+
wolfSSL_SetIOWriteCtx(ssl_s, &wwrite_ctx_s);
2189+
wolfSSL_SSLSetIOSend(ssl_s, test_tls13_mock_wantwrite_cb);
2190+
}
20742191
/* Test 0-RTT data */
20752192
wolfSSL_SetLoggingPrefix("client");
2076-
ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
2077-
&written), sizeof(msg));
2193+
2194+
ExpectIntEQ(test_tls13_early_data_write_until_write_ok(ssl_c, msg,
2195+
sizeof(msg), &written),
2196+
sizeof(msg));
20782197
ExpectIntEQ(written, sizeof(msg));
20792198

20802199
if (splitEarlyData) {
2081-
ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
2082-
&written), sizeof(msg));
2200+
ExpectIntEQ(test_tls13_early_data_write_until_write_ok(ssl_c, msg,
2201+
sizeof(msg), &written),
2202+
sizeof(msg));
20832203
ExpectIntEQ(written, sizeof(msg));
20842204
}
20852205

20862206
/* Read first 0-RTT data (if split otherwise entire data) */
20872207
wolfSSL_SetLoggingPrefix("server");
2088-
ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
2089-
&read), sizeof(msg));
2208+
ExpectIntEQ(test_tls13_early_data_read_until_write_ok(ssl_s, msgBuf,
2209+
sizeof(msgBuf), &read),
2210+
sizeof(msg));
20902211
ExpectIntEQ(read, sizeof(msg));
20912212
ExpectStrEQ(msg, msgBuf);
20922213

20932214
/* Test 0.5-RTT data */
2094-
ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
2215+
ExpectIntEQ(test_tls13_write_until_write_ok(ssl_s, msg4, sizeof(msg4)),
2216+
sizeof(msg4));
20952217

20962218
if (splitEarlyData) {
20972219
/* Read second 0-RTT data */
2098-
ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf,
2099-
sizeof(msgBuf), &read), sizeof(msg));
2220+
ExpectIntEQ(test_tls13_early_data_read_until_write_ok(ssl_s, msgBuf,
2221+
sizeof(msgBuf), &read),
2222+
sizeof(msg));
21002223
ExpectIntEQ(read, sizeof(msg));
21012224
ExpectStrEQ(msg, msgBuf);
21022225
}
21032226

21042227
if (params[i].isUdp) {
21052228
wolfSSL_SetLoggingPrefix("client");
2106-
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
2229+
ExpectIntEQ(test_tls13_connect_until_write_ok(ssl_c), -1);
21072230
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1),
21082231
WC_NO_ERR_TRACE(APP_DATA_READY));
21092232

21102233
/* Read server 0.5-RTT data */
2111-
ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)),
2234+
ExpectIntEQ(
2235+
test_tls13_read_until_write_ok(ssl_c, msgBuf, sizeof(msgBuf)),
21122236
sizeof(msg4));
21132237
ExpectStrEQ(msg4, msgBuf);
21142238

21152239
/* Complete handshake */
2116-
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
2240+
ExpectIntEQ(test_tls13_connect_until_write_ok(ssl_c), -1);
21172241
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1),
21182242
WOLFSSL_ERROR_WANT_READ);
21192243
/* Use wolfSSL_is_init_finished to check if handshake is
@@ -2125,42 +2249,51 @@ int test_tls13_early_data(void)
21252249
* early data parsing logic. */
21262250
wolfSSL_SetLoggingPrefix("server");
21272251
ExpectFalse(wolfSSL_is_init_finished(ssl_s));
2128-
ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf,
2129-
sizeof(msgBuf), &read), 0);
2252+
ExpectIntEQ(test_tls13_early_data_read_until_write_ok(ssl_s, msgBuf,
2253+
sizeof(msgBuf), &read),
2254+
0);
21302255
ExpectIntEQ(read, 0);
21312256
ExpectTrue(wolfSSL_is_init_finished(ssl_s));
21322257

21332258
wolfSSL_SetLoggingPrefix("client");
2134-
ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
2259+
ExpectIntEQ(test_tls13_connect_until_write_ok(ssl_c),
2260+
WOLFSSL_SUCCESS);
21352261
}
21362262
else {
21372263
wolfSSL_SetLoggingPrefix("client");
2138-
ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
2264+
ExpectIntEQ(test_tls13_connect_until_write_ok(ssl_c),
2265+
WOLFSSL_SUCCESS);
21392266

21402267
wolfSSL_SetLoggingPrefix("server");
21412268
ExpectFalse(wolfSSL_is_init_finished(ssl_s));
2142-
ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf,
2143-
sizeof(msgBuf), &read), 0);
2269+
ExpectIntEQ(test_tls13_early_data_read_until_write_ok(ssl_s, msgBuf,
2270+
sizeof(msgBuf), &read),
2271+
0);
21442272
ExpectIntEQ(read, 0);
21452273
ExpectTrue(wolfSSL_is_init_finished(ssl_s));
21462274

21472275
/* Read server 0.5-RTT data */
21482276
wolfSSL_SetLoggingPrefix("client");
2149-
ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)),
2277+
ExpectIntEQ(
2278+
test_tls13_read_until_write_ok(ssl_c, msgBuf, sizeof(msgBuf)),
21502279
sizeof(msg4));
21512280
ExpectStrEQ(msg4, msgBuf);
21522281
}
21532282

21542283
/* Test bi-directional write */
21552284
wolfSSL_SetLoggingPrefix("client");
2156-
ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
2285+
ExpectIntEQ(test_tls13_write_until_write_ok(ssl_c, msg2, sizeof(msg2)),
2286+
sizeof(msg2));
21572287
wolfSSL_SetLoggingPrefix("server");
2158-
ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)),
2288+
ExpectIntEQ(
2289+
test_tls13_read_until_write_ok(ssl_s, msgBuf, sizeof(msgBuf)),
21592290
sizeof(msg2));
21602291
ExpectStrEQ(msg2, msgBuf);
2161-
ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
2292+
ExpectIntEQ(test_tls13_write_until_write_ok(ssl_s, msg3, sizeof(msg3)),
2293+
sizeof(msg3));
21622294
wolfSSL_SetLoggingPrefix("client");
2163-
ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)),
2295+
ExpectIntEQ(
2296+
test_tls13_read_until_write_ok(ssl_c, msgBuf, sizeof(msgBuf)),
21642297
sizeof(msg3));
21652298
ExpectStrEQ(msg3, msgBuf);
21662299

0 commit comments

Comments
 (0)