Branch data Line data Source code
1 : : #include "sute.h"
2 : :
3 : : #define READ_FAIL_REL_PATH "path1/AAA/ZAW/D/e/f/b_file.txt"
4 : :
5 : : /**
6 : : * @brief Reset read-error mocks used by this test group
7 : : */
8 : 8 : static void test0031_reset_read_mocks(void)
9 : : {
10 : 8 : mocks_openat_reset();
11 : 8 : mocks_fdopen_reset();
12 : 8 : mocks_fread_reset();
13 : 8 : mocks_ferror_reset();
14 : 8 : }
15 : :
16 : : /**
17 : : * @brief Prepare the mutable fixture
18 : : *
19 : : * @return Test status
20 : : */
21 : 4 : static Return test0031_prepare_case(void)
22 : : {
23 : : /* The status that will be returned before exiting */
24 : : /* By default, assumes the function ran without errors */
25 : 4 : Return status = SUCCESS;
26 : :
27 : 4 : ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
28 : 4 : ASSERT(SUCCESS == prepare_mutable_fixture("tests/fixtures/diffs/diff1"));
29 : :
30 : 4 : deliver(status);
31 : : }
32 : :
33 : : /**
34 : : * @brief Compare captured application output and restore test resources
35 : : *
36 : : * @param[in] result Captured application output
37 : : * @param[in] pattern Expected output pattern buffer
38 : : * @param[in] error_buffer Captured stderr output
39 : : * @param[in] template_name Expected output template file
40 : : * @return Test status
41 : : */
42 : 4 : static Return test0031_check_case(
43 : : memory *result,
44 : : memory *pattern,
45 : : memory *error_buffer,
46 : : const char *template_name)
47 : : {
48 : : /* The status that will be returned before exiting */
49 : : /* By default, assumes the function ran without errors */
50 : 4 : Return status = SUCCESS;
51 : :
52 : 4 : ASSERT(error_buffer->length == 0);
53 : 4 : ASSERT(SUCCESS == get_file_content(template_name,pattern));
54 : 4 : ASSERT(SUCCESS == match_pattern(result,pattern,template_name));
55 : :
56 : 4 : ASSERT(SUCCESS == delete_path("read_fail.db"));
57 : 4 : ASSERT(SUCCESS == restore_mutable_fixture("tests/fixtures/diffs/diff1"));
58 : :
59 : 4 : deliver(status);
60 : : }
61 : :
62 : : /**
63 : : * @brief Verify read-error reporting when openat() fails for the target file
64 : : *
65 : : * @return Test status
66 : : */
67 : 1 : static Return test0031_1(void)
68 : : {
69 : 1 : INITTEST;
70 : :
71 : 1 : m_create(char,result,MEMORY_STRING);
72 : 1 : m_create(char,pattern,MEMORY_STRING);
73 : 1 : m_create(char,error_buffer,MEMORY_STRING);
74 : :
75 : 1 : ASSERT(SUCCESS == test0031_prepare_case());
76 : :
77 : 1 : const char *arguments = "--database=read_fail.db --progress"
78 : : " tests/fixtures/diffs/diff1";
79 : :
80 : 1 : test0031_reset_read_mocks();
81 : 1 : mocks_openat_set_target_suffix(READ_FAIL_REL_PATH);
82 : 1 : mocks_openat_set_errno(EIO);
83 : 1 : mocks_openat_enable(true);
84 : :
85 : 1 : ASSERT(SUCCESS == runit(arguments,result,error_buffer,COMPLETED,ALLOW_BOTH));
86 : :
87 : 1 : mocks_openat_enable(false);
88 : :
89 : 1 : ASSERT(SUCCESS == test0031_check_case(result,pattern,error_buffer,"templates/0031_001.txt"));
90 : :
91 : 1 : ASSERT(mocks_openat_call_count() == 1);
92 : 1 : ASSERT(mocks_fdopen_call_count() == 0);
93 : 1 : ASSERT(mocks_fread_call_count() == 0);
94 : 1 : ASSERT(mocks_ferror_call_count() == 0);
95 : :
96 : 1 : test0031_reset_read_mocks();
97 : :
98 : 1 : m_del(pattern);
99 : 1 : m_del(result);
100 : 1 : m_del(error_buffer);
101 : :
102 : 1 : RETURN_STATUS;
103 : : }
104 : :
105 : : /**
106 : : * @brief Verify read-error reporting when fdopen() fails for the target file
107 : : *
108 : : * @return Test status
109 : : */
110 : 1 : static Return test0031_2(void)
111 : : {
112 : 1 : INITTEST;
113 : :
114 : 1 : m_create(char,result,MEMORY_STRING);
115 : 1 : m_create(char,pattern,MEMORY_STRING);
116 : 1 : m_create(char,error_buffer,MEMORY_STRING);
117 : :
118 : 1 : ASSERT(SUCCESS == test0031_prepare_case());
119 : :
120 : 1 : const char *arguments = "--database=read_fail.db --progress"
121 : : " tests/fixtures/diffs/diff1";
122 : :
123 : 1 : test0031_reset_read_mocks();
124 : 1 : mocks_fdopen_set_target_suffix(READ_FAIL_REL_PATH);
125 : 1 : mocks_fdopen_set_errno(EIO);
126 : 1 : mocks_fdopen_enable(true);
127 : :
128 : 1 : ASSERT(SUCCESS == runit(arguments,result,error_buffer,COMPLETED,ALLOW_BOTH));
129 : :
130 : 1 : mocks_fdopen_enable(false);
131 : :
132 : 1 : ASSERT(SUCCESS == test0031_check_case(result,pattern,error_buffer,"templates/0031_001.txt"));
133 : :
134 : 1 : ASSERT(mocks_openat_call_count() == 0);
135 : 1 : ASSERT(mocks_fdopen_call_count() == 1);
136 : 1 : ASSERT(mocks_fread_call_count() == 0);
137 : 1 : ASSERT(mocks_ferror_call_count() == 0);
138 : :
139 : 1 : test0031_reset_read_mocks();
140 : :
141 : 1 : m_del(pattern);
142 : 1 : m_del(result);
143 : 1 : m_del(error_buffer);
144 : :
145 : 1 : RETURN_STATUS;
146 : : }
147 : :
148 : : /**
149 : : * @brief Verify that a zero fread without ferror is handled as EOF
150 : : *
151 : : * @return Test status
152 : : */
153 : 1 : static Return test0031_3(void)
154 : : {
155 : 1 : INITTEST;
156 : :
157 : 1 : m_create(char,result,MEMORY_STRING);
158 : 1 : m_create(char,pattern,MEMORY_STRING);
159 : 1 : m_create(char,error_buffer,MEMORY_STRING);
160 : :
161 : 1 : ASSERT(SUCCESS == test0031_prepare_case());
162 : :
163 : 1 : const char *arguments = "--database=read_fail.db --progress"
164 : : " tests/fixtures/diffs/diff1";
165 : :
166 : 1 : test0031_reset_read_mocks();
167 : 1 : mocks_fread_set_target_suffix(READ_FAIL_REL_PATH);
168 : 1 : mocks_fread_set_errno(EIO);
169 : 1 : mocks_fread_enable(true);
170 : 1 : mocks_ferror_enable(false);
171 : :
172 : 1 : ASSERT(SUCCESS == runit(arguments,result,error_buffer,COMPLETED,ALLOW_BOTH));
173 : :
174 : 1 : mocks_fread_enable(false);
175 : 1 : mocks_ferror_enable(true);
176 : :
177 : 1 : ASSERT(SUCCESS == test0031_check_case(result,pattern,error_buffer,"templates/0031_002.txt"));
178 : :
179 : 1 : ASSERT(mocks_openat_call_count() == 0);
180 : 1 : ASSERT(mocks_fdopen_call_count() == 0);
181 : 1 : ASSERT(mocks_fread_call_count() == 1);
182 : 1 : ASSERT(mocks_ferror_call_count() == 1);
183 : :
184 : 1 : test0031_reset_read_mocks();
185 : :
186 : 1 : m_del(pattern);
187 : 1 : m_del(result);
188 : 1 : m_del(error_buffer);
189 : :
190 : 1 : RETURN_STATUS;
191 : : }
192 : :
193 : : /**
194 : : * @brief Verify read-error reporting when ferror() confirms a zero fread
195 : : *
196 : : * @return Test status
197 : : */
198 : 1 : static Return test0031_4(void)
199 : : {
200 : 1 : INITTEST;
201 : :
202 : 1 : m_create(char,result,MEMORY_STRING);
203 : 1 : m_create(char,pattern,MEMORY_STRING);
204 : 1 : m_create(char,error_buffer,MEMORY_STRING);
205 : :
206 : 1 : ASSERT(SUCCESS == test0031_prepare_case());
207 : :
208 : 1 : const char *arguments = "--database=read_fail.db --progress"
209 : : " tests/fixtures/diffs/diff1";
210 : :
211 : 1 : test0031_reset_read_mocks();
212 : 1 : mocks_fread_set_target_suffix(READ_FAIL_REL_PATH);
213 : 1 : mocks_fread_set_errno(EIO);
214 : 1 : mocks_fread_enable(true);
215 : 1 : mocks_ferror_set_errno(EIO);
216 : 1 : mocks_ferror_enable(true);
217 : :
218 : 1 : ASSERT(SUCCESS == runit(arguments,result,error_buffer,COMPLETED,ALLOW_BOTH));
219 : :
220 : 1 : mocks_fread_enable(false);
221 : 1 : mocks_ferror_enable(true);
222 : :
223 : 1 : ASSERT(SUCCESS == test0031_check_case(result,pattern,error_buffer,"templates/0031_001.txt"));
224 : :
225 : 1 : ASSERT(mocks_openat_call_count() == 0);
226 : 1 : ASSERT(mocks_fdopen_call_count() == 0);
227 : 1 : ASSERT(mocks_fread_call_count() == 1);
228 : 1 : ASSERT(mocks_ferror_call_count() == 1);
229 : :
230 : 1 : test0031_reset_read_mocks();
231 : :
232 : 1 : m_del(pattern);
233 : 1 : m_del(result);
234 : 1 : m_del(error_buffer);
235 : :
236 : 1 : RETURN_STATUS;
237 : : }
238 : :
239 : : /**
240 : : * @brief Verify read-error handling at each file hashing I/O stage
241 : : *
242 : : * @return Test status
243 : : */
244 : 1 : Return test0031(void)
245 : : {
246 : 1 : INITTEST;
247 : :
248 : 1 : TEST(test0031_1,"Read error handling when openat fails");
249 : 1 : TEST(test0031_2,"Read error handling when fdopen fails");
250 : 1 : TEST(test0031_3,"Zero fread without ferror stays non-error");
251 : 1 : TEST(test0031_4,"Read error handling when ferror confirms fread failure");
252 : :
253 : 1 : RETURN_STATUS;
254 : : }
|