110341Syan/* $NetBSD: t_sysctl.c,v 1.1 2014/08/09 07:04:03 gson Exp $ */
212278Syan
310341Syan/*-
410341Syan * Copyright (c) 2014 The NetBSD Foundation, Inc.
510341Syan * All rights reserved.
610341Syan *
710341Syan * Redistribution and use in source and binary forms, with or without
810341Syan * modification, are permitted provided that the following conditions
910341Syan * are met:
1010341Syan * 1. Redistributions of source code must retain the above copyright
1110341Syan *    notice, this list of conditions and the following disclaimer.
1210341Syan * 2. Redistributions in binary form must reproduce the above copyright
1310341Syan *    notice, this list of conditions and the following disclaimer in the
1410341Syan *    documentation and/or other materials provided with the distribution.
1510341Syan *
1610341Syan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1710341Syan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1810341Syan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1910341Syan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2010341Syan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2110341Syan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2210341Syan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2310341Syan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2410341Syan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2510341Syan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2610341Syan * POSSIBILITY OF SUCH DAMAGE.
2710341Syan */
2810341Syan
2910341Syan#include <sys/cdefs.h>
3010341Syan__COPYRIGHT("@(#) Copyright (c) 2014\
3110341Syan The NetBSD Foundation, inc. All rights reserved.");
3210341Syan__RCSID("$NetBSD: t_sysctl.c,v 1.1 2014/08/09 07:04:03 gson Exp $");
3310341Syan
3410341Syan#include <sys/sysctl.h>
3510341Syan#include <errno.h>
3610341Syan#include <memory.h>
3710341Syan
3810341Syan#include <atf-c.h>
3910341Syan
4012278SyanATF_TC(bufsize);
4110341SyanATF_TC_HEAD(bufsize, tc)
4210341Syan{
4310341Syan	atf_tc_set_md_var(tc, "descr",
4410341Syan	        "Test sysctl integer reads with different buffer sizes");
4510341Syan}
4610341SyanATF_TC_BODY(bufsize, tc)
4710341Syan{
4810341Syan	union {
4910341Syan		int int_val;
5010341Syan		unsigned char space[256];
5110341Syan	} buf;
5210341Syan	size_t len;
5310341Syan	for (len = 0; len < sizeof(buf); len++) {
5410341Syan		size_t oldlen = len;
5510341Syan		int r;
5610341Syan		memset(&buf, 0xFF, sizeof(buf));
5710341Syan		r = sysctlbyname("kern.job_control", &buf, &oldlen, 0, (size_t) 0);
5810341Syan		if (len < sizeof(int)) {
5910341Syan			ATF_REQUIRE_EQ(r, -1);
6010341Syan			ATF_REQUIRE_EQ(errno, ENOMEM);
6110341Syan		} else {
6210341Syan			ATF_REQUIRE_EQ(r, 0);
6310341Syan			ATF_REQUIRE_EQ(buf.int_val, 1);
6410341Syan			ATF_REQUIRE_EQ(oldlen, sizeof(int));
6510341Syan		}
6610341Syan	}
6710341Syan}
6810341Syan
6910341SyanATF_TP_ADD_TCS(tp)
7010341Syan{
7110341Syan	ATF_TP_ADD_TC(tp, bufsize);
7210341Syan
7310341Syan	return atf_no_error();
7410341Syan}
7510341Syan