1*2c2f96dcSApple OSS Distributions #include <darwintest.h>
2*2c2f96dcSApple OSS Distributions
3*2c2f96dcSApple OSS Distributions #include "xnu_quick_test_helpers.h"
4*2c2f96dcSApple OSS Distributions
5*2c2f96dcSApple OSS Distributions #include <fcntl.h>
6*2c2f96dcSApple OSS Distributions #include <unistd.h>
7*2c2f96dcSApple OSS Distributions
8*2c2f96dcSApple OSS Distributions void
create_target_directory(const char * the_targetp)9*2c2f96dcSApple OSS Distributions create_target_directory( const char * the_targetp )
10*2c2f96dcSApple OSS Distributions {
11*2c2f96dcSApple OSS Distributions int err;
12*2c2f96dcSApple OSS Distributions const char * my_targetp;
13*2c2f96dcSApple OSS Distributions
14*2c2f96dcSApple OSS Distributions my_targetp = getenv("TMPDIR");
15*2c2f96dcSApple OSS Distributions if (my_targetp == NULL) {
16*2c2f96dcSApple OSS Distributions my_targetp = "/tmp";
17*2c2f96dcSApple OSS Distributions }
18*2c2f96dcSApple OSS Distributions
19*2c2f96dcSApple OSS Distributions T_ASSERT_LT( strlen( the_targetp ), (unsigned long)(PATH_MAX - 1),
20*2c2f96dcSApple OSS Distributions "check target path too long - \"%s\"", the_targetp );
21*2c2f96dcSApple OSS Distributions
22*2c2f96dcSApple OSS Distributions for (;;) {
23*2c2f96dcSApple OSS Distributions int my_rand;
24*2c2f96dcSApple OSS Distributions char my_name[64];
25*2c2f96dcSApple OSS Distributions
26*2c2f96dcSApple OSS Distributions my_rand = rand();
27*2c2f96dcSApple OSS Distributions sprintf( &my_name[0], "xnu_quick_test-%d", my_rand );
28*2c2f96dcSApple OSS Distributions T_ASSERT_LT( strlen( &my_name[0] ) + strlen( the_targetp ) + 2, (unsigned long)PATH_MAX,
29*2c2f96dcSApple OSS Distributions "check target path plus our test directory name is too long: "
30*2c2f96dcSApple OSS Distributions "target path - \"%s\" test directory name - \"%s\"",
31*2c2f96dcSApple OSS Distributions the_targetp, &my_name[0] );
32*2c2f96dcSApple OSS Distributions
33*2c2f96dcSApple OSS Distributions /* append generated directory name onto our path */
34*2c2f96dcSApple OSS Distributions g_target_path[0] = 0x00;
35*2c2f96dcSApple OSS Distributions strcat( &g_target_path[0], the_targetp );
36*2c2f96dcSApple OSS Distributions if (g_target_path[(strlen(the_targetp) - 1)] != '/') {
37*2c2f96dcSApple OSS Distributions strcat( &g_target_path[0], "/" );
38*2c2f96dcSApple OSS Distributions }
39*2c2f96dcSApple OSS Distributions strcat( &g_target_path[0], &my_name[0] );
40*2c2f96dcSApple OSS Distributions
41*2c2f96dcSApple OSS Distributions /* try to create the test directory */
42*2c2f96dcSApple OSS Distributions err = mkdir( &g_target_path[0], (S_IRWXU | S_IRWXG | S_IROTH));
43*2c2f96dcSApple OSS Distributions if (err == 0) {
44*2c2f96dcSApple OSS Distributions break;
45*2c2f96dcSApple OSS Distributions }
46*2c2f96dcSApple OSS Distributions err = errno;
47*2c2f96dcSApple OSS Distributions if (EEXIST != err) {
48*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL( "test directory creation failed - \"%s\" \n"
49*2c2f96dcSApple OSS Distributions "mkdir call failed with error %d - \"%s\"",
50*2c2f96dcSApple OSS Distributions &g_target_path[0], errno, strerror( err));
51*2c2f96dcSApple OSS Distributions }
52*2c2f96dcSApple OSS Distributions }
53*2c2f96dcSApple OSS Distributions } /* create_target_directory */
54*2c2f96dcSApple OSS Distributions
55*2c2f96dcSApple OSS Distributions /*
56*2c2f96dcSApple OSS Distributions * create_random_name - creates a file with a random / unique name in the given directory.
57*2c2f96dcSApple OSS Distributions * when do_open is true we create a file else we generaate a name that does not exist in the
58*2c2f96dcSApple OSS Distributions * given directory (we do not create anything when do_open is 0).
59*2c2f96dcSApple OSS Distributions * WARNING - caller provides enough space in path buffer for longest possible name.
60*2c2f96dcSApple OSS Distributions * WARNING - assumes caller has appended a trailing '/' on the path passed to us.
61*2c2f96dcSApple OSS Distributions * RAND_MAX is currently 2147483647 (ten characters plus one for a slash)
62*2c2f96dcSApple OSS Distributions */
63*2c2f96dcSApple OSS Distributions int
create_random_name(char * the_pathp,int do_open)64*2c2f96dcSApple OSS Distributions create_random_name( char *the_pathp, int do_open )
65*2c2f96dcSApple OSS Distributions {
66*2c2f96dcSApple OSS Distributions int i, my_err;
67*2c2f96dcSApple OSS Distributions int my_fd = -1;
68*2c2f96dcSApple OSS Distributions
69*2c2f96dcSApple OSS Distributions for (i = 0; i < 1; i++) {
70*2c2f96dcSApple OSS Distributions int my_rand;
71*2c2f96dcSApple OSS Distributions char *myp;
72*2c2f96dcSApple OSS Distributions char my_name[32];
73*2c2f96dcSApple OSS Distributions
74*2c2f96dcSApple OSS Distributions my_rand = rand();
75*2c2f96dcSApple OSS Distributions sprintf( &my_name[0], "%d", my_rand );
76*2c2f96dcSApple OSS Distributions T_ASSERT_LT_ULONG((strlen( &my_name[0] ) + strlen( the_pathp ) + 2), (unsigned long)PATH_MAX,
77*2c2f96dcSApple OSS Distributions "check if path to test file is less than PATH_MAX");
78*2c2f96dcSApple OSS Distributions
79*2c2f96dcSApple OSS Distributions // append generated file name onto our path
80*2c2f96dcSApple OSS Distributions myp = strrchr( the_pathp, '/' );
81*2c2f96dcSApple OSS Distributions *(myp + 1) = 0x00;
82*2c2f96dcSApple OSS Distributions strcat( the_pathp, &my_name[0] );
83*2c2f96dcSApple OSS Distributions if (do_open) {
84*2c2f96dcSApple OSS Distributions /* create a file with this name */
85*2c2f96dcSApple OSS Distributions my_fd = open( the_pathp, (O_RDWR | O_CREAT | O_EXCL),
86*2c2f96dcSApple OSS Distributions (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
87*2c2f96dcSApple OSS Distributions T_EXPECT_TRUE((my_fd != -1 || errno == EEXIST), "open file with name %s", the_pathp);
88*2c2f96dcSApple OSS Distributions
89*2c2f96dcSApple OSS Distributions if (errno == EEXIST) {
90*2c2f96dcSApple OSS Distributions continue;
91*2c2f96dcSApple OSS Distributions }
92*2c2f96dcSApple OSS Distributions } else {
93*2c2f96dcSApple OSS Distributions /* make sure the name is unique */
94*2c2f96dcSApple OSS Distributions struct stat my_sb;
95*2c2f96dcSApple OSS Distributions my_err = stat( the_pathp, &my_sb );
96*2c2f96dcSApple OSS Distributions T_EXPECT_TRUE((my_err == 0 || errno == ENOENT), "make sure the name is unique");
97*2c2f96dcSApple OSS Distributions
98*2c2f96dcSApple OSS Distributions if (errno == ENOENT) {
99*2c2f96dcSApple OSS Distributions break;
100*2c2f96dcSApple OSS Distributions }
101*2c2f96dcSApple OSS Distributions /* name already exists, try another */
102*2c2f96dcSApple OSS Distributions i--;
103*2c2f96dcSApple OSS Distributions continue;
104*2c2f96dcSApple OSS Distributions }
105*2c2f96dcSApple OSS Distributions }
106*2c2f96dcSApple OSS Distributions
107*2c2f96dcSApple OSS Distributions if (my_fd != -1) {
108*2c2f96dcSApple OSS Distributions close( my_fd );
109*2c2f96dcSApple OSS Distributions }
110*2c2f96dcSApple OSS Distributions
111*2c2f96dcSApple OSS Distributions if (do_open && my_fd == -1) {
112*2c2f96dcSApple OSS Distributions return 1;
113*2c2f96dcSApple OSS Distributions }
114*2c2f96dcSApple OSS Distributions
115*2c2f96dcSApple OSS Distributions return 0;
116*2c2f96dcSApple OSS Distributions } /* create_random_name */
117*2c2f96dcSApple OSS Distributions
118*2c2f96dcSApple OSS Distributions void
remove_target_directory()119*2c2f96dcSApple OSS Distributions remove_target_directory()
120*2c2f96dcSApple OSS Distributions {
121*2c2f96dcSApple OSS Distributions rmdir(&g_target_path[0]);
122*2c2f96dcSApple OSS Distributions }
123