11.1Sjruoho/* $NetBSD: t_gettimeofday.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
21.1Sjruoho
31.1Sjruoho/*-
41.1Sjruoho * Copyright (c) 2011 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.1Sjruoho__RCSID("$NetBSD: t_gettimeofday.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
331.1Sjruoho
341.1Sjruoho#include <sys/time.h>
351.1Sjruoho
361.1Sjruoho#include <atf-c.h>
371.1Sjruoho#include <errno.h>
381.1Sjruoho#include <string.h>
391.1Sjruoho
401.1SjruohoATF_TC(gettimeofday_err);
411.1SjruohoATF_TC_HEAD(gettimeofday_err, tc)
421.1Sjruoho{
431.1Sjruoho	atf_tc_set_md_var(tc, "descr", "Test errors from gettimeofday(2)");
441.1Sjruoho}
451.1Sjruoho
461.1SjruohoATF_TC_BODY(gettimeofday_err, tc)
471.1Sjruoho{
481.1Sjruoho
491.1Sjruoho	errno = 0;
501.1Sjruoho
511.1Sjruoho	ATF_REQUIRE_ERRNO(EFAULT, gettimeofday((void *)-1, NULL) != 0);
521.1Sjruoho}
531.1Sjruoho
541.1SjruohoATF_TC(gettimeofday_mono);
551.1SjruohoATF_TC_HEAD(gettimeofday_mono, tc)
561.1Sjruoho{
571.1Sjruoho	atf_tc_set_md_var(tc, "descr", "Test monotonicity of gettimeofday(2)");
581.1Sjruoho}
591.1Sjruoho
601.1SjruohoATF_TC_BODY(gettimeofday_mono, tc)
611.1Sjruoho{
621.1Sjruoho	static const size_t maxiter = 100;
631.1Sjruoho	struct timeval tv1, tv2;
641.1Sjruoho	size_t i;
651.1Sjruoho
661.1Sjruoho	for (i = 0; i < maxiter; i++) {
671.1Sjruoho
681.1Sjruoho		(void)memset(&tv1, 0, sizeof(struct timeval));
691.1Sjruoho		(void)memset(&tv2, 0, sizeof(struct timeval));
701.1Sjruoho
711.1Sjruoho		ATF_REQUIRE(gettimeofday(&tv1, NULL) == 0);
721.1Sjruoho		ATF_REQUIRE(gettimeofday(&tv2, NULL) == 0);
731.1Sjruoho
741.1Sjruoho		if (timercmp(&tv2, &tv1, <) != 0)
751.1Sjruoho			atf_tc_fail("time went backwards");
761.1Sjruoho	}
771.1Sjruoho}
781.1Sjruoho
791.1SjruohoATF_TP_ADD_TCS(tp)
801.1Sjruoho{
811.1Sjruoho
821.1Sjruoho	ATF_TP_ADD_TC(tp, gettimeofday_err);
831.1Sjruoho	ATF_TP_ADD_TC(tp, gettimeofday_mono);
841.1Sjruoho
851.1Sjruoho	return atf_no_error();
861.1Sjruoho}
87