summaryrefslogtreecommitdiff
path: root/tests/test-getentropy.c
blob: a05050813d64198a85896e4949d5625708e0b2b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Test the getentropy function.
   Copyright 2020-2023 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Paul Eggert.  */

#include <config.h>

#include <unistd.h>

#include <string.h>

#include "signature.h"
SIGNATURE_CHECK (getentropy, int, (void *, size_t));

#include "macros.h"

char empty_buf[256];
char buf[256];

int
main (int argc, char *argv[])
{
  /* If this test fails, there's something wrong with the setup anyway.  */
  ASSERT (getentropy (buf, sizeof buf) == 0);

  /* This test fails with probability 2**-2048.  (Run it again if so. :-) */
  ASSERT (memcmp (buf, empty_buf, sizeof buf) != 0);

  /* It is very unlikely that two calls to getentropy produce the same
     results.  */
  {
    char buf1[8];
    char buf2[8];

    ASSERT (getentropy (buf1, sizeof (buf1)) == 0);
    ASSERT (getentropy (buf2, sizeof (buf2)) == 0);
    ASSERT (memcmp (buf1, buf2, sizeof (buf1)) != 0);
  }

  return 0;
}