Branch data Line data Source code
1 : : #include "test_libtestitall_all.h"
2 : :
3 : : static const char expected_construct_path_null_filename_stderr_pattern[] =
4 : : "\\Aconstruct_path: filename must not be NULL\\Z";
5 : :
6 : : static const char expected_construct_path_missing_tmpdir_stderr_pattern[] =
7 : : "\\Aconstruct_path: TMPDIR is not set for \"tmpdir_missing\\.txt\"\\Z";
8 : :
9 : : /**
10 : : * @brief Save the current TMPDIR value and clear the output pointer when unset
11 : : *
12 : : * @param[out] saved_tmpdir_out Heap-allocated TMPDIR copy or NULL when unset
13 : : * @return Return status code
14 : : */
15 : 1 : static Return save_tmpdir_value(char **saved_tmpdir_out)
16 : : {
17 : : /* Status returned by this function through provide()
18 : : Default value assumes successful completion */
19 : 1 : Return status = SUCCESS;
20 : 1 : const char *original_tmpdir = NULL;
21 : :
22 [ - + ]: 1 : if(saved_tmpdir_out == NULL)
23 : : {
24 : 0 : status = FAILURE;
25 : : }
26 : :
27 [ + - ]: 1 : if(SUCCESS == status)
28 : : {
29 : 1 : *saved_tmpdir_out = NULL;
30 : 1 : original_tmpdir = getenv("TMPDIR");
31 : :
32 : 1 : IF(original_tmpdir != NULL)
33 : : {
34 : 1 : *saved_tmpdir_out = strdup(original_tmpdir);
35 : :
36 [ - + ]: 1 : if(*saved_tmpdir_out == NULL)
37 : : {
38 : 0 : status = FAILURE;
39 : : }
40 : : }
41 : : }
42 : :
43 : 1 : deliver(status);
44 : : }
45 : :
46 : : /**
47 : : * @brief Restore TMPDIR to a previously saved value or unset it when NULL
48 : : *
49 : : * @param[in] saved_tmpdir Saved TMPDIR value or NULL when TMPDIR was unset
50 : : * @return Return status code
51 : : */
52 : 1 : static Return restore_tmpdir_value(const char *saved_tmpdir)
53 : : {
54 : 1 : int restore_tmpdir_status = 0;
55 : :
56 : 1 : IF(saved_tmpdir != NULL)
57 : : {
58 : 1 : restore_tmpdir_status = setenv("TMPDIR",saved_tmpdir,1);
59 : : } else {
60 : 0 : restore_tmpdir_status = unsetenv("TMPDIR");
61 : : }
62 : :
63 [ + - ]: 1 : return(restore_tmpdir_status == 0 ? SUCCESS : FAILURE);
64 : : }
65 : :
66 : : /**
67 : : * @brief Check construct_path() diagnostic output for invalid inputs
68 : : *
69 : : * @return Return status code
70 : : */
71 : 1 : Return test_libtestitall_0006(void)
72 : : {
73 : 1 : INITTEST;
74 : :
75 : 1 : m_create(char,path,MEMORY_STRING);
76 : 1 : char *saved_tmpdir = NULL;
77 : 1 : Return saved_tmpdir_status = FAILURE;
78 : 1 : Return restore_tmpdir_status = SUCCESS;
79 : 1 : bool tmpdir_saved = false;
80 : :
81 : 1 : call(m_del(STDERR));
82 : :
83 : 1 : ASSERT(FAILURE == construct_path(NULL,path));
84 : 1 : ASSERT(SUCCESS == assert_stderr_matches_pattern(
85 : : expected_construct_path_null_filename_stderr_pattern));
86 : :
87 : 1 : saved_tmpdir_status = save_tmpdir_value(&saved_tmpdir);
88 : 1 : ASSERT(SUCCESS == saved_tmpdir_status);
89 : :
90 [ + - ]: 1 : if(SUCCESS == saved_tmpdir_status)
91 : : {
92 : 1 : tmpdir_saved = true;
93 : : }
94 : :
95 [ + - ]: 1 : if(SUCCESS == status)
96 : : {
97 : 1 : ASSERT(unsetenv("TMPDIR") == 0);
98 : : }
99 : :
100 : 1 : call(m_del(STDERR));
101 : :
102 [ + - ]: 1 : if(SUCCESS == status)
103 : : {
104 : 1 : ASSERT(FAILURE == construct_path("tmpdir_missing.txt",path));
105 : 1 : ASSERT(SUCCESS == assert_stderr_matches_pattern(
106 : : expected_construct_path_missing_tmpdir_stderr_pattern));
107 : : }
108 : :
109 [ + - ]: 1 : if(tmpdir_saved == true)
110 : : {
111 : 1 : restore_tmpdir_status = restore_tmpdir_value(saved_tmpdir);
112 : : }
113 : :
114 [ + - ]: 1 : if(SUCCESS == status)
115 : : {
116 : 1 : ASSERT(SUCCESS == restore_tmpdir_status);
117 : : }
118 : :
119 : 1 : free(saved_tmpdir);
120 : 1 : call(m_del(path));
121 : :
122 : 1 : RETURN_STATUS;
123 : : }
|