t_random.c revision 296373
1115720Smarkm/* $NetBSD: t_random.c,v 1.3 2012/03/29 08:56:06 jruoho Exp $ */ 2115720Smarkm 3115720Smarkm/*- 4115720Smarkm * Copyright (c) 2012 The NetBSD Foundation, Inc. 5115720Smarkm * All rights reserved. 6115720Smarkm * 7115720Smarkm * This code is derived from software contributed to The NetBSD Foundation 8115720Smarkm * by Jukka Ruohonen. 9115720Smarkm * 10115720Smarkm * Redistribution and use in source and binary forms, with or without 11115720Smarkm * modification, are permitted provided that the following conditions 12115720Smarkm * are met: 13115720Smarkm * 1. Redistributions of source code must retain the above copyright 14115720Smarkm * notice, this list of conditions and the following disclaimer. 15115720Smarkm * 2. Redistributions in binary form must reproduce the above copyright 16115720Smarkm * notice, this list of conditions and the following disclaimer in the 17115720Smarkm * documentation and/or other materials provided with the distribution. 18115720Smarkm * 19115720Smarkm * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20115720Smarkm * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21115720Smarkm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22115720Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23115720Smarkm * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24115720Smarkm * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25115720Smarkm * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26115720Smarkm * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27115720Smarkm * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28115720Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29115720Smarkm * POSSIBILITY OF SUCH DAMAGE. 30115720Smarkm */ 31115720Smarkm#include <sys/cdefs.h> 32115720Smarkm__RCSID("$NetBSD: t_random.c,v 1.3 2012/03/29 08:56:06 jruoho Exp $"); 33115720Smarkm 34115720Smarkm#include <atf-c.h> 35115720Smarkm#include <stdio.h> 36115720Smarkm#include <stdlib.h> 37115720Smarkm 38115720Smarkm/* 39115720Smarkm * TODO: Add some general RNG tests (cf. the famous "diehard" tests?). 40115720Smarkm */ 41115720Smarkm 42115720SmarkmATF_TC(random_same); 43115720SmarkmATF_TC_HEAD(random_same, tc) 44115720Smarkm{ 45115720Smarkm atf_tc_set_md_var(tc, "descr", 46115720Smarkm "Test that random(3) does not always return the same " 47115720Smarkm "value when the seed is initialized to zero"); 48115720Smarkm} 49115720Smarkm 50115720Smarkm#define MAX_ITER 10 51115720Smarkm 52115720SmarkmATF_TC_BODY(random_same, tc) 53115720Smarkm{ 54115720Smarkm long buf[MAX_ITER]; 55115720Smarkm size_t i, j; 56115720Smarkm 57115720Smarkm /* 58115720Smarkm * See CVE-2012-1577. 59115720Smarkm */ 60115720Smarkm srandom(0); 61115720Smarkm 62115720Smarkm for (i = 0; i < __arraycount(buf); i++) { 63115720Smarkm 64115720Smarkm buf[i] = random(); 65115720Smarkm 66115720Smarkm for (j = 0; j < i; j++) { 67115720Smarkm 68115720Smarkm (void)fprintf(stderr, "i = %zu, j = %zu: " 69115720Smarkm "%ld vs. %ld\n", i, j, buf[i], buf[j]); 70115720Smarkm 71115720Smarkm ATF_CHECK(buf[i] != buf[j]); 72115720Smarkm } 73115720Smarkm } 74115720Smarkm} 75115720Smarkm 76115720SmarkmATF_TP_ADD_TCS(tp) 77115720Smarkm{ 78115720Smarkm 79115720Smarkm ATF_TP_ADD_TC(tp, random_same); 80115720Smarkm 81115720Smarkm return atf_no_error(); 82115720Smarkm} 83115720Smarkm