Debian Codegraph @

u8stream/test/integration/universal_file_output_stream_test.c

raw symbols SCIP
1 /* File: universal_file_output_stream_test.c; Copyright and License: see below */
2
3 #include "universal_file_output_stream_test.h"
4 #include "u8/u8_error.h"
5 #include "u8/u8_fault_inject.h"
6 #include "u8_test_cond.h"
7 #include "u8stream/universal_file_input_stream.h"
8 #include "u8stream/universal_file_output_stream.h"
9 #include "u8dir/u8dir_file.h"
10 #include "test_fixture.h"
11 #include "test_expect.h"
12 #include "test_environment_assert.h"
13 #include "test_case_result.h"
14 #include <sys/stat.h>
15 #include <assert.h>
16
17 static test_fixture_t * set_up();
18 static void tear_down( test_fixture_t *fix );
19 static test_case_result_t test_file_write( test_fixture_t *fix );
20 static test_case_result_t test_wrong_mode( test_fixture_t *fix );
21 static test_case_result_t test_no_access( test_fixture_t *fix );
22 #ifndef NDEBUG
23 static test_case_result_t test_posix_errors_fault_injected( test_fixture_t *fix );
24 #endif
25
26 test_suite_t universal_file_output_stream_test_get_suite(void)
27 {
28 test_suite_t result;
29 test_suite_init( &result,
30 #ifndef NDEBUG
31 "universal_file_output_stream_test_get_suite (VARIANT: DEBUG)",
32 #else
33 "universal_file_output_stream_test_get_suite (VARIANT: RELEASE)",
34 #endif
35 TEST_CATEGORY_INTEGRATION | TEST_CATEGORY_CONTINUOUS | TEST_CATEGORY_COVERAGE,
36 &set_up,
37 &tear_down
38 );
39 test_suite_add_test_case( &result, "test_file_write", &test_file_write );
40 test_suite_add_test_case( &result, "test_wrong_mode", &test_wrong_mode );
41 test_suite_add_test_case( &result, "test_no_access", &test_no_access );
42 #ifndef NDEBUG
43 test_suite_add_test_case( &result, "test_posix_errors_fault_injected", &test_posix_errors_fault_injected );
44 #endif
45 return result;
46 }
47
48 struct test_fixture_struct {
49 const char * test_file_name;
50 };
51 typedef struct test_fixture_struct test_fixture_t; /* double declaration as reminder */
52 static test_fixture_t test_fixture;
53
54 static test_fixture_t * set_up()
55 {
56 test_fixture.test_file_name = "universal_file_output_stream_test.txt";
57 return &test_fixture;
58 }
59
60 static void tear_down( test_fixture_t *fix )
61 {
62 /* cleanup */
63 u8_error_t file_err;
64 file_err = u8dir_file_remove( (*fix).test_file_name );
65 TEST_ENVIRONMENT_ASSERT( U8_ERROR_NONE == file_err );
66 }
67
68 static test_case_result_t test_file_write( test_fixture_t *fix )
69 {
70 universal_file_output_stream_t create_file;
71 u8_error_t file_err = U8_ERROR_NONE;
72
73 /* write a file */
74 {
75 universal_file_output_stream_init( &create_file );
76 universal_output_stream_t *base_class = universal_file_output_stream_get_output_stream( &create_file );
77 TEST_EXPECT( NULL != base_class );
78 file_err = universal_file_output_stream_open( &create_file, (*fix).test_file_name );
79 const char content[] = "123";
80 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
81 file_err = universal_file_output_stream_write( &create_file, &content, sizeof(content) );
82 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
83 file_err = universal_file_output_stream_flush( &create_file);
84 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
85 file_err = universal_file_output_stream_close( &create_file );
86 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
87 file_err = universal_file_output_stream_destroy( &create_file );
88 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
89 }
90
91 /* read and check the file */
92 {
93 universal_file_input_stream_t in_file;
94 universal_file_input_stream_init( &in_file );
95 u8_error_t file_err = U8_ERROR_NONE;
96 file_err = universal_file_input_stream_open( &in_file, (*fix).test_file_name );
97 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
98 char content[8];
99 size_t read_bytes;
100 file_err = universal_file_input_stream_read( &in_file, &content, sizeof(content), &read_bytes );
101 TEST_EXPECT_EQUAL_INT( 4, read_bytes );
102 TEST_EXPECT_EQUAL_INT( 0, memcmp( &content, "123", sizeof("123") ) );
103 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
104 file_err = universal_file_input_stream_destroy( &in_file );
105 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
106 }
107
108 return TEST_CASE_RESULT_OK;
109 }
110
111 static test_case_result_t test_wrong_mode( test_fixture_t *fix )
112 {
113 universal_file_output_stream_t create_file;
114 u8_error_t file_err = U8_ERROR_NONE;
115
116 /* init a file */
117 {
118 universal_file_output_stream_init( &create_file );
119 }
120 /* write, flush and close before open */
121 {
122 const char content[] = "123";
123 file_err = universal_file_output_stream_write( &create_file, &content, sizeof(content) );
124 TEST_EXPECT_EQUAL_INT( U8_ERROR_WRONG_STATE, file_err );
125 file_err = universal_file_output_stream_flush( &create_file);
126 TEST_EXPECT_EQUAL_INT( U8_ERROR_WRONG_STATE, file_err );
127 file_err = universal_file_output_stream_close( &create_file );
128 TEST_EXPECT_EQUAL_INT( U8_ERROR_WRONG_STATE, file_err );
129 }
130 /* open twice */
131 {
132 file_err = universal_file_output_stream_open( &create_file, (*fix).test_file_name );
133 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
134 file_err = universal_file_output_stream_open( &create_file, (*fix).test_file_name );
135 TEST_EXPECT_EQUAL_INT( U8_ERROR_WRONG_STATE, file_err );
136 }
137 /* destroy without close */
138 {
139 file_err = universal_file_output_stream_destroy( &create_file );
140 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
141 }
142
143 return TEST_CASE_RESULT_OK;
144 }
145
146 static test_case_result_t test_no_access( test_fixture_t *fix )
147 {
148 universal_file_output_stream_t create_file;
149 u8_error_t file_err = U8_ERROR_NONE;
150
151 /* write a file */
152 {
153 universal_file_output_stream_init( &create_file );
154 u8_error_t file_err = U8_ERROR_NONE;
155 file_err = universal_file_output_stream_open( &create_file, (*fix).test_file_name );
156 const char content[] = "123";
157 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
158 file_err = universal_file_output_stream_write( &create_file, &content, sizeof(content) );
159 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
160 file_err = universal_file_output_stream_close( &create_file );
161 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
162 file_err = universal_file_output_stream_destroy( &create_file );
163 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
164 }
165 /* open the already existing file */
166 {
167 universal_file_output_stream_init( &create_file );
168 file_err = universal_file_output_stream_open( &create_file, (*fix).test_file_name );
169 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
170 const char new_content[] = "ab";
171 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
172 file_err = universal_file_output_stream_write( &create_file, &new_content, sizeof(new_content) );
173 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
174 file_err = universal_file_output_stream_close( &create_file );
175 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
176 file_err = universal_file_output_stream_destroy( &create_file );
177 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
178 }
179 /* open a directory */
180 {
181 universal_file_output_stream_init( &create_file );
182 const u8dir_file_t directory = ".";
183 file_err = universal_file_output_stream_open( &create_file, directory );
184 TEST_EXPECT_EQUAL_INT( U8_ERROR_AT_FILE_WRITE, file_err );
185 char bad_content[] = "oh no";
186 file_err = universal_file_output_stream_write( &create_file, &bad_content, sizeof(bad_content) );
187 TEST_EXPECT_EQUAL_INT( U8_ERROR_WRONG_STATE, file_err );
188 file_err = universal_file_output_stream_close( &create_file );
189 TEST_EXPECT_EQUAL_INT( U8_ERROR_WRONG_STATE, file_err );
190 }
191 /* open an existing file without write-permissions */
192 #ifdef _WIN32
193 /* this part of the test case does not work on windows/wine */
194 #else
195 {
196 int mode_err;
197 mode_err = chmod( (*fix).test_file_name, (mode_t)0 );
198 TEST_ENVIRONMENT_ASSERT( mode_err == 0 );
199
200 universal_file_output_stream_init( &create_file );
201 file_err = universal_file_output_stream_open( &create_file, (*fix).test_file_name );
202 TEST_EXPECT_EQUAL_INT( U8_ERROR_AT_FILE_WRITE, file_err );
203
204 mode_err = chmod( (*fix).test_file_name, (mode_t)0777 );
205 TEST_ENVIRONMENT_ASSERT( mode_err == 0 );
206 }
207 #endif
208 /* destroy */
209 {
210 file_err = universal_file_output_stream_destroy( &create_file );
211 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
212 }
213
214 return TEST_CASE_RESULT_OK;
215 }
216
217 #ifndef NDEBUG
218 static test_case_result_t test_posix_errors_fault_injected( test_fixture_t *fix )
219 {
220 universal_file_output_stream_t create_file;
221 u8_error_t file_err = U8_ERROR_NONE;
222
223 /* open a file */
224 {
225 universal_file_output_stream_init( &create_file );
226 file_err = universal_file_output_stream_open( &create_file, (*fix).test_file_name );
227 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
228 }
229
230 /* write to the file */
231 U8_FAULT_INJECT_SETUP( U8_TEST_COND_FWRITE );
232 {
233 const char content[] = "123";
234 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
235 file_err = universal_file_output_stream_write( &create_file, &content, sizeof(content) );
236 TEST_EXPECT_EQUAL_INT( U8_ERROR_AT_FILE_WRITE, file_err );
237 }
238 U8_FAULT_INJECT_RESET();
239
240 /* flush the file */
241 U8_FAULT_INJECT_SETUP( U8_TEST_COND_FFLUSH );
242 {
243 file_err = universal_file_output_stream_flush( &create_file);
244 TEST_EXPECT_EQUAL_INT( U8_ERROR_AT_FILE_WRITE, file_err );
245 }
246 U8_FAULT_INJECT_RESET();
247
248 /* close the file */
249 U8_FAULT_INJECT_SETUP( U8_TEST_COND_FCLOSE );
250 {
251 file_err = universal_file_output_stream_close( &create_file );
252 TEST_EXPECT_EQUAL_INT( U8_ERROR_AT_FILE_WRITE, file_err );
253 file_err = universal_file_output_stream_destroy( &create_file );
254 TEST_EXPECT_EQUAL_INT( U8_ERROR_NONE, file_err );
255 }
256 U8_FAULT_INJECT_RESET();
257
258 return TEST_CASE_RESULT_OK;
259 }
260 #endif
261
262
263 /*
264 * Copyright 2024-2026 Andreas Warnke
265 *
266 * Licensed under the Apache License, Version 2.0 (the "License");
267 * you may not use this file except in compliance with the License.
268 * You may obtain a copy of the License at
269 *
270 * http://www.apache.org/licenses/LICENSE-2.0
271 *
272 * Unless required by applicable law or agreed to in writing, software
273 * distributed under the License is distributed on an "AS IS" BASIS,
274 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
275 * See the License for the specific language governing permissions and
276 * limitations under the License.
277 */