1 /* Test dl-hash functions.
2    Copyright (C) 2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 
20 #include <simple-dl-hash.h>
21 #include <simple-dl-new-hash.h>
22 #include <dl-hash.h>
23 #include <dl-new-hash.h>
24 #include <support/support.h>
25 #include <support/check.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 
30 typedef unsigned int (*hash_f) (const char *);
31 
32 
33 
34 static int
do_fill_test(size_t len,int fill,const char * name,hash_f testf,hash_f expecf)35 do_fill_test (size_t len, int fill, const char *name, hash_f testf,
36 	      hash_f expecf)
37 {
38   uint32_t expec, res;
39   char buf[len + 1];
40   memset (buf, fill, len);
41   buf[len] = '\0';
42 
43   expec = expecf (buf);
44   res = testf (buf);
45   if (expec != res)
46     FAIL_EXIT1 ("FAIL: fill(%d) %s(%zu), %x != %x\n", fill, name, len, expec,
47 		res);
48 
49   return 0;
50 }
51 
52 static int
do_fill_tests(size_t len,int fill)53 do_fill_tests (size_t len, int fill)
54 {
55   if (do_fill_test (len, fill, "dl_new_hash", &_dl_new_hash,
56 		    &__simple_dl_new_hash))
57     return 1;
58 
59   return do_fill_test (len, fill, "dl_elf_hash", &_dl_elf_hash,
60 		       &__simple_dl_elf_hash);
61 }
62 
63 static int
do_rand_test(size_t len,const char * name,hash_f testf,hash_f expecf)64 do_rand_test (size_t len, const char *name, hash_f testf, hash_f expecf)
65 {
66   uint32_t expec, res;
67   size_t i;
68   char buf[len + 1];
69   char v;
70   for (i = 0; i < len; ++i)
71     {
72       v = random ();
73       if (v == 0)
74 	v = 1;
75 
76       buf[i] = v;
77     }
78   buf[len] = '\0';
79 
80   expec = expecf (buf);
81   res = testf (buf);
82   if (expec != res)
83     FAIL_EXIT1 ("FAIL: random %s(%zu), %x != %x\n", name, len, expec, res);
84 
85   return 0;
86 }
87 
88 static int
do_rand_tests(size_t len)89 do_rand_tests (size_t len)
90 {
91   if (do_rand_test (len, "dl_new_hash", &_dl_new_hash, &__simple_dl_new_hash))
92     return 1;
93 
94   return do_rand_test (len, "dl_elf_hash", &_dl_elf_hash, &__simple_dl_elf_hash);
95 }
96 
97 static int
do_test(void)98 do_test (void)
99 {
100   size_t i, j;
101   for (i = 0; i < 100; ++i)
102     {
103       for (j = 0; j < 8192; ++j)
104 	{
105 	  if (do_rand_tests (i))
106 	    return 1;
107 
108 	  if (do_fill_tests (i, -1) || do_fill_tests (i, 1)
109 	      || do_fill_tests (i, 0x80) || do_fill_tests (i, 0x88))
110 	    return 1;
111 	}
112     }
113   return 0;
114 }
115 
116 #include <support/test-driver.c>
117