11556Srgrimes/* $NetBSD: t_sethostname.c,v 1.2 2011/06/02 12:42:27 jruoho Exp $ */ 21556Srgrimes 31556Srgrimes/*- 41556Srgrimes * Copyright (c) 2011 The NetBSD Foundation, Inc. 51556Srgrimes * All rights reserved. 61556Srgrimes * 71556Srgrimes * This code is derived from software contributed to The NetBSD Foundation 81556Srgrimes * by Jukka Ruohonen. 91556Srgrimes * 101556Srgrimes * Redistribution and use in source and binary forms, with or without 111556Srgrimes * modification, are permitted provided that the following conditions 121556Srgrimes * are met: 131556Srgrimes * 1. Redistributions of source code must retain the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer. 151556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161556Srgrimes * notice, this list of conditions and the following disclaimer in the 171556Srgrimes * documentation and/or other materials provided with the distribution. 181556Srgrimes * 191556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201556Srgrimes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211556Srgrimes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221556Srgrimes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231556Srgrimes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241556Srgrimes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251556Srgrimes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261556Srgrimes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271556Srgrimes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281556Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291556Srgrimes * POSSIBILITY OF SUCH DAMAGE. 301556Srgrimes */ 311556Srgrimes#include <sys/cdefs.h> 321556Srgrimes__RCSID("$NetBSD: t_sethostname.c,v 1.2 2011/06/02 12:42:27 jruoho Exp $"); 331556Srgrimes 3436150Scharnier#include <sys/param.h> 3536150Scharnier 3636150Scharnier#include <atf-c.h> 371556Srgrimes#include <errno.h> 3899110Sobrien#include <string.h> 3999110Sobrien#include <unistd.h> 401556Srgrimes 411556Srgrimesstatic char host[MAXHOSTNAMELEN]; 4246684Skris 431556Srgrimesstatic const char hosts[][MAXHOSTNAMELEN] = { 441556Srgrimes "1234567890", 4517987Speter "abcdefghijklmnopqrst", 4617987Speter "!#\xa4%&/(..xasS812=!=!(I(!;X;;X.as.dasa=?;,..<>|**^\xa8", 4717987Speter "--------------------------------------------------------------------" 4817987Speter}; 4917987Speter 5017987SpeterATF_TC_WITH_CLEANUP(sethostname_basic); 5118016SpeterATF_TC_HEAD(sethostname_basic, tc) 52104282Smux{ 5318018Speter atf_tc_set_md_var(tc, "descr", "A basic test of sethostname(3)"); 5438536Scracauer atf_tc_set_md_var(tc, "require.user", "root"); 5529983Smsmith} 5617987Speter 571556SrgrimesATF_TC_BODY(sethostname_basic, tc) 581556Srgrimes{ 591556Srgrimes char name[MAXHOSTNAMELEN]; 601556Srgrimes size_t i; 611556Srgrimes 621556Srgrimes for (i = 0; i < __arraycount(hosts); i++) { 631556Srgrimes 641556Srgrimes (void)memset(name, 0, sizeof(name)); 651556Srgrimes 661556Srgrimes ATF_REQUIRE(sethostname(hosts[i], sizeof(hosts[i])) == 0); 67149018Sstefanf ATF_REQUIRE(gethostname(name, sizeof(name)) == 0); 68149018Sstefanf ATF_REQUIRE(strcmp(hosts[i], name) == 0); 69149018Sstefanf } 70149018Sstefanf 711556Srgrimes (void)sethostname(host, sizeof(host)); 7250394Stg} 7350394Stg 741556SrgrimesATF_TC_CLEANUP(sethostname_basic, tc) 751556Srgrimes{ 761556Srgrimes (void)sethostname(host, sizeof(host)); 771556Srgrimes} 7817987Speter 7990111SimpATF_TC_WITH_CLEANUP(sethostname_limit); 8017987SpeterATF_TC_HEAD(sethostname_limit, tc) 811556Srgrimes{ 821556Srgrimes atf_tc_set_md_var(tc, "descr", "Too long host name errors out?"); 831556Srgrimes atf_tc_set_md_var(tc, "require.user", "root"); 8450394Stg} 851556Srgrimes 861556SrgrimesATF_TC_BODY(sethostname_limit, tc) 871556Srgrimes{ 881556Srgrimes char name[MAXHOSTNAMELEN + 1]; 891556Srgrimes 901556Srgrimes (void)memset(name, 0, sizeof(name)); 9129983Smsmith 9229983Smsmith ATF_REQUIRE(sethostname(name, sizeof(name)) == -1); 9329983Smsmith} 9429983Smsmith 9529983SmsmithATF_TC_CLEANUP(sethostname_limit, tc) 961556Srgrimes{ 9750394Stg (void)sethostname(host, sizeof(host)); 981556Srgrimes} 9929983Smsmith 10029983SmsmithATF_TC_WITH_CLEANUP(sethostname_perm); 10150394StgATF_TC_HEAD(sethostname_perm, tc) 10229983Smsmith{ 10329983Smsmith atf_tc_set_md_var(tc, "descr", "Can normal user set the host name?"); 10459436Scracauer atf_tc_set_md_var(tc, "require.user", "unprivileged"); 10529983Smsmith} 10629983Smsmith 10729983SmsmithATF_TC_BODY(sethostname_perm, tc) 10850394Stg{ 10950394Stg 11050394Stg errno = 0; 11129983Smsmith 11259436Scracauer ATF_REQUIRE_ERRNO(EPERM, sethostname(host, sizeof(host)) == -1); 11359436Scracauer} 11429983Smsmith 11529983SmsmithATF_TC_CLEANUP(sethostname_perm, tc) 11629983Smsmith{ 11729983Smsmith (void)sethostname(host, sizeof(host)); 11829983Smsmith} 11929983Smsmith 12029983SmsmithATF_TP_ADD_TCS(tp) 12129983Smsmith{ 12229983Smsmith 12329983Smsmith (void)memset(host, 0, sizeof(host)); 12429983Smsmith 12529983Smsmith ATF_REQUIRE(gethostname(host, sizeof(host)) == 0); 12629983Smsmith 12729983Smsmith ATF_TP_ADD_TC(tp, sethostname_basic); 12829983Smsmith ATF_TP_ADD_TC(tp, sethostname_limit); 12929983Smsmith ATF_TP_ADD_TC(tp, sethostname_perm); 1301556Srgrimes 1311556Srgrimes return atf_no_error(); 1321556Srgrimes} 1331556Srgrimes