1 // Copyright (C) 2010-2018 Joel Rosdahl
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)
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
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
17 // This file contains tests for functions in hashutil.c.
19 #include "../src/ccache.h"
20 #include "../src/hashutil.h"
21 #include "framework.h"
26 TEST(hash_command_output_simple)
31 CHECK(hash_command_output(&h1, "echo", "not used"));
32 CHECK(hash_command_output(&h2, "echo", "not used"));
33 CHECK(hash_equal(&h1, &h2));
36 TEST(hash_command_output_space_removal)
41 CHECK(hash_command_output(&h1, "echo", "not used"));
42 CHECK(hash_command_output(&h2, " echo ", "not used"));
43 CHECK(hash_equal(&h1, &h2));
46 TEST(hash_command_output_hash_inequality)
51 CHECK(hash_command_output(&h1, "echo foo", "not used"));
52 CHECK(hash_command_output(&h2, "echo bar", "not used"));
53 CHECK(!hash_equal(&h1, &h2));
56 TEST(hash_command_output_compiler_substitution)
61 CHECK(hash_command_output(&h1, "echo foo", "not used"));
62 CHECK(hash_command_output(&h2, "%compiler% foo", "echo"));
63 CHECK(hash_equal(&h1, &h2));
66 TEST(hash_command_output_stdout_versus_stderr)
72 create_file("stderr.sh", "#!/bin/sh\necho foo >&2\n");
73 chmod("stderr.sh", 0555);
74 CHECK(hash_command_output(&h1, "echo foo", "not used"));
75 CHECK(hash_command_output(&h2, "./stderr.sh", "not used"));
77 create_file("stderr.bat", "@echo off\r\necho foo>&2\r\n");
78 CHECK(hash_command_output(&h1, "echo foo", "not used"));
79 CHECK(hash_command_output(&h2, "stderr.bat", "not used"));
81 CHECK(hash_equal(&h1, &h2));
84 TEST(hash_multicommand_output)
90 create_file("foo.sh", "#!/bin/sh\necho foo\necho bar\n");
91 chmod("foo.sh", 0555);
92 CHECK(hash_multicommand_output(&h2, "echo foo; echo bar", "not used"));
93 CHECK(hash_multicommand_output(&h1, "./foo.sh", "not used"));
95 create_file("foo.bat", "@echo off\r\necho foo\r\necho bar\r\n");
96 CHECK(hash_multicommand_output(&h2, "echo foo; echo bar", "not used"));
97 CHECK(hash_multicommand_output(&h1, "foo.bat", "not used"));
99 CHECK(hash_equal(&h1, &h2));
102 TEST(hash_multicommand_output_error_handling)
104 struct mdfour h1, h2;
107 CHECK(!hash_multicommand_output(&h2, "false; true", "not used"));
110 TEST(check_for_temporal_macros)
112 const char time_start[] =
115 const char time_middle[] =
116 "#define a __TIME__\n"
118 const char time_end[] =
119 "#define a __TIME__";
121 const char date_start[] =
124 const char date_middle[] =
125 "#define ab __DATE__\n"
127 const char date_end[] =
128 "#define ab __DATE__";
130 const char no_temporal[] =
131 "#define ab _ _DATE__\n"
132 "#define ab __ DATE__\n"
133 "#define ab __D ATE__\n"
134 "#define ab __DA TE__\n"
135 "#define ab __DAT E__\n"
136 "#define ab __DATE __\n"
137 "#define ab __DATE_ _\n"
138 "#define ab _ _TIME__\n"
139 "#define ab __ TIME__\n"
140 "#define ab __T IME__\n"
141 "#define ab __TI ME__\n"
142 "#define ab __TIM E__\n"
143 "#define ab __TIME __\n"
144 "#define ab __TIME_ _\n";
146 CHECK(check_for_temporal_macros(time_start + 0, sizeof(time_start) - 0));
147 CHECK(!check_for_temporal_macros(time_start + 1, sizeof(time_start) - 1));
149 CHECK(check_for_temporal_macros(time_middle + 0, sizeof(time_middle) - 0));
150 CHECK(check_for_temporal_macros(time_middle + 1, sizeof(time_middle) - 1));
151 CHECK(check_for_temporal_macros(time_middle + 2, sizeof(time_middle) - 2));
152 CHECK(check_for_temporal_macros(time_middle + 3, sizeof(time_middle) - 3));
153 CHECK(check_for_temporal_macros(time_middle + 4, sizeof(time_middle) - 4));
154 CHECK(check_for_temporal_macros(time_middle + 5, sizeof(time_middle) - 5));
155 CHECK(check_for_temporal_macros(time_middle + 6, sizeof(time_middle) - 6));
156 CHECK(check_for_temporal_macros(time_middle + 7, sizeof(time_middle) - 7));
158 CHECK(check_for_temporal_macros(time_end + 0, sizeof(time_end) - 0));
159 CHECK(check_for_temporal_macros(time_end + sizeof(time_end) - 9, 9));
160 CHECK(!check_for_temporal_macros(time_end + sizeof(time_end) - 8, 8));
162 CHECK(check_for_temporal_macros(date_start + 0, sizeof(date_start) - 0));
163 CHECK(!check_for_temporal_macros(date_start + 1, sizeof(date_start) - 1));
165 CHECK(check_for_temporal_macros(date_middle + 0, sizeof(date_middle) - 0));
166 CHECK(check_for_temporal_macros(date_middle + 1, sizeof(date_middle) - 1));
167 CHECK(check_for_temporal_macros(date_middle + 2, sizeof(date_middle) - 2));
168 CHECK(check_for_temporal_macros(date_middle + 3, sizeof(date_middle) - 3));
169 CHECK(check_for_temporal_macros(date_middle + 4, sizeof(date_middle) - 4));
170 CHECK(check_for_temporal_macros(date_middle + 5, sizeof(date_middle) - 5));
171 CHECK(check_for_temporal_macros(date_middle + 6, sizeof(date_middle) - 6));
172 CHECK(check_for_temporal_macros(date_middle + 7, sizeof(date_middle) - 7));
174 CHECK(check_for_temporal_macros(date_end + 0, sizeof(date_end) - 0));
175 CHECK(check_for_temporal_macros(date_end + sizeof(date_end) - 9, 9));
176 CHECK(!check_for_temporal_macros(date_end + sizeof(date_end) - 8, 8));
178 CHECK(!check_for_temporal_macros(no_temporal + 0, sizeof(no_temporal) - 0));
179 CHECK(!check_for_temporal_macros(no_temporal + 1, sizeof(no_temporal) - 1));
180 CHECK(!check_for_temporal_macros(no_temporal + 2, sizeof(no_temporal) - 2));
181 CHECK(!check_for_temporal_macros(no_temporal + 3, sizeof(no_temporal) - 3));
182 CHECK(!check_for_temporal_macros(no_temporal + 4, sizeof(no_temporal) - 4));
183 CHECK(!check_for_temporal_macros(no_temporal + 5, sizeof(no_temporal) - 5));
184 CHECK(!check_for_temporal_macros(no_temporal + 6, sizeof(no_temporal) - 6));
185 CHECK(!check_for_temporal_macros(no_temporal + 7, sizeof(no_temporal) - 7));