1 // Copyright (C) 2010-2019 Joel Rosdahl
2 //
3 // This program is free software; you can redistribute it and/or modify it
4 // under the terms of the GNU General Public License as published by the Free
5 // Software Foundation; either version 3 of the License, or (at your option)
6 // any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but WITHOUT
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 // more details.
12 //
13 // You should have received a copy of the GNU General Public License along with
14 // this program; if not, write to the Free Software Foundation, Inc., 51
15 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 // This file contains tests for functions in hashutil.c.
18
19 #include "../src/ccache.h"
20 #include "../src/hashutil.h"
21 #include "framework.h"
22 #include "util.h"
23
24 TEST_SUITE(hashutil)
25
TEST(hash_command_output_simple)26 TEST(hash_command_output_simple)
27 {
28 struct hash *h1 = hash_init();
29 struct hash *h2 = hash_init();
30
31 CHECK(hash_command_output(h1, "echo", "not used"));
32 CHECK(hash_command_output(h2, "echo", "not used"));
33 CHECK(hash_equal(h1, h2));
34
35 hash_free(h2);
36 hash_free(h1);
37 }
38
TEST(hash_command_output_space_removal)39 TEST(hash_command_output_space_removal)
40 {
41 struct hash *h1 = hash_init();
42 struct hash *h2 = hash_init();
43
44 CHECK(hash_command_output(h1, "echo", "not used"));
45 CHECK(hash_command_output(h2, " echo ", "not used"));
46 CHECK(hash_equal(h1, h2));
47
48 hash_free(h2);
49 hash_free(h1);
50 }
51
TEST(hash_command_output_hash_inequality)52 TEST(hash_command_output_hash_inequality)
53 {
54 struct hash *h1 = hash_init();
55 struct hash *h2 = hash_init();
56
57 CHECK(hash_command_output(h1, "echo foo", "not used"));
58 CHECK(hash_command_output(h2, "echo bar", "not used"));
59 CHECK(!hash_equal(h1, h2));
60
61 hash_free(h2);
62 hash_free(h1);
63 }
64
TEST(hash_command_output_compiler_substitution)65 TEST(hash_command_output_compiler_substitution)
66 {
67 struct hash *h1 = hash_init();
68 struct hash *h2 = hash_init();
69
70 CHECK(hash_command_output(h1, "echo foo", "not used"));
71 CHECK(hash_command_output(h2, "%compiler% foo", "echo"));
72 CHECK(hash_equal(h1, h2));
73
74 hash_free(h2);
75 hash_free(h1);
76 }
77
TEST(hash_command_output_stdout_versus_stderr)78 TEST(hash_command_output_stdout_versus_stderr)
79 {
80 struct hash *h1 = hash_init();
81 struct hash *h2 = hash_init();
82
83 #ifndef _WIN32
84 create_file("stderr.sh", "#!/bin/sh\necho foo >&2\n");
85 chmod("stderr.sh", 0555);
86 CHECK(hash_command_output(h1, "echo foo", "not used"));
87 CHECK(hash_command_output(h2, "./stderr.sh", "not used"));
88 #else
89 create_file("stderr.bat", "@echo off\r\necho foo>&2\r\n");
90 CHECK(hash_command_output(h1, "echo foo", "not used"));
91 CHECK(hash_command_output(h2, "stderr.bat", "not used"));
92 #endif
93 CHECK(hash_equal(h1, h2));
94
95 hash_free(h2);
96 hash_free(h1);
97 }
98
TEST(hash_multicommand_output)99 TEST(hash_multicommand_output)
100 {
101 struct hash *h1 = hash_init();
102 struct hash *h2 = hash_init();
103
104 #ifndef _WIN32
105 create_file("foo.sh", "#!/bin/sh\necho foo\necho bar\n");
106 chmod("foo.sh", 0555);
107 CHECK(hash_multicommand_output(h2, "echo foo; echo bar", "not used"));
108 CHECK(hash_multicommand_output(h1, "./foo.sh", "not used"));
109 #else
110 create_file("foo.bat", "@echo off\r\necho foo\r\necho bar\r\n");
111 CHECK(hash_multicommand_output(h2, "echo foo; echo bar", "not used"));
112 CHECK(hash_multicommand_output(h1, "foo.bat", "not used"));
113 #endif
114 CHECK(hash_equal(h1, h2));
115
116 hash_free(h2);
117 hash_free(h1);
118 }
119
TEST(hash_multicommand_output_error_handling)120 TEST(hash_multicommand_output_error_handling)
121 {
122 struct hash *h1 = hash_init();
123 struct hash *h2 = hash_init();
124
125 CHECK(!hash_multicommand_output(h2, "false; true", "not used"));
126
127 hash_free(h2);
128 hash_free(h1);
129 }
130
TEST(check_for_temporal_macros)131 TEST(check_for_temporal_macros)
132 {
133 const char time_start[] =
134 "__TIME__\n"
135 "int a;\n";
136 const char time_middle[] =
137 "#define a __TIME__\n"
138 "int a;\n";
139 const char time_end[] =
140 "#define a __TIME__";
141
142 const char date_start[] =
143 "__DATE__\n"
144 "int ab;\n";
145 const char date_middle[] =
146 "#define ab __DATE__\n"
147 "int ab;\n";
148 const char date_end[] =
149 "#define ab __DATE__";
150
151 const char no_temporal[] =
152 "#define ab a__DATE__\n"
153 "#define ab __DATE__a\n"
154 "#define ab A__DATE__\n"
155 "#define ab __DATE__A\n"
156 "#define ab 0__DATE__\n"
157 "#define ab __DATE__0\n"
158 "#define ab _ _DATE__\n"
159 "#define ab _ _DATE__\n"
160 "#define ab __ DATE__\n"
161 "#define ab __D ATE__\n"
162 "#define ab __DA TE__\n"
163 "#define ab __DAT E__\n"
164 "#define ab __DATE __\n"
165 "#define ab __DATE_ _\n"
166 "#define ab _ _TIME__\n"
167 "#define ab __ TIME__\n"
168 "#define ab __T IME__\n"
169 "#define ab __TI ME__\n"
170 "#define ab __TIM E__\n"
171 "#define ab __TIME __\n"
172 "#define ab __TIME_ _\n";
173
174 CHECK(check_for_temporal_macros(time_start + 0, sizeof(time_start) - 0));
175 CHECK(!check_for_temporal_macros(time_start + 1, sizeof(time_start) - 1));
176
177 CHECK(check_for_temporal_macros(time_middle + 0, sizeof(time_middle) - 0));
178 CHECK(check_for_temporal_macros(time_middle + 1, sizeof(time_middle) - 1));
179 CHECK(check_for_temporal_macros(time_middle + 2, sizeof(time_middle) - 2));
180 CHECK(check_for_temporal_macros(time_middle + 3, sizeof(time_middle) - 3));
181 CHECK(check_for_temporal_macros(time_middle + 4, sizeof(time_middle) - 4));
182 CHECK(check_for_temporal_macros(time_middle + 5, sizeof(time_middle) - 5));
183 CHECK(check_for_temporal_macros(time_middle + 6, sizeof(time_middle) - 6));
184 CHECK(check_for_temporal_macros(time_middle + 7, sizeof(time_middle) - 7));
185
186 CHECK(check_for_temporal_macros(time_end + 0, sizeof(time_end) - 0));
187 CHECK(check_for_temporal_macros(time_end + sizeof(time_end) - 9, 9));
188 CHECK(!check_for_temporal_macros(time_end + sizeof(time_end) - 8, 8));
189
190 CHECK(check_for_temporal_macros(date_start + 0, sizeof(date_start) - 0));
191 CHECK(!check_for_temporal_macros(date_start + 1, sizeof(date_start) - 1));
192
193 CHECK(check_for_temporal_macros(date_middle + 0, sizeof(date_middle) - 0));
194 CHECK(check_for_temporal_macros(date_middle + 1, sizeof(date_middle) - 1));
195 CHECK(check_for_temporal_macros(date_middle + 2, sizeof(date_middle) - 2));
196 CHECK(check_for_temporal_macros(date_middle + 3, sizeof(date_middle) - 3));
197 CHECK(check_for_temporal_macros(date_middle + 4, sizeof(date_middle) - 4));
198 CHECK(check_for_temporal_macros(date_middle + 5, sizeof(date_middle) - 5));
199 CHECK(check_for_temporal_macros(date_middle + 6, sizeof(date_middle) - 6));
200 CHECK(check_for_temporal_macros(date_middle + 7, sizeof(date_middle) - 7));
201
202 CHECK(check_for_temporal_macros(date_end + 0, sizeof(date_end) - 0));
203 CHECK(check_for_temporal_macros(date_end + sizeof(date_end) - 9, 9));
204 CHECK(!check_for_temporal_macros(date_end + sizeof(date_end) - 8, 8));
205
206 CHECK(!check_for_temporal_macros(no_temporal + 0, sizeof(no_temporal) - 0));
207 CHECK(!check_for_temporal_macros(no_temporal + 1, sizeof(no_temporal) - 1));
208 CHECK(!check_for_temporal_macros(no_temporal + 2, sizeof(no_temporal) - 2));
209 CHECK(!check_for_temporal_macros(no_temporal + 3, sizeof(no_temporal) - 3));
210 CHECK(!check_for_temporal_macros(no_temporal + 4, sizeof(no_temporal) - 4));
211 CHECK(!check_for_temporal_macros(no_temporal + 5, sizeof(no_temporal) - 5));
212 CHECK(!check_for_temporal_macros(no_temporal + 6, sizeof(no_temporal) - 6));
213 CHECK(!check_for_temporal_macros(no_temporal + 7, sizeof(no_temporal) - 7));
214 }
215
216 TEST_SUITE_END
217