t_random.c revision 1.3
11.3Sjruoho/* $NetBSD: t_random.c,v 1.3 2012/03/29 08:56:06 jruoho Exp $ */
21.1Sjruoho
31.1Sjruoho/*-
41.1Sjruoho * Copyright (c) 2012 The NetBSD Foundation, Inc.
51.1Sjruoho * All rights reserved.
61.1Sjruoho *
71.1Sjruoho * This code is derived from software contributed to The NetBSD Foundation
81.1Sjruoho * by Jukka Ruohonen.
91.1Sjruoho *
101.1Sjruoho * Redistribution and use in source and binary forms, with or without
111.1Sjruoho * modification, are permitted provided that the following conditions
121.1Sjruoho * are met:
131.1Sjruoho * 1. Redistributions of source code must retain the above copyright
141.1Sjruoho *    notice, this list of conditions and the following disclaimer.
151.1Sjruoho * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjruoho *    notice, this list of conditions and the following disclaimer in the
171.1Sjruoho *    documentation and/or other materials provided with the distribution.
181.1Sjruoho *
191.1Sjruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sjruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sjruoho * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sjruoho * POSSIBILITY OF SUCH DAMAGE.
301.1Sjruoho */
311.1Sjruoho#include <sys/cdefs.h>
321.3Sjruoho__RCSID("$NetBSD: t_random.c,v 1.3 2012/03/29 08:56:06 jruoho Exp $");
331.1Sjruoho
341.1Sjruoho#include <atf-c.h>
351.3Sjruoho#include <stdio.h>
361.1Sjruoho#include <stdlib.h>
371.1Sjruoho
381.1Sjruoho/*
391.1Sjruoho * TODO: Add some general RNG tests (cf. the famous "diehard" tests?).
401.1Sjruoho */
411.1Sjruoho
421.3SjruohoATF_TC(random_same);
431.3SjruohoATF_TC_HEAD(random_same, tc)
441.1Sjruoho{
451.1Sjruoho	atf_tc_set_md_var(tc, "descr",
461.3Sjruoho	    "Test that random(3) does not always return the same "
471.3Sjruoho	    "value when the seed is initialized to zero");
481.1Sjruoho}
491.1Sjruoho
501.3Sjruoho#define MAX_ITER 10
511.3Sjruoho
521.3SjruohoATF_TC_BODY(random_same, tc)
531.1Sjruoho{
541.3Sjruoho	long buf[MAX_ITER];
551.1Sjruoho	size_t i, j;
561.1Sjruoho
571.2Sjruoho	/*
581.2Sjruoho	 * See CVE-2012-1577.
591.2Sjruoho	 */
601.1Sjruoho	srandom(0);
611.1Sjruoho
621.3Sjruoho	for (i = 0; i < __arraycount(buf); i++) {
631.3Sjruoho
641.3Sjruoho		buf[i] = random();
651.3Sjruoho
661.3Sjruoho		for (j = 0; j < i; j++) {
671.1Sjruoho
681.3Sjruoho			(void)fprintf(stderr, "i = %zu, j = %zu: "
691.3Sjruoho			    "%ld vs. %ld\n", i, j, buf[i], buf[j]);
701.3Sjruoho
711.3Sjruoho			ATF_CHECK(buf[i] != buf[j]);
721.3Sjruoho		}
731.1Sjruoho	}
741.1Sjruoho}
751.1Sjruoho
761.1SjruohoATF_TP_ADD_TCS(tp)
771.1Sjruoho{
781.1Sjruoho
791.3Sjruoho	ATF_TP_ADD_TC(tp, random_same);
801.1Sjruoho
811.1Sjruoho	return atf_no_error();
821.1Sjruoho}
83