]>
git.saurik.com Git - apple/xnu.git/blob - tests/xnu_quick_test_helpers.c
1 #include <darwintest.h>
3 #include "xnu_quick_test_helpers.h"
9 create_target_directory( const char * the_targetp
)
12 const char * my_targetp
;
14 my_targetp
= getenv("TMPDIR");
15 if (my_targetp
== NULL
) {
19 T_ASSERT_LT( strlen( the_targetp
), (unsigned long)(PATH_MAX
- 1),
20 "check target path too long - \"%s\"", the_targetp
);
27 sprintf( &my_name
[0], "xnu_quick_test-%d", my_rand
);
28 T_ASSERT_LT( strlen( &my_name
[0] ) + strlen( the_targetp
) + 2, (unsigned long)PATH_MAX
,
29 "check target path plus our test directory name is too long: "
30 "target path - \"%s\" test directory name - \"%s\"",
31 the_targetp
, &my_name
[0] );
33 /* append generated directory name onto our path */
34 g_target_path
[0] = 0x00;
35 strcat( &g_target_path
[0], the_targetp
);
36 if (g_target_path
[(strlen(the_targetp
) - 1)] != '/') {
37 strcat( &g_target_path
[0], "/" );
39 strcat( &g_target_path
[0], &my_name
[0] );
41 /* try to create the test directory */
42 err
= mkdir( &g_target_path
[0], (S_IRWXU
| S_IRWXG
| S_IROTH
));
48 T_ASSERT_FAIL( "test directory creation failed - \"%s\" \n"
49 "mkdir call failed with error %d - \"%s\"",
50 &g_target_path
[0], errno
, strerror( err
));
53 } /* create_target_directory */
56 * create_random_name - creates a file with a random / unique name in the given directory.
57 * when do_open is true we create a file else we generaate a name that does not exist in the
58 * given directory (we do not create anything when do_open is 0).
59 * WARNING - caller provides enough space in path buffer for longest possible name.
60 * WARNING - assumes caller has appended a trailing '/' on the path passed to us.
61 * RAND_MAX is currently 2147483647 (ten characters plus one for a slash)
64 create_random_name( char *the_pathp
, int do_open
)
69 for (i
= 0; i
< 1; i
++) {
75 sprintf( &my_name
[0], "%d", my_rand
);
76 T_ASSERT_LT_ULONG((strlen( &my_name
[0] ) + strlen( the_pathp
) + 2), (unsigned long)PATH_MAX
,
77 "check if path to test file is less than PATH_MAX");
79 // append generated file name onto our path
80 myp
= strrchr( the_pathp
, '/' );
82 strcat( the_pathp
, &my_name
[0] );
84 /* create a file with this name */
85 my_fd
= open( the_pathp
, (O_RDWR
| O_CREAT
| O_EXCL
),
86 (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
));
87 T_EXPECT_TRUE((my_fd
!= -1 || errno
== EEXIST
), "open file with name %s", the_pathp
);
89 if (errno
== EEXIST
) {
93 /* make sure the name is unique */
95 my_err
= stat( the_pathp
, &my_sb
);
96 T_EXPECT_TRUE((my_err
== 0 || errno
== ENOENT
), "make sure the name is unique");
98 if (errno
== ENOENT
) {
101 /* name already exists, try another */
111 if (do_open
&& my_fd
== -1) {
116 } /* create_random_name */
119 remove_target_directory()
121 rmdir(&g_target_path
[0]);