1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @brief This tests various file system functions. |
---|
7 | * |
---|
8 | * This test exercises the following routines: |
---|
9 | * |
---|
10 | * - lseek() |
---|
11 | * - dup() |
---|
12 | * - dup2() |
---|
13 | * - fdatasync() |
---|
14 | * - fsync() |
---|
15 | * - pathconf() |
---|
16 | * - fpathconf() |
---|
17 | * - umask() |
---|
18 | * - utime() |
---|
19 | * - utimes() |
---|
20 | * - utimensat() |
---|
21 | * - futimens() |
---|
22 | * - sync() |
---|
23 | */ |
---|
24 | |
---|
25 | /* |
---|
26 | * COPYRIGHT (C) 1989, 2021 On-Line Applications Research Corporation (OAR). |
---|
27 | * |
---|
28 | * Redistribution and use in source and binary forms, with or without |
---|
29 | * modification, are permitted provided that the following conditions |
---|
30 | * are met: |
---|
31 | * 1. Redistributions of source code must retain the above copyright |
---|
32 | * notice, this list of conditions and the following disclaimer. |
---|
33 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
34 | * notice, this list of conditions and the following disclaimer in the |
---|
35 | * documentation and/or other materials provided with the distribution. |
---|
36 | * |
---|
37 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
38 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
39 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
40 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
41 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
42 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
43 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
44 | * * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
45 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
47 | * POSSIBILITY OF SUCH DAMAGE. |
---|
48 | */ |
---|
49 | |
---|
50 | #ifdef HAVE_CONFIG_H |
---|
51 | #include "config.h" |
---|
52 | #endif |
---|
53 | |
---|
54 | #include <rtems.h> |
---|
55 | #include <rtems/libio.h> |
---|
56 | #include <rtems/score/timespec.h> |
---|
57 | #include <rtems/score/todimpl.h> |
---|
58 | #include <sys/time.h> |
---|
59 | #include <fcntl.h> |
---|
60 | #include <unistd.h> |
---|
61 | #include <errno.h> |
---|
62 | #include <utime.h> |
---|
63 | #include <tmacros.h> |
---|
64 | |
---|
65 | #include <stdio.h> |
---|
66 | #include <unistd.h> |
---|
67 | |
---|
68 | #include <pmacros.h> |
---|
69 | |
---|
70 | const char rtems_test_name[] = "PSX 13"; |
---|
71 | |
---|
72 | /** |
---|
73 | * @brief Initializes the three files to be used for the test. |
---|
74 | */ |
---|
75 | static void InitFiles( void ) |
---|
76 | { |
---|
77 | int count; |
---|
78 | int rv; |
---|
79 | FILE *fp1, *fp2, *fp3; |
---|
80 | char letter; |
---|
81 | int number; |
---|
82 | |
---|
83 | fp1 = fopen( "testfile1.tst", "wt" ); |
---|
84 | rtems_test_assert( fp1 != NULL ); |
---|
85 | |
---|
86 | fp2 = fopen( "testfile2.tst", "wt" ); |
---|
87 | rtems_test_assert( fp2 != NULL ); |
---|
88 | |
---|
89 | fp3 = fopen( "testfile4.tst", "wb" ); |
---|
90 | rtems_test_assert( fp3 != NULL ); |
---|
91 | |
---|
92 | letter = 'a'; |
---|
93 | |
---|
94 | for( count = 0 ; count < (26*4); ++count) { |
---|
95 | fprintf( fp1, "%c", letter ); |
---|
96 | fprintf( fp2, "%c", letter ); |
---|
97 | |
---|
98 | ++letter; |
---|
99 | if( letter > 'z' ) |
---|
100 | letter = 'a'; |
---|
101 | } |
---|
102 | |
---|
103 | number = 0; |
---|
104 | |
---|
105 | for( count = 0; count < 40; ++count ) { |
---|
106 | fwrite( &number, 1, sizeof(int), fp3 ); |
---|
107 | |
---|
108 | ++number; |
---|
109 | if( number > 9 ) |
---|
110 | number = 0; |
---|
111 | } |
---|
112 | |
---|
113 | rv = fclose( fp1 ); |
---|
114 | rtems_test_assert( rv != EOF ); |
---|
115 | |
---|
116 | rv = fclose( fp2 ); |
---|
117 | rtems_test_assert( rv != EOF ); |
---|
118 | |
---|
119 | rv = fclose( fp3 ); |
---|
120 | rtems_test_assert( rv != EOF ); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * @brief Exercises lseek() by lseeking on the console. |
---|
125 | */ |
---|
126 | static void DeviceLSeekTest( void ) |
---|
127 | { |
---|
128 | int rv; |
---|
129 | int fd; |
---|
130 | |
---|
131 | fd = open( "/dev/console", O_RDONLY ); |
---|
132 | rtems_test_assert( fd != -1 ); |
---|
133 | |
---|
134 | rv = lseek( fd, 5, SEEK_SET ); |
---|
135 | rtems_test_assert( rv == -1 ); |
---|
136 | rtems_test_assert( errno == ESPIPE ); |
---|
137 | |
---|
138 | rv = close( fd ); |
---|
139 | rtems_test_assert( rv == 0 ); |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * @brief Exercises dup(). |
---|
144 | */ |
---|
145 | static void DupTest( void ) |
---|
146 | { |
---|
147 | int fd1, fd2; |
---|
148 | int flags; |
---|
149 | int rv; |
---|
150 | |
---|
151 | fd1 = open( "testfile1.tst", O_RDONLY ); |
---|
152 | rtems_test_assert( fd1 != -1 ); |
---|
153 | |
---|
154 | fd2 = dup( fd1 ); |
---|
155 | rtems_test_assert( fd2 != -1 ); |
---|
156 | |
---|
157 | rv = fcntl( fd1, F_SETFL, O_APPEND ); |
---|
158 | rtems_test_assert( rv != -1 ); |
---|
159 | |
---|
160 | flags = fcntl( fd2, F_GETFL ) & O_APPEND; |
---|
161 | rtems_test_assert( flags == 0 ); |
---|
162 | |
---|
163 | rv = close( fd1 ); |
---|
164 | rtems_test_assert( rv == 0 ); |
---|
165 | |
---|
166 | rv = close( fd2 ); |
---|
167 | rtems_test_assert( rv == 0 ); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * @brief Exercises dup2(). |
---|
172 | */ |
---|
173 | static void Dup2Test( void ) |
---|
174 | { |
---|
175 | int fd1, fd2; |
---|
176 | int flags; |
---|
177 | int rv; |
---|
178 | |
---|
179 | fd1 = open( "testfile1.tst", O_RDONLY ); |
---|
180 | rtems_test_assert( fd1 != -1 ); |
---|
181 | |
---|
182 | fd2 = open( "testfile2.tst", O_RDONLY ); |
---|
183 | rtems_test_assert( fd2 != -1 ); |
---|
184 | |
---|
185 | /* make sure dup2 works if both fd1 and fd2 are valid file descriptors. */ |
---|
186 | rv = dup2( fd1, fd2 ); |
---|
187 | rtems_test_assert( rv != -1 ); |
---|
188 | |
---|
189 | rv = fcntl( fd1, F_SETFL, O_APPEND ); |
---|
190 | rtems_test_assert( rv != -1 ); |
---|
191 | |
---|
192 | flags = fcntl( fd1, F_GETFL ) & O_APPEND; |
---|
193 | rtems_test_assert( flags == O_APPEND ); |
---|
194 | |
---|
195 | /* make sure dup2 fails correctly if one or the other arguments are invalid. */ |
---|
196 | /* this assumes -1 is an invalid value for a file descriptor!!! (POSIX book, p.135) */ |
---|
197 | rv = close( fd1 ); |
---|
198 | rtems_test_assert( rv == 0 ); |
---|
199 | |
---|
200 | fd1 = -1; |
---|
201 | |
---|
202 | rv = dup2( fd1, fd2 ); |
---|
203 | rtems_test_assert( rv == -1 ); |
---|
204 | |
---|
205 | fd1 = dup( fd2 ); |
---|
206 | fd2 = -1; |
---|
207 | |
---|
208 | rv = dup2( fd1, fd2 ); |
---|
209 | rtems_test_assert( rv == -1 ); |
---|
210 | |
---|
211 | rv = close( fd1 ); |
---|
212 | rtems_test_assert( rv == 0 ); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * @brief Exercises fdatasync(). |
---|
217 | */ |
---|
218 | static void FDataSyncTest( void ) |
---|
219 | { |
---|
220 | int fd; |
---|
221 | int rv; |
---|
222 | |
---|
223 | /* Try it with a RD_ONLY file. */ |
---|
224 | fd = open( "testfile1.tst", O_RDONLY ); |
---|
225 | rtems_test_assert( fd != -1 ); |
---|
226 | |
---|
227 | rv = fdatasync( fd ); |
---|
228 | rtems_test_assert( rv == -1 ); |
---|
229 | rtems_test_assert( errno == EBADF ); |
---|
230 | |
---|
231 | rv = close(fd); |
---|
232 | rtems_test_assert( rv == 0 ); |
---|
233 | |
---|
234 | /* Try it with a bad file descriptor */ |
---|
235 | fd = -1; |
---|
236 | |
---|
237 | rv = fdatasync( fd ); |
---|
238 | rtems_test_assert( rv == -1 ); |
---|
239 | rtems_test_assert( errno == EBADF ); |
---|
240 | |
---|
241 | /* Okay - now the success case... */ |
---|
242 | fd = open( "testfile1.tst", O_RDWR ); |
---|
243 | rv = fdatasync( fd ); |
---|
244 | rtems_test_assert( rv == 0 ); |
---|
245 | |
---|
246 | rv = close( fd ); |
---|
247 | rtems_test_assert( rv == 0 ); |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * @brief Exercises umask(). |
---|
252 | */ |
---|
253 | static void UMaskTest( void ) |
---|
254 | { |
---|
255 | mode_t rv; |
---|
256 | |
---|
257 | (void) umask( 023 ); |
---|
258 | |
---|
259 | rv = umask( 022 ); |
---|
260 | rtems_test_assert( rv == 023 ); |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * @brief Exercises utime(). |
---|
265 | */ |
---|
266 | static void UTimeTest( void ) |
---|
267 | { |
---|
268 | int rv; |
---|
269 | struct utimbuf time; |
---|
270 | struct timespec current_time; |
---|
271 | struct stat fstat; |
---|
272 | |
---|
273 | /* ENOENT test case */ |
---|
274 | |
---|
275 | /* Case: Pass an invalid filename. */ |
---|
276 | rv = utime( "!This is an =invalid p@thname!!! :)", NULL ); |
---|
277 | rtems_test_assert( rv == -1 ); |
---|
278 | rtems_test_assert( errno == ENOENT ); |
---|
279 | |
---|
280 | /* EACCES test case */ |
---|
281 | |
---|
282 | /* Case: Change user ID to someone besides root */ |
---|
283 | rv = seteuid( 1 ); |
---|
284 | rtems_test_assert( rv == 0 ); |
---|
285 | |
---|
286 | rv = utime( "testfile1.tst", NULL ); |
---|
287 | rtems_test_assert( rv == -1 ); |
---|
288 | rtems_test_assert( errno == EACCES ); |
---|
289 | |
---|
290 | rv = seteuid( 0 ); |
---|
291 | rtems_test_assert( rv == 0 ); |
---|
292 | |
---|
293 | /* EINVAL test cases */ |
---|
294 | |
---|
295 | /* Case: Invalid access time */ |
---|
296 | time.actime = -1; |
---|
297 | time.modtime = 54321; |
---|
298 | |
---|
299 | rv = utime( "testfile1.tst", &time ); |
---|
300 | rtems_test_assert( rv == -1 ); |
---|
301 | rtems_test_assert( errno == EINVAL ); |
---|
302 | |
---|
303 | /* Case: Invalid modified time */ |
---|
304 | time.actime = 12345; |
---|
305 | time.modtime = -1; |
---|
306 | |
---|
307 | rv = utime( "testfile1.tst", &time ); |
---|
308 | rtems_test_assert( rv == -1 ); |
---|
309 | rtems_test_assert( errno == EINVAL ); |
---|
310 | |
---|
311 | /* Successful test cases */ |
---|
312 | |
---|
313 | /* Case: Test without times argument */ |
---|
314 | clock_gettime( CLOCK_REALTIME, ¤t_time ); |
---|
315 | |
---|
316 | rv = utime( "testfile1.tst", NULL ); |
---|
317 | rtems_test_assert( rv == 0 ); |
---|
318 | |
---|
319 | rv = stat( "testfile1.tst", &fstat ); |
---|
320 | rtems_test_assert( rv == 0 ); |
---|
321 | rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec ); |
---|
322 | rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec ); |
---|
323 | |
---|
324 | /* Case: time is filled with valid values */ |
---|
325 | time.actime = 12345; |
---|
326 | time.modtime = 54321; |
---|
327 | |
---|
328 | rv = utime( "testfile1.tst", &time ); |
---|
329 | rtems_test_assert( rv == 0 ); |
---|
330 | |
---|
331 | /* Check that it actually changed the time */ |
---|
332 | rv = stat( "testfile1.tst", &fstat ); |
---|
333 | rtems_test_assert( rv == 0 ); |
---|
334 | rtems_test_assert( fstat.st_atime == 12345 ); |
---|
335 | rtems_test_assert( fstat.st_mtime == 54321 ); |
---|
336 | } |
---|
337 | |
---|
338 | /** |
---|
339 | * @brief Exercises utimes(). |
---|
340 | */ |
---|
341 | static void UTimesTest( void ) |
---|
342 | { |
---|
343 | int rv; |
---|
344 | struct timeval time[2]; |
---|
345 | struct timespec current_time; |
---|
346 | struct stat fstat; |
---|
347 | |
---|
348 | /* ENOENT test case */ |
---|
349 | |
---|
350 | /* Case: First, an invalid filename. */ |
---|
351 | rv = utimes( "!This is an =invalid p@thname!!! : )", NULL); |
---|
352 | rtems_test_assert( rv == -1 ); |
---|
353 | rtems_test_assert( errno == ENOENT ); |
---|
354 | |
---|
355 | /* EACCES test case */ |
---|
356 | |
---|
357 | /* Change the user ID of the process to someone besides root */ |
---|
358 | rv = seteuid( 1 ); |
---|
359 | rtems_test_assert( rv == 0 ); |
---|
360 | |
---|
361 | rv = utimes( "testfile1.tst", NULL ); |
---|
362 | rtems_test_assert( rv == -1 ); |
---|
363 | rtems_test_assert( errno == EACCES ); |
---|
364 | |
---|
365 | rv = seteuid( 0 ); |
---|
366 | rtems_test_assert( rv == 0 ); |
---|
367 | |
---|
368 | /* EINVAL test cases */ |
---|
369 | |
---|
370 | /* Case: Negative access time tv_sec value */ |
---|
371 | time[0].tv_sec = -1; |
---|
372 | time[0].tv_usec = 12345; |
---|
373 | time[1].tv_sec = 54321; |
---|
374 | time[1].tv_usec = 54321; |
---|
375 | |
---|
376 | rv = utimes( "testfile1.tst", time ); |
---|
377 | rtems_test_assert( rv == -1 ); |
---|
378 | rtems_test_assert( errno == EINVAL ); |
---|
379 | |
---|
380 | /* Case: Negative modified time second value */ |
---|
381 | time[0].tv_sec = 12345; |
---|
382 | time[1].tv_sec = -1; |
---|
383 | |
---|
384 | rv = utimes( "testfile1.tst", time ); |
---|
385 | rtems_test_assert( rv == -1 ); |
---|
386 | rtems_test_assert( errno == EINVAL ); |
---|
387 | |
---|
388 | /* Case: Negative access time microsecond value */ |
---|
389 | time[1].tv_sec = 54321; |
---|
390 | time[0].tv_usec = -1; |
---|
391 | |
---|
392 | rv = utimes( "testfile1.tst", time ); |
---|
393 | rtems_test_assert( rv == -1 ); |
---|
394 | rtems_test_assert( errno == EINVAL ); |
---|
395 | |
---|
396 | /* Case: Negative modified time microsecond value */ |
---|
397 | time[0].tv_usec = 12345; |
---|
398 | time[1].tv_usec = -1; |
---|
399 | |
---|
400 | rv = utimes( "testfile1.tst", time ); |
---|
401 | rtems_test_assert( rv == -1 ); |
---|
402 | rtems_test_assert( errno == EINVAL ); |
---|
403 | |
---|
404 | /* Case: Access time microsecond value too large */ |
---|
405 | time[0].tv_usec = 1000000; |
---|
406 | time[1].tv_usec = 54321; |
---|
407 | |
---|
408 | rv = utimes( "testfile1.tst", time ); |
---|
409 | rtems_test_assert( rv == -1 ); |
---|
410 | rtems_test_assert( errno == EINVAL ); |
---|
411 | |
---|
412 | /* Case: Modified time microsecond value too large */ |
---|
413 | time[1].tv_usec = 1000000; |
---|
414 | time[0].tv_usec = 12345; |
---|
415 | |
---|
416 | rv = utimes( "testfile1.tst", time ); |
---|
417 | rtems_test_assert( rv == -1 ); |
---|
418 | rtems_test_assert( errno == EINVAL ); |
---|
419 | |
---|
420 | /* Successful test cases */ |
---|
421 | |
---|
422 | /* Case: Test without times argument */ |
---|
423 | clock_gettime( CLOCK_REALTIME, ¤t_time ); |
---|
424 | |
---|
425 | rv = utimes( "testfile1.tst", NULL ); |
---|
426 | rtems_test_assert( rv == 0 ); |
---|
427 | |
---|
428 | rv = stat( "testfile1.tst", &fstat ); |
---|
429 | rtems_test_assert( rv == 0 ); |
---|
430 | rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec ); |
---|
431 | rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec ); |
---|
432 | |
---|
433 | /* Case: time is filled with valid values */ |
---|
434 | time[0].tv_sec = 12345; |
---|
435 | time[0].tv_usec = 12345; |
---|
436 | time[1].tv_sec = 54321; |
---|
437 | time[1].tv_usec = 54321; |
---|
438 | |
---|
439 | rv = utimes( "testfile1.tst", time ); |
---|
440 | rtems_test_assert( rv == 0 ); |
---|
441 | |
---|
442 | /* Check that it actually changed the time */ |
---|
443 | rv = stat( "testfile1.tst", &fstat ); |
---|
444 | rtems_test_assert( rv == 0 ); |
---|
445 | rtems_test_assert( fstat.st_atime == 12345 ); |
---|
446 | rtems_test_assert( fstat.st_mtime == 54321 ); |
---|
447 | } |
---|
448 | |
---|
449 | /** |
---|
450 | * @brief Exercises utimensat(). |
---|
451 | */ |
---|
452 | static void UTimensatTest( void ) |
---|
453 | { |
---|
454 | int rv; |
---|
455 | struct timespec time[2]; |
---|
456 | struct timespec current_time; |
---|
457 | struct stat fstat; |
---|
458 | |
---|
459 | /* ENOSYS test cases */ |
---|
460 | |
---|
461 | /* Case: Pass an unsupported file descriptor */ |
---|
462 | rv = utimensat( |
---|
463 | 0, |
---|
464 | "!This is an =invalid p@thname!!! : )", |
---|
465 | NULL, |
---|
466 | 0 |
---|
467 | ); |
---|
468 | rtems_test_assert( rv == -1 ); |
---|
469 | rtems_test_assert( errno == ENOSYS ); |
---|
470 | |
---|
471 | /* Case: Pass unsupported flag */ |
---|
472 | rv = utimensat( |
---|
473 | AT_FDCWD, |
---|
474 | "!This is an =invalid p@thname!!! : )", |
---|
475 | NULL, |
---|
476 | 1 |
---|
477 | ); |
---|
478 | rtems_test_assert( rv == -1 ); |
---|
479 | rtems_test_assert( errno == ENOSYS ); |
---|
480 | |
---|
481 | /* ENOENT test case */ |
---|
482 | |
---|
483 | /* Use an invalid filename. */ |
---|
484 | rv = utimensat( |
---|
485 | AT_FDCWD, |
---|
486 | "!This is an =invalid p@thname!!! : )", |
---|
487 | NULL, |
---|
488 | 0 |
---|
489 | ); |
---|
490 | rtems_test_assert( rv == -1 ); |
---|
491 | rtems_test_assert( errno == ENOENT ); |
---|
492 | |
---|
493 | rv = stat( "testfile1.tst", &fstat ); |
---|
494 | rtems_test_assert( rv == 0 ); |
---|
495 | |
---|
496 | /* EACCES test Cases */ |
---|
497 | |
---|
498 | /* Case: When times is NULL and the user has insufficient privileges */ |
---|
499 | |
---|
500 | /* Change the user ID of the process to someone besides root */ |
---|
501 | rv = seteuid( 1 ); |
---|
502 | rtems_test_assert( rv == 0 ); |
---|
503 | |
---|
504 | rv = utimensat( AT_FDCWD, "testfile1.tst", NULL, 0 ); |
---|
505 | rtems_test_assert( rv == -1 ); |
---|
506 | rtems_test_assert( errno == EACCES ); |
---|
507 | |
---|
508 | rv = seteuid( 0 ); |
---|
509 | rtems_test_assert( rv == 0 ); |
---|
510 | |
---|
511 | /* Case: File is read-only and time's tv_nsec members are UTIME_NOW */ |
---|
512 | |
---|
513 | /* Change file to be read-only */ |
---|
514 | rv = chmod( "testfile1.tst", 06444 ); |
---|
515 | rtems_test_assert( rv == 0 ); |
---|
516 | |
---|
517 | _Timespec_Set( &time[0], 0, UTIME_NOW ); |
---|
518 | _Timespec_Set( &time[1], 0, UTIME_NOW ); |
---|
519 | |
---|
520 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
521 | rtems_test_assert( rv == -1 ); |
---|
522 | rtems_test_assert( errno == EACCES ); |
---|
523 | |
---|
524 | rv = chmod( "testfile1.tst", fstat.st_mode ); |
---|
525 | rtems_test_assert( rv == 0 ); |
---|
526 | |
---|
527 | /* EINVAL test cases */ |
---|
528 | |
---|
529 | /* Case: Negative access time second value */ |
---|
530 | _Timespec_Set( &time[0], -12345, 12345 ); |
---|
531 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
532 | |
---|
533 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
534 | rtems_test_assert( rv == -1 ); |
---|
535 | rtems_test_assert( errno == EINVAL ); |
---|
536 | |
---|
537 | /* Case: Negative modified time second value */ |
---|
538 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
539 | _Timespec_Set( &time[1], -54321, 54321 ); |
---|
540 | |
---|
541 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
542 | rtems_test_assert( rv == -1 ); |
---|
543 | rtems_test_assert( errno == EINVAL ); |
---|
544 | |
---|
545 | /* Case: Negative access time nanosecond value */ |
---|
546 | _Timespec_Set( &time[0], 12345, -12345 ); |
---|
547 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
548 | |
---|
549 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
550 | rtems_test_assert( rv == -1 ); |
---|
551 | rtems_test_assert( errno == EINVAL ); |
---|
552 | |
---|
553 | /* Case: Negative modified time nanosecond value */ |
---|
554 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
555 | _Timespec_Set( &time[1], 54321, -54321 ); |
---|
556 | |
---|
557 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
558 | rtems_test_assert( rv == -1 ); |
---|
559 | rtems_test_assert( errno == EINVAL ); |
---|
560 | |
---|
561 | /* Case: Access time nanosecond value too large */ |
---|
562 | _Timespec_Set( &time[0], 12345, TOD_NANOSECONDS_PER_SECOND ); |
---|
563 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
564 | |
---|
565 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
566 | rtems_test_assert( rv == -1 ); |
---|
567 | rtems_test_assert( errno == EINVAL ); |
---|
568 | |
---|
569 | /* Case: Modified time nanosecond value too large */ |
---|
570 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
571 | _Timespec_Set( &time[1], 54321, TOD_NANOSECONDS_PER_SECOND ); |
---|
572 | |
---|
573 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
574 | rtems_test_assert( rv == -1 ); |
---|
575 | rtems_test_assert( errno == EINVAL ); |
---|
576 | |
---|
577 | /* Successful test cases */ |
---|
578 | |
---|
579 | /* Case: Test without times argument */ |
---|
580 | clock_gettime( CLOCK_REALTIME, ¤t_time ); |
---|
581 | |
---|
582 | rv = utimensat( AT_FDCWD, "testfile1.tst", NULL, 0 ); |
---|
583 | rtems_test_assert( rv == 0 ); |
---|
584 | |
---|
585 | rv = stat( "testfile1.tst", &fstat ); |
---|
586 | rtems_test_assert( rv == 0 ); |
---|
587 | rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec ); |
---|
588 | rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec ); |
---|
589 | |
---|
590 | /* Case: Running with access time nanosecond field equal to UTIME_NOW */ |
---|
591 | _Timespec_Set( &time[0], 12345, UTIME_NOW ); |
---|
592 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
593 | |
---|
594 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
595 | rtems_test_assert( rv == 0 ); |
---|
596 | |
---|
597 | rv = stat( "testfile1.tst", &fstat ); |
---|
598 | rtems_test_assert( rv == 0 ); |
---|
599 | rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec ); |
---|
600 | rtems_test_assert( fstat.st_mtime == 54321 ); |
---|
601 | |
---|
602 | /* Case: Running with modified time nanosecond field equal to UTIME_NOW */ |
---|
603 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
604 | _Timespec_Set( &time[1], 54321, UTIME_NOW ); |
---|
605 | |
---|
606 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
607 | rtems_test_assert( rv == 0 ); |
---|
608 | |
---|
609 | rv = stat( "testfile1.tst", &fstat ); |
---|
610 | rtems_test_assert( rv == 0 ); |
---|
611 | rtems_test_assert( fstat.st_atime == 12345 ); |
---|
612 | rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec ); |
---|
613 | |
---|
614 | /* Case: Normal run */ |
---|
615 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
616 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
617 | |
---|
618 | rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 ); |
---|
619 | rtems_test_assert( rv == 0 ); |
---|
620 | |
---|
621 | /* Check that it actually changed the time */ |
---|
622 | rv = stat( "testfile1.tst", &fstat ); |
---|
623 | rtems_test_assert( rv == 0 ); |
---|
624 | rtems_test_assert( fstat.st_atime == 12345 ); |
---|
625 | rtems_test_assert( fstat.st_mtime == 54321 ); |
---|
626 | } |
---|
627 | |
---|
628 | /** |
---|
629 | * @brief Exercises futimens(). |
---|
630 | */ |
---|
631 | static void FutimensTest( void ) |
---|
632 | { |
---|
633 | int rv; |
---|
634 | int fd; |
---|
635 | struct timespec time[2]; |
---|
636 | struct timespec current_time; |
---|
637 | struct stat fstat; |
---|
638 | |
---|
639 | /* EBADF test case */ |
---|
640 | |
---|
641 | /* Case: Pass an invalid file descriptor */ |
---|
642 | _Timespec_Set_to_zero( &time[0] ); |
---|
643 | _Timespec_Set_to_zero( &time[1] ); |
---|
644 | rv = futimens( -1, time ); |
---|
645 | rtems_test_assert( rv == -1 ); |
---|
646 | rtems_test_assert( errno == EBADF ); |
---|
647 | |
---|
648 | fd = open( "testfile1.tst", O_RDWR ); |
---|
649 | rtems_test_assert( fd != -1 ); |
---|
650 | |
---|
651 | /* EACCES test cases */ |
---|
652 | |
---|
653 | /* Case: When times is NULL and the user has insufficient privileges */ |
---|
654 | |
---|
655 | /* Change the user ID of the process to someone besides root */ |
---|
656 | rv = seteuid( 1 ); |
---|
657 | rtems_test_assert( rv == 0 ); |
---|
658 | |
---|
659 | rv = futimens( fd, NULL ); |
---|
660 | rtems_test_assert( rv == -1 ); |
---|
661 | rtems_test_assert( errno == EACCES ); |
---|
662 | |
---|
663 | rv = seteuid( 0 ); |
---|
664 | rtems_test_assert( rv == 0 ); |
---|
665 | |
---|
666 | /* Case: File is read-only and time's tv_nsec members are UTIME_NOW */ |
---|
667 | |
---|
668 | /* Change file to be read-only */ |
---|
669 | rv = chmod( "testfile1.tst", 06444 ); |
---|
670 | rtems_test_assert( rv == 0 ); |
---|
671 | |
---|
672 | _Timespec_Set( &time[0], 0, UTIME_NOW ); |
---|
673 | _Timespec_Set( &time[1], 0, UTIME_NOW ); |
---|
674 | |
---|
675 | rv = futimens( fd, time ); |
---|
676 | rtems_test_assert( rv == -1 ); |
---|
677 | rtems_test_assert( errno == EACCES ); |
---|
678 | |
---|
679 | rv = chmod( "testfile1.tst", fstat.st_mode ); |
---|
680 | rtems_test_assert( rv == 0 ); |
---|
681 | |
---|
682 | /* EINVAL test cases */ |
---|
683 | |
---|
684 | /* Case: Negative access time second value */ |
---|
685 | _Timespec_Set( &time[0], -12345, 12345 ); |
---|
686 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
687 | |
---|
688 | rv = futimens( fd, time ); |
---|
689 | rtems_test_assert( rv == -1 ); |
---|
690 | rtems_test_assert( errno == EINVAL ); |
---|
691 | |
---|
692 | /* Case: Negative modified time second value */ |
---|
693 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
694 | _Timespec_Set( &time[1], -54321, 54321 ); |
---|
695 | |
---|
696 | rv = futimens( fd, time ); |
---|
697 | rtems_test_assert( rv == -1 ); |
---|
698 | rtems_test_assert( errno == EINVAL ); |
---|
699 | |
---|
700 | /* Case: Negative access time nanosecond value */ |
---|
701 | _Timespec_Set( &time[0], 12345, -12345 ); |
---|
702 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
703 | |
---|
704 | rv = futimens( fd, time ); |
---|
705 | rtems_test_assert( rv == -1 ); |
---|
706 | rtems_test_assert( errno == EINVAL ); |
---|
707 | |
---|
708 | /* Case: Negative modified time nanosecond value */ |
---|
709 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
710 | _Timespec_Set( &time[1], 54321, -54321 ); |
---|
711 | |
---|
712 | rv = futimens( fd, time ); |
---|
713 | rtems_test_assert( rv == -1 ); |
---|
714 | rtems_test_assert( errno == EINVAL ); |
---|
715 | |
---|
716 | /* Case: Access time nanosecond value too large */ |
---|
717 | _Timespec_Set( &time[0], 12345, TOD_NANOSECONDS_PER_SECOND ); |
---|
718 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
719 | |
---|
720 | rv = futimens( fd, time ); |
---|
721 | rtems_test_assert( rv == -1 ); |
---|
722 | rtems_test_assert( errno == EINVAL ); |
---|
723 | |
---|
724 | /* Case: Modified time nanosecond value too large */ |
---|
725 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
726 | _Timespec_Set( &time[1], 54321, TOD_NANOSECONDS_PER_SECOND ); |
---|
727 | |
---|
728 | rv = futimens( fd, time ); |
---|
729 | rtems_test_assert( rv == -1 ); |
---|
730 | rtems_test_assert( errno == EINVAL ); |
---|
731 | |
---|
732 | /* Successful test cases */ |
---|
733 | |
---|
734 | /* Case: Test without times argument */ |
---|
735 | clock_gettime( CLOCK_REALTIME, ¤t_time ); |
---|
736 | |
---|
737 | rv = futimens( fd, NULL ); |
---|
738 | rtems_test_assert( rv == 0 ); |
---|
739 | |
---|
740 | rv = stat( "testfile1.tst", &fstat ); |
---|
741 | rtems_test_assert( rv == 0 ); |
---|
742 | rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec ); |
---|
743 | rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec ); |
---|
744 | |
---|
745 | /* Case: Running with access time nanosecond field equal to UTIME_NOW */ |
---|
746 | _Timespec_Set( &time[0], 12345, UTIME_NOW ); |
---|
747 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
748 | |
---|
749 | rv = futimens( fd, time ); |
---|
750 | rtems_test_assert( rv == 0 ); |
---|
751 | |
---|
752 | rv = stat( "testfile1.tst", &fstat ); |
---|
753 | rtems_test_assert( rv == 0 ); |
---|
754 | rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec ); |
---|
755 | rtems_test_assert( fstat.st_mtime == 54321 ); |
---|
756 | |
---|
757 | /* Case: Running with modified time nanosecond field equal to UTIME_NOW */ |
---|
758 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
759 | _Timespec_Set( &time[1], 54321, UTIME_NOW ); |
---|
760 | |
---|
761 | rv = futimens( fd, time ); |
---|
762 | rtems_test_assert( rv == 0 ); |
---|
763 | |
---|
764 | rv = stat( "testfile1.tst", &fstat ); |
---|
765 | rtems_test_assert( rv == 0 ); |
---|
766 | rtems_test_assert( fstat.st_atime == 12345 ); |
---|
767 | rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec ); |
---|
768 | |
---|
769 | /* Case: Normal run */ |
---|
770 | _Timespec_Set( &time[0], 12345, 12345 ); |
---|
771 | _Timespec_Set( &time[1], 54321, 54321 ); |
---|
772 | |
---|
773 | rv = futimens( fd, time ); |
---|
774 | rtems_test_assert( rv == 0 ); |
---|
775 | |
---|
776 | /* Check that it actually changed the time */ |
---|
777 | rv = stat( "testfile1.tst", &fstat ); |
---|
778 | rtems_test_assert( rv == 0 ); |
---|
779 | rtems_test_assert( fstat.st_atime == 12345 ); |
---|
780 | rtems_test_assert( fstat.st_mtime == 54321 ); |
---|
781 | } |
---|
782 | |
---|
783 | /** |
---|
784 | * @brief Exercises pathconf(). |
---|
785 | */ |
---|
786 | static void PathConfTest( void ) |
---|
787 | { |
---|
788 | int rv; |
---|
789 | |
---|
790 | rv = pathconf( "thisfiledoesnotexist", _PC_LINK_MAX ); |
---|
791 | rtems_test_assert( rv == -1 ); |
---|
792 | |
---|
793 | rv = pathconf( "testfile1.tst", _PC_LINK_MAX ); |
---|
794 | rtems_test_assert( rv != -1 ); |
---|
795 | } |
---|
796 | |
---|
797 | /** |
---|
798 | * @brief Exercises fpathconf(). |
---|
799 | */ |
---|
800 | static void FPathConfTest( void ) |
---|
801 | { |
---|
802 | int rv; |
---|
803 | int fd; |
---|
804 | |
---|
805 | fd = -1; |
---|
806 | rv = fpathconf( fd, _PC_LINK_MAX ); |
---|
807 | rtems_test_assert( rv == -1 ); |
---|
808 | |
---|
809 | fd = open( "testfile1.tst", O_RDWR ); |
---|
810 | rtems_test_assert( fd != -1 ); |
---|
811 | |
---|
812 | rv = fpathconf( fd, _PC_LINK_MAX ); |
---|
813 | rtems_test_assert( rv != -1 ); |
---|
814 | |
---|
815 | rv = fpathconf( fd, _PC_MAX_CANON ); |
---|
816 | rtems_test_assert( rv != -1 ); |
---|
817 | |
---|
818 | rv = fpathconf( fd, _PC_MAX_INPUT ); |
---|
819 | rtems_test_assert( rv != -1 ); |
---|
820 | |
---|
821 | rv = fpathconf( fd, _PC_NAME_MAX ); |
---|
822 | rtems_test_assert( rv != -1 ); |
---|
823 | |
---|
824 | rv = fpathconf( fd, _PC_PATH_MAX ); |
---|
825 | rtems_test_assert( rv != -1 ); |
---|
826 | |
---|
827 | rv = fpathconf( fd, _PC_PIPE_BUF ); |
---|
828 | rtems_test_assert( rv != -1 ); |
---|
829 | |
---|
830 | rv = fpathconf( fd, _PC_CHOWN_RESTRICTED ); |
---|
831 | rtems_test_assert( rv != -1 ); |
---|
832 | |
---|
833 | rv = fpathconf( fd, _PC_NO_TRUNC ); |
---|
834 | rtems_test_assert( rv != -1 ); |
---|
835 | |
---|
836 | rv = fpathconf( fd, _PC_VDISABLE ); |
---|
837 | rtems_test_assert( rv != -1 ); |
---|
838 | |
---|
839 | rv = fpathconf( fd, _PC_ASYNC_IO ); |
---|
840 | rtems_test_assert( rv != -1 ); |
---|
841 | |
---|
842 | rv = fpathconf( fd, _PC_PRIO_IO ); |
---|
843 | rtems_test_assert( rv != -1 ); |
---|
844 | |
---|
845 | rv = fpathconf( fd, _PC_SYNC_IO ); |
---|
846 | rtems_test_assert( rv != -1 ); |
---|
847 | |
---|
848 | rv = fpathconf( fd, 255 ); |
---|
849 | rtems_test_assert( rv == -1 ); |
---|
850 | |
---|
851 | rv = close( fd ); |
---|
852 | rtems_test_assert( rv == 0 ); |
---|
853 | |
---|
854 | fd = open( "testfile1.tst", O_WRONLY ); |
---|
855 | rtems_test_assert( rv != -1 ); |
---|
856 | |
---|
857 | rv = fpathconf( fd, _PC_LINK_MAX ); |
---|
858 | rtems_test_assert( rv != -1 ); |
---|
859 | |
---|
860 | rv = close( fd ); |
---|
861 | rtems_test_assert( rv == 0 ); |
---|
862 | } |
---|
863 | |
---|
864 | /** |
---|
865 | * @brief Exercises fsync(). |
---|
866 | */ |
---|
867 | static void FSyncTest( void ) |
---|
868 | { |
---|
869 | int rv; |
---|
870 | int fd; |
---|
871 | |
---|
872 | fd = open( "testfile1.tst", O_RDWR ); |
---|
873 | rtems_test_assert( fd != -1 ); |
---|
874 | |
---|
875 | rv = fsync(fd); |
---|
876 | rtems_test_assert( rv != -1 ); |
---|
877 | |
---|
878 | rv = close( fd ); |
---|
879 | rtems_test_assert( rv == 0 ); |
---|
880 | } |
---|
881 | |
---|
882 | /** |
---|
883 | * @brief Exercises sync(). |
---|
884 | */ |
---|
885 | static void SyncTest( void ) |
---|
886 | { |
---|
887 | sync(); |
---|
888 | } |
---|
889 | |
---|
890 | /** |
---|
891 | * @brief The main entry point to the test. |
---|
892 | */ |
---|
893 | int test_main( void ); |
---|
894 | int test_main( void ) |
---|
895 | { |
---|
896 | TEST_BEGIN(); |
---|
897 | |
---|
898 | InitFiles(); |
---|
899 | |
---|
900 | DeviceLSeekTest(); |
---|
901 | DupTest(); |
---|
902 | Dup2Test(); |
---|
903 | FDataSyncTest(); |
---|
904 | UMaskTest(); |
---|
905 | UTimeTest(); |
---|
906 | UTimesTest(); |
---|
907 | UTimensatTest(); |
---|
908 | FutimensTest(); |
---|
909 | FSyncTest(); |
---|
910 | PathConfTest(); |
---|
911 | FPathConfTest(); |
---|
912 | SyncTest(); |
---|
913 | |
---|
914 | TEST_END(); |
---|
915 | |
---|
916 | rtems_test_exit( 0 ); |
---|
917 | } |
---|