xref: /xnu-8020.101.4/tests/xnu_quick_test_helpers.c (revision e7776783b89a353188416a9a346c6cdb4928faad)
1 #include <darwintest.h>
2 
3 #include "xnu_quick_test_helpers.h"
4 
5 #include <fcntl.h>
6 #include <unistd.h>
7 
8 void
create_target_directory(const char * the_targetp)9 create_target_directory( const char * the_targetp )
10 {
11 	int             err;
12 	const char *    my_targetp;
13 
14 	my_targetp = getenv("TMPDIR");
15 	if (my_targetp == NULL) {
16 		my_targetp = "/tmp";
17 	}
18 
19 	T_ASSERT_LT( strlen( the_targetp ), (unsigned long)(PATH_MAX - 1),
20 	    "check target path too long - \"%s\"", the_targetp );
21 
22 	for (;;) {
23 		int         my_rand;
24 		char        my_name[64];
25 
26 		my_rand = rand();
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] );
32 
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], "/" );
38 		}
39 		strcat( &g_target_path[0], &my_name[0] );
40 
41 		/* try to create the test directory */
42 		err = mkdir( &g_target_path[0], (S_IRWXU | S_IRWXG | S_IROTH));
43 		if (err == 0) {
44 			break;
45 		}
46 		err = errno;
47 		if (EEXIST != err) {
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));
51 		}
52 	}
53 } /* create_target_directory */
54 
55 /*
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)
62  */
63 int
create_random_name(char * the_pathp,int do_open)64 create_random_name( char *the_pathp, int do_open )
65 {
66 	int     i, my_err;
67 	int     my_fd = -1;
68 
69 	for (i = 0; i < 1; i++) {
70 		int         my_rand;
71 		char        *myp;
72 		char        my_name[32];
73 
74 		my_rand = rand();
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");
78 
79 		// append generated file name onto our path
80 		myp = strrchr( the_pathp, '/' );
81 		*(myp + 1) = 0x00;
82 		strcat( the_pathp, &my_name[0] );
83 		if (do_open) {
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);
88 
89 			if (errno == EEXIST) {
90 				continue;
91 			}
92 		} else {
93 			/* make sure the name is unique */
94 			struct stat     my_sb;
95 			my_err = stat( the_pathp, &my_sb );
96 			T_EXPECT_TRUE((my_err == 0 || errno == ENOENT), "make sure the name is unique");
97 
98 			if (errno == ENOENT) {
99 				break;
100 			}
101 			/* name already exists, try another */
102 			i--;
103 			continue;
104 		}
105 	}
106 
107 	if (my_fd != -1) {
108 		close( my_fd );
109 	}
110 
111 	if (do_open && my_fd == -1) {
112 		return 1;
113 	}
114 
115 	return 0;
116 } /* create_random_name */
117 
118 void
remove_target_directory()119 remove_target_directory()
120 {
121 	rmdir(&g_target_path[0]);
122 }
123