setreuid01.c revision 923b23ff1fd1b77bd895949f9a6b4508c6485f33
1/*
2 * Copyright (c) 2000 Silicon Graphics, Inc.  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 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like.  Any license provided herein, whether implied or
15 * otherwise, applies only to this software file.  Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA  94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
33/* $Id: setreuid01.c,v 1.6 2009/11/02 13:57:18 subrata_modak Exp $ */
34/**********************************************************
35 *
36 *    OS Test - Silicon Graphics, Inc.
37 *
38 *    TEST IDENTIFIER	: setreuid01
39 *
40 *    EXECUTED BY	: anyone
41 *
42 *    TEST TITLE	: Basic test for setreuid(2)
43 *
44 *    PARENT DOCUMENT	: usctpl01
45 *
46 *    TEST CASE TOTAL	: 5
47 *
48 *    WALL CLOCK TIME	: 1
49 *
50 *    CPU TYPES		: ALL
51 *
52 *    AUTHOR		: William Roske
53 *
54 *    CO-PILOT		: Dave Fenner
55 *
56 *    DATE STARTED	: 05/14/92
57 *
58 *    INITIAL RELEASE	: UNICOS 7.0
59 *
60 *    TEST CASES
61 *
62 *	1.) setreuid(2) returns...(See Description)
63 *
64 *    INPUT SPECIFICATIONS
65 *	The standard options for system call tests are accepted.
66 *	(See the parse_opts(3) man page).
67 *
68 *    OUTPUT SPECIFICATIONS
69 *
70 *    DURATION
71 *	Terminates - with frequency and infinite modes.
72 *
73 *    SIGNALS
74 *	Uses SIGUSR1 to pause before test if option set.
75 *	(See the parse_opts(3) man page).
76 *
77 *    RESOURCES
78 *	None
79 *
80 *    ENVIRONMENTAL NEEDS
81 *      No run-time environmental needs.
82 *
83 *    SPECIAL PROCEDURAL REQUIREMENTS
84 *	None
85 *
86 *    INTERCASE DEPENDENCIES
87 *	None
88 *
89 *    DETAILED DESCRIPTION
90 *	This is a Phase I test for the setreuid(2) system call.  It is intended
91 *	to provide a limited exposure of the system call, for now.  It
92 *	should/will be extended when full functional tests are written for
93 *	setreuid(2).
94 *
95 *	Setup:
96 *	  Setup signal handling.
97 *	  Pause for SIGUSR1 if option specified.
98 *
99 *	Test:
100 *	 Loop if the proper options are given.
101 *	  Execute system call
102 *	  Check return code, if system call failed (return=-1)
103 *		Log the errno and Issue a FAIL message.
104 *	  Otherwise, Issue a PASS message.
105 *
106 *	Cleanup:
107 *	  Print errno log and/or timing stats if options given
108 *
109 *
110 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
111
112#include <errno.h>
113#include <string.h>
114#include <signal.h>
115
116#include <sys/types.h>
117
118#include "test.h"
119#include "usctest.h"
120
121void setup();
122void cleanup();
123
124char *TCID = "setreuid01";	/* Test program identifier.    */
125int TST_TOTAL = 5;		/* Total number of test cases. */
126extern int Tst_count;		/* Test Case counter for tst_* routines */
127
128int exp_enos[] = { 0, 0 };	/* Zero terminated list of expected errnos */
129
130int ruid, euid;			/* real and effective user ids */
131
132int main(int ac, char **av)
133{
134	int lc;			/* loop counter */
135	char *msg;		/* message returned from parse_opts */
136
137    /***************************************************************
138     * parse standard options
139     ***************************************************************/
140	if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL)
141		tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
142
143    /***************************************************************
144     * perform global setup for test
145     ***************************************************************/
146	setup();
147
148	/* set the expected errnos... */
149	TEST_EXP_ENOS(exp_enos);
150
151    /***************************************************************
152     * check looping state if -c option given
153     ***************************************************************/
154	for (lc = 0; TEST_LOOPING(lc); lc++) {
155
156		/* reset Tst_count in case we are looping. */
157		Tst_count = 0;
158
159		/*
160		 * TEST CASE:
161		 *  Don't change either real or effective uid
162		 */
163		ruid = getuid();	/* get real uid */
164		euid = geteuid();	/* get effective uid */
165
166		/* Call setreuid(2) */
167		TEST(setreuid(-1, -1));
168
169		/* check return code */
170		if (TEST_RETURN == -1) {
171			TEST_ERROR_LOG(TEST_ERRNO);
172			tst_resm(TFAIL,
173				 "setreuid -  Don't change either real or effective uid failed, errno=%d : %s",
174				 TEST_ERRNO, strerror(TEST_ERRNO));
175		} else {
176	    /***************************************************************
177	     * only perform functional verification if flag set (-f not given)
178	     ***************************************************************/
179			if (STD_FUNCTIONAL_TEST) {
180				/* No Verification test, yet... */
181				tst_resm(TPASS,
182					 "setreuid -  Don't change either real or effective uid returned %ld",
183					 TEST_RETURN);
184			}
185		}
186
187		/*
188		 * TEST CASE:
189		 *  change effective to effective uid
190		 */
191
192		/* Call setreuid(2) */
193		TEST(setreuid(-1, euid));
194
195		/* check return code */
196		if (TEST_RETURN == -1) {
197			TEST_ERROR_LOG(TEST_ERRNO);
198			tst_resm(TFAIL,
199				 "setreuid -  change effective to effective uid failed, errno=%d : %s",
200				 TEST_ERRNO, strerror(TEST_ERRNO));
201		} else {
202	    /***************************************************************
203	     * only perform functional verification if flag set (-f not given)
204	     ***************************************************************/
205			if (STD_FUNCTIONAL_TEST) {
206				/* No Verification test, yet... */
207				tst_resm(TPASS,
208					 "setreuid -  change effective to effective uid returned %ld",
209					 TEST_RETURN);
210			}
211		}
212
213		/*
214		 * TEST CASE:
215		 *  change real to real uid
216		 */
217
218		/* Call setreuid(2) */
219		TEST(setreuid(ruid, -1));
220
221		/* check return code */
222		if (TEST_RETURN == -1) {
223			TEST_ERROR_LOG(TEST_ERRNO);
224			tst_resm(TFAIL,
225				 "setreuid -  change real to real uid failed, errno=%d : %s",
226				 TEST_ERRNO, strerror(TEST_ERRNO));
227		} else {
228	    /***************************************************************
229	     * only perform functional verification if flag set (-f not given)
230	     ***************************************************************/
231			if (STD_FUNCTIONAL_TEST) {
232				/* No Verification test, yet... */
233				tst_resm(TPASS,
234					 "setreuid -  change real to real uid returned %ld",
235					 TEST_RETURN);
236			}
237		}
238
239		/*
240		 * TEST CASE:
241		 *  change effective to real uid
242		 */
243
244		/* Call setreuid(2) */
245		TEST(setreuid(-1, ruid));
246
247		/* check return code */
248		if (TEST_RETURN == -1) {
249			TEST_ERROR_LOG(TEST_ERRNO);
250			tst_resm(TFAIL,
251				 "setreuid -  change effective to real uid failed, errno=%d : %s",
252				 TEST_ERRNO, strerror(TEST_ERRNO));
253		} else {
254	    /***************************************************************
255	     * only perform functional verification if flag set (-f not given)
256	     ***************************************************************/
257			if (STD_FUNCTIONAL_TEST) {
258				/* No Verification test, yet... */
259				tst_resm(TPASS,
260					 "setreuid -  change effective to real uid returned %ld",
261					 TEST_RETURN);
262			}
263		}
264
265		/*
266		 * TEST CASE:
267		 *  try to change real to current real
268		 */
269
270		/* Call setreuid(2) */
271		TEST(setreuid(ruid, ruid));
272
273		/* check return code */
274		if (TEST_RETURN == -1) {
275			TEST_ERROR_LOG(TEST_ERRNO);
276			tst_resm(TFAIL,
277				 "setreuid -  try to change real to current real failed, errno=%d : %s",
278				 TEST_ERRNO, strerror(TEST_ERRNO));
279		} else {
280	    /***************************************************************
281	     * only perform functional verification if flag set (-f not given)
282	     ***************************************************************/
283			if (STD_FUNCTIONAL_TEST) {
284				/* No Verification test, yet... */
285				tst_resm(TPASS,
286					 "setreuid -  try to change real to current real returned %ld",
287					 TEST_RETURN);
288			}
289		}
290
291	}			/* End for TEST_LOOPING */
292
293    /***************************************************************
294     * cleanup and exit
295     ***************************************************************/
296	cleanup();
297
298	return 0;
299}				/* End main */
300
301/***************************************************************
302 * setup() - performs all ONE TIME setup for this test.
303 ***************************************************************/
304void setup()
305{
306	/* capture signals */
307	tst_sig(NOFORK, DEF_HANDLER, cleanup);
308
309	/* Pause if that option was specified */
310	TEST_PAUSE;
311
312	/* make a temp dir and cd to it */
313	tst_tmpdir();
314}				/* End setup() */
315
316/***************************************************************
317 * cleanup() - performs all ONE TIME cleanup for this test at
318 *		completion or premature exit.
319 ***************************************************************/
320void cleanup()
321{
322	/*
323	 * print timing stats if that option was specified.
324	 * print errno log if that option was specified.
325	 */
326	TEST_CLEANUP;
327
328	/* remove files and temp dir */
329	tst_rmdir();
330
331	/* exit with return code appropriate for results */
332	tst_exit();
333}				/* End cleanup() */
334