]>
git.proxmox.com Git - mirror_spl.git/blob - module/splat/splat-random.c
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Random Number Generator Tests.
25 \*****************************************************************************/
27 #include <sys/random.h>
29 #include "splat-internal.h"
31 #define SPLAT_KRNG_NAME "krng"
32 #define SPLAT_KRNG_DESC "Kernel Random Number Generator Tests"
34 #define SPLAT_KRNG_TEST1_ID 0x0301
35 #define SPLAT_KRNG_TEST1_NAME "freq"
36 #define SPLAT_KRNG_TEST1_DESC "Frequency Test"
38 #define KRNG_NUM_BITS 1048576
39 #define KRNG_NUM_BYTES (KRNG_NUM_BITS >> 3)
40 #define KRNG_NUM_BITS_DIV2 (KRNG_NUM_BITS >> 1)
41 #define KRNG_ERROR_RANGE 2097
43 /* Random Number Generator Tests
44 There can be meny more tests on quality of the
45 random number generator. For now we are only
46 testing the frequency of particular bits.
47 We could also test consecutive sequences,
48 randomness within a particular block, etc.
49 but is probably not necessary for our purposes */
52 splat_krng_test1(struct file
*file
, void *arg
)
55 int i
, j
, diff
, num
= 0, rc
= 0;
57 buf
= kmalloc(sizeof(*buf
) * KRNG_NUM_BYTES
, GFP_KERNEL
);
63 memset(buf
, 0, sizeof(*buf
) * KRNG_NUM_BYTES
);
66 random_get_pseudo_bytes(buf
, sizeof(uint8_t) * KRNG_NUM_BYTES
);
68 for (i
= 0; i
< KRNG_NUM_BYTES
; i
++) {
70 for (j
= 0; j
< 8; j
++) {
71 uint8_t tmp2
= ((tmp
>> j
) & 0x01);
80 diff
= KRNG_NUM_BITS_DIV2
- num
;
84 splat_print(file
, "Test 1 Number of ones: %d\n", num
);
85 splat_print(file
, "Test 1 Difference from expected: %d Allowed: %d\n",
86 diff
, KRNG_ERROR_RANGE
);
88 if (diff
> KRNG_ERROR_RANGE
)
97 splat_subsystem_t
*sub
;
99 sub
= kmalloc(sizeof(*sub
), GFP_KERNEL
);
103 memset(sub
, 0, sizeof(*sub
));
104 strncpy(sub
->desc
.name
, SPLAT_KRNG_NAME
, SPLAT_NAME_SIZE
);
105 strncpy(sub
->desc
.desc
, SPLAT_KRNG_DESC
, SPLAT_DESC_SIZE
);
106 INIT_LIST_HEAD(&sub
->subsystem_list
);
107 INIT_LIST_HEAD(&sub
->test_list
);
108 spin_lock_init(&sub
->test_lock
);
109 sub
->desc
.id
= SPLAT_SUBSYSTEM_KRNG
;
111 splat_test_init(sub
, SPLAT_KRNG_TEST1_NAME
, SPLAT_KRNG_TEST1_DESC
,
112 SPLAT_KRNG_TEST1_ID
, splat_krng_test1
);
118 splat_krng_fini(splat_subsystem_t
*sub
)
122 splat_test_fini(sub
, SPLAT_KRNG_TEST1_ID
);
128 splat_krng_id(void) {
129 return SPLAT_SUBSYSTEM_KRNG
;