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