Branch data Line data Source code
1 : : #include "sute.h"
2 : : #include <sys/stat.h>
3 : : #include <unistd.h>
4 : :
5 : : /**
6 : : * @brief Unit tests for create_tmpdir()
7 : : *
8 : : * @details
9 : : * - Verifies fallback to P_tmpdir or /tmp when TMPDIR is unset
10 : : * - Verifies TMPDIR is honored when it is set
11 : : */
12 : 1 : Return test0035(void)
13 : : {
14 : 1 : INITTEST;
15 : :
16 : 1 : create(char,fallback_tmpdir);
17 : 1 : create(char,nested_tmpdir);
18 : 1 : char *saved_tmpdir = NULL;
19 : 1 : const char *original_tmpdir = getenv("TMPDIR");
20 : 1 : const char *expected_root = "/tmp";
21 : 1 : const char *fallback_path = NULL;
22 : 1 : const char *nested_path = NULL;
23 : 1 : int remove_nested_status = 0;
24 : 1 : int remove_fallback_status = 0;
25 : 1 : int restore_tmpdir_status = 0;
26 : :
27 : : #ifdef P_tmpdir
28 [ + - ]: 1 : if(P_tmpdir[0] != '\0')
29 : : {
30 : 1 : expected_root = P_tmpdir;
31 : : }
32 : : #endif
33 : :
34 [ + - ]: 1 : if(original_tmpdir != NULL)
35 : : {
36 : 1 : saved_tmpdir = strdup(original_tmpdir);
37 : 1 : ASSERT(saved_tmpdir != NULL);
38 : : }
39 : :
40 [ + - ]: 1 : if(SUCCESS == status)
41 : : {
42 : 1 : ASSERT(unsetenv("TMPDIR") == 0);
43 : : }
44 : :
45 : 1 : ASSERT(SUCCESS == create_tmpdir(fallback_tmpdir));
46 : 1 : fallback_path = getcstring(fallback_tmpdir);
47 : :
48 [ + - ]: 1 : if(SUCCESS == status)
49 : : {
50 : 1 : const size_t expected_root_length = strlen(expected_root);
51 : 1 : const char nested_prefix[] = TESTITALL_APP_NAME ".";
52 : :
53 : 1 : ASSERT(0 == strncmp(fallback_path,expected_root,expected_root_length));
54 : :
55 [ - + ]: 1 : if('/' == expected_root[expected_root_length - 1U])
56 : : {
57 : 0 : ASSERT(0 == strncmp(fallback_path + expected_root_length,nested_prefix,strlen(nested_prefix)));
58 : : } else {
59 : 1 : ASSERT('/' == fallback_path[expected_root_length]);
60 : 1 : ASSERT(0 == strncmp(fallback_path + expected_root_length + 1U,nested_prefix,strlen(nested_prefix)));
61 : : }
62 : : }
63 : :
64 [ + - ]: 1 : if(SUCCESS == status)
65 : : {
66 : : struct stat directory_stat;
67 : 1 : ASSERT(0 == stat(fallback_path,&directory_stat));
68 : 1 : ASSERT(S_ISDIR(directory_stat.st_mode));
69 : : }
70 : :
71 [ + - ]: 1 : if(SUCCESS == status)
72 : : {
73 : 1 : ASSERT(SUCCESS == set_environment_variable("TMPDIR",fallback_path));
74 : : }
75 : :
76 : 1 : ASSERT(SUCCESS == create_tmpdir(nested_tmpdir));
77 : 1 : nested_path = getcstring(nested_tmpdir);
78 : :
79 [ + - ]: 1 : if(SUCCESS == status)
80 : : {
81 : 1 : const size_t prefix_length = strlen(fallback_path);
82 : 1 : const char nested_prefix[] = TESTITALL_APP_NAME ".";
83 : 1 : ASSERT(0 == strncmp(nested_path,fallback_path,prefix_length));
84 : 1 : ASSERT('/' == nested_path[prefix_length]);
85 : 1 : ASSERT(0 == strncmp(nested_path + prefix_length + 1U,nested_prefix,strlen(nested_prefix)));
86 : : }
87 : :
88 [ + - ]: 1 : if(SUCCESS == status)
89 : : {
90 : : struct stat directory_stat;
91 : 1 : ASSERT(0 == stat(nested_path,&directory_stat));
92 : 1 : ASSERT(S_ISDIR(directory_stat.st_mode));
93 : : }
94 : :
95 [ + - + - ]: 1 : if(nested_path != NULL && nested_path[0] != '\0')
96 : : {
97 : 1 : remove_nested_status = rmdir(nested_path);
98 : : }
99 : :
100 [ + - + - ]: 1 : if(fallback_path != NULL && fallback_path[0] != '\0')
101 : : {
102 : 1 : remove_fallback_status = rmdir(fallback_path);
103 : : }
104 : :
105 [ + - ]: 1 : if(saved_tmpdir != NULL)
106 : : {
107 : 1 : restore_tmpdir_status = setenv("TMPDIR",saved_tmpdir,1);
108 : : } else {
109 : 0 : restore_tmpdir_status = unsetenv("TMPDIR");
110 : : }
111 : :
112 [ + - ]: 1 : if(SUCCESS == status)
113 : : {
114 : 1 : ASSERT(remove_nested_status == 0);
115 : 1 : ASSERT(remove_fallback_status == 0);
116 : 1 : ASSERT(restore_tmpdir_status == 0);
117 : : }
118 : :
119 : 1 : free(saved_tmpdir);
120 : :
121 [ - + - + ]: 1 : call(del(nested_tmpdir));
122 [ - + - + ]: 1 : call(del(fallback_tmpdir));
123 : :
124 : 1 : RETURN_STATUS;
125 : : }
|