t_sethostname.c revision 276478
11553Srgrimes/* $NetBSD: t_sethostname.c,v 1.3 2012/03/25 08:17:54 joerg Exp $ */
21553Srgrimes
31553Srgrimes/*-
41553Srgrimes * Copyright (c) 2011 The NetBSD Foundation, Inc.
51553Srgrimes * All rights reserved.
61553Srgrimes *
71553Srgrimes * This code is derived from software contributed to The NetBSD Foundation
81553Srgrimes * by Jukka Ruohonen.
91553Srgrimes *
101553Srgrimes * Redistribution and use in source and binary forms, with or without
111553Srgrimes * modification, are permitted provided that the following conditions
121553Srgrimes * are met:
131553Srgrimes * 1. Redistributions of source code must retain the above copyright
141553Srgrimes *    notice, this list of conditions and the following disclaimer.
151553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161553Srgrimes *    notice, this list of conditions and the following disclaimer in the
171553Srgrimes *    documentation and/or other materials provided with the distribution.
181553Srgrimes *
191553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201553Srgrimes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211553Srgrimes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221553Srgrimes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231553Srgrimes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241553Srgrimes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251553Srgrimes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261553Srgrimes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271553Srgrimes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281553Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291553Srgrimes * POSSIBILITY OF SUCH DAMAGE.
301553Srgrimes */
3130642Scharnier#include <sys/cdefs.h>
321553Srgrimes__RCSID("$NetBSD: t_sethostname.c,v 1.3 2012/03/25 08:17:54 joerg Exp $");
3330642Scharnier
3430642Scharnier#include <sys/param.h>
3550479Speter
361553Srgrimes#include <atf-c.h>
371553Srgrimes#include <errno.h>
381553Srgrimes#include <string.h>
391553Srgrimes#include <unistd.h>
401553Srgrimes
411553Srgrimesstatic char host[MAXHOSTNAMELEN];
421553Srgrimes
431553Srgrimesstatic const char hosts[][MAXHOSTNAMELEN] = {
441553Srgrimes	"1234567890",
451553Srgrimes	"abcdefghijklmnopqrst",
461553Srgrimes	"!#\xa4%&/(..xasS812=!=!(I(!;X;;X.as.dasa=?;,..<>|**^\xa8",
47246209Scharnier	"--------------------------------------------------------------------"
481553Srgrimes};
491553Srgrimes
501553SrgrimesATF_TC_WITH_CLEANUP(sethostname_basic);
511553SrgrimesATF_TC_HEAD(sethostname_basic, tc)
521553Srgrimes{
531553Srgrimes	atf_tc_set_md_var(tc, "descr", "A basic test of sethostname(3)");
541553Srgrimes	atf_tc_set_md_var(tc, "require.user", "root");
551553Srgrimes}
561553Srgrimes
571553SrgrimesATF_TC_BODY(sethostname_basic, tc)
581553Srgrimes{
591553Srgrimes	char name[MAXHOSTNAMELEN];
601553Srgrimes	size_t i;
611553Srgrimes
621553Srgrimes	atf_tc_skip("screws up the test host's hostname on FreeBSD");
631553Srgrimes
641553Srgrimes	for (i = 0; i < __arraycount(hosts); i++) {
651553Srgrimes
661553Srgrimes		(void)memset(name, 0, sizeof(name));
671553Srgrimes
681553Srgrimes#ifdef __FreeBSD__
691553Srgrimes		/*
701553Srgrimes		 * Sanity checks to ensure that the wrong invariant isn't being
711553Srgrimes		 * tested for per PR # 181127
721553Srgrimes		 */
731553Srgrimes		ATF_REQUIRE_EQ(sizeof(hosts[i]), MAXHOSTNAMELEN);
741553Srgrimes		ATF_REQUIRE_EQ(sizeof(name), MAXHOSTNAMELEN);
751553Srgrimes
761553Srgrimes		ATF_REQUIRE(sethostname(hosts[i], sizeof(hosts[i]) - 1) == 0);
771553Srgrimes		ATF_REQUIRE(gethostname(name, sizeof(name) - 1) == 0);
7830830Scharnier#else
791553Srgrimes		ATF_REQUIRE(sethostname(hosts[i], sizeof(hosts[i])) == 0);
801553Srgrimes		ATF_REQUIRE(gethostname(name, sizeof(name)) == 0);
811553Srgrimes#endif
821553Srgrimes		ATF_REQUIRE(strcmp(hosts[i], name) == 0);
831553Srgrimes	}
841553Srgrimes
851553Srgrimes	(void)sethostname(host, sizeof(host));
861553Srgrimes}
871553Srgrimes
881553SrgrimesATF_TC_CLEANUP(sethostname_basic, tc)
891553Srgrimes{
901553Srgrimes	(void)sethostname(host, sizeof(host));
911553Srgrimes}
921553Srgrimes
931553SrgrimesATF_TC_WITH_CLEANUP(sethostname_limit);
941553SrgrimesATF_TC_HEAD(sethostname_limit, tc)
951553Srgrimes{
961553Srgrimes	atf_tc_set_md_var(tc, "descr", "Too long host name errors out?");
971553Srgrimes	atf_tc_set_md_var(tc, "require.user", "root");
981553Srgrimes}
991553Srgrimes
1001553SrgrimesATF_TC_BODY(sethostname_limit, tc)
1011553Srgrimes{
1021553Srgrimes	char name[MAXHOSTNAMELEN + 1];
1031553Srgrimes
1041553Srgrimes	(void)memset(name, 0, sizeof(name));
1051553Srgrimes
1061553Srgrimes	ATF_REQUIRE(sethostname(name, sizeof(name)) == -1);
1071553Srgrimes}
1081553Srgrimes
1091553SrgrimesATF_TC_CLEANUP(sethostname_limit, tc)
1101553Srgrimes{
1111553Srgrimes#ifdef __FreeBSD__
1121553Srgrimes	ATF_REQUIRE(sethostname(host, MAXHOSTNAMELEN - 1 ) == 0);
1131553Srgrimes	ATF_REQUIRE(sethostname(host, MAXHOSTNAMELEN) == -1);
1141553Srgrimes#endif
1151553Srgrimes	(void)sethostname(host, sizeof(host));
1161553Srgrimes}
1171553Srgrimes
1181553SrgrimesATF_TC_WITH_CLEANUP(sethostname_perm);
1191553SrgrimesATF_TC_HEAD(sethostname_perm, tc)
1201553Srgrimes{
1218857Srgrimes	atf_tc_set_md_var(tc, "descr", "Can normal user set the host name?");
1221553Srgrimes	atf_tc_set_md_var(tc, "require.user", "unprivileged");
1231553Srgrimes}
1241553Srgrimes
1251553SrgrimesATF_TC_BODY(sethostname_perm, tc)
1261553Srgrimes{
1271553Srgrimes
1281553Srgrimes	errno = 0;
1291553Srgrimes
1301553Srgrimes	ATF_REQUIRE_ERRNO(EPERM, sethostname(host, sizeof(host)) == -1);
1311553Srgrimes}
1321553Srgrimes
1331553SrgrimesATF_TC_CLEANUP(sethostname_perm, tc)
1341553Srgrimes{
1351553Srgrimes	(void)sethostname(host, sizeof(host));
1361553Srgrimes}
13730830Scharnier
1381553SrgrimesATF_TP_ADD_TCS(tp)
1391553Srgrimes{
1401553Srgrimes
1411553Srgrimes	(void)memset(host, 0, sizeof(host));
1421553Srgrimes
1431553Srgrimes	ATF_REQUIRE(gethostname(host, sizeof(host)) == 0);
1441553Srgrimes
1451553Srgrimes	ATF_TP_ADD_TC(tp, sethostname_basic);
1461553Srgrimes	ATF_TP_ADD_TC(tp, sethostname_limit);
1471553Srgrimes	ATF_TP_ADD_TC(tp, sethostname_perm);
1481553Srgrimes
1491553Srgrimes	return atf_no_error();
1501553Srgrimes}
1511553Srgrimes