Home | History | Annotate | Download | only in setdomainname
      1 /*
      2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
      3  *
      4  * This program is free software; you can redistribute it and/or modify it
      5  * under the terms of version 2 of the GNU General Public License as
      6  * published by the Free Software Foundation.
      7  *
      8  * This program is distributed in the hope that it would be useful, but
      9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     11  *
     12  * You should have received a copy of the GNU General Public License
     13  * along with this program;  if not, write to the Free Software
     14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     15  *
     16  */
     17  /*******************************************************************
     18  *
     19  *    TEST IDENTIFIER   : setdomainname02
     20  *
     21  *    EXECUTED BY       : root / superuser
     22  *
     23  *    TEST TITLE        : test for checking error conditions for setdomainame(2)
     24  *
     25  *    TEST CASE TOTAL   : 3
     26  *
     27  *    AUTHOR            : Saji Kumar.V.R <saji.kumar (at) wipro.com>
     28  *
     29  *    SIGNALS
     30  *      Uses SIGUSR1 to pause before test if option set.
     31  *      (See the parse_opts(3) man page).
     32  *
     33  * DESCRIPTION
     34  * 	Verify that,
     35  *   1) setdomainname(2) returns -1 and sets errno to EINVAL if the parameter,
     36  *	len is less than zero
     37  *   2) setdomainname(2) returns -1 and sets errno to EINVAL if value of
     38  *	len is greater than the maximum allowed value
     39  *   3) setdomainname(2) returns -1 and sets errno to EFAULT for a bad address
     40  *	for name
     41  *
     42  * ALGORITHM
     43  * Setup:
     44  *   Setup signal handling.
     45  *   Pause for SIGUSR1 if option specified.
     46  *   Save current domainname
     47  *
     48  *  Test:
     49  *   Loop if the proper options are given.
     50  *   Execute system call
     51  *   Check return code, if (system call failed (return=-1)) &
     52  *			   (errno set == expected errno)
     53  *              Issue sys call fails with expected return value and errno.
     54  *   Otherwise,
     55  *      Issue sys call returns unexpected value.
     56  *
     57  *  Cleanup:
     58  *        Restore old domain name.
     59  *        Print errno log and/or timing stats if options given
     60  *  Side Effects :
     61  *	 setdomainname() is resetting value to NULL, if an invalid address
     62  *	 is given for name. So, to overcome this problem, domainname is
     63  *	 resetting to original value as part of cleanup() routine.
     64  *
     65  * USAGE:  <for command-line>
     66  *  setdomainname02  [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
     67  *		where,  -c n : Run n copies concurrently.
     68  *			-e   : Turn on errno logging.
     69  *			-h   : Show help screen
     70  *			-f   : Turn off functional testing
     71  *			-i n : Execute test n times.
     72  *			-I x : Execute test for x seconds.
     73  *			-p   : Pause for SIGUSR1 before starting
     74  *			-P x : Pause for x seconds between iterations.
     75  *			-t   : Turn on syscall timing.
     76  *
     77  *********************************************************************/
     78 
     79 #include "test.h"
     80 
     81 #include <errno.h>
     82 #include <sys/utsname.h>
     83 
     84 #define MAX_NAME_LEN _UTSNAME_DOMAIN_LENGTH - 1
     85 
     86 static void cleanup(void);
     87 static void setup(void);
     88 
     89 char *TCID = "setdomainname02";
     90 int TST_TOTAL = 3;
     91 
     92 static char old_domain_name[MAX_NAME_LEN];
     93 static struct test_case_t {
     94 	char *desc;
     95 	char *name;
     96 	int len;
     97 	int exp_errno;
     98 	char err_desc[10];
     99 } test_cases[] = {
    100 	{
    101 	"test with len = -1", "test_dom", -1, EINVAL, "EINVAL"}, {
    102 	"test with len > allowed maximum", "test_dom", MAX_NAME_LEN + 1,
    103 		    EINVAL, "EINVAL"}, {
    104 "test with name = NULL", NULL, MAX_NAME_LEN, EFAULT, "EFAULT"},};
    105 
    106 int main(int ac, char **av)
    107 {
    108 	int lc, ind;
    109 
    110 	tst_parse_opts(ac, av, NULL, NULL);
    111 
    112 	setup();		/* global setup */
    113 
    114 	/* The following loop checks looping state if -i option given */
    115 
    116 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    117 		/* reset tst_count in case we are looping */
    118 		tst_count = 0;
    119 
    120 		for (ind = 0; ind < TST_TOTAL; ind++) {
    121 
    122 			/*
    123 			 * call the system call with the TEST() macro
    124 			 */
    125 			TEST(setdomainname(test_cases[ind].name,
    126 					   (size_t) test_cases[ind].len));
    127 
    128 			if ((TEST_RETURN == -1) &&
    129 			    (TEST_ERRNO == test_cases[ind].exp_errno)) {
    130 				tst_resm(TPASS, "expected failure; Got %s",
    131 					 test_cases[ind].err_desc);
    132 			} else {
    133 				tst_resm(TFAIL, "Call failed to produce "
    134 					 "expected error;  Expected errno: %d "
    135 					 "Got : %d, %s",
    136 					 test_cases[ind].exp_errno,
    137 					 TEST_ERRNO, strerror(TEST_ERRNO));
    138 			}
    139 		}
    140 	}
    141 
    142 	cleanup();
    143 
    144 	tst_exit();
    145 
    146 }
    147 
    148 /*
    149  * setup() - performs all the ONE TIME setup for this test.
    150  */
    151 void setup(void)
    152 {
    153 	tst_require_root();
    154 
    155 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    156 
    157 	/* Save current domainname */
    158 	if ((getdomainname(old_domain_name, MAX_NAME_LEN)) < 0) {
    159 		tst_brkm(TBROK, NULL, "getdomainname() failed while"
    160 			 " getting current domain name");
    161 	}
    162 
    163 	TEST_PAUSE;
    164 
    165 }
    166 
    167 /*
    168  * cleanup() - performs all the ONE TIME cleanup for this test at completion
    169  * 	       or premature exit.
    170  */
    171 void cleanup(void)
    172 {
    173 
    174 	/* Restore domain name */
    175 	if ((setdomainname(old_domain_name, sizeof(old_domain_name)))
    176 	    < 0) {
    177 		tst_resm(TWARN, "setdomainname() failed while restoring"
    178 			 " domainname to \"%s\"", old_domain_name);
    179 	}
    180 
    181 }
    182