ustat02.c revision 1e6f5a673655551de5734ff31ef48cd63b604e6d
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 along 13 * with this program; if not, write the Free Software Foundation, Inc., 59 14 * Temple Place - Suite 330, Boston MA 02111-1307, USA. 15 * 16 */ 17/************************************************************************** 18 * 19 * TEST IDENTIFIER : ustat02 20 * 21 * 22 * EXECUTED BY : Anyone 23 * 24 * TEST TITLE : Test checking for basic error conditions 25 * for ustat(2) 26 * 27 * TEST CASE TOTAL : 2 28 * $ 29 * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com> 30 * 31 * SIGNALS 32 * Uses SIGUSR1 to pause before test if option set. 33 * (See the parse_opts(3) man page). 34 * 35 * DESCRIPTION 36 * This test case checks whether ustat(2) system call returns 37 * appropriate error number for invalid 38 * dev_t parameter. Next, it checks for bad address paramater. 39 * 40 * Setup: 41 * Setup signal handling. 42 * Pause for SIGUSR1 if option specified. 43 * For testing error on invalid parameter, set dev_num to -1 44 * 45 * Test: 46 * Loop if the proper options are given. 47 * Execute system call with invaid flag parameter 48 * and then for invalid user 49 * Check return code, if system call fails with errno == expected errno 50 * Issue syscall passed with expected errno 51 * Otherwise, 52 * Issue syscall failed to produce expected errno 53 * 54 * Cleanup: 55 * Do cleanup for the test. 56 * $ 57 * USAGE: <for command-line> 58 * ustat02 [-c n] [-e] [-i n] [-I x] [-p x] [-t] [-h] [-f] [-p] 59 * where 60 * -c n: run n copies simultaneously 61 * -e : Turn on errno logging. 62 * -i n : Execute test n times. 63 * -I x : Execute test for x seconds. 64 * -p : Pause for SIGUSR1 before starting 65 * -P x : Pause for x seconds between iterations. 66 * -t : Turn on syscall timing. 67 * 68 *RESTRICTIONS: None 69 *****************************************************************************/ 70#include <errno.h> 71#include "test.h" 72#include "usctest.h" 73#include <sys/types.h> 74#include <unistd.h> /* libc[45] */ 75#include <ustat.h> /* glibc2 */ 76#include <sys/stat.h> 77 78static void setup(); 79static void cleanup(); 80 81char *TCID = "ustat02"; /* Test program identifier. */ 82 83static int exp_enos[] = { EINVAL, EFAULT, 0 }; 84 85static struct test_case_t { 86 char *err_desc; /*error description */ 87 int exp_errno; /* expected error number */ 88 char *exp_errval; /*Expected errorvalue string */ 89} testcase[] = { 90 { 91 "Invalid parameter", EINVAL, "EINVAL"}, 92#ifndef UCLINUX 93 /* Skip since uClinux does not implement memory protection */ 94 { 95 "Bad address", EFAULT, "EFAULT"} 96#endif 97}; 98 99int TST_TOTAL = sizeof(testcase) / sizeof(*testcase); /* Total number of test cases. */ 100 101dev_t dev_num[2]; 102struct ustat *ubuf; 103struct stat *buf; 104 105int main(int ac, char **av) 106{ 107 108 int lc, i; /* loop counter */ 109 char *msg; /* message returned from parse_opts */ 110 111 /* parse standard options */ 112 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 113 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 114 115 setup(); 116 117 for (lc = 0; TEST_LOOPING(lc); lc++) { 118 119 Tst_count = 0; 120 121 for (i = 0; i < TST_TOTAL; i++) { 122 if (i == 0) { 123 TEST(ustat(dev_num[i], ubuf)); 124 } else { 125 TEST(ustat(dev_num[i], (struct ustat *)-1)); 126 } 127 128 if ((TEST_RETURN == -1) && (TEST_ERRNO == testcase[i]. 129 exp_errno)) { 130 tst_resm(TPASS, "ustat(2) expected failure;" 131 " Got errno - %s : %s", 132 testcase[i].exp_errval, 133 testcase[i].err_desc); 134 } else { 135 tst_resm(TFAIL, "ustat(2) failed to produce" 136 " expected error; %d, errno" 137 ": %s and got %d", 138 testcase[i].exp_errno, 139 testcase[i].exp_errval, TEST_ERRNO); 140 } 141 142 TEST_ERROR_LOG(TEST_ERRNO); 143 } /*End of TEST LOOPS */ 144 } 145 146 /*Clean up and exit */ 147 cleanup(); 148 149 tst_exit(); 150} /*End of main */ 151 152/* setup() - performs all ONE TIME setup for this test */ 153void setup() 154{ 155 156 tst_sig(NOFORK, DEF_HANDLER, cleanup); 157 158 /* set the expected errnos... */ 159 TEST_EXP_ENOS(exp_enos); 160 161 TEST_PAUSE; 162 163 dev_num[0] = -1; 164 165 /* Allocating memory for ustat and stat structure variables */ 166 if ((ubuf = (struct ustat *)malloc(sizeof(struct ustat))) == NULL) { 167 tst_brkm(TBROK, NULL, "Failed to allocate Memory"); 168 } 169 170 if ((buf = (struct stat *)malloc(sizeof(struct stat))) == NULL) { 171 free(ubuf); 172 tst_brkm(TBROK, NULL, "Failed to allocate Memory"); 173 } 174 175 /* Finding out a valid device number */ 176 if (stat("/", buf) != 0) { 177 free(buf); 178 free(ubuf); 179 tst_brkm(TBROK, NULL, "stat(2) failed. Exiting without" 180 "invoking ustat(2)"); 181 } 182 dev_num[1] = buf->st_dev; 183} 184 185/* 186* cleanup() - Performs one time cleanup for this test at 187* completion or premature exit 188*/ 189void cleanup() 190{ 191 free(ubuf); 192 free(buf); 193 /* 194 * print timing stats if that option was specified. 195 * print errno log if that option was specified. 196 */ 197 TEST_CLEANUP; 198 199}