@@ -46751,25 +46751,12 @@ static int test_extra_alerts_bad_psk(void)
4675146751}
4675246752#endif
4675346753
46754- #if defined(OPENSSL_EXTRA) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
46755- /*
46756- * Emulates wolfSSL_shutdown that goes on EAGAIN,
46757- * by returning on output WOLFSSL_ERROR_WANT_WRITE.*/
46758- static int custom_wolfSSL_shutdown(WOLFSSL *ssl, char *buf,
46759- int sz, void *ctx)
46760- {
46761- (void)ssl;
46762- (void)buf;
46763- (void)ctx;
46764- (void)sz;
46765-
46766- return WOLFSSL_CBIO_ERR_WANT_WRITE;
46767- }
46768-
46769- static int test_multiple_alerts_EAGAIN(void)
46754+ #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
46755+ static int test_multiple_shutdown_nonblocking(void)
4677046756{
4677146757 EXPECT_DECLS;
4677246758 size_t size_of_last_packet = 0;
46759+ int dummy_recv_buffer;
4677346760
4677446761 /* declare wolfSSL objects */
4677546762 struct test_memio_ctx test_ctx;
@@ -46779,46 +46766,68 @@ static int test_multiple_alerts_EAGAIN(void)
4677946766 XMEMSET(&test_ctx, 0, sizeof(test_ctx));
4678046767
4678146768 /* Create and initialize WOLFSSL_CTX and WOLFSSL objects */
46782- #ifdef USE_TLSV13
46783- ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
46784- wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
46785- #else
4678646769 ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
4678746770 wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
46788- #endif
46771+
4678946772 ExpectNotNull(ctx_c);
4679046773 ExpectNotNull(ssl_c);
4679146774 ExpectNotNull(ctx_s);
4679246775 ExpectNotNull(ssl_s);
4679346776
46794- /* Load client certificates into WOLFSSL_CTX */
46795- ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c, "./certs/ca-cert.pem", NULL), WOLFSSL_SUCCESS);
46796-
4679746777 ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
4679846778
46799- /*
46800- * We set the custom callback for the IO to emulate multiple EAGAINs
46801- * on shutdown, so we can check that we don't send multiple packets.
46802- * */
46803- wolfSSL_SSLSetIOSend(ssl_c, custom_wolfSSL_shutdown);
46779+ /* buffers should be empty now */
46780+ ExpectIntEQ(test_ctx.c_len, 0);
46781+ ExpectIntEQ(test_ctx.s_len, 0);
46782+ ExpectIntEQ(ssl_c->buffers.outputBuffer.length, 0);
46783+
46784+ test_memio_simulate_want_write(&test_ctx, 0, 1);
4680446785
4680546786 /*
46806- * We call wolfSSL_shutdown multiple times to reproduce the behaviour,
46807- * to check that it doesn't add the CLOSE_NOTIFY packet multiple times
46808- * on the output buffer.
46787+ * We call wolfSSL_shutdown multiple times to to check that it doesn't add
46788+ * the CLOSE_NOTIFY packet multiple times on the output buffer.
4680946789 * */
46810- wolfSSL_shutdown(ssl_c);
46811- wolfSSL_shutdown( ssl_c);
46790+ ExpectIntEQ( wolfSSL_shutdown(ssl_c), -1 );
46791+ ExpectIntEQ(wolfSSL_get_error( ssl_c, 0), WOLFSSL_ERROR_WANT_WRITE );
4681246792
46793+ /* store the size of the packet */
4681346794 if (ssl_c != NULL) {
4681446795 size_of_last_packet = ssl_c->buffers.outputBuffer.length;
4681546796 }
46816- wolfSSL_shutdown(ssl_c);
4681746797
46818- /*
46819- * Finally we check the length of the output buffer.
46820- * */
46821- ExpectIntEQ((ssl_c->buffers.outputBuffer.length - size_of_last_packet), 0);
46798+ /* invoke it multiple times shouldn't change the wolfssl internal output buffer size */
46799+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), -1);
46800+ ExpectIntEQ(wolfSSL_get_error(ssl_c, 0), WOLFSSL_ERROR_WANT_WRITE);
46801+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), -1);
46802+ ExpectIntEQ(wolfSSL_get_error(ssl_c, 0), WOLFSSL_ERROR_WANT_WRITE);
46803+
46804+ ExpectIntEQ(ssl_c->buffers.outputBuffer.length, size_of_last_packet);
46805+
46806+ /* now send the CLOSE_NOTIFY to the server for real, expecting shutdown not done */
46807+ test_memio_simulate_want_write(&test_ctx, 0, 0);
46808+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SHUTDOWN_NOT_DONE);
46809+
46810+ /* output buffer should be empty and socket buffer should contain the message */
46811+ ExpectIntEQ(ssl_c->buffers.outputBuffer.length, 0);
46812+ ExpectIntEQ(test_ctx.s_len, size_of_last_packet);
46813+
46814+
46815+ /* this should try to read from the socket */
46816+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), -1);
46817+ ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
46818+
46819+ /* complete the bidirectional shutdown */
46820+
46821+ /* check that server received the shutdown alert */
46822+ ExpectIntEQ(wolfSSL_recv(ssl_s, &dummy_recv_buffer, 0, 0), 0);
46823+ ExpectIntEQ(wolfSSL_get_error(ssl_s, 0), WOLFSSL_ERROR_ZERO_RETURN);
46824+
46825+ /* send the shutdown from the server side */
46826+ ExpectIntEQ(wolfSSL_shutdown(ssl_s), WOLFSSL_SUCCESS);
46827+
46828+ /* This should return success and zero return */
46829+ ExpectIntEQ(wolfSSL_shutdown(ssl_c), WOLFSSL_SUCCESS);
46830+ ExpectIntEQ(wolfSSL_get_error(ssl_c, 0), WOLFSSL_ERROR_ZERO_RETURN);
4682246831
4682346832 /* Cleanup and return */
4682446833 wolfSSL_CTX_free(ctx_c);
@@ -46829,7 +46838,7 @@ static int test_multiple_alerts_EAGAIN(void)
4682946838 return EXPECT_RESULT();
4683046839}
4683146840#else
46832- static int test_multiple_alerts_EAGAIN (void)
46841+ static int test_multiple_shutdown_nonblocking (void)
4683346842{
4683446843 return TEST_SKIPPED;
4683546844}
@@ -51365,7 +51374,7 @@ TEST_CASE testCases[] = {
5136551374 TEST_DECL(test_extra_alerts_wrong_cs),
5136651375 TEST_DECL(test_extra_alerts_skip_hs),
5136751376 TEST_DECL(test_extra_alerts_bad_psk),
51368- TEST_DECL(test_multiple_alerts_EAGAIN ),
51377+ TEST_DECL(test_multiple_shutdown_nonblocking ),
5136951378 /* Can't memory test as client/server Asserts. */
5137051379 TEST_DECL(test_harden_no_secure_renegotiation),
5137151380 TEST_DECL(test_override_alt_cert_chain),
0 commit comments