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