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