Branch data Line data Source code
1 : : #include "sute.h"
2 : :
3 : : /**
4 : : * @brief Stop startup when a read error follows valid configuration settings
5 : : *
6 : : * The fixture represents the prefix read before an I/O failure. The ferror
7 : : * mock reports that failure after inih applies the prefix. Complete output
8 : : * matching rejects any traversal output, and the database must remain absent
9 : : *
10 : : * @return Return status for startup failure without a database being created
11 : : */
12 : 1 : static Return test0046_1(void)
13 : : {
14 : 1 : INITTEST;
15 : 1 : const struct argument_test_case cases[] = {
16 : : {"--config=0046_001.conf",FAILURE,"templates/0003_013.txt","templates/0046_001.txt"},
17 : : {0}
18 : : };
19 : :
20 : 1 : ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
21 : 1 : ASSERT(SUCCESS == copy_from_origin("tests/templates/0046_001.conf",
22 : : "0046_001.conf",REQUIRE_SOURCE_EXISTS));
23 : 1 : ASSERT(SUCCESS == expect_file_access_from_root(".","0046_001.db",F_OK,FILE_NOT_FOUND));
24 : :
25 : 1 : mocks_ferror_reset();
26 : 1 : mocks_ferror_set_target_suffix("0046_001.conf");
27 : 1 : ASSERT(SUCCESS == run_and_match_templates(cases,NULL,NULL));
28 : 1 : ASSERT(mocks_ferror_call_count() == 1U);
29 : 1 : mocks_ferror_reset();
30 : :
31 : 1 : ASSERT(SUCCESS == expect_file_access_from_root(".","0046_001.db",F_OK,FILE_NOT_FOUND));
32 : 1 : call(delete_path_if_present("0046_001.db"));
33 : 1 : call(delete_path_if_present("0046_001.conf"));
34 : 1 : RETURN_STATUS;
35 : : }
36 : :
37 : : /**
38 : : * @brief Check retained settings while the read-error diagnostic is captured
39 : : * @return SUCCESS when reading fails after applying the configuration prefix
40 : : */
41 : 1 : static Return check_settings_after_read_error(void)
42 : : {
43 : 1 : INITTEST;
44 : 1 : char program_name[] = "testitall";
45 : 1 : struct argp_state state = {.name = program_name,.err_stream = stderr,.flags = ARGP_NO_EXIT};
46 : 1 : bool has_entries = false;
47 : 1 : m_create(char,path,MEMORY_STRING);
48 : :
49 : 1 : ASSERT(SUCCESS == construct_path("0046_002.conf",path));
50 : 1 : ASSERT(FAILURE == read_config_file(m_text(path),true,&has_entries,&state));
51 : 1 : ASSERT(has_entries == true);
52 : 1 : ASSERT(config->config_file_error_line == -1);
53 : : /* A valid prefix must remain applied when the final stream check reports EIO */
54 : 1 : ASSERT(config->roots.length == 1U);
55 : 1 : memory *roots = m_data(memory,conf(roots));
56 : 1 : ASSERT(strcmp(m_text(&roots[0]),"settings_read_before_failure") == 0);
57 : :
58 : 1 : call(m_del(path));
59 : 1 : deliver(status);
60 : : }
61 : :
62 : : /**
63 : : * @brief Preserve settings read before a stream error and check its full diagnostic
64 : : * @return Return status for retained settings, empty stdout, and the read-error message
65 : : */
66 : 1 : static Return test0046_2(void)
67 : : {
68 : 1 : INITTEST;
69 : 1 : Config *initial_config = config;
70 : 1 : Config test_config = {0};
71 : 1 : const LOGMODES initial_logger_mode = atomic_load_explicit(&rational_logger_mode,memory_order_relaxed);
72 : 1 : const RATIONAL_COLOR_MODE initial_color_mode = atomic_load_explicit(&rational_color_mode,memory_order_relaxed);
73 : 1 : config = &test_config;
74 : 1 : ASSERT(SUCCESS == init_config_and_check_output());
75 : 1 : m_create(char,pattern,MEMORY_STRING);
76 : :
77 : 1 : ASSERT(SUCCESS == copy_from_origin("tests/templates/0046_002.conf",
78 : : "0046_002.conf",REQUIRE_SOURCE_EXISTS));
79 : 1 : ASSERT(SUCCESS == get_file_content("templates/0046_002.txt",pattern));
80 : 1 : mocks_ferror_reset();
81 : 1 : mocks_ferror_set_target_suffix("0046_002.conf");
82 : 1 : ASSERT(SUCCESS == match_function_output(NULL,m_text(pattern),check_settings_after_read_error));
83 : 1 : ASSERT(mocks_ferror_call_count() == 1U);
84 : 1 : mocks_ferror_reset();
85 : :
86 : : /* Restore the runner's configuration and output policy after any test result */
87 : 1 : free_config();
88 : 1 : config = initial_config;
89 : 1 : rational_logger_mode = initial_logger_mode;
90 : 1 : rational_color_mode = initial_color_mode;
91 : 1 : call(delete_path_if_present("0046_002.conf"));
92 : 1 : call(m_del(pattern));
93 : 1 : RETURN_STATUS;
94 : : }
95 : :
96 : : /**
97 : : * @brief Check configuration read failures during reading and application startup
98 : : * @return Return status for retained settings and startup failure before scanning
99 : : */
100 : 1 : Return test0046(void)
101 : : {
102 : 1 : INITTEST;
103 : :
104 : 1 : TEST(test0046_1,"Read errors after configuration settings stop before scanning");
105 : 1 : TEST(test0046_2,"Settings read before a configuration stream error remain applied");
106 : :
107 : 1 : RETURN_STATUS;
108 : : }
|