remove p11-kit in gnutls.spec file
[platform/upstream/gnutls.git] / tests / rng-fork.c
1 /*
2  * Copyright (C) 2008-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * GnuTLS is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuTLS is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with GnuTLS.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #if !defined(_WIN32)
32 #include <sys/wait.h>
33 #endif
34
35 #include "utils.h"
36 #include <gnutls/gnutls.h>
37 #include <gnutls/crypto.h>
38
39 #if !defined(_WIN32)
40 static void dump(const char *name, unsigned char *buf, int buf_size)
41 {
42         int i;
43         printf("%s: ", name);
44         for (i = 0; i < buf_size; i++)
45                 printf("%.2x:", buf[i]);
46         printf("\n");
47 }
48
49 #define FILENAME "./rng-test"
50
51 void doit(void)
52 {
53         unsigned char buf1[64];
54         unsigned char buf2[64];
55         pid_t pid;
56         int ret;
57         FILE *fp;
58         unsigned i;
59
60         global_init();
61
62         for (i = GNUTLS_RND_NONCE; i <= GNUTLS_RND_KEY; i++) {
63                 pid = fork();
64                 if (pid == 0) {
65                         fp = fopen(FILENAME, "w");
66                         if (fp == NULL)
67                                 fail("cannot open file");
68
69                         gnutls_rnd(i, buf1, sizeof(buf1));
70                         if (debug)
71                                 dump("buf1", buf1, sizeof(buf1));
72
73                         fwrite(buf1, 1, sizeof(buf1), fp);
74                         fclose(fp);
75                         gnutls_global_deinit();
76                         exit(0);
77                 } else {
78                         /* daddy */
79                         gnutls_rnd(i, buf2, sizeof(buf2));
80                         if (debug)
81                                 dump("buf2", buf2, sizeof(buf2));
82                         waitpid(pid, NULL, 0);
83
84                         fp = fopen(FILENAME, "r");
85                         if (fp == NULL)
86                                 fail("cannot open file");
87
88                         ret = fread(buf1, 1, sizeof(buf1), fp);
89
90                         fclose(fp);
91                         remove(FILENAME);
92
93                         if (ret != sizeof(buf1)) {
94                                 fail("error testing the random generator (%u).\n", i);
95                                 return;
96                         }
97
98                         if (memcmp(buf1, buf2, sizeof(buf1)) == 0) {
99                                 fail("error in the random generator (%u). Produces same valus after fork()\n", i);
100                                 return;
101                         }
102                         if (debug)
103                                 success("success\n");
104                 }
105         }
106
107         for (i = 0; i <= 65539; i++) {
108                 ret = gnutls_rnd(GNUTLS_RND_NONCE, buf1, sizeof(buf1));
109                 if (ret < 0) {
110                         fail("Error iterating RNG-nonce more than %u times\n", i);
111                         exit(1);
112                 }
113         }
114
115         for (i = 0; i <= 65539; i++) {
116                 ret = gnutls_rnd(GNUTLS_RND_RANDOM, buf1, sizeof(buf1));
117                 if (ret < 0) {
118                         fail("Error iterating RNG-random more than %u times\n", i);
119                         exit(1);
120                 }
121         }
122
123         gnutls_global_deinit();
124 }
125 #else
126 void doit(void)
127 {
128         exit(77);
129 }
130 #endif