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