(root)/
glibc-2.38/
nss/
tst-nss-hash.c
       1  /* Test __nss_hash
       2     Copyright (C) 2022-2023 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  #include <support/support.h>
      20  #include <support/check.h>
      21  #include <stdio.h>
      22  #include <string.h>
      23  #include <stdlib.h>
      24  #include <nss.h>
      25  #include <simple-nss-hash.h>
      26  
      27  uint32_t __nss_hash (const void *__key, size_t __length);
      28  
      29  static int
      30  do_fill_tests (size_t len, int fill)
      31  {
      32    uint32_t expec, res;
      33    char buf[len];
      34    memset (buf, fill, len);
      35  
      36    expec = __simple_nss_hash (buf, len);
      37    res = __nss_hash (buf, len);
      38    if (expec != res)
      39      FAIL_EXIT1 ("FAIL: fill(%d) (%zu), %x != %x\n", fill, len, expec, res);
      40  
      41    return 0;
      42  }
      43  
      44  static int
      45  do_rand_tests (size_t len)
      46  {
      47    uint32_t expec, res;
      48    size_t i;
      49    char buf[len];
      50    for (i = 0; i < len; ++i)
      51      buf[i] = random ();
      52  
      53    expec = __simple_nss_hash (buf, len);
      54    res = __nss_hash (buf, len);
      55    if (expec != res)
      56      FAIL_EXIT1 ("FAIL: random (%zu), %x != %x\n", len, expec, res);
      57  
      58    return 0;
      59  }
      60  
      61  static int
      62  do_test (void)
      63  {
      64    size_t i, j;
      65    for (i = 0; i < 100; ++i)
      66      {
      67        for (j = 0; j < 8192; ++j)
      68  	{
      69  	  if (do_rand_tests (i))
      70  	    return 1;
      71  
      72  	  if (do_fill_tests (i, -1) || do_fill_tests (i, 1)
      73  	      || do_fill_tests (i, 0x80) || do_fill_tests (i, 0x88))
      74  	    return 1;
      75  	}
      76      }
      77    return 0;
      78  }
      79  
      80  #include <support/test-driver.c>