]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/xnu_quick_test_helpers.c
1 #include <darwintest.h>
3 #include "xnu_quick_test_helpers.h"
8 void create_target_directory( const char * the_targetp
)
11 const char * my_targetp
;
13 my_targetp
= getenv("TMPDIR");
14 if ( my_targetp
== NULL
)
17 T_ASSERT_LT( strlen( the_targetp
), (unsigned long)( PATH_MAX
- 1 ),
18 "check target path too long - \"%s\"", the_targetp
);
25 sprintf( &my_name
[0], "xnu_quick_test-%d", my_rand
);
26 T_ASSERT_LT( strlen( &my_name
[0] ) + strlen( the_targetp
) + 2, (unsigned long)PATH_MAX
,
27 "check target path plus our test directory name is too long: "
28 "target path - \"%s\" test directory name - \"%s\"",
29 the_targetp
, &my_name
[0] );
31 /* append generated directory name onto our path */
32 g_target_path
[0] = 0x00;
33 strcat( &g_target_path
[0], the_targetp
);
34 if ( g_target_path
[ (strlen(the_targetp
) - 1) ] != '/' ) {
35 strcat( &g_target_path
[0], "/" );
37 strcat( &g_target_path
[0], &my_name
[0] );
39 /* try to create the test directory */
40 err
= mkdir( &g_target_path
[0], (S_IRWXU
| S_IRWXG
| S_IROTH
) );
45 if ( EEXIST
!= err
) {
46 T_ASSERT_FAIL( "test directory creation failed - \"%s\" \n"
47 "mkdir call failed with error %d - \"%s\"",
48 &g_target_path
[0], errno
, strerror( err
) );
52 } /* create_target_directory */
55 * create_random_name - creates a file with a random / unique name in the given directory.
56 * when do_open is true we create a file else we generaate a name that does not exist in the
57 * given directory (we do not create anything when do_open is 0).
58 * WARNING - caller provides enough space in path buffer for longest possible name.
59 * WARNING - assumes caller has appended a trailing '/' on the path passed to us.
60 * RAND_MAX is currently 2147483647 (ten characters plus one for a slash)
62 int create_random_name( char *the_pathp
, int do_open
) {
66 for ( i
= 0; i
< 1; i
++ ) {
72 sprintf( &my_name
[0], "%d", my_rand
);
73 T_ASSERT_LT_ULONG((strlen( &my_name
[0] ) + strlen( the_pathp
) + 2), (unsigned long)PATH_MAX
,
74 "check if path to test file is less than PATH_MAX");
76 // append generated file name onto our path
77 myp
= strrchr( the_pathp
, '/' );
79 strcat( the_pathp
, &my_name
[0] );
81 /* create a file with this name */
82 my_fd
= open( the_pathp
, (O_RDWR
| O_CREAT
| O_EXCL
),
83 (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) );
84 T_EXPECT_TRUE((my_fd
!= -1 || errno
== EEXIST
), "open file with name %s", the_pathp
);
90 /* make sure the name is unique */
92 my_err
= stat( the_pathp
, &my_sb
);
93 T_EXPECT_TRUE((my_err
== 0 || errno
== ENOENT
), "make sure the name is unique");
95 if(errno
== ENOENT
) break;
96 /* name already exists, try another */
105 if(do_open
&& my_fd
== -1)
109 } /* create_random_name */
111 void remove_target_directory() {
112 rmdir(&g_target_path
[0]);