Blame tests/rng-fork.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2008-2012 Free Software Foundation, Inc.
Packit 549fdc
 *
Packit 549fdc
 * Author: Nikos Mavrogiannopoulos
Packit 549fdc
 *
Packit 549fdc
 * This file is part of GnuTLS.
Packit 549fdc
 *
Packit 549fdc
 * GnuTLS is free software: you can redistribute it and/or modify it
Packit 549fdc
 * under the terms of the GNU General Public License as published by
Packit 549fdc
 * the Free Software Foundation, either version 3 of the License, or
Packit 549fdc
 * (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * GnuTLS is distributed in the hope that it will be useful, but
Packit 549fdc
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
 * General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU General Public License
Packit 549fdc
 * along with GnuTLS.  If not, see <http://www.gnu.org/licenses/>.
Packit 549fdc
 *
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
#ifdef HAVE_CONFIG_H
Packit 549fdc
#include <config.h>
Packit 549fdc
#endif
Packit 549fdc
Packit 549fdc
#include <stdio.h>
Packit 549fdc
#include <stdlib.h>
Packit 549fdc
#include <unistd.h>
Packit 549fdc
#include <sys/types.h>
Packit 549fdc
#include <assert.h>
Packit 549fdc
#if !defined(_WIN32)
Packit 549fdc
#include <sys/wait.h>
Packit 549fdc
#endif
Packit 549fdc
Packit 549fdc
#include "utils.h"
Packit 549fdc
#include <gnutls/gnutls.h>
Packit 549fdc
#include <gnutls/crypto.h>
Packit 549fdc
Packit 549fdc
#if !defined(_WIN32)
Packit 549fdc
static void dump(const char *name, unsigned char *buf, int buf_size)
Packit 549fdc
{
Packit 549fdc
	int i;
Packit 549fdc
	printf("%s: ", name);
Packit 549fdc
	for (i = 0; i < buf_size; i++)
Packit 549fdc
		printf("%.2x:", buf[i]);
Packit 549fdc
	printf("\n");
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
#define FILENAME "./rng-test"
Packit 549fdc
Packit 549fdc
void doit(void)
Packit 549fdc
{
Packit 549fdc
	unsigned char buf1[64];
Packit 549fdc
	unsigned char buf2[64];
Packit 549fdc
	pid_t pid;
Packit 549fdc
	int ret;
Packit 549fdc
	FILE *fp;
Packit 549fdc
	unsigned i;
Packit 549fdc
Packit 549fdc
	global_init();
Packit 549fdc
Packit 549fdc
	for (i = GNUTLS_RND_NONCE; i <= GNUTLS_RND_KEY; i++) {
Packit 549fdc
		pid = fork();
Packit 549fdc
		if (pid == 0) {
Packit 549fdc
			fp = fopen(FILENAME, "w");
Packit 549fdc
			if (fp == NULL)
Packit 549fdc
				fail("cannot open file");
Packit 549fdc
Packit 549fdc
			assert(gnutls_rnd(i, buf1, sizeof(buf1)) >= 0);
Packit 549fdc
			if (debug)
Packit 549fdc
				dump("buf1", buf1, sizeof(buf1));
Packit 549fdc
Packit 549fdc
			fwrite(buf1, 1, sizeof(buf1), fp);
Packit 549fdc
			fclose(fp);
Packit 549fdc
			gnutls_global_deinit();
Packit 549fdc
			exit(0);
Packit 549fdc
		} else {
Packit 549fdc
			/* daddy */
Packit 549fdc
			assert(gnutls_rnd(i, buf2, sizeof(buf2)) >= 0);
Packit 549fdc
			if (debug)
Packit 549fdc
				dump("buf2", buf2, sizeof(buf2));
Packit 549fdc
			waitpid(pid, NULL, 0);
Packit 549fdc
Packit 549fdc
			fp = fopen(FILENAME, "r");
Packit 549fdc
			if (fp == NULL)
Packit 549fdc
				fail("cannot open file");
Packit 549fdc
Packit 549fdc
			ret = fread(buf1, 1, sizeof(buf1), fp);
Packit 549fdc
Packit 549fdc
			fclose(fp);
Packit 549fdc
			remove(FILENAME);
Packit 549fdc
Packit 549fdc
			if (ret != sizeof(buf1)) {
Packit 549fdc
				fail("error testing the random generator (%u).\n", i);
Packit 549fdc
				return;
Packit 549fdc
			}
Packit 549fdc
Packit 549fdc
			if (memcmp(buf1, buf2, sizeof(buf1)) == 0) {
Packit 549fdc
				fail("error in the random generator (%u). Produces same valus after fork()\n", i);
Packit 549fdc
				return;
Packit 549fdc
			}
Packit 549fdc
			if (debug)
Packit 549fdc
				success("success\n");
Packit 549fdc
		}
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	gnutls_global_deinit();
Packit 549fdc
}
Packit 549fdc
#else
Packit 549fdc
void doit(void)
Packit 549fdc
{
Packit 549fdc
	exit(77);
Packit 549fdc
}
Packit 549fdc
#endif