etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / entropy_test.c
blobc51a18452e7d8a0ae6ddfffeb541a1282846db28
1 /* $NetBSD: entropy_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: entropy_test.c,v 1.23 2007/06/19 23:46:59 tbox Exp */
22 /*! \file */
24 #include <config.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include <isc/entropy.h>
30 #include <isc/mem.h>
31 #include <isc/util.h>
32 #include <isc/string.h>
34 static void
35 hex_dump(const char *msg, void *data, unsigned int length) {
36 unsigned int len;
37 unsigned char *base;
38 isc_boolean_t first = ISC_TRUE;
40 base = data;
42 printf("DUMP of %d bytes: %s\n\t", length, msg);
43 for (len = 0; len < length; len++) {
44 if (len % 16 == 0 && !first)
45 printf("\n\t");
46 printf("%02x ", base[len]);
47 first = ISC_FALSE;
49 printf("\n");
52 static void
53 CHECK(const char *msg, isc_result_t result) {
54 if (result != ISC_R_SUCCESS) {
55 printf("FAILURE: %s: %s\n", msg, isc_result_totext(result));
56 exit(1);
60 int
61 main(int argc, char **argv) {
62 isc_mem_t *mctx;
63 unsigned char buffer[512];
64 isc_entropy_t *ent;
65 unsigned int returned;
66 unsigned int flags;
67 isc_result_t result;
69 UNUSED(argc);
70 UNUSED(argv);
72 mctx = NULL;
73 CHECK("isc_mem_create()",
74 isc_mem_create(0, 0, &mctx));
76 ent = NULL;
77 CHECK("isc_entropy_create()",
78 isc_entropy_create(mctx, &ent));
80 isc_entropy_stats(ent, stderr);
82 #if 1
83 CHECK("isc_entropy_createfilesource() 1",
84 isc_entropy_createfilesource(ent, "/dev/random"));
85 CHECK("isc_entropy_createfilesource() 2",
86 isc_entropy_createfilesource(ent, "/dev/random"));
87 #else
88 CHECK("isc_entropy_createfilesource() 3",
89 isc_entropy_createfilesource(ent, "/tmp/foo"));
90 #endif
92 fprintf(stderr,
93 "Reading 32 bytes of GOOD random data only, partial OK\n");
95 flags = 0;
96 flags |= ISC_ENTROPY_GOODONLY;
97 flags |= ISC_ENTROPY_PARTIAL;
98 result = isc_entropy_getdata(ent, buffer, 32, &returned, flags);
99 if (result == ISC_R_NOENTROPY) {
100 fprintf(stderr, "No entropy.\n");
101 goto any;
103 hex_dump("good data only:", buffer, returned);
105 any:
106 isc_entropy_stats(ent, stderr);
107 CHECK("isc_entropy_getdata() pseudorandom",
108 isc_entropy_getdata(ent, buffer, 128, NULL, 0));
109 hex_dump("pseudorandom data", buffer, 128);
111 isc_entropy_stats(ent, stderr);
112 flags = 0;
113 flags |= ISC_ENTROPY_GOODONLY;
114 flags |= ISC_ENTROPY_BLOCKING;
115 result = isc_entropy_getdata(ent, buffer, sizeof(buffer), &returned,
116 flags);
117 CHECK("good data only, blocking mode", result);
118 hex_dump("blocking mode data", buffer, sizeof(buffer));
121 isc_entropy_t *entcopy1 = NULL;
122 isc_entropy_t *entcopy2 = NULL;
123 isc_entropy_t *entcopy3 = NULL;
125 isc_entropy_attach(ent, &entcopy1);
126 isc_entropy_attach(ent, &entcopy2);
127 isc_entropy_attach(ent, &entcopy3);
129 isc_entropy_stats(ent, stderr);
131 isc_entropy_detach(&entcopy1);
132 isc_entropy_detach(&entcopy2);
133 isc_entropy_detach(&entcopy3);
136 isc_entropy_detach(&ent);
137 isc_mem_stats(mctx, stderr);
138 isc_mem_destroy(&mctx);
140 return (0);