From b2728a0112a93b508c87a7139f710e6e3095a5d5 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 6 Sep 2023 00:09:04 -0500 Subject: [PATCH] Update: Add check in f_socket_disconnect() to explicitly check socket file descriptor. Avoid depending on libc calls and instead explicitly check the socket file descriptor. Return F_file_descriptor_not when -1. Add unit test for this. --- level_0/f_socket/c/socket.c | 2 ++ level_0/f_socket/c/socket.h | 1 + level_0/f_socket/tests/unit/c/test-socket-disconnect.c | 11 +++++++++++ level_0/f_socket/tests/unit/c/test-socket-disconnect.h | 7 +++++++ level_0/f_socket/tests/unit/c/test-socket.c | 3 +++ 5 files changed, 24 insertions(+) diff --git a/level_0/f_socket/c/socket.c b/level_0/f_socket/c/socket.c index 8a12998..fdff250 100644 --- a/level_0/f_socket/c/socket.c +++ b/level_0/f_socket/c/socket.c @@ -265,6 +265,8 @@ extern "C" { if (!socket) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + if (socket->id == -1) return F_file_descriptor_not; + int result = 0; if (action == f_socket_close_fast_e) { diff --git a/level_0/f_socket/c/socket.h b/level_0/f_socket/c/socket.h index f8bf5c3..4abc7b4 100644 --- a/level_0/f_socket/c/socket.h +++ b/level_0/f_socket/c/socket.h @@ -376,6 +376,7 @@ extern "C" { * @return * F_okay on success. * F_connect_not if the socket is not connected. + * F_file_descriptor_not if socket.id is -1. * * F_busy_address (with error bit) if address is already in use (therefore unavailable). * F_file_descriptor (with error bit) if id is an invalid descriptor. diff --git a/level_0/f_socket/tests/unit/c/test-socket-disconnect.c b/level_0/f_socket/tests/unit/c/test-socket-disconnect.c index da567a0..32833b4 100644 --- a/level_0/f_socket/tests/unit/c/test-socket-disconnect.c +++ b/level_0/f_socket/tests/unit/c/test-socket-disconnect.c @@ -157,6 +157,17 @@ void test__f_socket_disconnect__parameter_checking(void **state) { } } +void test__f_socket_disconnect__returns_file_descriptor_not(void **state) { + + { + f_socket_t socket = f_socket_t_initialize; + + const f_status_t status = f_socket_disconnect(&socket, 0); + + assert_int_equal(status, F_file_descriptor_not); + } +} + void test__f_socket_disconnect__works_for_close_fast(void **state) { f_socket_t socket = f_socket_t_initialize; diff --git a/level_0/f_socket/tests/unit/c/test-socket-disconnect.h b/level_0/f_socket/tests/unit/c/test-socket-disconnect.h index a1093a7..75e7447 100644 --- a/level_0/f_socket/tests/unit/c/test-socket-disconnect.h +++ b/level_0/f_socket/tests/unit/c/test-socket-disconnect.h @@ -46,6 +46,13 @@ extern void test__f_socket_disconnect__fails_for_close_write(void **state); extern void test__f_socket_disconnect__parameter_checking(void **state); /** + * Test that the function returns F_file_descriptor_not. + * + * @see f_socket_disconnect() + */ +extern void test__f_socket_disconnect__returns_file_descriptor_not(void **state); + +/** * Test that function works for f_socket_close_fast_e. * * @see f_socket_disconnect() diff --git a/level_0/f_socket/tests/unit/c/test-socket.c b/level_0/f_socket/tests/unit/c/test-socket.c index a8f8e63..4273dec 100644 --- a/level_0/f_socket/tests/unit/c/test-socket.c +++ b/level_0/f_socket/tests/unit/c/test-socket.c @@ -47,6 +47,9 @@ int main(void) { cmocka_unit_test(test__f_socket_disconnect__fails_for_close_read), cmocka_unit_test(test__f_socket_disconnect__fails_for_close_unknown), cmocka_unit_test(test__f_socket_disconnect__fails_for_close_write), + + cmocka_unit_test(test__f_socket_disconnect__returns_file_descriptor_not), + cmocka_unit_test(test__f_socket_disconnect__works_for_close_fast), cmocka_unit_test(test__f_socket_disconnect__works_for_close_read), cmocka_unit_test(test__f_socket_disconnect__works_for_close_write), -- 1.8.3.1