Add sun4i ram controller definitions
[AROS.git] / test / clib / access_test.c
blob31cc6832d48d66f930ac86536f437710739d20ee
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <unistd.h>
11 #define SHOW_PROGRESS 0
13 // Global variables
14 int global_test_counter = 0;
15 int global_test_step_counter = 0;
16 int global_failure_indicator = 0;
18 /* 2008-03-22 - All test pass (if manual configuration is done correclty) */
20 /* COMPILE WITH '-nix' for unix2amiga path conversion */
22 /* These values are arbitrary and influence test results! */
23 /* Existing objects should have RWED permitions (even the volume!) */
25 #if defined __AROS__
26 const char * existing_volume = "/Work/";
27 const char * existing_directory = "/SYS/Classes";
28 const char * existing_file = "/SYS/Classes/Datatypes/bmp.datatype";
29 const char * non_existing_volume = "/sw243";
30 const char * non_existing_directory = "/SYS/sdgfer";
31 const char * non_existing_file = "/SYS/Classes/ggg.txt";
32 #else
33 const char * existing_volume = "/usr";
34 const char * existing_directory = "/usr/bin";
35 const char * existing_file = "/usr/bin/gcc";
36 const char * non_existing_volume = "/sw243";
37 const char * non_existing_directory = "/usr/sdgfer";
38 const char * non_existing_file = "/usr/lib/ggg.txt";
39 #endif
41 /* These files need to be prepared in application directory with appriopriate access rights! */
42 const char * read_only_file = "read_only_file";
43 const char * write_only_file = "write_only_file";
44 const char * execute_only_file = "execute_only_file";
45 const char * all_access_file = "all_access_file";
47 void reset_global_test_counter()
49 global_test_counter = 0;
50 global_test_step_counter = 0;
53 void next_test()
55 global_test_counter++;
56 global_test_step_counter = 0;
59 void next_test_step()
61 global_test_step_counter++;
64 // Reporting functions
65 void report(const char * status, const char * message)
67 printf("REPORT : %s : %s \n", status, message);
70 void report_progress(const char * message)
72 report("PROGRESS", message);
75 void report_failure(const char * message)
77 report("FAILED", message);
80 void report_failure_strerror()
82 report_failure((const char*)strerror(errno));
85 void test_report(const char * status, const char * message)
87 printf("TEST %d-%d : %s : %s \n", global_test_counter , global_test_step_counter, status, message);
90 void test_report_progress(const char * message)
92 #if SHOW_PROGRESS == 1
93 test_report("PROGRESS", message);
94 #endif
97 void test_report_failure(const char * message)
99 test_report("FAILED", message);
102 void test_report_success(const char * message)
104 test_report("OK", message);
107 void test_report_description(const char * message)
109 test_report("TEST DESCRIPTION", message);
112 void test_report_failure_strerror()
114 test_report_failure((const char*)strerror(errno));
117 // Test
118 int test_access_preparation()
122 int test_access(int mode)
124 report_progress("Start test_access");
125 if (mode & R_OK)
126 report_progress("R_OK");
127 if (mode & W_OK)
128 report_progress("W_OK");
129 if (mode & X_OK)
130 report_progress("X_OK");
131 if (mode == 0)
132 report_progress("F_OK");
136 * TEST
139 next_test_step();
141 test_report_description("access existing volume");
143 // access existing volume
144 if (access(existing_volume, mode) != 0)
146 test_report_failure_strerror();
147 test_report_failure("Failed accessing existing volume");
148 return -1;
150 test_report_success("Accessed existing volume");
151 test_report_success("SUCCESS");
154 * TEST END
158 * TEST
161 next_test_step();
163 test_report_description("access existing directory");
165 // access existing directory
166 if (access(existing_directory, mode) != 0)
168 test_report_failure_strerror();
169 test_report_failure("Failed accessing existing directory");
170 return -1;
172 test_report_success("Accessed existing directory");
173 test_report_success("SUCCESS");
176 * TEST END
180 * TEST
183 next_test_step();
185 test_report_description("access existing file");
187 // access existing file
188 if (access(existing_file, mode) != 0)
190 test_report_failure_strerror();
191 test_report_failure("Failed accessing existing file");
192 return -1;
194 test_report_success("Accessed existing file");
195 test_report_success("SUCCESS");
198 * TEST END
202 * TEST
205 next_test_step();
207 test_report_description("access non existing volume");
209 // access non existing volume
210 if (access(non_existing_volume, mode) == 0)
212 test_report_failure("This call should fail");
213 return -1;
215 else
217 if (errno != ENOENT)
219 test_report_failure_strerror();
220 test_report_failure("Different error expected");
221 return -1;
224 test_report_success("Correct error reported");
225 test_report_success("SUCCESS");
228 * TEST END
232 * TEST
235 next_test_step();
237 test_report_description("access non existing directory");
239 // access non existing directory
240 if (access(non_existing_directory, mode) == 0)
242 test_report_failure("This call should fail");
243 return -1;
245 else
247 if (errno != ENOENT)
249 test_report_failure_strerror();
250 test_report_failure("Different error expected");
251 return -1;
254 test_report_success("Correct error reported");
255 test_report_success("SUCCESS");
258 * TEST END
262 * TEST
265 next_test_step();
267 test_report_description("access non existing file");
269 // access non existing file
270 if (access(non_existing_file, mode) == 0)
272 test_report_failure("This call should fail");
273 return -1;
275 else
277 if (errno != ENOENT)
279 test_report_failure_strerror();
280 test_report_failure("Different error expected");
281 return -1;
284 test_report_success("Correct error reported");
285 test_report_success("SUCCESS");
288 * TEST END
291 return 0;
294 int test_access_wrapper(int mode)
296 next_test();
297 return test_access(mode);
300 int test_single_file_access_modes()
302 next_test();
304 /* Manual action required for test. See top of file */
306 report_progress("Start test_file_access_modes");
309 * TEST
312 next_test_step();
314 test_report_description("access F_OK for read_only_file");
316 // access F_OK for read_only_file
317 if (access(read_only_file, F_OK) != 0)
319 test_report_failure_strerror();
320 test_report_failure("Failed accessing file");
321 return -1;
323 test_report_success("File accessed");
324 test_report_success("SUCCESS");
327 * TEST END
331 * TEST
334 next_test_step();
336 test_report_description("access F_OK for write_only_file");
338 // access F_OK for write_only_file
339 if (access(write_only_file, F_OK) != 0)
341 test_report_failure_strerror();
342 test_report_failure("Failed accessing file");
343 return -1;
345 test_report_success("File accessed");
346 test_report_success("SUCCESS");
349 * TEST END
353 * TEST
356 next_test_step();
358 test_report_description("access F_OK for execute_only_file");
360 // access F_OK for execute_only_file
361 if (access(execute_only_file, F_OK) != 0)
363 test_report_failure_strerror();
364 test_report_failure("Failed accessing file");
365 return -1;
367 test_report_success("File accessed");
368 test_report_success("SUCCESS");
371 * TEST END
375 * TEST
378 next_test_step();
380 test_report_description("access R_OK for read_only_file");
382 // access R_OK for read_only_file
383 if (access(read_only_file, R_OK) != 0)
385 test_report_failure_strerror();
386 test_report_failure("Failed accessing file");
387 return -1;
389 test_report_success("File accessed");
390 test_report_success("SUCCESS");
393 * TEST END
397 * TEST
400 next_test_step();
402 test_report_description("access R_OK | W_OK for read_only_file");
404 // access R_OK | W_OK for read_only_file
405 if (access(read_only_file, R_OK | W_OK) != 0)
407 if (errno != EACCES)
409 test_report_failure_strerror();
410 test_report_failure("Different error expected");
411 return -1;
414 else
416 test_report_failure("This operation should fail");
417 return -1;
419 test_report_success("Correct error reported");
420 test_report_success("SUCCESS");
423 * TEST END
427 * TEST
430 next_test_step();
432 test_report_description("access R_OK | X_OK for read_only_file");
434 // access R_OK | X_OK for read_only_file
435 if (access(read_only_file, R_OK | X_OK) != 0)
437 if (errno != EACCES)
439 test_report_failure_strerror();
440 test_report_failure("Different error expected");
441 return -1;
444 else
446 test_report_failure("This operation should fail");
447 return -1;
449 test_report_success("Correct error reported");
450 test_report_success("SUCCESS");
453 * TEST END
457 * TEST
460 next_test_step();
462 test_report_description("access W_OK for write_only_file");
464 // access W_OK for write_only_file
465 if (access(write_only_file, W_OK) != 0)
467 test_report_failure_strerror();
468 test_report_failure("Failed accessing file");
469 return -1;
471 test_report_success("File accessed");
472 test_report_success("SUCCESS");
475 * TEST END
479 * TEST
482 next_test_step();
484 test_report_description("access W_OK | R_OK for write_only_file");
486 // access W_OK | R_OK for write_only_file
487 if (access(write_only_file, W_OK | R_OK) != 0)
489 if (errno != EACCES)
491 test_report_failure_strerror();
492 test_report_failure("Different error expected");
493 return -1;
496 else
498 test_report_failure("This operation should fail");
499 return -1;
501 test_report_success("Correct error reported");
502 test_report_success("SUCCESS");
505 * TEST END
509 * TEST
512 next_test_step();
514 test_report_description("access W_OK | X_OK for write_only_file");
516 // access W_OK | X_OK for write_only_file
517 if (access(write_only_file, W_OK | X_OK) != 0)
519 if (errno != EACCES)
521 test_report_failure_strerror();
522 test_report_failure("Different error expected");
523 return -1;
526 else
528 test_report_failure("This operation should fail");
529 return -1;
531 test_report_success("Correct error reported");
532 test_report_success("SUCCESS");
535 * TEST END
540 * TEST
543 next_test_step();
545 test_report_description("access X_OK for execute_only_file");
547 // access X_OK for execute_only_file
548 if (access(execute_only_file, X_OK) != 0)
550 test_report_failure_strerror();
551 test_report_failure("Failed accessing file");
552 return -1;
554 test_report_success("File accessed");
555 test_report_success("SUCCESS");
558 * TEST END
562 * TEST
565 next_test_step();
567 test_report_description("access X_OK | R_OK for execute_only_file");
569 // access X_OK | R_OK for execute_only_file
570 if (access(execute_only_file, X_OK | R_OK) != 0)
572 if (errno != EACCES)
574 test_report_failure_strerror();
575 test_report_failure("Different error expected");
576 return -1;
579 else
581 test_report_failure("This operation should fail");
582 return -1;
584 test_report_success("Correct error reported");
585 test_report_success("SUCCESS");
588 * TEST END
592 * TEST
595 next_test_step();
597 test_report_description("access X_OK | W_OK for execute_only_file");
599 // access X_OK | W_OK for execute_only_file
600 if (access(execute_only_file, X_OK | W_OK) != 0)
602 if (errno != EACCES)
604 test_report_failure_strerror();
605 test_report_failure("Different error expected");
606 return -1;
609 else
611 test_report_failure("This operation should fail");
612 return -1;
614 test_report_success("Correct error reported");
615 test_report_success("SUCCESS");
618 * TEST END
621 return 0;
624 int test_combined_file_access_modes()
626 next_test();
628 /* Manual action required for test. See top of file */
630 report_progress("Start test_combined_file_access_modes");
634 * TEST
637 next_test_step();
639 test_report_description("access R_OK | W_OK for all_access_file");
641 // access R_OK | W_OK for all_access_file
642 if (access(all_access_file, R_OK | W_OK) != 0)
644 test_report_failure_strerror();
645 test_report_failure("Failed accessing file");
646 return -1;
648 test_report_success("File accessed");
649 test_report_success("SUCCESS");
652 * TEST END
656 * TEST
659 next_test_step();
661 test_report_description("access R_OK | X_OK for all_access_file");
663 // access R_OK | X_OK for all_access_file
664 if (access(all_access_file, R_OK | X_OK) != 0)
666 test_report_failure_strerror();
667 test_report_failure("Failed accessing file");
668 return -1;
670 test_report_success("File accessed");
671 test_report_success("SUCCESS");
674 * TEST END
678 * TEST
681 next_test_step();
683 test_report_description("access W_OK | X_OK for all_access_file");
685 // access W_OK | X_OK for all_access_file
686 if (access(all_access_file, W_OK | X_OK) != 0)
688 test_report_failure_strerror();
689 test_report_failure("Failed accessing file");
690 return -1;
692 test_report_success("File accessed");
693 test_report_success("SUCCESS");
696 * TEST END
700 * TEST
703 next_test_step();
705 test_report_description("access R_OK | W_OK | X_OK for all_access_file");
707 // access R_OK | W_OK | X_OK for all_access_file
708 if (access(all_access_file, R_OK | W_OK | X_OK) != 0)
710 test_report_failure_strerror();
711 test_report_failure("Failed accessing file");
712 return -1;
714 test_report_success("File accessed");
715 test_report_success("SUCCESS");
718 * TEST END
722 * TEST
725 next_test_step();
727 test_report_description("access R_OK | W_OK | X_OK | F_OK for all_access_file");
729 // access R_OK | W_OK | X_OK | F_OK for all_access_file
730 if (access(all_access_file, R_OK | W_OK | X_OK | F_OK) != 0)
732 test_report_failure_strerror();
733 test_report_failure("Failed accessing file");
734 return -1;
736 test_report_success("File accessed");
737 test_report_success("SUCCESS");
740 * TEST END
743 return 0;
745 int main()
747 report_progress("Starting tests");
749 reset_global_test_counter();
751 // access test
752 if (test_access_wrapper(F_OK) != 0)
753 global_failure_indicator = 1;
755 if (test_access_wrapper(R_OK) != 0)
756 global_failure_indicator = 1;
758 if (test_access_wrapper(W_OK) != 0)
759 global_failure_indicator = 1;
761 if (test_access_wrapper(X_OK) != 0)
762 global_failure_indicator = 1;
764 if (test_access_wrapper(R_OK | W_OK | X_OK) != 0)
765 global_failure_indicator = 1;
767 if (test_single_file_access_modes() != 0)
768 global_failure_indicator = 1;
770 if (test_combined_file_access_modes() != 0)
771 global_failure_indicator = 1;
773 if (global_failure_indicator == 1)
774 report_failure("One of the tests FAILED");
775 else
776 report_progress("All tests SUCCEEDED");
778 report_progress("Tests finished");