util: import sha1 implementation from OpenBSD
[mesa.git] / src / compiler / glsl / tests / cache_test.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /* A collection of unit tests for cache.c */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <ftw.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 #include <inttypes.h>
34
35 #include "util/mesa-sha1.h"
36 #include "util/disk_cache.h"
37
38 bool error = false;
39
40 static void
41 expect_equal(uint64_t actual, uint64_t expected, const char *test)
42 {
43 if (actual != expected) {
44 fprintf(stderr, "Error: Test '%s' failed: Expected=%" PRIu64
45 ", Actual=%" PRIu64 "\n",
46 test, expected, actual);
47 error = true;
48 }
49 }
50
51 static void
52 expect_null(void *ptr, const char *test)
53 {
54 if (ptr != NULL) {
55 fprintf(stderr, "Error: Test '%s' failed: Result=%p, but expected NULL.\n",
56 test, ptr);
57 error = true;
58 }
59 }
60
61 static void
62 expect_non_null(void *ptr, const char *test)
63 {
64 if (ptr == NULL) {
65 fprintf(stderr, "Error: Test '%s' failed: Result=NULL, but expected something else.\n",
66 test);
67 error = true;
68 }
69 }
70
71 static void
72 expect_equal_str(const char *actual, const char *expected, const char *test)
73 {
74 if (strcmp(actual, expected)) {
75 fprintf(stderr, "Error: Test '%s' failed:\n\t"
76 "Expected=\"%s\", Actual=\"%s\"\n",
77 test, expected, actual);
78 error = true;
79 }
80 }
81
82 /* Callback for nftw used in rmrf_local below.
83 */
84 static int
85 remove_entry(const char *path,
86 const struct stat *sb,
87 int typeflag,
88 struct FTW *ftwbuf)
89 {
90 int err = remove(path);
91
92 if (err)
93 fprintf(stderr, "Error removing %s: %s\n", path, strerror(errno));
94
95 return err;
96 }
97
98 /* Recursively remove a directory.
99 *
100 * This is equivalent to "rm -rf <dir>" with one bit of protection
101 * that the directory name must begin with "." to ensure we don't
102 * wander around deleting more than intended.
103 *
104 * Returns 0 on success, -1 on any error.
105 */
106 static int
107 rmrf_local(const char *path)
108 {
109 if (path == NULL || *path == '\0' || *path != '.')
110 return -1;
111
112 return nftw(path, remove_entry, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
113 }
114
115 #define CACHE_TEST_TMP "./cache-test-tmp"
116
117 static void
118 test_disk_cache_create(void)
119 {
120 struct disk_cache *cache;
121 int err;
122
123 /* Before doing anything else, ensure that with
124 * MESA_GLSL_CACHE_DISABLE set, that disk_cache_create returns NULL.
125 */
126 setenv("MESA_GLSL_CACHE_DISABLE", "1", 1);
127 cache = disk_cache_create();
128 expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set");
129
130 unsetenv("MESA_GLSL_CACHE_DISABLE");
131
132 /* For the first real disk_cache_create() clear these environment
133 * variables to test creation of cache in home directory.
134 */
135 unsetenv("MESA_GLSL_CACHE_DIR");
136 unsetenv("XDG_CACHE_HOME");
137
138 cache = disk_cache_create();
139 expect_non_null(cache, "disk_cache_create with no environment variables");
140
141 disk_cache_destroy(cache);
142
143 /* Test with XDG_CACHE_HOME set */
144 setenv("XDG_CACHE_HOME", CACHE_TEST_TMP "/xdg-cache-home", 1);
145 cache = disk_cache_create();
146 expect_null(cache, "disk_cache_create with XDG_CACHE_HOME set with"
147 "a non-existing parent directory");
148
149 mkdir(CACHE_TEST_TMP, 0755);
150 cache = disk_cache_create();
151 expect_non_null(cache, "disk_cache_create with XDG_CACHE_HOME set");
152
153 disk_cache_destroy(cache);
154
155 /* Test with MESA_GLSL_CACHE_DIR set */
156 err = rmrf_local(CACHE_TEST_TMP);
157 expect_equal(err, 0, "Removing " CACHE_TEST_TMP);
158
159 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP "/mesa-glsl-cache-dir", 1);
160 cache = disk_cache_create();
161 expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set with"
162 "a non-existing parent directory");
163
164 mkdir(CACHE_TEST_TMP, 0755);
165 cache = disk_cache_create();
166 expect_non_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set");
167
168 disk_cache_destroy(cache);
169 }
170
171 static bool
172 does_cache_contain(struct disk_cache *cache, cache_key key)
173 {
174 void *result;
175
176 result = disk_cache_get(cache, key, NULL);
177
178 if (result) {
179 free(result);
180 return true;
181 }
182
183 return false;
184 }
185
186 static void
187 test_put_and_get(void)
188 {
189 struct disk_cache *cache;
190 /* If the text of this blob is changed, then blob_key_byte_zero
191 * also needs to be updated.
192 */
193 char blob[] = "This is a blob of thirty-seven bytes";
194 uint8_t blob_key[20];
195 uint8_t blob_key_byte_zero = 0xca;
196 char string[] = "While this string has thirty-four";
197 uint8_t string_key[20];
198 char *result;
199 size_t size;
200 uint8_t *one_KB, *one_MB;
201 uint8_t one_KB_key[20], one_MB_key[20];
202 int count;
203
204 cache = disk_cache_create();
205
206 _mesa_sha1_compute(blob, sizeof(blob), blob_key);
207
208 /* Ensure that disk_cache_get returns nothing before anything is added. */
209 result = disk_cache_get(cache, blob_key, &size);
210 expect_null(result, "disk_cache_get with non-existent item (pointer)");
211 expect_equal(size, 0, "disk_cache_get with non-existent item (size)");
212
213 /* Simple test of put and get. */
214 disk_cache_put(cache, blob_key, blob, sizeof(blob));
215
216 result = disk_cache_get(cache, blob_key, &size);
217 expect_equal_str(blob, result, "disk_cache_get of existing item (pointer)");
218 expect_equal(size, sizeof(blob), "disk_cache_get of existing item (size)");
219
220 free(result);
221
222 /* Test put and get of a second item. */
223 _mesa_sha1_compute(string, sizeof(string), string_key);
224 disk_cache_put(cache, string_key, string, sizeof(string));
225
226 result = disk_cache_get(cache, string_key, &size);
227 expect_equal_str(result, string, "2nd disk_cache_get of existing item (pointer)");
228 expect_equal(size, sizeof(string), "2nd disk_cache_get of existing item (size)");
229
230 free(result);
231
232 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
233 disk_cache_destroy(cache);
234
235 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
236 cache = disk_cache_create();
237
238 one_KB = calloc(1, 1024);
239
240 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
241 * interesting. But we do have want to take some special care with
242 * the hash we use here. The issue is that in this artificial case,
243 * (with only three files in the cache), the probability is good
244 * that each of the three files will end up in their own
245 * directory. Then, if the directory containing the .tmp file for
246 * the new item being added for disk_cache_put() is the chosen victim
247 * directory for eviction, then no suitable file will be found and
248 * nothing will be evicted.
249 *
250 * That's actually expected given how the eviction code is
251 * implemented, (which expects to only evict once things are more
252 * interestingly full than that).
253 *
254 * For this test, we force this signature to land in the same
255 * directory as the original blob first written to the cache.
256 */
257 _mesa_sha1_compute(one_KB, 1024, one_KB_key);
258 one_KB_key[0] = blob_key_byte_zero;
259
260 disk_cache_put(cache, one_KB_key, one_KB, 1024);
261
262 free(one_KB);
263
264 result = disk_cache_get(cache, one_KB_key, &size);
265 expect_non_null(result, "3rd disk_cache_get of existing item (pointer)");
266 expect_equal(size, 1024, "3rd disk_cache_get of existing item (size)");
267
268 free(result);
269
270 /* Ensure eviction happened by checking that only one of the two
271 * previously-added items can still be fetched.
272 */
273 count = 0;
274 if (does_cache_contain(cache, blob_key))
275 count++;
276
277 if (does_cache_contain(cache, string_key))
278 count++;
279
280 expect_equal(count, 1, "disk_cache_put eviction with MAX_SIZE=1K");
281
282 /* Now increase the size to 1M, add back both items, and ensure all
283 * three that have been added are available via disk_cache_get.
284 */
285 disk_cache_destroy(cache);
286
287 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
288 cache = disk_cache_create();
289
290 disk_cache_put(cache, blob_key, blob, sizeof(blob));
291 disk_cache_put(cache, string_key, string, sizeof(string));
292
293 count = 0;
294 if (does_cache_contain(cache, blob_key))
295 count++;
296
297 if (does_cache_contain(cache, string_key))
298 count++;
299
300 if (does_cache_contain(cache, one_KB_key))
301 count++;
302
303 expect_equal(count, 3, "no eviction before overflow with MAX_SIZE=1M");
304
305 /* Finally, check eviction again after adding an object of size 1M. */
306 one_MB = calloc(1024, 1024);
307
308 _mesa_sha1_compute(one_MB, 1024 * 1024, one_MB_key);
309 one_MB_key[0] = blob_key_byte_zero;;
310
311 disk_cache_put(cache, one_MB_key, one_MB, 1024 * 1024);
312
313 free(one_MB);
314
315 count = 0;
316 if (does_cache_contain(cache, blob_key))
317 count++;
318
319 if (does_cache_contain(cache, string_key))
320 count++;
321
322 if (does_cache_contain(cache, one_KB_key))
323 count++;
324
325 expect_equal(count, 2, "eviction after overflow with MAX_SIZE=1M");
326
327 disk_cache_destroy(cache);
328 }
329
330 static void
331 test_put_key_and_get_key(void)
332 {
333 struct disk_cache *cache;
334 bool result;
335
336 uint8_t key_a[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
337 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
338 uint8_t key_b[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
339 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
340 uint8_t key_a_collide[20] =
341 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
342 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
343
344 cache = disk_cache_create();
345
346 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
347 result = disk_cache_has_key(cache, key_a);
348 expect_equal(result, 0, "disk_cache_has_key before key added");
349
350 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
351 disk_cache_put_key(cache, key_a);
352 result = disk_cache_has_key(cache, key_a);
353 expect_equal(result, 1, "disk_cache_has_key after key added");
354
355 disk_cache_put_key(cache, key_b);
356 result = disk_cache_has_key(cache, key_b);
357 expect_equal(result, 1, "2nd disk_cache_has_key after key added");
358
359 /* Test that a key with the same two bytes as an existing key
360 * forces an eviction.
361 */
362 disk_cache_put_key(cache, key_a_collide);
363 result = disk_cache_has_key(cache, key_a_collide);
364 expect_equal(result, 1, "put_key of a colliding key lands in the cache");
365
366 result = disk_cache_has_key(cache, key_a);
367 expect_equal(result, 0, "put_key of a colliding key evicts from the cache");
368
369 /* And finally test that we can re-add the original key to re-evict
370 * the colliding key.
371 */
372 disk_cache_put_key(cache, key_a);
373 result = disk_cache_has_key(cache, key_a);
374 expect_equal(result, 1, "put_key of original key lands again");
375
376 result = disk_cache_has_key(cache, key_a_collide);
377 expect_equal(result, 0, "put_key of orginal key evicts the colliding key");
378
379 disk_cache_destroy(cache);
380 }
381
382 int
383 main(void)
384 {
385 int err;
386
387 test_disk_cache_create();
388
389 test_put_and_get();
390
391 test_put_key_and_get_key();
392
393 err = rmrf_local(CACHE_TEST_TMP);
394 expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again");
395
396 return error ? 1 : 0;
397 }