From: Kevin Day Date: Wed, 26 Mar 2025 22:43:56 +0000 (-0500) Subject: Progress: Continue working on completing the remove program. X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=4ad50604fb4f31d5f50225d01cfb4c885c6a767d;p=kevux-tools Progress: Continue working on completing the remove program. This wraps up most of the `remove` program functionality. It seems that I already had Time and EpochTime logic in place and I didn't have to do anything. Add the program unit tests for time and EpochTime. Fix some documentation comments in the program unit tests. There is a remaining `@todo` in the `remove` program that will need to be address in the FLL. I will have to brain storm on how I want to do this and I may end up adding callbacks to all file read and write functions. --- diff --git a/data/build/remove/settings-tests.remove b/data/build/remove/settings-tests.remove index 15047b5..698b97f 100644 --- a/data/build/remove/settings-tests.remove +++ b/data/build/remove/settings-tests.remove @@ -30,13 +30,15 @@ build_libraries-individual -lf_account -lf_color -lf_compare -lf_console -lf_con 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-file_mode.c test-remove-file_type.c build_sources_program test-remove-date_accessed.c test-remove-date_changed.c test-remove-date_updated.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-epochtime.c +build_sources_program test-remove-file_mode.c test-remove-file_type.c build_sources_program test-remove-force.c build_sources_program test-remove-group.c +build_sources_program test-remove-print_help.c test-remove-print_version.c build_sources_program test-remove-regular_no_args.c +build_sources_program test-remove-time.c build_sources_program test-remove-unix.c build_sources_program test-remove-user.c diff --git a/sources/c/program/kevux/tools/remove/main/convert.c b/sources/c/program/kevux/tools/remove/main/convert.c index 1959a2b..9d108d1 100644 --- a/sources/c/program/kevux/tools/remove/main/convert.c +++ b/sources/c/program/kevux/tools/remove/main/convert.c @@ -52,8 +52,6 @@ extern "C" { return; } - // @todo add Time and EpochTime processing here. - uint8_t matches = 0; f_range_t range_first = f_range_t_initialize; f_range_t range_second = f_range_t_initialize; diff --git a/tests/unit/remove/c/test-remove-date_accessed.h b/tests/unit/remove/c/test-remove-date_accessed.h index 1627a06..a518876 100644 --- a/tests/unit/remove/c/test-remove-date_accessed.h +++ b/tests/unit/remove/c/test-remove-date_accessed.h @@ -11,27 +11,27 @@ #define _TEST__KT_remove__date_accessed /** - * Test that the remove works when the --changed parameter with date parameters passed. + * Test that the remove works when the --accessed parameter with date parameters passed. */ extern void test__kt_remove__date_accessed__date_works(void **state); /** - * Test that the remove works when the --changed parameter with "now" parameter passed. + * Test that the remove works when the --accessed parameter with "now" parameter passed. */ extern void test__kt_remove__date_accessed__now_works(void **state); /** - * Test that the remove works when the --changed parameter with "today" parameter passed. + * Test that the remove works when the --accessed parameter with "today" parameter passed. */ extern void test__kt_remove__date_accessed__today_works(void **state); /** - * Test that the remove works when the --changed parameter with "tomorrow" parameter passed. + * Test that the remove works when the --accessed parameter with "tomorrow" parameter passed. */ extern void test__kt_remove__date_accessed__tomorrow_works(void **state); /** - * Test that the remove works when the --changed parameter with "yesterday" parameter passed. + * Test that the remove works when the --accessed parameter with "yesterday" parameter passed. */ extern void test__kt_remove__date_accessed__yesterday_works(void **state); diff --git a/tests/unit/remove/c/test-remove-date_updated.h b/tests/unit/remove/c/test-remove-date_updated.h index 96b93ad..a96ad23 100644 --- a/tests/unit/remove/c/test-remove-date_updated.h +++ b/tests/unit/remove/c/test-remove-date_updated.h @@ -11,27 +11,27 @@ #define _TEST__KT_remove__date_updated /** - * Test that the remove works when the --changed parameter with date parameters passed. + * Test that the remove works when the --updated parameter with date parameters passed. */ extern void test__kt_remove__date_updated__date_works(void **state); /** - * Test that the remove works when the --changed parameter with "now" parameter passed. + * Test that the remove works when the --updated parameter with "now" parameter passed. */ extern void test__kt_remove__date_updated__now_works(void **state); /** - * Test that the remove works when the --changed parameter with "today" parameter passed. + * Test that the remove works when the --updated parameter with "today" parameter passed. */ extern void test__kt_remove__date_updated__today_works(void **state); /** - * Test that the remove works when the --changed parameter with "tomorrow" parameter passed. + * Test that the remove works when the --updated parameter with "tomorrow" parameter passed. */ extern void test__kt_remove__date_updated__tomorrow_works(void **state); /** - * Test that the remove works when the --changed parameter with "yesterday" parameter passed. + * Test that the remove works when the --updated parameter with "yesterday" parameter passed. */ extern void test__kt_remove__date_updated__yesterday_works(void **state); diff --git a/tests/unit/remove/c/test-remove-epochtime.c b/tests/unit/remove/c/test-remove-epochtime.c new file mode 100644 index 0000000..129a48e --- /dev/null +++ b/tests/unit/remove/c/test-remove-epochtime.c @@ -0,0 +1,483 @@ +#include "test-remove.h" +#include "test-remove-epochtime.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void test__kt_remove__epochtime__accessed_works(void **state) { + + mock_unwrap = 0; + mock_unwrap_f_time_clock_get = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + + const uint8_t params_total = 2; + const uint8_t types_total = 8; + const uint8_t dates_total = 24; + + struct stat stats[types_total]; + + memset(stats, 0, sizeof(struct stat) * types_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_static_t parameters[] = { + macro_f_string_static_t_initialize_1("1970::172800", 0, 12), + macro_f_string_static_t_initialize_1("::172800", 0, 8), + }; + + const f_string_static_t operators[] = { + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + }; + + const f_time_spec_t time_spec_dates[] = { + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Not + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + }; + + bool time_spec_removes[] = { + F_true, // Equal + F_true, + F_false, + F_false, + F_false, // Less + F_false, + F_false, + F_true, + F_true, // Less Equal + F_true, + F_false, + F_true, + F_false, // More + F_false, + F_true, + F_false, + F_true, // More Equal + F_true, + F_true, + F_false, + F_false, // Not + F_false, + F_true, + F_true, + }; + + { + uint8_t type = 0; + uint8_t date = 0; + uint8_t param = 0; + + for (; type < types_total; ++type) { + + for (date = 0; date < dates_total; ++date) { + + for (param = 0; param < params_total; ++param) { + + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_accessed_s, operators[date].string, parameters[param].string, 0 }; + + stats[type].st_atim = time_spec_dates[date]; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[type].st_mode)); + will_return(__wrap_f_file_stat, &stats[type]); + will_return(__wrap_f_file_stat, F_okay); + + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + + if (time_spec_removes[date]) { + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + 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(5, argv, 0); + + assert_int_equal(result, 0); + } // for + } // for + } // for + } +} + +void test__kt_remove__epochtime__changed_works(void **state) { + + mock_unwrap = 0; + mock_unwrap_f_time_clock_get = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + + const uint8_t params_total = 2; + const uint8_t types_total = 8; + const uint8_t dates_total = 24; + + struct stat stats[types_total]; + + memset(stats, 0, sizeof(struct stat) * types_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_static_t parameters[] = { + macro_f_string_static_t_initialize_1("1970::172800", 0, 12), + macro_f_string_static_t_initialize_1("::172800", 0, 8), + }; + + const f_string_static_t operators[] = { + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + }; + + const f_time_spec_t time_spec_dates[] = { + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Not + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + }; + + bool time_spec_removes[] = { + F_true, // Equal + F_true, + F_false, + F_false, + F_false, // Less + F_false, + F_false, + F_true, + F_true, // Less Equal + F_true, + F_false, + F_true, + F_false, // More + F_false, + F_true, + F_false, + F_true, // More Equal + F_true, + F_true, + F_false, + F_false, // Not + F_false, + F_true, + F_true, + }; + + { + uint8_t type = 0; + uint8_t date = 0; + uint8_t param = 0; + + for (; type < types_total; ++type) { + + for (date = 0; date < dates_total; ++date) { + + for (param = 0; param < params_total; ++param) { + + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_changed_s, operators[date].string, parameters[param].string, 0 }; + + stats[type].st_ctim = time_spec_dates[date]; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[type].st_mode)); + will_return(__wrap_f_file_stat, &stats[type]); + will_return(__wrap_f_file_stat, F_okay); + + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + + if (time_spec_removes[date]) { + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + 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(5, argv, 0); + + assert_int_equal(result, 0); + } // for + } // for + } // for + } +} + +void test__kt_remove__epochtime__updated_works(void **state) { + + mock_unwrap = 0; + mock_unwrap_f_time_clock_get = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + + const uint8_t params_total = 2; + const uint8_t types_total = 8; + const uint8_t dates_total = 24; + + struct stat stats[types_total]; + + memset(stats, 0, sizeof(struct stat) * types_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_static_t parameters[] = { + macro_f_string_static_t_initialize_1("1970::172800", 0, 12), + macro_f_string_static_t_initialize_1("::172800", 0, 8), + }; + + const f_string_static_t operators[] = { + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + }; + + const f_time_spec_t time_spec_dates[] = { + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Not + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + }; + + bool time_spec_removes[] = { + F_true, // Equal + F_true, + F_false, + F_false, + F_false, // Less + F_false, + F_false, + F_true, + F_true, // Less Equal + F_true, + F_false, + F_true, + F_false, // More + F_false, + F_true, + F_false, + F_true, // More Equal + F_true, + F_true, + F_false, + F_false, // Not + F_false, + F_true, + F_true, + }; + + { + uint8_t type = 0; + uint8_t date = 0; + uint8_t param = 0; + + for (; type < types_total; ++type) { + + for (date = 0; date < dates_total; ++date) { + + for (param = 0; param < params_total; ++param) { + + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_updated_s, operators[date].string, parameters[param].string, 0 }; + + stats[type].st_mtim = time_spec_dates[date]; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[type].st_mode)); + will_return(__wrap_f_file_stat, &stats[type]); + will_return(__wrap_f_file_stat, F_okay); + + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + + if (time_spec_removes[date]) { + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + 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(5, argv, 0); + + assert_int_equal(result, 0); + } // for + } // for + } // for + } +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/tests/unit/remove/c/test-remove-epochtime.h b/tests/unit/remove/c/test-remove-epochtime.h new file mode 100644 index 0000000..949933a --- /dev/null +++ b/tests/unit/remove/c/test-remove-epochtime.h @@ -0,0 +1,29 @@ +/** + * Kevux Tools - Remove + * + * Project: Kevux Tools + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the remove. + */ +#ifndef _TEST__KT_remove__epochtime +#define _TEST__KT_remove__epochtime + +/** + * Test that the remove works when the --accessed parameter with EpochTime parameters passed. + */ +extern void test__kt_remove__epochtime__accessed_works(void **state); + +/** + * Test that the remove works when the --changed parameter with EpochTime parameters passed. + */ +extern void test__kt_remove__epochtime__changed_works(void **state); + + +/** + * Test that the remove works when the --updated parameter with EpochTime parameters passed. + */ +extern void test__kt_remove__epochtime__updated_works(void **state); + +#endif // _TEST__KT_remove__epochtime diff --git a/tests/unit/remove/c/test-remove-time.c b/tests/unit/remove/c/test-remove-time.c new file mode 100644 index 0000000..e1e606d --- /dev/null +++ b/tests/unit/remove/c/test-remove-time.c @@ -0,0 +1,504 @@ +#include "test-remove.h" +#include "test-remove-time.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void test__kt_remove__time__accessed_works(void **state) { + + mock_unwrap = 0; + mock_unwrap_f_time_clock_get = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + + const uint8_t params_total = 2; + const uint8_t types_total = 8; + const uint8_t dates_total = 24; + + struct stat stats[types_total]; + + memset(stats, 0, sizeof(struct stat) * types_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_static_t parameters[] = { + macro_f_string_static_t_initialize_1("1970:172800000000000", 0, 20), + macro_f_string_static_t_initialize_1(":172800000000000", 0, 16), + }; + + const f_string_static_t operators[] = { + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + }; + + const f_time_spec_t time_spec_dates[] = { + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Not + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + }; + + bool time_spec_removes[] = { + F_true, // Equal + F_true, + F_false, + F_false, + F_false, // Less + F_false, + F_false, + F_true, + F_true, // Less Equal + F_true, + F_false, + F_true, + F_false, // More + F_false, + F_true, + F_false, + F_true, // More Equal + F_true, + F_true, + F_false, + F_false, // Not + F_false, + F_true, + F_true, + }; + + const f_time_spec_t time_spec_clocks = macro_f_time_spec_t_initialize_1(2 * 86400, 123); + + { + uint8_t type = 0; + uint8_t date = 0; + uint8_t param = 0; + + for (; type < types_total; ++type) { + + for (date = 0; date < dates_total; ++date) { + + for (param = 0; param < params_total; ++param) { + + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_accessed_s, operators[date].string, parameters[param].string, 0 }; + + stats[type].st_atim = time_spec_dates[date]; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[type].st_mode)); + will_return(__wrap_f_file_stat, &stats[type]); + will_return(__wrap_f_file_stat, F_okay); + + if (param) { + will_return(__wrap_f_time_clock_get, &time_spec_clocks); + will_return(__wrap_f_time_clock_get, F_okay); + } + + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + + if (time_spec_removes[date]) { + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + 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(5, argv, 0); + + assert_int_equal(result, 0); + } // for + } // for + } // for + } +} + +void test__kt_remove__time__changed_works(void **state) { + + mock_unwrap = 0; + mock_unwrap_f_time_clock_get = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + + const uint8_t params_total = 2; + const uint8_t types_total = 8; + const uint8_t dates_total = 24; + + struct stat stats[types_total]; + + memset(stats, 0, sizeof(struct stat) * types_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_static_t parameters[] = { + macro_f_string_static_t_initialize_1("1970:172800000000000", 0, 20), + macro_f_string_static_t_initialize_1(":172800000000000", 0, 16), + }; + + const f_string_static_t operators[] = { + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + }; + + const f_time_spec_t time_spec_dates[] = { + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Not + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + }; + + bool time_spec_removes[] = { + F_true, // Equal + F_true, + F_false, + F_false, + F_false, // Less + F_false, + F_false, + F_true, + F_true, // Less Equal + F_true, + F_false, + F_true, + F_false, // More + F_false, + F_true, + F_false, + F_true, // More Equal + F_true, + F_true, + F_false, + F_false, // Not + F_false, + F_true, + F_true, + }; + + const f_time_spec_t time_spec_clocks = macro_f_time_spec_t_initialize_1(2 * 86400, 123); + + { + uint8_t type = 0; + uint8_t date = 0; + uint8_t param = 0; + + for (; type < types_total; ++type) { + + for (date = 0; date < dates_total; ++date) { + + for (param = 0; param < params_total; ++param) { + + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_changed_s, operators[date].string, parameters[param].string, 0 }; + + stats[type].st_ctim = time_spec_dates[date]; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[type].st_mode)); + will_return(__wrap_f_file_stat, &stats[type]); + will_return(__wrap_f_file_stat, F_okay); + + if (param) { + will_return(__wrap_f_time_clock_get, &time_spec_clocks); + will_return(__wrap_f_time_clock_get, F_okay); + } + + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + + if (time_spec_removes[date]) { + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + 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(5, argv, 0); + + assert_int_equal(result, 0); + } // for + } // for + } // for + } +} + +void test__kt_remove__time__updated_works(void **state) { + + mock_unwrap = 0; + mock_unwrap_f_time_clock_get = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + + const uint8_t params_total = 2; + const uint8_t types_total = 8; + const uint8_t dates_total = 24; + + struct stat stats[types_total]; + + memset(stats, 0, sizeof(struct stat) * types_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_static_t parameters[] = { + macro_f_string_static_t_initialize_1("1970:172800000000000", 0, 20), + macro_f_string_static_t_initialize_1(":172800000000000", 0, 16), + }; + + const f_string_static_t operators[] = { + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + }; + + const f_time_spec_t time_spec_dates[] = { + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Not + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + }; + + bool time_spec_removes[] = { + F_true, // Equal + F_true, + F_false, + F_false, + F_false, // Less + F_false, + F_false, + F_true, + F_true, // Less Equal + F_true, + F_false, + F_true, + F_false, // More + F_false, + F_true, + F_false, + F_true, // More Equal + F_true, + F_true, + F_false, + F_false, // Not + F_false, + F_true, + F_true, + }; + + const f_time_spec_t time_spec_clocks = macro_f_time_spec_t_initialize_1(2 * 86400, 123); + + { + uint8_t type = 0; + uint8_t date = 0; + uint8_t param = 0; + + for (; type < types_total; ++type) { + + for (date = 0; date < dates_total; ++date) { + + for (param = 0; param < params_total; ++param) { + + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_updated_s, operators[date].string, parameters[param].string, 0 }; + + stats[type].st_mtim = time_spec_dates[date]; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[type].st_mode)); + will_return(__wrap_f_file_stat, &stats[type]); + will_return(__wrap_f_file_stat, F_okay); + + if (param) { + will_return(__wrap_f_time_clock_get, &time_spec_clocks); + will_return(__wrap_f_time_clock_get, F_okay); + } + + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + + if (time_spec_removes[date]) { + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + 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(5, argv, 0); + + assert_int_equal(result, 0); + } // for + } // for + } // for + } +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/tests/unit/remove/c/test-remove-time.h b/tests/unit/remove/c/test-remove-time.h new file mode 100644 index 0000000..4733f73 --- /dev/null +++ b/tests/unit/remove/c/test-remove-time.h @@ -0,0 +1,29 @@ +/** + * Kevux Tools - Remove + * + * Project: Kevux Tools + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the remove. + */ +#ifndef _TEST__KT_remove__time +#define _TEST__KT_remove__time + +/** + * Test that the remove works when the --accessed parameter with Time parameters passed. + */ +extern void test__kt_remove__time__accessed_works(void **state); + +/** + * Test that the remove works when the --changed parameter with Time parameters passed. + */ +extern void test__kt_remove__time__changed_works(void **state); + + +/** + * Test that the remove works when the --updated parameter with Time parameters passed. + */ +extern void test__kt_remove__time__updated_works(void **state); + +#endif // _TEST__KT_remove__time diff --git a/tests/unit/remove/c/test-remove-unix.c b/tests/unit/remove/c/test-remove-unix.c index 695e9f2..dd925cc 100644 --- a/tests/unit/remove/c/test-remove-unix.c +++ b/tests/unit/remove/c/test-remove-unix.c @@ -7,7 +7,157 @@ extern "C" { #endif -void test__kt_remove__unix__works(void **state) { + +void test__kt_remove__unix__accessed_works(void **state) { + + mock_unwrap = 0; + mock_unwrap_f_time_clock_get = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + + const uint8_t types_total = 8; + const uint8_t dates_total = 24; + + struct stat stats[types_total]; + + memset(stats, 0, sizeof(struct stat) * types_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_static_t parameter = macro_f_string_static_t_initialize_1("172800", 0, 6); + + const f_string_static_t operators[] = { + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + }; + + const f_time_spec_t time_spec_dates[] = { + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Not + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + }; + + bool time_spec_removes[] = { + F_true, // Equal + F_true, + F_false, + F_false, + F_false, // Less + F_false, + F_false, + F_true, + F_true, // Less Equal + F_true, + F_false, + F_true, + F_false, // More + F_false, + F_true, + F_false, + F_true, // More Equal + F_true, + F_true, + F_false, + F_false, // Not + F_false, + F_true, + F_true, + }; + + { + uint8_t type = 0; + uint8_t date = 0; + + for (; type < types_total; ++type) { + + for (date = 0; date < dates_total; ++date) { + + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_accessed_s, operators[date].string, parameter.string, 0 }; + + stats[type].st_atim = time_spec_dates[date]; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[type].st_mode)); + will_return(__wrap_f_file_stat, &stats[type]); + will_return(__wrap_f_file_stat, F_okay); + + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + + if (time_spec_removes[date]) { + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + 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(5, argv, 0); + + assert_int_equal(result, 0); + } // for + } // for + } +} + +void test__kt_remove__unix__changed_works(void **state) { mock_unwrap = 0; mock_unwrap_f_time_clock_get = 0; @@ -156,6 +306,155 @@ void test__kt_remove__unix__works(void **state) { } } +void test__kt_remove__unix__updated_works(void **state) { + + mock_unwrap = 0; + mock_unwrap_f_time_clock_get = 0; + + const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9); + + const uint8_t types_total = 8; + const uint8_t dates_total = 24; + + struct stat stats[types_total]; + + memset(stats, 0, sizeof(struct stat) * types_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_static_t parameter = macro_f_string_static_t_initialize_1("172800", 0, 6); + + const f_string_static_t operators[] = { + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_equal_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_less_equal_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_more_equal_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + kt_remove_date_symbol_not_s, + }; + + const f_time_spec_t time_spec_dates[] = { + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More Equal + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Not + macro_f_time_spec_t_initialize_1(2 * 86400, 0), + macro_f_time_spec_t_initialize_1(3 * 86400, 10), + macro_f_time_spec_t_initialize_1(1 * 86400, 4000), + }; + + bool time_spec_removes[] = { + F_true, // Equal + F_true, + F_false, + F_false, + F_false, // Less + F_false, + F_false, + F_true, + F_true, // Less Equal + F_true, + F_false, + F_true, + F_false, // More + F_false, + F_true, + F_false, + F_true, // More Equal + F_true, + F_true, + F_false, + F_false, // Not + F_false, + F_true, + F_true, + }; + + { + uint8_t type = 0; + uint8_t date = 0; + + for (; type < types_total; ++type) { + + for (date = 0; date < dates_total; ++date) { + + const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_updated_s, operators[date].string, parameter.string, 0 }; + + stats[type].st_mtim = time_spec_dates[date]; + + // Pre-process file. + will_return(__wrap_f_file_exists, F_true); + will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[type].st_mode)); + will_return(__wrap_f_file_stat, &stats[type]); + will_return(__wrap_f_file_stat, F_okay); + + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + will_return(__wrap_f_directory_empty, F_true); + } + + if (time_spec_removes[date]) { + if (macro_f_file_type_is_directory(stats[type].st_mode)) { + 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(5, argv, 0); + + assert_int_equal(result, 0); + } // for + } // for + } +} + #ifdef __cplusplus } // extern "C" #endif diff --git a/tests/unit/remove/c/test-remove-unix.h b/tests/unit/remove/c/test-remove-unix.h index 6e3d8d5..8ec3fd8 100644 --- a/tests/unit/remove/c/test-remove-unix.h +++ b/tests/unit/remove/c/test-remove-unix.h @@ -11,8 +11,18 @@ #define _TEST__KT_remove__unix /** - * Test that the remove works when the --changed parameter with date parameters passed. + * Test that the remove works when the --accessed parameter with Unix parameters passed. */ -extern void test__kt_remove__unix__works(void **state); +extern void test__kt_remove__unix__accessed_works(void **state); + +/** + * Test that the remove works when the --changed parameter with Unix parameters passed. + */ +extern void test__kt_remove__unix__changed_works(void **state); + +/** + * Test that the remove works when the --updated parameter with Unix parameters passed. + */ +extern void test__kt_remove__unix__updated_works(void **state); #endif // _TEST__KT_remove__unix diff --git a/tests/unit/remove/c/test-remove.c b/tests/unit/remove/c/test-remove.c index b368c15..d37e40f 100644 --- a/tests/unit/remove/c/test-remove.c +++ b/tests/unit/remove/c/test-remove.c @@ -61,6 +61,10 @@ int main(void) { 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__epochtime__accessed_works), + cmocka_unit_test(test__kt_remove__epochtime__changed_works), + cmocka_unit_test(test__kt_remove__epochtime__updated_works), + cmocka_unit_test(test__kt_remove__file_mode__different_works), cmocka_unit_test(test__kt_remove__file_mode__same_works), cmocka_unit_test(test__kt_remove__file_mode__similar_works), @@ -81,7 +85,13 @@ int main(void) { 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__unix__works), + cmocka_unit_test(test__kt_remove__time__accessed_works), + cmocka_unit_test(test__kt_remove__time__changed_works), + cmocka_unit_test(test__kt_remove__time__updated_works), + + cmocka_unit_test(test__kt_remove__unix__accessed_works), + cmocka_unit_test(test__kt_remove__unix__changed_works), + cmocka_unit_test(test__kt_remove__unix__updated_works), cmocka_unit_test(test__kt_remove__user__different_works), cmocka_unit_test(test__kt_remove__user__name_works), diff --git a/tests/unit/remove/c/test-remove.h b/tests/unit/remove/c/test-remove.h index 7cf84cf..aced9ec 100644 --- a/tests/unit/remove/c/test-remove.h +++ b/tests/unit/remove/c/test-remove.h @@ -36,6 +36,7 @@ #include "test-remove-directory_no_args.h" #include "test-remove-directory_recurse_simple.h" #include "test-remove-directory_tree_simple.h" +#include "test-remove-epochtime.h" #include "test-remove-file_mode.h" #include "test-remove-file_type.h" #include "test-remove-force.h" @@ -43,6 +44,7 @@ #include "test-remove-print_help.h" #include "test-remove-print_version.h" #include "test-remove-regular_no_args.h" +#include "test-remove-time.h" #include "test-remove-unix.h" #include "test-remove-user.h"