From 1fc531f39107f01d6d9f7c4cd63dec8392789e3e Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 6 Oct 2025 12:43:40 +0200 Subject: [PATCH] test766: verify CURLOPT_SOCKOPTFUNCTION error on accept This test does active FTP with a socketopt callback that returns error for the CURLSOCKTYPE_ACCEPT "purpose" to make sure we test and exercise this error path - without leaks. --- tests/data/Makefile.am | 2 +- tests/data/test766 | 54 ++++++++++++++++ tests/libtest/Makefile.inc | 1 + tests/libtest/lib766.c | 127 +++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 tests/data/test766 create mode 100644 tests/libtest/lib766.c diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am index 5063e0210f..db1f86e356 100644 --- a/tests/data/Makefile.am +++ b/tests/data/Makefile.am @@ -109,7 +109,7 @@ test727 test728 test729 test730 test731 test732 test733 test734 test735 \ test736 test737 test738 test739 test740 test741 test742 test743 test744 \ test745 test746 test747 test748 test749 test750 test751 test752 test753 \ test754 test755 test756 test757 test758 test759 test760 test761 test762 \ -test763 test764 test765 \ +test763 test764 test765 test766 \ \ test780 test781 test782 test783 test784 test785 test786 test787 test788 \ test789 test790 test791 test792 test793 test794 test796 test797 \ diff --git a/tests/data/test766 b/tests/data/test766 new file mode 100644 index 0000000000..6beb341810 --- /dev/null +++ b/tests/data/test766 @@ -0,0 +1,54 @@ + + + +FTP +PORT +CURLOPT_SOCKOPTFUNCTION + + + +# Server-side + + +hello + + + +# Client-side + + +ftp + + +lib%TESTNUMBER + + +FTP PORT with sockopt callback refusing the accept + + +ftp://%HOSTIP:%FTPPORT/path/%TESTNUMBER + + + +# Verify data after the test has been "shot" + +# Strip off parts of the PORT and EPRT commands that might differ + +s/^PORT (.*)/PORT/ +s/^EPRT \|1\|(.*)/EPRT \|1\|/ + + +USER anonymous +PASS ftp@example.com +PWD +CWD path +EPRT |1| + + +# 42 == CURLE_ABORTED_BY_CALLBACK + +42 + + + + diff --git a/tests/libtest/Makefile.inc b/tests/libtest/Makefile.inc index d8735a44e1..cac833f26f 100644 --- a/tests/libtest/Makefile.inc +++ b/tests/libtest/Makefile.inc @@ -76,6 +76,7 @@ TESTS_C = \ lib694.c lib695.c \ lib751.c lib753.c lib758.c \ lib757.c \ + lib766.c \ lib1156.c \ lib1301.c lib1308.c \ lib1485.c \ diff --git a/tests/libtest/lib766.c b/tests/libtest/lib766.c new file mode 100644 index 0000000000..4a64c3e0a2 --- /dev/null +++ b/tests/libtest/lib766.c @@ -0,0 +1,127 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at https://curl.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * SPDX-License-Identifier: curl + * + ***************************************************************************/ + +#include "first.h" + +#include "memdebug.h" + +static int sockopt_766(void *clientp, + curl_socket_t curlfd, + curlsocktype purpose) +{ + (void)clientp; + (void)curlfd; + if(purpose == CURLSOCKTYPE_ACCEPT) { + curl_mfprintf(stderr, + "Return error from CURLOPT_SOCKOPTFUNCTION callback\n"); + return 1; /* force error */ + } + return 0; +} + +static CURLcode test_lib766(const char *URL) +{ + CURL *easy = NULL; + CURLM *multi = NULL; + CURLcode res = CURLE_OK; + int running; + int msgs_left; + CURLMsg *msg; + + start_test_timing(); + + res_global_init(CURL_GLOBAL_ALL); + if(res) { + return res; + } + + easy_init(easy); + easy_setopt(easy, CURLOPT_VERBOSE, 1L); + easy_setopt(easy, CURLOPT_URL, URL); + easy_setopt(easy, CURLOPT_FTPPORT, "-"); + easy_setopt(easy, CURLOPT_SOCKOPTFUNCTION, sockopt_766); + + multi_init(multi); + + multi_add_handle(multi, easy); + + for(;;) { + struct timeval interval; + fd_set fdread; + fd_set fdwrite; + fd_set fdexcep; + long timeout = -99; + int maxfd = -99; + + multi_perform(multi, &running); + + abort_on_test_timeout(); + + if(!running) + break; /* done */ + + FD_ZERO(&fdread); + FD_ZERO(&fdwrite); + FD_ZERO(&fdexcep); + + multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd); + + /* At this point, maxfd is guaranteed to be greater or equal than -1. */ + + multi_timeout(multi, &timeout); + + /* At this point, timeout is guaranteed to be greater or equal than -1. */ + + if(timeout != -1L) { + int itimeout; +#if LONG_MAX > INT_MAX + itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout; +#else + itimeout = (int)timeout; +#endif + interval.tv_sec = itimeout/1000; + interval.tv_usec = (itimeout%1000)*1000; + } + else { + interval.tv_sec = 0; + interval.tv_usec = 100000L; /* 100 ms */ + } + + select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval); + + abort_on_test_timeout(); + } + + msg = curl_multi_info_read(multi, &msgs_left); + if(msg) + res = msg->data.result; + +test_cleanup: + + curl_multi_cleanup(multi); + curl_easy_cleanup(easy); + curl_global_cleanup(); + + return res; +}