tests: Increase timeouts on test-shutdown.sh
[nbdkit/ericb.git] / tests / test-random.c
blob3cb55e056734aaa149c040b42f6519a81da84823
1 /* nbdkit
2 * Copyright (C) 2017-2018 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <time.h>
43 #include <guestfs.h>
45 #include "random.h"
47 #include "test.h"
49 #define SIZE 1024*1024
50 static char data[SIZE];
52 static unsigned histogram[256];
54 /* After reading the whole disk above, we then read randomly selected
55 * subsets of the disk and compare the data (it should be identical).
57 #define RSIZE 10240
58 #define NR_READS 50
60 int
61 main (int argc, char *argv[])
63 guestfs_h *g;
64 int r, i;
65 struct random_state random_state;
66 unsigned offset;
67 char sizearg[32];
68 char *rdata;
69 size_t rsize;
71 snprintf (sizearg, sizeof sizearg, "%d", SIZE);
73 if (test_start_nbdkit ("random", sizearg, NULL) == -1)
74 exit (EXIT_FAILURE);
76 g = guestfs_create ();
77 if (g == NULL) {
78 perror ("guestfs_create");
79 exit (EXIT_FAILURE);
82 r = guestfs_add_drive_opts (g, "",
83 GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
84 GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
85 GUESTFS_ADD_DRIVE_OPTS_PROTOCOL, "nbd",
86 GUESTFS_ADD_DRIVE_OPTS_SERVER, server,
87 -1);
88 if (r == -1)
89 exit (EXIT_FAILURE);
91 if (guestfs_launch (g) == -1)
92 exit (EXIT_FAILURE);
94 /* Read the whole device. */
95 rdata = guestfs_pread_device (g, "/dev/sda", SIZE, 0, &rsize);
96 if (rdata == NULL)
97 exit (EXIT_FAILURE);
98 if (rsize != SIZE) {
99 fprintf (stderr, "test-random: short read\n");
100 exit (EXIT_FAILURE);
102 memcpy (data, rdata, SIZE);
103 free (rdata);
105 /* Test that the data is sufficiently random using a simple
106 * histogram. This just tests for gross errors and is not a
107 * complete statistical study.
109 for (i = 0; i < SIZE; ++i) {
110 unsigned char c = (unsigned char) data[i];
111 histogram[c]++;
113 for (i = 0; i < 256; ++i) {
114 const unsigned expected = SIZE / 256;
115 if (histogram[i] < 80 * expected / 100) {
116 fprintf (stderr, "test-random: "
117 "random data is not uniformly distributed\n"
118 "eg. byte %d occurs %u times (expected about %u times)\n",
119 i, histogram[i], expected);
120 exit (EXIT_FAILURE);
124 /* Randomly read parts of the disk to ensure we get the same data.
126 xsrandom (time (NULL), &random_state);
127 for (i = 0; i < NR_READS; ++i) {
128 offset = xrandom (&random_state);
129 offset %= SIZE - RSIZE;
130 rdata = guestfs_pread_device (g, "/dev/sda", RSIZE, offset, &rsize);
131 if (rdata == NULL)
132 exit (EXIT_FAILURE);
133 if (rsize != RSIZE) {
134 fprintf (stderr, "test-random: short read\n");
135 exit (EXIT_FAILURE);
137 if (memcmp (&data[offset], rdata, rsize) != 0) {
138 fprintf (stderr, "test-random: returned different data\n");
139 exit (EXIT_FAILURE);
141 free (rdata);
144 if (guestfs_shutdown (g) == -1)
145 exit (EXIT_FAILURE);
147 guestfs_close (g);
148 exit (EXIT_SUCCESS);