157718be8SEnji Cooper /* $NetBSD: t_sethostname.c,v 1.3 2012/03/25 08:17:54 joerg Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper * by Jukka Ruohonen.
957718be8SEnji Cooper *
1057718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper * are met:
1357718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper *
1957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper */
3157718be8SEnji Cooper #include <sys/cdefs.h>
3257718be8SEnji Cooper __RCSID("$NetBSD: t_sethostname.c,v 1.3 2012/03/25 08:17:54 joerg Exp $");
3357718be8SEnji Cooper
3457718be8SEnji Cooper #include <sys/param.h>
3557718be8SEnji Cooper
3657718be8SEnji Cooper #include <atf-c.h>
3757718be8SEnji Cooper #include <errno.h>
3857718be8SEnji Cooper #include <string.h>
3957718be8SEnji Cooper #include <unistd.h>
4057718be8SEnji Cooper
4157718be8SEnji Cooper static char host[MAXHOSTNAMELEN];
4257718be8SEnji Cooper
4357718be8SEnji Cooper static const char hosts[][MAXHOSTNAMELEN] = {
4457718be8SEnji Cooper "1234567890",
4557718be8SEnji Cooper "abcdefghijklmnopqrst",
4657718be8SEnji Cooper "!#\xa4%&/(..xasS812=!=!(I(!;X;;X.as.dasa=?;,..<>|**^\xa8",
4757718be8SEnji Cooper "--------------------------------------------------------------------"
4857718be8SEnji Cooper };
4957718be8SEnji Cooper
5057718be8SEnji Cooper ATF_TC_WITH_CLEANUP(sethostname_basic);
ATF_TC_HEAD(sethostname_basic,tc)5157718be8SEnji Cooper ATF_TC_HEAD(sethostname_basic, tc)
5257718be8SEnji Cooper {
5357718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "A basic test of sethostname(3)");
5457718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "root");
5557718be8SEnji Cooper }
5657718be8SEnji Cooper
ATF_TC_BODY(sethostname_basic,tc)5757718be8SEnji Cooper ATF_TC_BODY(sethostname_basic, tc)
5857718be8SEnji Cooper {
5957718be8SEnji Cooper char name[MAXHOSTNAMELEN];
6057718be8SEnji Cooper size_t i;
6157718be8SEnji Cooper
62*8bd02ea5SEnji Cooper atf_tc_skip("screws up the test host's hostname on FreeBSD");
63*8bd02ea5SEnji Cooper
6457718be8SEnji Cooper for (i = 0; i < __arraycount(hosts); i++) {
6557718be8SEnji Cooper
6657718be8SEnji Cooper (void)memset(name, 0, sizeof(name));
6757718be8SEnji Cooper
68*8bd02ea5SEnji Cooper #ifdef __FreeBSD__
694cf97cc9SEnji Cooper /*
704cf97cc9SEnji Cooper * Sanity checks to ensure that the wrong invariant isn't being
714cf97cc9SEnji Cooper * tested for per PR # 181127
724cf97cc9SEnji Cooper */
734cf97cc9SEnji Cooper ATF_REQUIRE_EQ(sizeof(hosts[i]), MAXHOSTNAMELEN);
744cf97cc9SEnji Cooper ATF_REQUIRE_EQ(sizeof(name), MAXHOSTNAMELEN);
754cf97cc9SEnji Cooper
764cf97cc9SEnji Cooper ATF_REQUIRE(sethostname(hosts[i], sizeof(hosts[i]) - 1) == 0);
774cf97cc9SEnji Cooper ATF_REQUIRE(gethostname(name, sizeof(name) - 1) == 0);
784cf97cc9SEnji Cooper #else
7957718be8SEnji Cooper ATF_REQUIRE(sethostname(hosts[i], sizeof(hosts[i])) == 0);
8057718be8SEnji Cooper ATF_REQUIRE(gethostname(name, sizeof(name)) == 0);
814cf97cc9SEnji Cooper #endif
8257718be8SEnji Cooper ATF_REQUIRE(strcmp(hosts[i], name) == 0);
8357718be8SEnji Cooper }
8457718be8SEnji Cooper
8557718be8SEnji Cooper (void)sethostname(host, sizeof(host));
8657718be8SEnji Cooper }
8757718be8SEnji Cooper
ATF_TC_CLEANUP(sethostname_basic,tc)8857718be8SEnji Cooper ATF_TC_CLEANUP(sethostname_basic, tc)
8957718be8SEnji Cooper {
9057718be8SEnji Cooper (void)sethostname(host, sizeof(host));
9157718be8SEnji Cooper }
9257718be8SEnji Cooper
9357718be8SEnji Cooper ATF_TC_WITH_CLEANUP(sethostname_limit);
ATF_TC_HEAD(sethostname_limit,tc)9457718be8SEnji Cooper ATF_TC_HEAD(sethostname_limit, tc)
9557718be8SEnji Cooper {
9657718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Too long host name errors out?");
9757718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "root");
9857718be8SEnji Cooper }
9957718be8SEnji Cooper
ATF_TC_BODY(sethostname_limit,tc)10057718be8SEnji Cooper ATF_TC_BODY(sethostname_limit, tc)
10157718be8SEnji Cooper {
10257718be8SEnji Cooper char name[MAXHOSTNAMELEN + 1];
10357718be8SEnji Cooper
10457718be8SEnji Cooper (void)memset(name, 0, sizeof(name));
10557718be8SEnji Cooper
10657718be8SEnji Cooper ATF_REQUIRE(sethostname(name, sizeof(name)) == -1);
10757718be8SEnji Cooper }
10857718be8SEnji Cooper
ATF_TC_CLEANUP(sethostname_limit,tc)10957718be8SEnji Cooper ATF_TC_CLEANUP(sethostname_limit, tc)
11057718be8SEnji Cooper {
111*8bd02ea5SEnji Cooper #ifdef __FreeBSD__
1124cf97cc9SEnji Cooper ATF_REQUIRE(sethostname(host, MAXHOSTNAMELEN - 1 ) == 0);
1134cf97cc9SEnji Cooper ATF_REQUIRE(sethostname(host, MAXHOSTNAMELEN) == -1);
1144cf97cc9SEnji Cooper #endif
11557718be8SEnji Cooper (void)sethostname(host, sizeof(host));
11657718be8SEnji Cooper }
11757718be8SEnji Cooper
11857718be8SEnji Cooper ATF_TC_WITH_CLEANUP(sethostname_perm);
ATF_TC_HEAD(sethostname_perm,tc)11957718be8SEnji Cooper ATF_TC_HEAD(sethostname_perm, tc)
12057718be8SEnji Cooper {
12157718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Can normal user set the host name?");
12257718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "unprivileged");
12357718be8SEnji Cooper }
12457718be8SEnji Cooper
ATF_TC_BODY(sethostname_perm,tc)12557718be8SEnji Cooper ATF_TC_BODY(sethostname_perm, tc)
12657718be8SEnji Cooper {
12757718be8SEnji Cooper
12857718be8SEnji Cooper errno = 0;
12957718be8SEnji Cooper
13057718be8SEnji Cooper ATF_REQUIRE_ERRNO(EPERM, sethostname(host, sizeof(host)) == -1);
13157718be8SEnji Cooper }
13257718be8SEnji Cooper
ATF_TC_CLEANUP(sethostname_perm,tc)13357718be8SEnji Cooper ATF_TC_CLEANUP(sethostname_perm, tc)
13457718be8SEnji Cooper {
13557718be8SEnji Cooper (void)sethostname(host, sizeof(host));
13657718be8SEnji Cooper }
13757718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)13857718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
13957718be8SEnji Cooper {
14057718be8SEnji Cooper
14157718be8SEnji Cooper (void)memset(host, 0, sizeof(host));
14257718be8SEnji Cooper
14357718be8SEnji Cooper ATF_REQUIRE(gethostname(host, sizeof(host)) == 0);
14457718be8SEnji Cooper
14557718be8SEnji Cooper ATF_TP_ADD_TC(tp, sethostname_basic);
14657718be8SEnji Cooper ATF_TP_ADD_TC(tp, sethostname_limit);
14757718be8SEnji Cooper ATF_TP_ADD_TC(tp, sethostname_perm);
14857718be8SEnji Cooper
14957718be8SEnji Cooper return atf_no_error();
15057718be8SEnji Cooper }
151