Home | History | Annotate | Line # | Download | only in gen
t_sethostname.c revision 1.2.2.2
      1  1.2.2.2  cherry /* $NetBSD: t_sethostname.c,v 1.2.2.2 2011/06/23 14:20:39 cherry Exp $ */
      2  1.2.2.2  cherry 
      3  1.2.2.2  cherry /*-
      4  1.2.2.2  cherry  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  1.2.2.2  cherry  * All rights reserved.
      6  1.2.2.2  cherry  *
      7  1.2.2.2  cherry  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.2.2  cherry  * by Jukka Ruohonen.
      9  1.2.2.2  cherry  *
     10  1.2.2.2  cherry  * Redistribution and use in source and binary forms, with or without
     11  1.2.2.2  cherry  * modification, are permitted provided that the following conditions
     12  1.2.2.2  cherry  * are met:
     13  1.2.2.2  cherry  * 1. Redistributions of source code must retain the above copyright
     14  1.2.2.2  cherry  *    notice, this list of conditions and the following disclaimer.
     15  1.2.2.2  cherry  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.2.2  cherry  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.2.2  cherry  *    documentation and/or other materials provided with the distribution.
     18  1.2.2.2  cherry  *
     19  1.2.2.2  cherry  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2.2.2  cherry  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.2.2  cherry  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.2.2  cherry  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2.2.2  cherry  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2.2.2  cherry  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2.2.2  cherry  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2.2.2  cherry  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2.2.2  cherry  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2.2.2  cherry  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2.2.2  cherry  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2.2.2  cherry  */
     31  1.2.2.2  cherry #include <sys/cdefs.h>
     32  1.2.2.2  cherry __RCSID("$NetBSD: t_sethostname.c,v 1.2.2.2 2011/06/23 14:20:39 cherry Exp $");
     33  1.2.2.2  cherry 
     34  1.2.2.2  cherry #include <sys/param.h>
     35  1.2.2.2  cherry 
     36  1.2.2.2  cherry #include <atf-c.h>
     37  1.2.2.2  cherry #include <errno.h>
     38  1.2.2.2  cherry #include <string.h>
     39  1.2.2.2  cherry #include <unistd.h>
     40  1.2.2.2  cherry 
     41  1.2.2.2  cherry static char host[MAXHOSTNAMELEN];
     42  1.2.2.2  cherry 
     43  1.2.2.2  cherry static const char hosts[][MAXHOSTNAMELEN] = {
     44  1.2.2.2  cherry 	"1234567890",
     45  1.2.2.2  cherry 	"abcdefghijklmnopqrst",
     46  1.2.2.2  cherry 	"!#%&/(..xasS812=!=!(I(!;X;;X.as.dasa=?;,..<>|**^",
     47  1.2.2.2  cherry 	"--------------------------------------------------------------------"
     48  1.2.2.2  cherry };
     49  1.2.2.2  cherry 
     50  1.2.2.2  cherry ATF_TC_WITH_CLEANUP(sethostname_basic);
     51  1.2.2.2  cherry ATF_TC_HEAD(sethostname_basic, tc)
     52  1.2.2.2  cherry {
     53  1.2.2.2  cherry 	atf_tc_set_md_var(tc, "descr", "A basic test of sethostname(3)");
     54  1.2.2.2  cherry 	atf_tc_set_md_var(tc, "require.user", "root");
     55  1.2.2.2  cherry }
     56  1.2.2.2  cherry 
     57  1.2.2.2  cherry ATF_TC_BODY(sethostname_basic, tc)
     58  1.2.2.2  cherry {
     59  1.2.2.2  cherry 	char name[MAXHOSTNAMELEN];
     60  1.2.2.2  cherry 	size_t i;
     61  1.2.2.2  cherry 
     62  1.2.2.2  cherry 	for (i = 0; i < __arraycount(hosts); i++) {
     63  1.2.2.2  cherry 
     64  1.2.2.2  cherry 		(void)memset(name, 0, sizeof(name));
     65  1.2.2.2  cherry 
     66  1.2.2.2  cherry 		ATF_REQUIRE(sethostname(hosts[i], sizeof(hosts[i])) == 0);
     67  1.2.2.2  cherry 		ATF_REQUIRE(gethostname(name, sizeof(name)) == 0);
     68  1.2.2.2  cherry 		ATF_REQUIRE(strcmp(hosts[i], name) == 0);
     69  1.2.2.2  cherry 	}
     70  1.2.2.2  cherry 
     71  1.2.2.2  cherry 	(void)sethostname(host, sizeof(host));
     72  1.2.2.2  cherry }
     73  1.2.2.2  cherry 
     74  1.2.2.2  cherry ATF_TC_CLEANUP(sethostname_basic, tc)
     75  1.2.2.2  cherry {
     76  1.2.2.2  cherry 	(void)sethostname(host, sizeof(host));
     77  1.2.2.2  cherry }
     78  1.2.2.2  cherry 
     79  1.2.2.2  cherry ATF_TC_WITH_CLEANUP(sethostname_limit);
     80  1.2.2.2  cherry ATF_TC_HEAD(sethostname_limit, tc)
     81  1.2.2.2  cherry {
     82  1.2.2.2  cherry 	atf_tc_set_md_var(tc, "descr", "Too long host name errors out?");
     83  1.2.2.2  cherry 	atf_tc_set_md_var(tc, "require.user", "root");
     84  1.2.2.2  cherry }
     85  1.2.2.2  cherry 
     86  1.2.2.2  cherry ATF_TC_BODY(sethostname_limit, tc)
     87  1.2.2.2  cherry {
     88  1.2.2.2  cherry 	char name[MAXHOSTNAMELEN + 1];
     89  1.2.2.2  cherry 
     90  1.2.2.2  cherry 	(void)memset(name, 0, sizeof(name));
     91  1.2.2.2  cherry 
     92  1.2.2.2  cherry 	ATF_REQUIRE(sethostname(name, sizeof(name)) == -1);
     93  1.2.2.2  cherry }
     94  1.2.2.2  cherry 
     95  1.2.2.2  cherry ATF_TC_CLEANUP(sethostname_limit, tc)
     96  1.2.2.2  cherry {
     97  1.2.2.2  cherry 	(void)sethostname(host, sizeof(host));
     98  1.2.2.2  cherry }
     99  1.2.2.2  cherry 
    100  1.2.2.2  cherry ATF_TC_WITH_CLEANUP(sethostname_perm);
    101  1.2.2.2  cherry ATF_TC_HEAD(sethostname_perm, tc)
    102  1.2.2.2  cherry {
    103  1.2.2.2  cherry 	atf_tc_set_md_var(tc, "descr", "Can normal user set the host name?");
    104  1.2.2.2  cherry 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
    105  1.2.2.2  cherry }
    106  1.2.2.2  cherry 
    107  1.2.2.2  cherry ATF_TC_BODY(sethostname_perm, tc)
    108  1.2.2.2  cherry {
    109  1.2.2.2  cherry 
    110  1.2.2.2  cherry 	errno = 0;
    111  1.2.2.2  cherry 
    112  1.2.2.2  cherry 	ATF_REQUIRE_ERRNO(EPERM, sethostname(host, sizeof(host)) == -1);
    113  1.2.2.2  cherry }
    114  1.2.2.2  cherry 
    115  1.2.2.2  cherry ATF_TC_CLEANUP(sethostname_perm, tc)
    116  1.2.2.2  cherry {
    117  1.2.2.2  cherry 	(void)sethostname(host, sizeof(host));
    118  1.2.2.2  cherry }
    119  1.2.2.2  cherry 
    120  1.2.2.2  cherry ATF_TP_ADD_TCS(tp)
    121  1.2.2.2  cherry {
    122  1.2.2.2  cherry 
    123  1.2.2.2  cherry 	(void)memset(host, 0, sizeof(host));
    124  1.2.2.2  cherry 
    125  1.2.2.2  cherry 	ATF_REQUIRE(gethostname(host, sizeof(host)) == 0);
    126  1.2.2.2  cherry 
    127  1.2.2.2  cherry 	ATF_TP_ADD_TC(tp, sethostname_basic);
    128  1.2.2.2  cherry 	ATF_TP_ADD_TC(tp, sethostname_limit);
    129  1.2.2.2  cherry 	ATF_TP_ADD_TC(tp, sethostname_perm);
    130  1.2.2.2  cherry 
    131  1.2.2.2  cherry 	return atf_no_error();
    132  1.2.2.2  cherry }
    133