From: Kevin Day Date: Thu, 14 Sep 2023 04:28:27 +0000 (-0500) Subject: Update: Minor clean ups and have f_socket_connect() return F_file_descriptor. X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=e3feca6664626b81551977ace37e8aaebff4147a;p=fll Update: Minor clean ups and have f_socket_connect() return F_file_descriptor. The f_socket_connect() should return F_file_descriptor when socket.id is in use. Add appropriate unit tests. --- diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index a84f241..8f12018 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -1467,7 +1467,7 @@ extern "C" { * @param path * The path file name. * @param mode - * The file mode to open in. + * The file mode permission to open in (such as F_file_mode_all_rwx_d). * @param file * The data related to the file being opened. * This will be updated with the file descriptor. diff --git a/level_0/f_socket/c/socket.c b/level_0/f_socket/c/socket.c index fdff250..08ab88a 100644 --- a/level_0/f_socket/c/socket.c +++ b/level_0/f_socket/c/socket.c @@ -176,6 +176,8 @@ extern "C" { #ifndef _di_f_socket_connect_ f_status_t f_socket_connect(const f_socket_t socket) { + if (socket.id != -1) return F_file_descriptor; + if (connect(socket.id, (struct sockaddr *) &socket.address, socket.length) == -1) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EADDRINUSE) return F_status_set_error(F_busy_address); diff --git a/level_0/f_socket/c/socket.h b/level_0/f_socket/c/socket.h index 4abc7b4..ae9dfbf 100644 --- a/level_0/f_socket/c/socket.h +++ b/level_0/f_socket/c/socket.h @@ -251,6 +251,7 @@ extern "C" { * * @return * F_okay on success. + * F_file_descriptor if socket file descriptor is already connected (socket.id != -1). * * F_access_denied (with error bit) on access denied. * F_available_not_address (with error bit) if address is unavailable (is non-existent or not local). diff --git a/level_0/f_socket/tests/unit/c/test-socket-connect.c b/level_0/f_socket/tests/unit/c/test-socket-connect.c index f4d32ff..7612a91 100644 --- a/level_0/f_socket/tests/unit/c/test-socket-connect.c +++ b/level_0/f_socket/tests/unit/c/test-socket-connect.c @@ -62,6 +62,18 @@ void test__f_socket_connect__fails(void **state) { } // for } +void test__f_socket_connect__returns_file_descriptor(void **state) { + + { + f_socket_t socket = f_socket_t_initialize; + socket.id = 1; + + const f_status_t status = f_socket_connect(socket); + + assert_int_equal(status, F_file_descriptor); + } +} + void test__f_socket_connect__works(void **state) { f_socket_t socket = f_socket_t_initialize; diff --git a/level_0/f_socket/tests/unit/c/test-socket-connect.h b/level_0/f_socket/tests/unit/c/test-socket-connect.h index ab90426..b807476 100644 --- a/level_0/f_socket/tests/unit/c/test-socket-connect.h +++ b/level_0/f_socket/tests/unit/c/test-socket-connect.h @@ -18,6 +18,13 @@ extern void test__f_socket_connect__fails(void **state); /** + * Test that the function returns F_file_descriptor_not. + * + * @see f_socket_connect() + */ +extern void test__f_socket_connect__returns_file_descriptor(void **state); + +/** * Test that function works. * * @see f_socket_connect() 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 4273dec..f9740fd 100644 --- a/level_0/f_socket/tests/unit/c/test-socket.c +++ b/level_0/f_socket/tests/unit/c/test-socket.c @@ -35,6 +35,7 @@ int main(void) { cmocka_unit_test(test__f_socket_bind_local__works), cmocka_unit_test(test__f_socket_connect__fails), + cmocka_unit_test(test__f_socket_connect__returns_file_descriptor), cmocka_unit_test(test__f_socket_connect__works), cmocka_unit_test(test__f_socket_create__fails),