ustat02.c revision 2c28215423293e443469a07ae7011135d058b671
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.    */
82extern int Tst_count;		/* Test Case counter for tst_* routines */
83
84static int exp_enos[] = { EINVAL, EFAULT, 0 };
85
86static struct test_case_t {
87	char *err_desc;		/*error description */
88	int exp_errno;		/* expected error number */
89	char *exp_errval;	/*Expected errorvalue string */
90} testcase[] = {
91	{
92	"Invalid parameter", EINVAL, "EINVAL"},
93#ifndef UCLINUX
94	    /* Skip since uClinux does not implement memory protection */
95	{
96	"Bad address", EFAULT, "EFAULT"}
97#endif
98};
99
100int TST_TOTAL = sizeof(testcase) / sizeof(*testcase);	/* Total number of test cases. */
101
102dev_t dev_num[2];
103struct ustat *ubuf;
104struct stat *buf;
105
106int main(int ac, char **av)
107{
108
109	int lc, i;		/* loop counter */
110	char *msg;		/* message returned from parse_opts */
111
112	/* parse standard options */
113<<<<<<< HEAD
114	if ((msg = parse_opts(ac, av, NULL, NULL))
115	    != NULL) {
116		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
117=======
118	if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL))
119	    != NULL) {
120		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
121>>>>>>> master
122	}
123
124	setup();
125
126	for (lc = 0; TEST_LOOPING(lc); lc++) {
127
128		Tst_count = 0;
129
130		for (i = 0; i < TST_TOTAL; i++) {
131			if (i == 0) {
132				TEST(ustat(dev_num[i], ubuf));
133			} else {
134				TEST(ustat(dev_num[i], (struct ustat *)-1));
135			}
136
137			if ((TEST_RETURN == -1) && (TEST_ERRNO == testcase[i].
138						    exp_errno)) {
139				tst_resm(TPASS, "ustat(2) expected failure;"
140					 " Got errno - %s : %s",
141					 testcase[i].exp_errval,
142					 testcase[i].err_desc);
143			} else {
144				tst_resm(TFAIL, "ustat(2) failed to produce"
145					 " expected error; %d, errno"
146					 ": %s and got %d",
147					 testcase[i].exp_errno,
148					 testcase[i].exp_errval, TEST_ERRNO);
149			}
150
151			TEST_ERROR_LOG(TEST_ERRNO);
152		}		/*End of TEST LOOPS */
153	}
154
155	/*Clean up and exit */
156	cleanup();
157
158	tst_exit();
159}				/*End of main */
160
161/* setup() - performs all ONE TIME setup for this test */
162void setup()
163{
164
165	tst_sig(NOFORK, DEF_HANDLER, cleanup);
166
167	/* set the expected errnos... */
168	TEST_EXP_ENOS(exp_enos);
169
170	TEST_PAUSE;
171
172	dev_num[0] = -1;
173
174	/* Allocating memory for ustat and stat structure variables */
175	if ((ubuf = (struct ustat *)malloc(sizeof(struct ustat))) == NULL) {
176		tst_brkm(TBROK, NULL, "Failed to allocate Memory");
177	}
178
179	if ((buf = (struct stat *)malloc(sizeof(struct stat))) == NULL) {
180		free(ubuf);
181		tst_brkm(TBROK, NULL, "Failed to allocate Memory");
182	}
183
184	/* Finding out a valid device number */
185	if (stat("/", buf) != 0) {
186		free(buf);
187		free(ubuf);
188		tst_brkm(TBROK, NULL, "stat(2) failed. Exiting without"
189			 "invoking ustat(2)");
190	}
191	dev_num[1] = buf->st_dev;
192}
193
194/*
195* cleanup() - Performs one time cleanup for this test at
196* completion or premature exit
197*/
198void cleanup()
199{
200	free(ubuf);
201	free(buf);
202	/*
203	 * print timing stats if that option was specified.
204	 * print errno log if that option was specified.
205	 */
206	TEST_CLEANUP;
207
208}