• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 /**********************************************************
18  *
19  *    TEST IDENTIFIER	: capget02
20  *
21  *    EXECUTED BY	: anyone
22  *
23  *    TEST TITLE	: Tests for error conditions.
24  *
25  *    TEST CASE TOTAL	: 5
26  *
27  *    AUTHOR		: Saji Kumar.V.R <saji.kumar@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) capget() fails with errno set to EFAULT if an invalid address
36  *	   is given for header
37  *	2) capget() fails with errno set to EFAULT if an invalid address
38  *	   is given for data
39  *	3) capget() fails with errno set to EINVAL if an invalid value
40  *	   is given for header->version
41  *	4) capget() fails with errno set to EINVAL if header->pid < 0
42  *	5) capget() fails with errno set to ESRCH if the process with
43  *	   pid, header->pid does not exit
44  *
45  *
46  * 	Setup:
47  * 	  Setup signal handling.
48  *	  Pause for SIGUSR1 if option specified.
49  *
50  * 	Test:
51  *	 Loop if the proper options are given.
52  * 	  call capget with proper arguments
53  *	  if capget() fails with expected errno
54  *		Test passed
55  *	  Otherwise
56  *		Test failed
57  *
58  * 	Cleanup:
59  * 	  Print errno log and/or timing stats if options given
60  *
61  * USAGE:  <for command-line>
62  * capget02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
63  *			where,  -c n : Run n copies concurrently.
64  *				-e   : Turn on errno logging.
65  *				-h   : Show help screen
66  *				-f   : Turn off functional testing
67  *				-i n : Execute test n times.
68  *				-I x : Execute test for x seconds.
69  *				-p   : Pause for SIGUSR1 before starting
70  *				-P x : Pause for x seconds between iterations.
71  *				-t   : Turn on syscall timing.
72  *
73  ****************************************************************/
74 
75 #include <errno.h>
76 #include "test.h"
77 #include "lapi/syscalls.h"
78 
79 /**************************************************************************/
80 /*                                                                        */
81 /*   Some archs do not have the manpage documented sys/capability.h file, */
82 /*   and require the use of the line below                                */
83 
84 #include <linux/capability.h>
85 
86 /*   If you are having issues with including this file and have the sys/  */
87 /*   version, then you may want to try switching to it. -Robbie W.        */
88 /**************************************************************************/
89 
90 static void setup();
91 static void cleanup();
92 static void test_setup(int);
93 
94 char *TCID = "capget02";
95 
96 static struct __user_cap_header_struct header;
97 static struct __user_cap_data_struct data;
98 
99 struct test_case_t {
100 	cap_user_header_t headerp;
101 	cap_user_data_t datap;
102 	int exp_errno;
103 	char *errdesc;
104 } test_cases[] = {
105 #ifndef UCLINUX
106 	/* Skip since uClinux does not implement memory protection */
107 	{
108 	(cap_user_header_t) - 1, &data, EFAULT, "EFAULT"}, {
109 	&header, (cap_user_data_t) - 1, EFAULT, "EFAULT"},
110 #endif
111 	{
112 	&header, &data, EINVAL, "EINVAL"}, {
113 	&header, &data, EINVAL, "EINVAL"}, {
114 	&header, &data, ESRCH, "ESRCH"}
115 };
116 
117 int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
118 
main(int ac,char ** av)119 int main(int ac, char **av)
120 {
121 
122 	int lc, i;
123 
124 	tst_parse_opts(ac, av, NULL, NULL);
125 
126 	setup();
127 
128 	for (lc = 0; TEST_LOOPING(lc); lc++) {
129 
130 		tst_count = 0;
131 
132 		for (i = 0; i < TST_TOTAL; ++i) {
133 			test_setup(i);
134 			TEST(ltp_syscall(__NR_capget, test_cases[i].headerp,
135 				     test_cases[i].datap));
136 
137 			if (TEST_RETURN == -1 &&
138 			    TEST_ERRNO == test_cases[i].exp_errno) {
139 				tst_resm(TPASS | TTERRNO,
140 					 "capget failed as expected");
141 			} else {
142 				tst_resm(TFAIL | TTERRNO,
143 					 "capget failed unexpectedly (%ld)",
144 					 TEST_RETURN);
145 			}
146 		}
147 	}
148 
149 	cleanup();
150 
151 	tst_exit();
152 
153 }
154 
155 /* setup() - performs all ONE TIME setup for this test */
setup(void)156 void setup(void)
157 {
158 
159 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
160 
161 	TEST_PAUSE;
162 
163 }
164 
165 /*
166  *cleanup() -  performs all ONE TIME cleanup for this test at
167  *		completion or premature exit.
168  */
cleanup(void)169 void cleanup(void)
170 {
171 }
172 
test_setup(int i)173 void test_setup(int i)
174 {
175 #ifdef UCLINUX
176 	i = i + 2;
177 #endif
178 	switch (i) {
179 
180 	case 0:
181 		break;
182 	case 1:
183 		header.version = _LINUX_CAPABILITY_VERSION;
184 		header.pid = getpid();
185 		break;
186 	case 2:
187 		header.version = 0;
188 		header.pid = getpid();
189 		break;
190 	case 3:
191 		header.version = _LINUX_CAPABILITY_VERSION;
192 		header.pid = -1;
193 		break;
194 	case 4:
195 		header.version = _LINUX_CAPABILITY_VERSION;
196 		header.pid = tst_get_unused_pid(cleanup);
197 		break;
198 	}
199 }
200