t_random.c revision 1.2
11.2Sjruoho/* $NetBSD: t_random.c,v 1.2 2012/03/28 10:38:00 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.2Sjruoho__RCSID("$NetBSD: t_random.c,v 1.2 2012/03/28 10:38:00 jruoho Exp $");
331.1Sjruoho
341.1Sjruoho#include <atf-c.h>
351.1Sjruoho#include <stdlib.h>
361.1Sjruoho
371.1Sjruoho/*
381.1Sjruoho * TODO: Add some general RNG tests (cf. the famous "diehard" tests?).
391.1Sjruoho */
401.1Sjruoho
411.1SjruohoATF_TC(random_zero);
421.1SjruohoATF_TC_HEAD(random_zero, tc)
431.1Sjruoho{
441.1Sjruoho	atf_tc_set_md_var(tc, "descr",
451.1Sjruoho	    "Test that random(3) does not always return "
461.1Sjruoho	    "zero when the seed is initialized to zero");
471.1Sjruoho}
481.1Sjruoho
491.1SjruohoATF_TC_BODY(random_zero, tc)
501.1Sjruoho{
511.1Sjruoho	const size_t n = 1000000;
521.1Sjruoho	size_t i, j;
531.1Sjruoho	long x;
541.1Sjruoho
551.2Sjruoho	/*
561.2Sjruoho	 * See CVE-2012-1577.
571.2Sjruoho	 */
581.1Sjruoho	srandom(0);
591.1Sjruoho
601.1Sjruoho	for (i = j = 0; i < n; i++) {
611.1Sjruoho
621.1Sjruoho		if ((x = random()) == 0)
631.1Sjruoho			j++;
641.1Sjruoho	}
651.1Sjruoho
661.1Sjruoho	ATF_REQUIRE(j != n);
671.1Sjruoho}
681.1Sjruoho
691.1SjruohoATF_TP_ADD_TCS(tp)
701.1Sjruoho{
711.1Sjruoho
721.1Sjruoho	ATF_TP_ADD_TC(tp, random_zero);
731.1Sjruoho
741.1Sjruoho	return atf_no_error();
751.1Sjruoho}
76