From af5e622e5164549ac08a33655b7ff1d3d12644db Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Fri, 25 Oct 2024 14:15:57 +1000 Subject: [PATCH] Add valgrind test for cat Signed-off-by: Ronnie Sahlberg --- lib/libsmb2.c | 6 ++++ tests/prog_cat.c | 32 ++++++++++++-------- tests/test_0302_cat_valgrind_socket_error.sh | 19 ++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) create mode 100755 tests/test_0302_cat_valgrind_socket_error.sh diff --git a/lib/libsmb2.c b/lib/libsmb2.c index 01a62c07..c9c52f2c 100644 --- a/lib/libsmb2.c +++ b/lib/libsmb2.c @@ -2602,6 +2602,12 @@ disconnect_cb_1(struct smb2_context *smb2, int status, struct disconnect_data *dc_data = private_data; struct smb2_pdu *pdu; + if (status != SMB2_STATUS_SUCCESS) { + smb2_set_nterror(smb2, status, "%s", nterror_to_str(status)); + dc_data->cb(smb2, -ENOMEM, NULL, dc_data->cb_data); + free(dc_data); + return; + } pdu = smb2_cmd_logoff_async(smb2, disconnect_cb_2, dc_data); if (pdu == NULL) { dc_data->cb(smb2, -ENOMEM, NULL, dc_data->cb_data); diff --git a/tests/prog_cat.c b/tests/prog_cat.c index 9464abd7..71a6787c 100644 --- a/tests/prog_cat.c +++ b/tests/prog_cat.c @@ -39,7 +39,7 @@ struct pollfd { int poll(struct pollfd *fds, unsigned int nfds, int timo); #endif -int is_finished; +int is_finished = 0; uint8_t buf[256 * 1024]; uint32_t pos; @@ -72,13 +72,14 @@ void pr_cb(struct smb2_context *smb2, int status, if (status < 0) { printf("failed to read file (%s) %s\n", strerror(-status), smb2_get_error(smb2)); - exit(10); + is_finished = 1; + return; } if (status == 0) { if (smb2_close_async(smb2, fh, cl_cb, NULL) < 0) { printf("Failed to call smb2_close_async()\n"); - exit(10); + is_finished = 1; } return; } @@ -88,7 +89,8 @@ void pr_cb(struct smb2_context *smb2, int status, pos += status; if (smb2_pread_async(smb2, fh, buf, 102400, pos, pr_cb, fh) < 0) { printf("Failed to call smb2_pread_async()\n"); - exit(10); + is_finished = 1; + return; } } @@ -100,12 +102,14 @@ void of_cb(struct smb2_context *smb2, int status, if (status) { printf("failed to open file (%s) %s\n", strerror(-status), smb2_get_error(smb2)); - exit(10); + is_finished = 1; + return; } if (smb2_pread_async(smb2, fh, buf, 102400, 0, pr_cb, fh) < 0) { printf("Failed to call smb2_pread_async()\n"); - exit(10); + is_finished = 1; + return; } } @@ -115,13 +119,15 @@ void cf_cb(struct smb2_context *smb2, int status, if (status) { printf("failed to connect share (%s) %s\n", strerror(-status), smb2_get_error(smb2)); - exit(10); + is_finished = 1; + return; } if (smb2_open_async(smb2, private_data, O_RDONLY, of_cb, NULL) < 0) { printf("Failed to call smb2_open_async()\n"); - exit(10); + is_finished = 1; + return; } } @@ -130,6 +136,7 @@ int main(int argc, char *argv[]) struct smb2_context *smb2; struct smb2_url *url; struct pollfd pfd; + int rc = 0; if (argc < 2) { usage(); @@ -152,7 +159,7 @@ int main(int argc, char *argv[]) if (smb2_connect_share_async(smb2, url->server, url->share, url->user, cf_cb, (void *)url->path) != 0) { printf("smb2_connect_share failed. %s\n", smb2_get_error(smb2)); - exit(10); + goto finished; } while (!is_finished) { @@ -161,7 +168,7 @@ int main(int argc, char *argv[]) if (poll(&pfd, 1, 1000) < 0) { printf("Poll failed"); - exit(10); + goto finished; } if (pfd.revents == 0) { continue; @@ -169,12 +176,13 @@ int main(int argc, char *argv[]) if (smb2_service(smb2, pfd.revents) < 0) { printf("smb2_service failed with : %s\n", smb2_get_error(smb2)); - break; + goto finished; } } + finished: smb2_destroy_url(url); smb2_destroy_context(smb2); - return 0; + return rc; } diff --git a/tests/test_0302_cat_valgrind_socket_error.sh b/tests/test_0302_cat_valgrind_socket_error.sh new file mode 100755 index 00000000..def4d022 --- /dev/null +++ b/tests/test_0302_cat_valgrind_socket_error.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +. ./functions.sh + +echo "basic cat test with valgrind and session errors" + +# This test depends on the file CAT existing on the share used for testing +# TODO: should create the file first + +NUM_CALLS=`libtool --mode=execute strace ./prog_cat "${TESTURL}/CAT" 2>&1 >/dev/null | grep readv |wc -l` + +for IDX in `seq 1 $NUM_CALLS`; do + echo -n "Testing prog_cat on root of share with socket failure at #${IDX} ..." + READV_CLOSE=${IDX} LD_PRELOAD=./ld_sockerr.so libtool --mode=execute valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 ./prog_cat "${TESTURL}/CAT" >/dev/null 2>valgrind.out || failure + + success +done + +exit 0