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