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