Imported Upstream version 3.4
[platform/upstream/ccache.git] / unittest / test_hashutil.c
1 // Copyright (C) 2010-2018 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
26 TEST(hash_command_output_simple)
27 {
28         struct mdfour h1, h2;
29         hash_start(&h1);
30         hash_start(&h2);
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
36 TEST(hash_command_output_space_removal)
37 {
38         struct mdfour h1, h2;
39         hash_start(&h1);
40         hash_start(&h2);
41         CHECK(hash_command_output(&h1, "echo", "not used"));
42         CHECK(hash_command_output(&h2, " echo ", "not used"));
43         CHECK(hash_equal(&h1, &h2));
44 }
45
46 TEST(hash_command_output_hash_inequality)
47 {
48         struct mdfour h1, h2;
49         hash_start(&h1);
50         hash_start(&h2);
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));
54 }
55
56 TEST(hash_command_output_compiler_substitution)
57 {
58         struct mdfour h1, h2;
59         hash_start(&h1);
60         hash_start(&h2);
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));
64 }
65
66 TEST(hash_command_output_stdout_versus_stderr)
67 {
68         struct mdfour h1, h2;
69         hash_start(&h1);
70         hash_start(&h2);
71 #ifndef _WIN32
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"));
76 #else
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"));
80 #endif
81         CHECK(hash_equal(&h1, &h2));
82 }
83
84 TEST(hash_multicommand_output)
85 {
86         struct mdfour h1, h2;
87         hash_start(&h1);
88         hash_start(&h2);
89 #ifndef _WIN32
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"));
94 #else
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"));
98 #endif
99         CHECK(hash_equal(&h1, &h2));
100 }
101
102 TEST(hash_multicommand_output_error_handling)
103 {
104         struct mdfour h1, h2;
105         hash_start(&h1);
106         hash_start(&h2);
107         CHECK(!hash_multicommand_output(&h2, "false; true", "not used"));
108 }
109
110 TEST(check_for_temporal_macros)
111 {
112         const char time_start[] =
113           "__TIME__\n"
114           "int a;\n";
115         const char time_middle[] =
116           "#define a __TIME__\n"
117           "int a;\n";
118         const char time_end[] =
119           "#define a __TIME__";
120
121         const char date_start[] =
122           "__DATE__\n"
123           "int ab;\n";
124         const char date_middle[] =
125           "#define ab __DATE__\n"
126           "int ab;\n";
127         const char date_end[] =
128           "#define ab __DATE__";
129
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";
145
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));
148
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));
157
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));
161
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));
164
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));
173
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));
177
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));
186 }
187
188 TEST_SUITE_END