From: Kevin Day Date: Tue, 18 Mar 2025 23:05:38 +0000 (-0500) Subject: Progress: Continue working on completing the remove program. X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=39d1578b020a27884e35690253b10acc23f9d4fc;p=kevux-tools Progress: Continue working on completing the remove program. I forgot to perform the `git add` for the previous commit bf990a10a2c24f6304bb04dc2569765abb5b2347. This includes changes that were supposed to be in that commit. Add file type based program unit tests. Add user based program unit tests. Add group based program unit tests. Fix bug exposed by the program unit tests where the id tests are only being performed as a group test even when it needs to be a user test. This is fixed by creating and passing the `is_user` boolean to the functions. Fix bug exposed by the program unit tests where the `--same` and `--different` boolean logic is reversed resulting in `--same` removing for different users and `--different` removing for the same user. --- diff --git a/data/build/remove/settings-mocks.remove b/data/build/remove/settings-mocks.remove index 779abd9..15e4dec 100644 --- a/data/build/remove/settings-mocks.remove +++ b/data/build/remove/settings-mocks.remove @@ -85,6 +85,8 @@ flags_library -fPIC flags_program-android -fPIE -Wl,-z,relro # Inject mocks. +flags -Wl,--wrap=f_account_group_id_by_name +flags -Wl,--wrap=f_account_id_by_name flags -Wl,--wrap=f_directory_empty flags -Wl,--wrap=f_directory_remove flags -Wl,--wrap=f_file_exists @@ -93,4 +95,5 @@ flags -Wl,--wrap=f_file_remove flags -Wl,--wrap=f_file_stat flags -Wl,--wrap=fl_directory_do flags -Wl,--wrap=fll_program_print_version +flags -Wl,--wrap=geteuid flags -Wl,--wrap=kt_remove_print_message_help diff --git a/data/build/remove/settings-tests.remove b/data/build/remove/settings-tests.remove index efc5e30..12896a8 100644 --- a/data/build/remove/settings-tests.remove +++ b/data/build/remove/settings-tests.remove @@ -31,8 +31,11 @@ build_libraries-individual_thread -lf_thread build_sources_program test-remove.c build_sources_program test-remove-print_help.c test-remove-print_version.c -build_sources_program test-remove-directory_no_args.c test-remove-regular_no_args.c -build_sources_program test-remove-directory_recurse_simple.c test-remove-directory_tree_simple.c +build_sources_program test-remove-file_type.c +build_sources_program test-remove-group.c +build_sources_program test-remove-directory_no_args.c test-remove-directory_recurse_simple.c test-remove-directory_tree_simple.c +build_sources_program test-remove-regular_no_args.c +build_sources_program test-remove-user.c build_script no build_shared yes diff --git a/sources/c/program/kevux/tools/remove/main/common.c b/sources/c/program/kevux/tools/remove/main/common.c index c9b40b0..fb87165 100644 --- a/sources/c/program/kevux/tools/remove/main/common.c +++ b/sources/c/program/kevux/tools/remove/main/common.c @@ -332,7 +332,7 @@ extern "C" { } } - kt_remove_setting_load_id(main, &main->program.parameters.array[kt_remove_parameter_group_e], &main->setting.groups, kt_remove_long_group_s, kt_remove_main_flag_group_d); + kt_remove_setting_load_id(main, &main->program.parameters.array[kt_remove_parameter_group_e], &main->setting.groups, kt_remove_long_group_s, kt_remove_main_flag_group_d, F_false); if (F_status_is_error(main->setting.state.status)) return; if (main->program.parameters.array[kt_remove_parameter_mode_e].result & f_console_result_found_e) { @@ -422,7 +422,7 @@ extern "C" { } } - kt_remove_setting_load_id(main, &main->program.parameters.array[kt_remove_parameter_user_e], &main->setting.users, kt_remove_long_user_s, kt_remove_main_flag_user_d); + kt_remove_setting_load_id(main, &main->program.parameters.array[kt_remove_parameter_user_e], &main->setting.users, kt_remove_long_user_s, kt_remove_main_flag_user_d, F_true); if (F_status_is_error(main->setting.state.status)) return; if (main->program.parameters.array[kt_remove_parameter_prompt_e].result & f_console_result_found_e) { @@ -507,6 +507,10 @@ extern "C" { } } + if (main->program.parameters.array[kt_remove_parameter_unknown_e].result & f_console_result_found_e) { + main->setting.flag |= kt_remove_main_flag_unknown_d | kt_remove_main_flag_option_used_d; + } + if (main->program.parameters.array[kt_remove_parameter_utc_e].result & f_console_result_found_e) { main->setting.flag |= kt_remove_main_flag_utc_d; @@ -553,7 +557,7 @@ extern "C" { #endif // _di_kt_remove_setting_load_ #ifndef _di_kt_remove_setting_load_id_ - void kt_remove_setting_load_id(kt_remove_main_t * const main, f_console_parameter_t * const parameter, f_ids_t * const ids, const f_string_static_t name, const uint64_t flag) { + void kt_remove_setting_load_id(kt_remove_main_t * const main, f_console_parameter_t * const parameter, f_ids_t * const ids, const f_string_static_t name, const uint64_t flag, const uint8_t is_user) { if (!main) return; @@ -587,7 +591,7 @@ extern "C" { for (f_number_unsigned_t i = 0; i < total_arguments; ++i) { - ids->array[ids->used] = kt_remove_get_id(main, main->program.parameters.arguments.array[parameter->values.array[i]], F_false); + ids->array[ids->used] = kt_remove_get_id(main, main->program.parameters.arguments.array[parameter->values.array[i]], is_user); if (F_status_is_error(main->setting.state.status)) { kt_remove_print_error(&main->program.error, macro_kt_remove_f(kt_remove_get_id)); diff --git a/sources/c/program/kevux/tools/remove/main/common.h b/sources/c/program/kevux/tools/remove/main/common.h index 71bed6b..9777871 100644 --- a/sources/c/program/kevux/tools/remove/main/common.h +++ b/sources/c/program/kevux/tools/remove/main/common.h @@ -77,12 +77,15 @@ extern "C" { * Must not be NULL. * @param flag * The flags to assign when the group or user is loaded. + * @param is_user + * If F_true, then this is a user. + * If F_false, then this is a group. * * @see f_memory_array_increase_by() * @see kt_remove_get_id() */ #ifndef _di_kt_remove_setting_load_id_ - extern void kt_remove_setting_load_id(kt_remove_main_t * const main, f_console_parameter_t * const parameter, f_ids_t * const ids, const f_string_static_t name, const uint64_t flag); + extern void kt_remove_setting_load_id(kt_remove_main_t * const main, f_console_parameter_t * const parameter, f_ids_t * const ids, const f_string_static_t name, const uint64_t flag, const uint8_t is_user); #endif // _di_kt_remove_setting_load_id_ #ifdef __cplusplus diff --git a/sources/c/program/kevux/tools/remove/main/common/define.h b/sources/c/program/kevux/tools/remove/main/common/define.h index b246ee6..711c35a 100644 --- a/sources/c/program/kevux/tools/remove/main/common/define.h +++ b/sources/c/program/kevux/tools/remove/main/common/define.h @@ -242,6 +242,7 @@ extern "C" { * - tree: Remove directory tree (parent directories) (remove a/b/c, removes a/b/c, then a/b/, then a). * - updated: Remove by last updated datetime. * - user: Remove by UID. + * - unknown: Remove by file type: unknown. * - utc: Process dates in UTC mode. * - version: Print version. * - version_copyright_help: A helper flag representing version, copyright, and help flag bits being set. @@ -282,9 +283,10 @@ extern "C" { #define kt_remove_main_flag_tree_d 0x20000000 #define kt_remove_main_flag_updated_d 0x40000000 #define kt_remove_main_flag_user_d 0x80000000 - #define kt_remove_main_flag_utc_d 0x100000000 - #define kt_remove_main_flag_version_d 0x200000000 - #define kt_remove_main_flag_version_copyright_help_d 0x200008008 + #define kt_remove_main_flag_unknown_d 0x100000000 + #define kt_remove_main_flag_utc_d 0x200000000 + #define kt_remove_main_flag_version_d 0x400000000 + #define kt_remove_main_flag_version_copyright_help_d 0x400008008 #endif // _di_kt_remove_main_flag_e_ /** diff --git a/sources/c/program/kevux/tools/remove/main/common/enumeration.h b/sources/c/program/kevux/tools/remove/main/common/enumeration.h index 6b75896..8f2c9b8 100644 --- a/sources/c/program/kevux/tools/remove/main/common/enumeration.h +++ b/sources/c/program/kevux/tools/remove/main/common/enumeration.h @@ -58,6 +58,7 @@ extern "C" { kt_remove_parameter_tree_e, kt_remove_parameter_updated_e, kt_remove_parameter_user_e, + kt_remove_parameter_unknown_e, kt_remove_parameter_utc_e, }; // enum @@ -90,10 +91,11 @@ extern "C" { macro_f_console_parameter_t_initialize_3(kt_remove_short_tree_s, kt_remove_long_tree_s, 0, f_console_flag_normal_e), \ macro_f_console_parameter_t_initialize_3(kt_remove_short_updated_s, kt_remove_long_updated_s, 2, f_console_flag_normal_e), \ macro_f_console_parameter_t_initialize_3(kt_remove_short_user_s, kt_remove_long_user_s, 1, f_console_flag_normal_e), \ + macro_f_console_parameter_t_initialize_5( kt_remove_long_unknown_s, 0, f_console_flag_normal_e), \ macro_f_console_parameter_t_initialize_5( kt_remove_long_utc_s, 0, f_console_flag_normal_e), \ } - #define kt_remove_total_parameters_d (f_console_parameter_state_type_total_d + 26) + #define kt_remove_total_parameters_d (f_console_parameter_state_type_total_d + 27) #endif // _di_kt_remove_parameter_e_ #ifdef __cplusplus diff --git a/sources/c/program/kevux/tools/remove/main/common/string.c b/sources/c/program/kevux/tools/remove/main/common/string.c index f7dfc94..8e49a8b 100644 --- a/sources/c/program/kevux/tools/remove/main/common/string.c +++ b/sources/c/program/kevux/tools/remove/main/common/string.c @@ -149,6 +149,7 @@ extern "C" { const f_string_static_t kt_remove_long_tree_s = macro_f_string_static_t_initialize_1(KT_REMOVE_long_tree_s, 0, KT_REMOVE_long_tree_s_length); const f_string_static_t kt_remove_long_updated_s = macro_f_string_static_t_initialize_1(KT_REMOVE_long_updated_s, 0, KT_REMOVE_long_updated_s_length); const f_string_static_t kt_remove_long_user_s = macro_f_string_static_t_initialize_1(KT_REMOVE_long_user_s, 0, KT_REMOVE_long_user_s_length); + const f_string_static_t kt_remove_long_unknown_s = macro_f_string_static_t_initialize_1(KT_REMOVE_long_unknown_s, 0, KT_REMOVE_long_unknown_s_length); const f_string_static_t kt_remove_long_utc_s = macro_f_string_static_t_initialize_1(KT_REMOVE_long_utc_s, 0, KT_REMOVE_long_utc_s_length); #endif // _di_kt_remove_parameter_s_ diff --git a/sources/c/program/kevux/tools/remove/main/common/string.h b/sources/c/program/kevux/tools/remove/main/common/string.h index dc155f3..f012d0b 100644 --- a/sources/c/program/kevux/tools/remove/main/common/string.h +++ b/sources/c/program/kevux/tools/remove/main/common/string.h @@ -421,6 +421,7 @@ extern "C" { #define KT_REMOVE_long_tree_s "tree" #define KT_REMOVE_long_updated_s "updated" #define KT_REMOVE_long_user_s "user" + #define KT_REMOVE_long_unknown_s "unknown" #define KT_REMOVE_long_utc_s "utc" #define KT_REMOVE_short_accessed_s_length 1 @@ -473,6 +474,7 @@ extern "C" { #define KT_REMOVE_long_tree_s_length 4 #define KT_REMOVE_long_updated_s_length 7 #define KT_REMOVE_long_user_s_length 4 + #define KT_REMOVE_long_unknown_s_length 7 #define KT_REMOVE_long_utc_s_length 3 extern const f_string_static_t kt_remove_short_accessed_s; @@ -524,6 +526,7 @@ extern "C" { extern const f_string_static_t kt_remove_long_tree_s; extern const f_string_static_t kt_remove_long_updated_s; extern const f_string_static_t kt_remove_long_user_s; + extern const f_string_static_t kt_remove_long_unknown_s; extern const f_string_static_t kt_remove_long_utc_s; #endif // _di_kt_remove_parameter_s_ diff --git a/sources/c/program/kevux/tools/remove/main/common/type.c b/sources/c/program/kevux/tools/remove/main/common/type.c index 4452849..8cb0fda 100644 --- a/sources/c/program/kevux/tools/remove/main/common/type.c +++ b/sources/c/program/kevux/tools/remove/main/common/type.c @@ -4,7 +4,6 @@ extern "C" { #endif - #ifndef _di_kt_remove_cache_delete_ void kt_remove_cache_delete(kt_remove_cache_t * const cache) { diff --git a/sources/c/program/kevux/tools/remove/main/operate.c b/sources/c/program/kevux/tools/remove/main/operate.c index acc6998..75ca8a1 100644 --- a/sources/c/program/kevux/tools/remove/main/operate.c +++ b/sources/c/program/kevux/tools/remove/main/operate.c @@ -18,9 +18,10 @@ extern "C" { if (kt_remove_signal_check(main)) return; const uint16_t flag_operate = kt_remove_preprocess_file(main, path, 0); + f_status_t status = F_no; if (F_status_is_error_not(main->setting.state.status) && !(main->setting.flag & kt_remove_main_flag_simulate_d) && !(flag_operate & kt_remove_flag_file_operate_processed_d)) { - main->setting.state.status = flag_operate & kt_remove_flag_file_operate_directory_d + status = main->setting.state.status = flag_operate & kt_remove_flag_file_operate_directory_d ? kt_remove_operate_file_directory(main, path, flag_operate) : kt_remove_operate_file_remove(main, path, flag_operate); } @@ -34,7 +35,7 @@ extern "C" { } if (F_status_is_error_not(main->setting.state.status)) { - main->setting.state.status = F_okay; + main->setting.state.status = status; } } #endif // _di_kt_remove_operate_file_ diff --git a/sources/c/program/kevux/tools/remove/main/operate.h b/sources/c/program/kevux/tools/remove/main/operate.h index 98627ed..c7c5da2 100644 --- a/sources/c/program/kevux/tools/remove/main/operate.h +++ b/sources/c/program/kevux/tools/remove/main/operate.h @@ -21,17 +21,15 @@ extern "C" { * Must not be NULL. * * This alters main.setting.state.status: - * F_yes on success and file remove. - * F_no on success and file not removed. + * F_no on success and not removed. * F_data_not on success but path is an empty string. * - * F_no (with error bit set) on file not removed due to failure. - * - * Errors (with error bit) from: f_string_dynamic_append(). + * Success from: kt_remove_operate_file_directory() + * Success from: kt_remove_operate_file_remove() * * Errors (with error bit) from: kt_remove_operate_file_directory(). - * Errors (with error bit) from: kt_remove_operate_file_remove_delete(). * Errors (with error bit) from: kt_remove_operate_file_remove(). + * Errors (with error bit) from: kt_remove_operate_memory_save(). * Errors (with error bit) from: kt_remove_preprocess_file(). * @param path * The path to the file to operate on. @@ -40,11 +38,9 @@ extern "C" { * This should always be FALSE if calling from within a fl_directory_do() callback. * This is because fl_directory_do() handles directory traversal and processing. * - * @see f_string_dynamic_append() - * * @see kt_remove_operate_file_directory() - * @see kt_remove_operate_file_remove_delete() * @see kt_remove_operate_file_remove() + * @see kt_remove_operate_memory_save() * @see kt_remove_preprocess_file() */ #ifndef _di_kt_remove_operate_file_ @@ -114,17 +110,13 @@ extern "C" { * Must not be NULL. * * This alters main.setting.state.status: - * F_yes on success and file remove. - * F_no on success and file not removed. + * F_okay on success. * F_data_not on success but path is an empty string. * * F_no (with error bit set) on file not removed due to failure. * - * Errors (with error bit) from: f_string_dynamic_append(). - * - * Errors (with error bit) from: kt_remove_operate_file_directory(). - * Errors (with error bit) from: kt_remove_operate_file_remove_delete(). * Errors (with error bit) from: kt_remove_operate_file_remove(). + * Errors (with error bit) from: kt_remove_operate_memory_save(). * Errors (with error bit) from: kt_remove_preprocess_file(). * @param path * The path to the file to operate on. @@ -133,11 +125,8 @@ extern "C" { * This should always be FALSE if calling from within a fl_directory_do() callback. * This is because fl_directory_do() handles directory traversal and processing. * - * @see f_string_dynamic_append() - * - * @see kt_remove_operate_file_directory() - * @see kt_remove_operate_file_remove_delete() * @see kt_remove_operate_file_remove() + * @see kt_remove_operate_memory_save() * @see kt_remove_preprocess_file() */ #ifndef _di_kt_remove_operate_file_parent_ diff --git a/sources/c/program/kevux/tools/remove/main/preprocess.c b/sources/c/program/kevux/tools/remove/main/preprocess.c index b1f91c6..8a382ed 100644 --- a/sources/c/program/kevux/tools/remove/main/preprocess.c +++ b/sources/c/program/kevux/tools/remove/main/preprocess.c @@ -133,6 +133,12 @@ extern "C" { } } + if (main->setting.flag & kt_remove_main_flag_unknown_d) { + if (macro_f_file_type_is_unknown(statistics.st_mode)) { + flag_out |= kt_remove_flag_file_operate_remove_d; + } + } + if (main->setting.flag & kt_remove_main_flag_user_d) { for (i = 0; i < main->setting.users.used; ++i) { @@ -146,13 +152,13 @@ extern "C" { } if (main->setting.flag & kt_remove_main_flag_same_d) { - if (statistics.st_uid != geteuid()) { + if (statistics.st_uid == geteuid()) { flag_out |= kt_remove_flag_file_operate_remove_d; } } if (main->setting.flag & kt_remove_main_flag_different_d) { - if (statistics.st_uid == geteuid()) { + if (statistics.st_uid != geteuid()) { flag_out |= kt_remove_flag_file_operate_remove_d; } } diff --git a/sources/c/program/kevux/tools/remove/main/print/message.c b/sources/c/program/kevux/tools/remove/main/print/message.c index a4c55fe..327e2bb 100644 --- a/sources/c/program/kevux/tools/remove/main/print/message.c +++ b/sources/c/program/kevux/tools/remove/main/print/message.c @@ -53,6 +53,7 @@ extern "C" { fll_program_print_help_option_long(print, kt_remove_long_local_s, f_console_symbol_long_normal_s, " Designate dates are in local time, unless time zone is specified."); fll_program_print_help_option_long(print, kt_remove_long_remember_s, f_console_symbol_long_normal_s, "Remember paths of files already deleted so as to not potentially error out on already removed path."); + fll_program_print_help_option_long(print, kt_remove_long_unknown_s, f_console_symbol_long_normal_s, " Remove by file type of unknown."); fll_program_print_help_option_long(print, kt_remove_long_utc_s, f_console_symbol_long_normal_s, " Designate dates are in UTC, unless time zone is specified."); f_print_dynamic_raw(f_string_eol_s, print->to); diff --git a/sources/c/program/kevux/tools/remove/main/print/simulate.c b/sources/c/program/kevux/tools/remove/main/print/simulate.c index 92dbb2f..f121182 100644 --- a/sources/c/program/kevux/tools/remove/main/print/simulate.c +++ b/sources/c/program/kevux/tools/remove/main/print/simulate.c @@ -121,31 +121,35 @@ extern "C" { if (!(main->setting.flag & kt_remove_main_flag_simulate_d)) return F_output_not; if (main->setting.flag & kt_remove_main_flag_block_d) { - fll_print_format(" block %r%r", print->to, (macro_f_file_type_get(statistics.st_mode) == F_file_type_block_d) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); + fll_print_format(" block %r%r", print->to, macro_f_file_type_is_block(statistics.st_mode) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); } if (main->setting.flag & kt_remove_main_flag_character_d) { - fll_print_format(" character %r%r", print->to, (macro_f_file_type_get(statistics.st_mode) == F_file_type_character_d) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); + fll_print_format(" character %r%r", print->to, macro_f_file_type_is_character(statistics.st_mode) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); } if (main->setting.flag & kt_remove_main_flag_directory_d) { - fll_print_format(" directory %r%r", print->to, (macro_f_file_type_get(statistics.st_mode) == F_file_type_directory_d) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); + fll_print_format(" directory %r%r", print->to, macro_f_file_type_is_directory(statistics.st_mode) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); } if (main->setting.flag & kt_remove_main_flag_fifo_d) { - fll_print_format(" fifo %r%r", print->to, (macro_f_file_type_get(statistics.st_mode) == F_file_type_fifo_d) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); + fll_print_format(" fifo %r%r", print->to, macro_f_file_type_is_fifo(statistics.st_mode) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); } if (main->setting.flag & kt_remove_main_flag_link_d) { - fll_print_format(" link %r%r", print->to, (macro_f_file_type_get(statistics.st_mode) == F_file_type_link_d) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); + fll_print_format(" link %r%r", print->to, macro_f_file_type_is_link(statistics.st_mode) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); } if (main->setting.flag & kt_remove_main_flag_regular_d) { - fll_print_format(" regular %r%r", print->to, (macro_f_file_type_get(statistics.st_mode) == F_file_type_regular_d) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); + fll_print_format(" regular %r%r", print->to, macro_f_file_type_is_regular(statistics.st_mode) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); } if (main->setting.flag & kt_remove_main_flag_socket_d) { - fll_print_format(" socket %r%r", print->to, (macro_f_file_type_get(statistics.st_mode) == F_file_type_socket_d) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); + fll_print_format(" socket %r%r", print->to, macro_f_file_type_is_socket(statistics.st_mode) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); + } + + if (main->setting.flag & kt_remove_main_flag_unknown_d) { + fll_print_format(" unknown %r%r", print->to, macro_f_file_type_is_unknown(statistics.st_mode) ? kt_remove_yes_s : kt_remove_no_s, f_string_eol_s); } f_number_unsigned_t i = 0; diff --git a/tests/unit/remove/c/mock-remove.c b/tests/unit/remove/c/mock-remove.c index f9e509c..becce9c 100644 --- a/tests/unit/remove/c/mock-remove.c +++ b/tests/unit/remove/c/mock-remove.c @@ -9,6 +9,28 @@ int mock_unwrap_fl_directory_do = 0; f_status_t mock_status = 0; +f_status_t __wrap_f_account_group_id_by_name(const f_string_static_t name, f_gid_t * const id) { + + const f_gid_t mock_id = mock_type(f_gid_t); + + if (id) { + *id = mock_id; + } + + return mock_type(f_status_t); +} + +f_status_t __wrap_f_account_id_by_name(const f_string_static_t name, f_uid_t * const id) { + + const f_uid_t mock_id = mock_type(f_uid_t); + + if (id) { + *id = mock_id; + } + + return mock_type(f_status_t); +} + f_status_t __wrap_f_directory_empty(const f_string_static_t path) { return mock_type(f_status_t); } @@ -67,6 +89,10 @@ f_status_t __wrap_fll_program_print_version(fl_print_t * const print, const f_st return mock_status; } +uid_t __wrap_geteuid(void) { + return mock_type(uid_t); +} + f_status_t __wrap_kt_remove_print_message_help(fl_print_t * const print, const f_color_context_t context) { mock_status = mock_type(f_status_t); diff --git a/tests/unit/remove/c/mock-remove.h b/tests/unit/remove/c/mock-remove.h index 9fa4ede..e3cb847 100644 --- a/tests/unit/remove/c/mock-remove.h +++ b/tests/unit/remove/c/mock-remove.h @@ -35,6 +35,9 @@ extern f_status_t mock_status; extern void __real_fl_directory_do(const f_string_static_t path, f_directory_recurse_do_t * const recurse); +extern f_status_t __wrap_f_account_group_id_by_name(const f_string_static_t name, f_gid_t * const id); +extern f_status_t __wrap_f_account_id_by_name(const f_string_static_t name, f_uid_t * const id); + extern f_status_t __wrap_f_directory_empty(const f_string_static_t path); extern f_status_t __wrap_f_directory_remove(const f_string_static_t path, const int depth_max, const bool preserve); @@ -47,6 +50,8 @@ extern void __wrap_fl_directory_do(const f_string_static_t path, f_directory_rec extern f_status_t __wrap_fll_program_print_version(fl_print_t * const print, const f_string_static_t version); +extern uid_t __wrap_geteuid(void); + extern f_status_t __wrap_kt_remove_print_message_help(fl_print_t * const print, const f_color_context_t context); #ifdef __cplusplus diff --git a/tests/unit/remove/c/test-remove-directory_no_args.c b/tests/unit/remove/c/test-remove-directory_no_args.c index 7971eba..7fe8d7d 100644 --- a/tests/unit/remove/c/test-remove-directory_no_args.c +++ b/tests/unit/remove/c/test-remove-directory_no_args.c @@ -113,7 +113,7 @@ void test__kt_remove__directory_no_args__one_empty_exists_link_not(void **state) // Pre-process file. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -140,7 +140,7 @@ void test__kt_remove__directory_no_args__one_empty_exists_link_not(void **state) // Pre-process file. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -167,7 +167,7 @@ void test__kt_remove__directory_no_args__one_empty_exists_link_not(void **state) // Pre-process file. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -344,7 +344,7 @@ void test__kt_remove__directory_no_args__one_empty_not_exists_link_not(void **st // Pre-process file. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -374,7 +374,7 @@ void test__kt_remove__directory_no_args__one_empty_not_exists_link_not(void **st // Pre-process file. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -404,7 +404,7 @@ void test__kt_remove__directory_no_args__one_empty_not_exists_link_not(void **st // Pre-process file. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -443,7 +443,7 @@ void test__kt_remove__directory_no_args__two_empty_exists_and_not(void **state) // Pre-process file 2. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -473,7 +473,7 @@ void test__kt_remove__directory_no_args__two_empty_exists_and_not(void **state) // Pre-process file 2. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -503,7 +503,7 @@ void test__kt_remove__directory_no_args__two_empty_exists_and_not(void **state) // Pre-process file 2. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -677,7 +677,7 @@ void test__kt_remove__directory_no_args__two_empty_exists_link_not(void **state) // Pre-process file 1. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -720,7 +720,7 @@ void test__kt_remove__directory_no_args__two_empty_exists_link_not(void **state) // Pre-process file 1. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -763,7 +763,7 @@ void test__kt_remove__directory_no_args__two_empty_exists_link_not(void **state) // Pre-process file 1. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); diff --git a/tests/unit/remove/c/test-remove-directory_tree_simple.c b/tests/unit/remove/c/test-remove-directory_tree_simple.c index 1b31cc0..c5cdb18 100644 --- a/tests/unit/remove/c/test-remove-directory_tree_simple.c +++ b/tests/unit/remove/c/test-remove-directory_tree_simple.c @@ -31,6 +31,95 @@ void test__kt_remove__directory_tree_simple__one_child_none_parent_one_exists_li will_return(__wrap_f_file_stat, &stat_regular); will_return(__wrap_f_file_stat, F_okay); + // Target processing. + will_return(__wrap_f_file_remove, F_okay); + + // Pre-process parent directory 1. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stat_directory); + will_return(__wrap_f_file_stat, F_okay); + will_return(__wrap_f_directory_empty, F_false); + + // Parent 1 processing. + will_return(__wrap_f_directory_remove, F_okay); + + const int result = kt_main_test__remove(3, argv, 0); + + assert_int_equal(result, 0); + } +} + +void test__kt_remove__directory_tree_simple__one_child_none_parent_one_exists_link_not(void **state) { + + mock_unwrap = 0; + + const f_string_static_t target_full = macro_f_string_static_t_initialize_1("parent_1/to_remove", 0, 18); + + struct stat stat_directory; + struct stat stat_regular; + + { + const f_string_t argv[] = { "mocked_main", target_full.string, "-t", 0 }; + + memset(&stat_directory, 0, sizeof(struct stat)); + memset(&stat_regular, 0, sizeof(struct stat)); + + stat_directory.st_mode = F_file_mode_all_d | F_file_type_directory_d; + stat_regular.st_mode = F_file_mode_all_d | F_file_type_regular_d; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stat_regular); + will_return(__wrap_f_file_stat, F_okay); + + // Target processing. + will_return(__wrap_f_file_remove, F_okay); + + // Pre-process parent directory 1. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stat_directory); + will_return(__wrap_f_file_stat, F_okay); + will_return(__wrap_f_directory_empty, F_false); + + // Parent 1 processing. + will_return(__wrap_f_directory_remove, F_okay); + + const int result = kt_main_test__remove(3, argv, 0); + + assert_int_equal(result, 0); + } +} + +void test__kt_remove__directory_tree_simple__one_child_none_parent_two_exists_link(void **state) { + + mock_unwrap = 0; + + const f_string_static_t target_full = macro_f_string_static_t_initialize_1("parent_2/parent_1/to_remove", 0, 18); + + struct stat stat_directory; + struct stat stat_regular; + + { + const f_string_t argv[] = { "mocked_main", target_full.string, "-t", 0 }; + + memset(&stat_directory, 0, sizeof(struct stat)); + memset(&stat_regular, 0, sizeof(struct stat)); + + stat_directory.st_mode = F_file_mode_all_d | F_file_type_directory_d; + stat_regular.st_mode = F_file_mode_all_d | F_file_type_regular_d; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_true); // A link, kt_remove_flag_file_operate_link_d is set. + will_return(__wrap_f_file_stat, &stat_regular); + will_return(__wrap_f_file_stat, F_okay); + + // Target processing. + will_return(__wrap_f_file_remove, F_okay); + // Pre-process parent directory 1. will_return(__wrap_f_file_exists, F_true); will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. @@ -38,12 +127,72 @@ void test__kt_remove__directory_tree_simple__one_child_none_parent_one_exists_li will_return(__wrap_f_file_stat, F_okay); will_return(__wrap_f_directory_empty, F_false); + // Parent 1 processing. + will_return(__wrap_f_directory_remove, F_okay); + + // Pre-process parent directory 2. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stat_directory); + will_return(__wrap_f_file_stat, F_okay); + will_return(__wrap_f_directory_empty, F_false); + + // Parent 2 processing. + will_return(__wrap_f_directory_remove, F_okay); + + const int result = kt_main_test__remove(3, argv, 0); + + assert_int_equal(result, 0); + } +} + +void test__kt_remove__directory_tree_simple__one_child_none_parent_two_exists_link_not(void **state) { + + mock_unwrap = 0; + + const f_string_static_t target_full = macro_f_string_static_t_initialize_1("parent_2/parent_1/to_remove", 0, 18); + + struct stat stat_directory; + struct stat stat_regular; + + { + const f_string_t argv[] = { "mocked_main", target_full.string, "-t", 0 }; + + memset(&stat_directory, 0, sizeof(struct stat)); + memset(&stat_regular, 0, sizeof(struct stat)); + + stat_directory.st_mode = F_file_mode_all_d | F_file_type_directory_d; + stat_regular.st_mode = F_file_mode_all_d | F_file_type_regular_d; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stat_regular); + will_return(__wrap_f_file_stat, F_okay); + // Target processing. will_return(__wrap_f_file_remove, F_okay); + // Pre-process parent directory 1. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stat_directory); + will_return(__wrap_f_file_stat, F_okay); + will_return(__wrap_f_directory_empty, F_false); + // Parent 1 processing. will_return(__wrap_f_directory_remove, F_okay); + // Pre-process parent directory 2. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stat_directory); + will_return(__wrap_f_file_stat, F_okay); + will_return(__wrap_f_directory_empty, F_false); + + // Parent 2 processing. + will_return(__wrap_f_directory_remove, F_okay); + const int result = kt_main_test__remove(3, argv, 0); assert_int_equal(result, 0); diff --git a/tests/unit/remove/c/test-remove-directory_tree_simple.h b/tests/unit/remove/c/test-remove-directory_tree_simple.h index 86a33dc..9d4f911 100644 --- a/tests/unit/remove/c/test-remove-directory_tree_simple.h +++ b/tests/unit/remove/c/test-remove-directory_tree_simple.h @@ -15,4 +15,19 @@ */ extern void test__kt_remove__directory_tree_simple__one_child_none_parent_one_exists_link(void **state); +/** + * Test that the remove works for one file that exists and is not a link, for directory files that has no children and one parent with the tree argument. + */ +extern void test__kt_remove__directory_tree_simple__one_child_none_parent_one_exists_link_not(void **state); + +/** + * Test that the remove works for one file that exists and is a link, for directory files that has no children and two parents with the tree argument. + */ +extern void test__kt_remove__directory_tree_simple__one_child_none_parent_two_exists_link(void **state); + +/** + * Test that the remove works for one file that exists and is not a link, for directory files that has no children and two parents with the tree argument. + */ +extern void test__kt_remove__directory_tree_simple__one_child_none_parent_two_exists_link_not(void **state); + #endif // _TEST__KT_remove__directory_tree_simple diff --git a/tests/unit/remove/c/test-remove-file_type.c b/tests/unit/remove/c/test-remove-file_type.c new file mode 100644 index 0000000..0ac15a1 --- /dev/null +++ b/tests/unit/remove/c/test-remove-file_type.c @@ -0,0 +1,88 @@ +#include "test-remove.h" +#include "test-remove-file_type.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void test__kt_remove__file_type__works(void **state) { + + mock_unwrap = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + + const uint8_t total = 8; + + struct stat stats[total]; + + memset(stats, 0, sizeof(struct stat) * total); + + stats[0].st_mode = F_file_mode_all_d | F_file_type_block_d; + stats[1].st_mode = F_file_mode_all_d | F_file_type_character_d; + stats[2].st_mode = F_file_mode_all_d | F_file_type_directory_d; + stats[3].st_mode = F_file_mode_all_d | F_file_type_fifo_d; + stats[4].st_mode = F_file_mode_all_d | F_file_type_link_d; + stats[5].st_mode = F_file_mode_all_d | F_file_type_regular_d; + stats[6].st_mode = F_file_mode_all_d | F_file_type_socket_d; + stats[7].st_mode = F_file_mode_all_d & ~S_IFMT; + + const f_string_t types[] = { + "-" KT_REMOVE_short_block_s, + "-" KT_REMOVE_short_character_s, + "-" KT_REMOVE_short_directory_s, + "-" KT_REMOVE_short_fifo_s, + "-" KT_REMOVE_short_link_s, + "-" KT_REMOVE_short_regular_s, + "-" KT_REMOVE_short_socket_s, + "--" KT_REMOVE_long_unknown_s, + }; + + { + uint8_t i = 0; + uint8_t type = 0; + + for (; i < total; ++i) { + + for (type = 0; type < total; ++type) { + + const f_string_t argv[] = { "mocked_main", target.string, types[type], 0 }; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[i].st_mode)); + will_return(__wrap_f_file_stat, &stats[i]); + will_return(__wrap_f_file_stat, F_okay); + + // Process file. + if (i == type) { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + will_return(__wrap_fl_directory_do, 1); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, f_directory_recurse_do_flag_action_e | f_directory_recurse_do_flag_directory_e | f_directory_recurse_do_flag_top_after_e); + will_return(__wrap_f_directory_remove, F_okay); + } + else { + will_return(__wrap_f_file_remove, F_okay); + } + } + else { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + } + + const int result = kt_main_test__remove(3, argv, 0); + + assert_int_equal(result, 0); + } + } // for + } +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/tests/unit/remove/c/test-remove-file_type.h b/tests/unit/remove/c/test-remove-file_type.h new file mode 100644 index 0000000..df07314 --- /dev/null +++ b/tests/unit/remove/c/test-remove-file_type.h @@ -0,0 +1,18 @@ +/** + * Kevux Tools - Remove + * + * Project: Kevux Tools + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the remove. + */ +#ifndef _TEST__KT_remove__file_type +#define _TEST__KT_remove__file_type + +/** + * Test that the remove removes or doesn't remove based on file type. + */ +extern void test__kt_remove__file_type__works(void **state); + +#endif // _TEST__KT_remove__file_type diff --git a/tests/unit/remove/c/test-remove-group.c b/tests/unit/remove/c/test-remove-group.c new file mode 100644 index 0000000..c1a5aab --- /dev/null +++ b/tests/unit/remove/c/test-remove-group.c @@ -0,0 +1,73 @@ +#include "test-remove.h" +#include "test-remove-group.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void test__kt_remove__group__name_works(void **state) { + + mock_unwrap = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_group_s, "some_name", 0 }; + + const uint8_t total = 4; + const f_uid_t group_id = 2; + + struct stat stats[total]; + + memset(stats, 0, sizeof(struct stat) * total); + + stats[0].st_mode = F_file_mode_all_d | F_file_type_directory_d; + stats[1].st_mode = F_file_mode_all_d | F_file_type_directory_d; + stats[2].st_mode = F_file_mode_all_d | F_file_type_regular_d; + stats[3].st_mode = F_file_mode_all_d | F_file_type_regular_d; + + stats[0].st_gid = 1; + stats[1].st_gid = 2; + stats[2].st_gid = 1; + stats[3].st_gid = 2; + + for (uint8_t i = 0; i < total; ++i) { + + will_return(__wrap_f_account_group_id_by_name, group_id); + will_return(__wrap_f_account_group_id_by_name, F_okay); + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stats[i]); + will_return(__wrap_f_file_stat, F_okay); + + // Process file. + if (stats[i].st_gid == group_id) { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + will_return(__wrap_fl_directory_do, 1); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, f_directory_recurse_do_flag_action_e | f_directory_recurse_do_flag_directory_e | f_directory_recurse_do_flag_top_after_e); + will_return(__wrap_f_directory_remove, F_okay); + } + else { + will_return(__wrap_f_file_remove, F_okay); + } + } + else { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + } + + const int result = kt_main_test__remove(4, argv, 0); + + assert_int_equal(result, 0); + } // for +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/tests/unit/remove/c/test-remove-group.h b/tests/unit/remove/c/test-remove-group.h new file mode 100644 index 0000000..01f9398 --- /dev/null +++ b/tests/unit/remove/c/test-remove-group.h @@ -0,0 +1,18 @@ +/** + * Kevux Tools - Remove + * + * Project: Kevux Tools + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the remove. + */ +#ifndef _TEST__KT_remove__group +#define _TEST__KT_remove__group + +/** + * Test that the remove removes or doesn't remove based on group name. + */ +extern void test__kt_remove__group__name_works(void **state); + +#endif // _TEST__KT_remove__group diff --git a/tests/unit/remove/c/test-remove-regular_no_args.c b/tests/unit/remove/c/test-remove-regular_no_args.c index b099e85..63484be 100644 --- a/tests/unit/remove/c/test-remove-regular_no_args.c +++ b/tests/unit/remove/c/test-remove-regular_no_args.c @@ -88,7 +88,7 @@ void test__kt_remove__regular_no_args__one_exists_link_not(void **state) { // Pre-process file. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -108,7 +108,7 @@ void test__kt_remove__regular_no_args__one_exists_link_not(void **state) { // Pre-process file. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -128,7 +128,7 @@ void test__kt_remove__regular_no_args__one_exists_link_not(void **state) { // Pre-process file. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -193,7 +193,7 @@ void test__kt_remove__regular_no_args__two_exists_and_not(void **state) { // Pre-process file 2. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -216,7 +216,7 @@ void test__kt_remove__regular_no_args__two_exists_and_not(void **state) { // Pre-process file 2. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -239,7 +239,7 @@ void test__kt_remove__regular_no_args__two_exists_and_not(void **state) { // Pre-process file 2. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -360,7 +360,7 @@ void test__kt_remove__regular_no_args__two_exists_link_not(void **state) { // Pre-process file 1. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -369,7 +369,7 @@ void test__kt_remove__regular_no_args__two_exists_link_not(void **state) { // Pre-process file 2. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -389,7 +389,7 @@ void test__kt_remove__regular_no_args__two_exists_link_not(void **state) { // Pre-process file 1. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -398,7 +398,7 @@ void test__kt_remove__regular_no_args__two_exists_link_not(void **state) { // Pre-process file 2. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -418,7 +418,7 @@ void test__kt_remove__regular_no_args__two_exists_link_not(void **state) { // Pre-process file 1. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); @@ -427,7 +427,7 @@ void test__kt_remove__regular_no_args__two_exists_link_not(void **state) { // Pre-process file 2. will_return(__wrap_f_file_exists, F_true); - will_return(__wrap_f_file_is, F_false); // A link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. will_return(__wrap_f_file_stat, &statistics); will_return(__wrap_f_file_stat, F_okay); diff --git a/tests/unit/remove/c/test-remove-user.c b/tests/unit/remove/c/test-remove-user.c new file mode 100644 index 0000000..21b0864 --- /dev/null +++ b/tests/unit/remove/c/test-remove-user.c @@ -0,0 +1,193 @@ +#include "test-remove.h" +#include "test-remove-user.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void test__kt_remove__user__different_works(void **state) { + + mock_unwrap = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_different_s, 0 }; + + const uint8_t total = 4; + const f_uid_t user_id = 2; + + struct stat stats[total]; + + memset(stats, 0, sizeof(struct stat) * total); + + stats[0].st_mode = F_file_mode_all_d | F_file_type_directory_d; + stats[1].st_mode = F_file_mode_all_d | F_file_type_directory_d; + stats[2].st_mode = F_file_mode_all_d | F_file_type_regular_d; + stats[3].st_mode = F_file_mode_all_d | F_file_type_regular_d; + + stats[0].st_uid = 1; + stats[1].st_uid = 2; + stats[2].st_uid = 1; + stats[3].st_uid = 2; + + for (uint8_t i = 0; i < total; ++i) { + + will_return(__wrap_geteuid, user_id); + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stats[i]); + will_return(__wrap_f_file_stat, F_okay); + + // Process file. + if (stats[i].st_uid == user_id) { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + } + else { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + will_return(__wrap_fl_directory_do, 1); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, f_directory_recurse_do_flag_action_e | f_directory_recurse_do_flag_directory_e | f_directory_recurse_do_flag_top_after_e); + will_return(__wrap_f_directory_remove, F_okay); + } + else { + will_return(__wrap_f_file_remove, F_okay); + } + } + + const int result = kt_main_test__remove(3, argv, 0); + + assert_int_equal(result, 0); + } // for +} + +void test__kt_remove__user__name_works(void **state) { + + mock_unwrap = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_user_s, "some_name", 0 }; + + const uint8_t total = 4; + const f_uid_t user_id = 2; + + struct stat stats[total]; + + memset(stats, 0, sizeof(struct stat) * total); + + stats[0].st_mode = F_file_mode_all_d | F_file_type_directory_d; + stats[1].st_mode = F_file_mode_all_d | F_file_type_directory_d; + stats[2].st_mode = F_file_mode_all_d | F_file_type_regular_d; + stats[3].st_mode = F_file_mode_all_d | F_file_type_regular_d; + + stats[0].st_uid = 1; + stats[1].st_uid = 2; + stats[2].st_uid = 1; + stats[3].st_uid = 2; + + for (uint8_t i = 0; i < total; ++i) { + + will_return(__wrap_f_account_id_by_name, user_id); + will_return(__wrap_f_account_id_by_name, F_okay); + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stats[i]); + will_return(__wrap_f_file_stat, F_okay); + + // Process file. + if (stats[i].st_uid == user_id) { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + will_return(__wrap_fl_directory_do, 1); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, f_directory_recurse_do_flag_action_e | f_directory_recurse_do_flag_directory_e | f_directory_recurse_do_flag_top_after_e); + will_return(__wrap_f_directory_remove, F_okay); + } + else { + will_return(__wrap_f_file_remove, F_okay); + } + } + else { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + } + + const int result = kt_main_test__remove(4, argv, 0); + + assert_int_equal(result, 0); + } // for +} + +void test__kt_remove__user__same_works(void **state) { + + mock_unwrap = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_same_s, 0 }; + + const uint8_t total = 4; + const f_uid_t user_id = 2; + + struct stat stats[total]; + + memset(stats, 0, sizeof(struct stat) * total); + + stats[0].st_mode = F_file_mode_all_d | F_file_type_directory_d; + stats[1].st_mode = F_file_mode_all_d | F_file_type_directory_d; + stats[2].st_mode = F_file_mode_all_d | F_file_type_regular_d; + stats[3].st_mode = F_file_mode_all_d | F_file_type_regular_d; + + stats[0].st_uid = 1; + stats[1].st_uid = 2; + stats[2].st_uid = 1; + stats[3].st_uid = 2; + + for (uint8_t i = 0; i < total; ++i) { + + will_return(__wrap_geteuid, user_id); + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, F_false); // Not a link, kt_remove_flag_file_operate_link_d is not set. + will_return(__wrap_f_file_stat, &stats[i]); + will_return(__wrap_f_file_stat, F_okay); + + // Process file. + if (stats[i].st_uid == user_id) { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + will_return(__wrap_fl_directory_do, 1); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, &target); + will_return(__wrap_fl_directory_do, f_directory_recurse_do_flag_action_e | f_directory_recurse_do_flag_directory_e | f_directory_recurse_do_flag_top_after_e); + will_return(__wrap_f_directory_remove, F_okay); + } + else { + will_return(__wrap_f_file_remove, F_okay); + } + } + else { + if (macro_f_file_type_is_directory(stats[i].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + } + + const int result = kt_main_test__remove(3, argv, 0); + + assert_int_equal(result, 0); + } // for +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/tests/unit/remove/c/test-remove-user.h b/tests/unit/remove/c/test-remove-user.h new file mode 100644 index 0000000..3e59030 --- /dev/null +++ b/tests/unit/remove/c/test-remove-user.h @@ -0,0 +1,28 @@ +/** + * Kevux Tools - Remove + * + * Project: Kevux Tools + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the remove. + */ +#ifndef _TEST__KT_remove__user +#define _TEST__KT_remove__user + +/** + * Test that the remove removes or doesn't remove based on user is the different from the current user. + */ +extern void test__kt_remove__user__different_works(void **state); + +/** + * Test that the remove removes or doesn't remove based on user name. + */ +extern void test__kt_remove__user__name_works(void **state); + +/** + * Test that the remove removes or doesn't remove based on user is the same as the current user. + */ +extern void test__kt_remove__user__same_works(void **state); + +#endif // _TEST__KT_remove__user diff --git a/tests/unit/remove/c/test-remove.c b/tests/unit/remove/c/test-remove.c index f86c513..a89106d 100644 --- a/tests/unit/remove/c/test-remove.c +++ b/tests/unit/remove/c/test-remove.c @@ -39,6 +39,13 @@ int main(void) { cmocka_unit_test(test__kt_remove__directory_recurse_simple__one_child_two_exists_link_not), cmocka_unit_test(test__kt_remove__directory_tree_simple__one_child_none_parent_one_exists_link), + cmocka_unit_test(test__kt_remove__directory_tree_simple__one_child_none_parent_one_exists_link_not), + cmocka_unit_test(test__kt_remove__directory_tree_simple__one_child_none_parent_two_exists_link), + cmocka_unit_test(test__kt_remove__directory_tree_simple__one_child_none_parent_two_exists_link_not), + + cmocka_unit_test(test__kt_remove__file_type__works), + + cmocka_unit_test(test__kt_remove__group__name_works), cmocka_unit_test(test__kt_remove__regular_no_args__one_exists_link), cmocka_unit_test(test__kt_remove__regular_no_args__one_exists_link_not), @@ -48,6 +55,10 @@ int main(void) { cmocka_unit_test(test__kt_remove__regular_no_args__two_exists_link), cmocka_unit_test(test__kt_remove__regular_no_args__two_exists_link_not), cmocka_unit_test(test__kt_remove__regular_no_args__two_exists_not), + + cmocka_unit_test(test__kt_remove__user__different_works), + cmocka_unit_test(test__kt_remove__user__name_works), + cmocka_unit_test(test__kt_remove__user__same_works), }; return cmocka_run_group_tests(tests, setup, setdown); diff --git a/tests/unit/remove/c/test-remove.h b/tests/unit/remove/c/test-remove.h index 1b40857..d24851a 100644 --- a/tests/unit/remove/c/test-remove.h +++ b/tests/unit/remove/c/test-remove.h @@ -30,12 +30,15 @@ #include "main-test-remove.h" // Test includes. -#include "test-remove-print_help.h" -#include "test-remove-print_version.h" #include "test-remove-directory_no_args.h" #include "test-remove-directory_recurse_simple.h" #include "test-remove-directory_tree_simple.h" +#include "test-remove-file_type.h" +#include "test-remove-group.h" +#include "test-remove-print_help.h" +#include "test-remove-print_version.h" #include "test-remove-regular_no_args.h" +#include "test-remove-user.h" #ifdef __cplusplus extern "C" {