[v2,3/6] nss: Add tests for the nss_hash in nss_hash.h
Checks
Context |
Check |
Description |
dj/TryBot-apply_patch |
success
|
Patch applied to master at the time it was sent
|
Commit Message
If we want to further optimize the function tests are needed.
---
nss/Makefile | 1 +
nss/tst-nss-hash.c | 105 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 106 insertions(+)
create mode 100644 nss/tst-nss-hash.c
Comments
* Noah Goldstein via Libc-alpha:
> +static int
> +do_fill_tests (size_t len, int fill)
> +{
> + uint32_t expec, res;
> + char buf[len];
> + memset (buf, fill, len);
> +
> + expec = simple_nss_hash (buf, len);
> + res = __nss_hash (buf, len);
> + if (expec != res)
> + {
> + printf ("FAIL: fill(%d) (%lu), %x != %x\n", fill, len, expec, res);
> + return 1;
> + }
> +
> + return 0;
> +}
%lu needs to be %zu, otherwise this fails to build on various
architectures.
Thanks,
Florian
On Mon, Apr 25, 2022 at 10:38 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Noah Goldstein via Libc-alpha:
>
> > +static int
> > +do_fill_tests (size_t len, int fill)
> > +{
> > + uint32_t expec, res;
> > + char buf[len];
> > + memset (buf, fill, len);
> > +
> > + expec = simple_nss_hash (buf, len);
> > + res = __nss_hash (buf, len);
> > + if (expec != res)
> > + {
> > + printf ("FAIL: fill(%d) (%lu), %x != %x\n", fill, len, expec, res);
> > + return 1;
> > + }
> > +
> > + return 0;
> > +}
>
> %lu needs to be %zu, otherwise this fails to build on various
> architectures.
Fixed in v3.
>
> Thanks,
> Florian
>
* Noah Goldstein:
> On Mon, Apr 25, 2022 at 10:38 AM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * Noah Goldstein via Libc-alpha:
>>
>> > +static int
>> > +do_fill_tests (size_t len, int fill)
>> > +{
>> > + uint32_t expec, res;
>> > + char buf[len];
>> > + memset (buf, fill, len);
>> > +
>> > + expec = simple_nss_hash (buf, len);
>> > + res = __nss_hash (buf, len);
>> > + if (expec != res)
>> > + {
>> > + printf ("FAIL: fill(%d) (%lu), %x != %x\n", fill, len, expec, res);
>> > + return 1;
>> > + }
>> > +
>> > + return 0;
>> > +}
>>
>> %lu needs to be %zu, otherwise this fails to build on various
>> architectures.
>
> Fixed in v3.
Thanks; I can confirm the new version builds fine.
Florian
On Tue, Apr 26, 2022 at 3:36 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Noah Goldstein:
>
> > On Mon, Apr 25, 2022 at 10:38 AM Florian Weimer <fweimer@redhat.com> wrote:
> >>
> >> * Noah Goldstein via Libc-alpha:
> >>
> >> > +static int
> >> > +do_fill_tests (size_t len, int fill)
> >> > +{
> >> > + uint32_t expec, res;
> >> > + char buf[len];
> >> > + memset (buf, fill, len);
> >> > +
> >> > + expec = simple_nss_hash (buf, len);
> >> > + res = __nss_hash (buf, len);
> >> > + if (expec != res)
> >> > + {
> >> > + printf ("FAIL: fill(%d) (%lu), %x != %x\n", fill, len, expec, res);
> >> > + return 1;
> >> > + }
> >> > +
> >> > + return 0;
> >> > +}
> >>
> >> %lu needs to be %zu, otherwise this fails to build on various
> >> architectures.
> >
> > Fixed in v3.
>
> Thanks; I can confirm the new version builds fine.
>
> Florian
Thanks, good to push?
>
* Noah Goldstein:
> On Tue, Apr 26, 2022 at 3:36 AM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * Noah Goldstein:
>>
>> > On Mon, Apr 25, 2022 at 10:38 AM Florian Weimer <fweimer@redhat.com> wrote:
>> >>
>> >> * Noah Goldstein via Libc-alpha:
>> >>
>> >> > +static int
>> >> > +do_fill_tests (size_t len, int fill)
>> >> > +{
>> >> > + uint32_t expec, res;
>> >> > + char buf[len];
>> >> > + memset (buf, fill, len);
>> >> > +
>> >> > + expec = simple_nss_hash (buf, len);
>> >> > + res = __nss_hash (buf, len);
>> >> > + if (expec != res)
>> >> > + {
>> >> > + printf ("FAIL: fill(%d) (%lu), %x != %x\n", fill, len, expec, res);
>> >> > + return 1;
>> >> > + }
>> >> > +
>> >> > + return 0;
>> >> > +}
>> >>
>> >> %lu needs to be %zu, otherwise this fails to build on various
>> >> architectures.
>> >
>> > Fixed in v3.
>>
>> Thanks; I can confirm the new version builds fine.
>>
>> Florian
>
> Thanks, good to push?
You said you wanted to post a v4, I think?
Anyway, I commented on a few nits.
Thanks,
Florian
On Wed, Apr 27, 2022 at 5:48 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Noah Goldstein:
>
> > On Tue, Apr 26, 2022 at 3:36 AM Florian Weimer <fweimer@redhat.com> wrote:
> >>
> >> * Noah Goldstein:
> >>
> >> > On Mon, Apr 25, 2022 at 10:38 AM Florian Weimer <fweimer@redhat.com> wrote:
> >> >>
> >> >> * Noah Goldstein via Libc-alpha:
> >> >>
> >> >> > +static int
> >> >> > +do_fill_tests (size_t len, int fill)
> >> >> > +{
> >> >> > + uint32_t expec, res;
> >> >> > + char buf[len];
> >> >> > + memset (buf, fill, len);
> >> >> > +
> >> >> > + expec = simple_nss_hash (buf, len);
> >> >> > + res = __nss_hash (buf, len);
> >> >> > + if (expec != res)
> >> >> > + {
> >> >> > + printf ("FAIL: fill(%d) (%lu), %x != %x\n", fill, len, expec, res);
> >> >> > + return 1;
> >> >> > + }
> >> >> > +
> >> >> > + return 0;
> >> >> > +}
> >> >>
> >> >> %lu needs to be %zu, otherwise this fails to build on various
> >> >> architectures.
> >> >
> >> > Fixed in v3.
> >>
> >> Thanks; I can confirm the new version builds fine.
> >>
> >> Florian
> >
> > Thanks, good to push?
>
> You said you wanted to post a v4, I think?
Recent v3 like a doofus, will mix your nits as well as Adhemerval's in V4.
>
> Anyway, I commented on a few nits.
>
> Thanks,
> Florian
>
@@ -62,6 +62,7 @@ tests := \
test-digits-dots \
test-netdb \
tst-nss-getpwent \
+ tst-nss-hash \
tst-nss-test1 \
tst-nss-test2 \
tst-nss-test4 \
new file mode 100644
@@ -0,0 +1,105 @@
+/* Test __nss_hash
+ Copyright (C) 2017-2022 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <support/support.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <nss.h>
+
+uint32_t __nss_hash (const void *__key, size_t __length);
+
+/* Simplist implementation of __nss_hash. */
+static uint32_t
+simple_nss_hash (const void *keyarg, size_t len)
+{
+ const unsigned char *key;
+ size_t i;
+ uint32_t h = 0;
+ key = keyarg;
+
+ for (i = 0; i < len; ++i)
+ {
+ h = *key++ + 65599 * h;
+ }
+ return h;
+}
+
+static int
+do_fill_tests (size_t len, int fill)
+{
+ uint32_t expec, res;
+ char buf[len];
+ memset (buf, fill, len);
+
+ expec = simple_nss_hash (buf, len);
+ res = __nss_hash (buf, len);
+ if (expec != res)
+ {
+ printf ("FAIL: fill(%d) (%lu), %x != %x\n", fill, len, expec, res);
+ return 1;
+ }
+
+ return 0;
+}
+
+static int
+do_rand_tests (size_t len)
+{
+ uint32_t expec, res;
+ size_t i;
+ char buf[len];
+ for (i = 0; i < len; ++i)
+ {
+ buf[i] = random ();
+ }
+
+ expec = simple_nss_hash (buf, len);
+ res = __nss_hash (buf, len);
+ if (expec != res)
+ {
+ printf ("FAIL: random (%lu), %x != %x\n", len, expec, res);
+ return 1;
+ }
+
+ return 0;
+}
+
+static int
+do_test (void)
+{
+ size_t i, j;
+ for (i = 0; i < 100; ++i)
+ {
+ for (j = 0; j < 8192; ++j)
+ {
+ if (do_rand_tests (i))
+ {
+ return 1;
+ }
+ if (do_fill_tests (i, -1) || do_fill_tests (i, 1)
+ || do_fill_tests (i, 0x80) || do_fill_tests (i, 0x88))
+ {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+#include <support/test-driver.c>