compiler/clib: Remove usage of acb_environptr field outside __arosc_environ.c
[AROS.git] / test / clib / access_test.c
blobcfc31a9684b340caaffeedeca0a4ed2035c5e5fe
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <unistd.h>
6 #define SHOW_PROGRESS 0
8 // Global variables
9 int global_test_counter = 0;
10 int global_test_step_counter = 0;
11 int global_failure_indicator = 0;
13 /* 2008-03-22 - All test pass (if manual configuration is done correclty) */
15 /* COMPILE WITH '-nix' for unix2amiga path conversion */
17 /* These values are arbitrary and influence test results! */
18 /* Existing objects should have RWED permitions (even the volume!) */
20 #if defined __AROS__
21 const char * existing_volume = "/Work/";
22 const char * existing_directory = "/SYS/Classes";
23 const char * existing_file = "/SYS/Classes/Datatypes/bmp.datatype";
24 const char * non_existing_volume = "/sw243";
25 const char * non_existing_directory = "/SYS/sdgfer";
26 const char * non_existing_file = "/SYS/Classes/ggg.txt";
27 #else
28 const char * existing_volume = "/usr";
29 const char * existing_directory = "/usr/bin";
30 const char * existing_file = "/usr/bin/gcc";
31 const char * non_existing_volume = "/sw243";
32 const char * non_existing_directory = "/usr/sdgfer";
33 const char * non_existing_file = "/usr/lib/ggg.txt";
34 #endif
36 /* These files need to be prepared in application directory with appriopriate access rights! */
37 const char * read_only_file = "read_only_file";
38 const char * write_only_file = "write_only_file";
39 const char * execute_only_file = "execute_only_file";
40 const char * all_access_file = "all_access_file";
42 void reset_global_test_counter()
44 global_test_counter = 0;
45 global_test_step_counter = 0;
48 void next_test()
50 global_test_counter++;
51 global_test_step_counter = 0;
54 void next_test_step()
56 global_test_step_counter++;
59 // Reporting functions
60 void report(const char * status, const char * message)
62 printf("REPORT : %s : %s \n", status, message);
65 void report_progress(const char * message)
67 report("PROGRESS", message);
70 void report_failure(const char * message)
72 report("FAILED", message);
75 void report_failure_strerror()
77 report_failure((const char*)strerror(errno));
80 void test_report(const char * status, const char * message)
82 printf("TEST %d-%d : %s : %s \n", global_test_counter , global_test_step_counter, status, message);
85 void test_report_progress(const char * message)
87 #if SHOW_PROGRESS == 1
88 test_report("PROGRESS", message);
89 #endif
92 void test_report_failure(const char * message)
94 test_report("FAILED", message);
97 void test_report_success(const char * message)
99 test_report("OK", message);
102 void test_report_description(const char * message)
104 test_report("TEST DESCRIPTION", message);
107 void test_report_failure_strerror()
109 test_report_failure((const char*)strerror(errno));
112 // Test
113 int test_access_preparation()
117 int test_access(int mode)
119 report_progress("Start test_access");
120 if (mode & R_OK)
121 report_progress("R_OK");
122 if (mode & W_OK)
123 report_progress("W_OK");
124 if (mode & X_OK)
125 report_progress("X_OK");
126 if (mode == 0)
127 report_progress("F_OK");
131 * TEST
134 next_test_step();
136 test_report_description("access existing volume");
138 // access existing volume
139 if (access(existing_volume, mode) != 0)
141 test_report_failure_strerror();
142 test_report_failure("Failed accessing existing volume");
143 return -1;
145 test_report_success("Accessed existing volume");
146 test_report_success("SUCCESS");
149 * TEST END
153 * TEST
156 next_test_step();
158 test_report_description("access existing directory");
160 // access existing directory
161 if (access(existing_directory, mode) != 0)
163 test_report_failure_strerror();
164 test_report_failure("Failed accessing existing directory");
165 return -1;
167 test_report_success("Accessed existing directory");
168 test_report_success("SUCCESS");
171 * TEST END
175 * TEST
178 next_test_step();
180 test_report_description("access existing file");
182 // access existing file
183 if (access(existing_file, mode) != 0)
185 test_report_failure_strerror();
186 test_report_failure("Failed accessing existing file");
187 return -1;
189 test_report_success("Accessed existing file");
190 test_report_success("SUCCESS");
193 * TEST END
197 * TEST
200 next_test_step();
202 test_report_description("access non existing volume");
204 // access non existing volume
205 if (access(non_existing_volume, mode) == 0)
207 test_report_failure("This call should fail");
208 return -1;
210 else
212 if (errno != ENOENT)
214 test_report_failure_strerror();
215 test_report_failure("Different error expected");
216 return -1;
219 test_report_success("Correct error reported");
220 test_report_success("SUCCESS");
223 * TEST END
227 * TEST
230 next_test_step();
232 test_report_description("access non existing directory");
234 // access non existing directory
235 if (access(non_existing_directory, mode) == 0)
237 test_report_failure("This call should fail");
238 return -1;
240 else
242 if (errno != ENOENT)
244 test_report_failure_strerror();
245 test_report_failure("Different error expected");
246 return -1;
249 test_report_success("Correct error reported");
250 test_report_success("SUCCESS");
253 * TEST END
257 * TEST
260 next_test_step();
262 test_report_description("access non existing file");
264 // access non existing file
265 if (access(non_existing_file, mode) == 0)
267 test_report_failure("This call should fail");
268 return -1;
270 else
272 if (errno != ENOENT)
274 test_report_failure_strerror();
275 test_report_failure("Different error expected");
276 return -1;
279 test_report_success("Correct error reported");
280 test_report_success("SUCCESS");
283 * TEST END
286 return 0;
289 int test_access_wrapper(int mode)
291 next_test();
292 return test_access(mode);
295 int test_single_file_access_modes()
297 next_test();
299 /* Manual action required for test. See top of file */
301 report_progress("Start test_file_access_modes");
304 * TEST
307 next_test_step();
309 test_report_description("access F_OK for read_only_file");
311 // access F_OK for read_only_file
312 if (access(read_only_file, F_OK) != 0)
314 test_report_failure_strerror();
315 test_report_failure("Failed accessing file");
316 return -1;
318 test_report_success("File accessed");
319 test_report_success("SUCCESS");
322 * TEST END
326 * TEST
329 next_test_step();
331 test_report_description("access F_OK for write_only_file");
333 // access F_OK for write_only_file
334 if (access(write_only_file, F_OK) != 0)
336 test_report_failure_strerror();
337 test_report_failure("Failed accessing file");
338 return -1;
340 test_report_success("File accessed");
341 test_report_success("SUCCESS");
344 * TEST END
348 * TEST
351 next_test_step();
353 test_report_description("access F_OK for execute_only_file");
355 // access F_OK for execute_only_file
356 if (access(execute_only_file, F_OK) != 0)
358 test_report_failure_strerror();
359 test_report_failure("Failed accessing file");
360 return -1;
362 test_report_success("File accessed");
363 test_report_success("SUCCESS");
366 * TEST END
370 * TEST
373 next_test_step();
375 test_report_description("access R_OK for read_only_file");
377 // access R_OK for read_only_file
378 if (access(read_only_file, R_OK) != 0)
380 test_report_failure_strerror();
381 test_report_failure("Failed accessing file");
382 return -1;
384 test_report_success("File accessed");
385 test_report_success("SUCCESS");
388 * TEST END
392 * TEST
395 next_test_step();
397 test_report_description("access R_OK | W_OK for read_only_file");
399 // access R_OK | W_OK for read_only_file
400 if (access(read_only_file, R_OK | W_OK) != 0)
402 if (errno != EACCES)
404 test_report_failure_strerror();
405 test_report_failure("Different error expected");
406 return -1;
409 else
411 test_report_failure("This operation should fail");
412 return -1;
414 test_report_success("Correct error reported");
415 test_report_success("SUCCESS");
418 * TEST END
422 * TEST
425 next_test_step();
427 test_report_description("access R_OK | X_OK for read_only_file");
429 // access R_OK | X_OK for read_only_file
430 if (access(read_only_file, R_OK | X_OK) != 0)
432 if (errno != EACCES)
434 test_report_failure_strerror();
435 test_report_failure("Different error expected");
436 return -1;
439 else
441 test_report_failure("This operation should fail");
442 return -1;
444 test_report_success("Correct error reported");
445 test_report_success("SUCCESS");
448 * TEST END
452 * TEST
455 next_test_step();
457 test_report_description("access W_OK for write_only_file");
459 // access W_OK for write_only_file
460 if (access(write_only_file, W_OK) != 0)
462 test_report_failure_strerror();
463 test_report_failure("Failed accessing file");
464 return -1;
466 test_report_success("File accessed");
467 test_report_success("SUCCESS");
470 * TEST END
474 * TEST
477 next_test_step();
479 test_report_description("access W_OK | R_OK for write_only_file");
481 // access W_OK | R_OK for write_only_file
482 if (access(write_only_file, W_OK | R_OK) != 0)
484 if (errno != EACCES)
486 test_report_failure_strerror();
487 test_report_failure("Different error expected");
488 return -1;
491 else
493 test_report_failure("This operation should fail");
494 return -1;
496 test_report_success("Correct error reported");
497 test_report_success("SUCCESS");
500 * TEST END
504 * TEST
507 next_test_step();
509 test_report_description("access W_OK | X_OK for write_only_file");
511 // access W_OK | X_OK for write_only_file
512 if (access(write_only_file, W_OK | X_OK) != 0)
514 if (errno != EACCES)
516 test_report_failure_strerror();
517 test_report_failure("Different error expected");
518 return -1;
521 else
523 test_report_failure("This operation should fail");
524 return -1;
526 test_report_success("Correct error reported");
527 test_report_success("SUCCESS");
530 * TEST END
535 * TEST
538 next_test_step();
540 test_report_description("access X_OK for execute_only_file");
542 // access X_OK for execute_only_file
543 if (access(execute_only_file, X_OK) != 0)
545 test_report_failure_strerror();
546 test_report_failure("Failed accessing file");
547 return -1;
549 test_report_success("File accessed");
550 test_report_success("SUCCESS");
553 * TEST END
557 * TEST
560 next_test_step();
562 test_report_description("access X_OK | R_OK for execute_only_file");
564 // access X_OK | R_OK for execute_only_file
565 if (access(execute_only_file, X_OK | R_OK) != 0)
567 if (errno != EACCES)
569 test_report_failure_strerror();
570 test_report_failure("Different error expected");
571 return -1;
574 else
576 test_report_failure("This operation should fail");
577 return -1;
579 test_report_success("Correct error reported");
580 test_report_success("SUCCESS");
583 * TEST END
587 * TEST
590 next_test_step();
592 test_report_description("access X_OK | W_OK for execute_only_file");
594 // access X_OK | W_OK for execute_only_file
595 if (access(execute_only_file, X_OK | W_OK) != 0)
597 if (errno != EACCES)
599 test_report_failure_strerror();
600 test_report_failure("Different error expected");
601 return -1;
604 else
606 test_report_failure("This operation should fail");
607 return -1;
609 test_report_success("Correct error reported");
610 test_report_success("SUCCESS");
613 * TEST END
616 return 0;
619 int test_combined_file_access_modes()
621 next_test();
623 /* Manual action required for test. See top of file */
625 report_progress("Start test_combined_file_access_modes");
629 * TEST
632 next_test_step();
634 test_report_description("access R_OK | W_OK for all_access_file");
636 // access R_OK | W_OK for all_access_file
637 if (access(all_access_file, R_OK | W_OK) != 0)
639 test_report_failure_strerror();
640 test_report_failure("Failed accessing file");
641 return -1;
643 test_report_success("File accessed");
644 test_report_success("SUCCESS");
647 * TEST END
651 * TEST
654 next_test_step();
656 test_report_description("access R_OK | X_OK for all_access_file");
658 // access R_OK | X_OK for all_access_file
659 if (access(all_access_file, R_OK | X_OK) != 0)
661 test_report_failure_strerror();
662 test_report_failure("Failed accessing file");
663 return -1;
665 test_report_success("File accessed");
666 test_report_success("SUCCESS");
669 * TEST END
673 * TEST
676 next_test_step();
678 test_report_description("access W_OK | X_OK for all_access_file");
680 // access W_OK | X_OK for all_access_file
681 if (access(all_access_file, W_OK | X_OK) != 0)
683 test_report_failure_strerror();
684 test_report_failure("Failed accessing file");
685 return -1;
687 test_report_success("File accessed");
688 test_report_success("SUCCESS");
691 * TEST END
695 * TEST
698 next_test_step();
700 test_report_description("access R_OK | W_OK | X_OK for all_access_file");
702 // access R_OK | W_OK | X_OK for all_access_file
703 if (access(all_access_file, R_OK | W_OK | X_OK) != 0)
705 test_report_failure_strerror();
706 test_report_failure("Failed accessing file");
707 return -1;
709 test_report_success("File accessed");
710 test_report_success("SUCCESS");
713 * TEST END
717 * TEST
720 next_test_step();
722 test_report_description("access R_OK | W_OK | X_OK | F_OK for all_access_file");
724 // access R_OK | W_OK | X_OK | F_OK for all_access_file
725 if (access(all_access_file, R_OK | W_OK | X_OK | F_OK) != 0)
727 test_report_failure_strerror();
728 test_report_failure("Failed accessing file");
729 return -1;
731 test_report_success("File accessed");
732 test_report_success("SUCCESS");
735 * TEST END
738 return 0;
740 int main()
742 report_progress("Starting tests");
744 reset_global_test_counter();
746 // access test
747 if (test_access_wrapper(F_OK) != 0)
748 global_failure_indicator = 1;
750 if (test_access_wrapper(R_OK) != 0)
751 global_failure_indicator = 1;
753 if (test_access_wrapper(W_OK) != 0)
754 global_failure_indicator = 1;
756 if (test_access_wrapper(X_OK) != 0)
757 global_failure_indicator = 1;
759 if (test_access_wrapper(R_OK | W_OK | X_OK) != 0)
760 global_failure_indicator = 1;
762 if (test_single_file_access_modes() != 0)
763 global_failure_indicator = 1;
765 if (test_combined_file_access_modes() != 0)
766 global_failure_indicator = 1;
768 if (global_failure_indicator == 1)
769 report_failure("One of the tests FAILED");
770 else
771 report_progress("All tests SUCCEEDED");
773 report_progress("Tests finished");