blob: e197dcc76e621550b8881ac99b65f7ba9d4f9567 [file] [log] [blame]
robbiewd771da72002-12-27 18:41:54 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiewd771da72002-12-27 18:41:54 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiewd771da72002-12-27 18:41:54 +000019 * TEST IDENTIFIER : capget02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiewd771da72002-12-27 18:41:54 +000021 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiewd771da72002-12-27 18:41:54 +000023 * TEST TITLE : Tests for error conditions.
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiewd771da72002-12-27 18:41:54 +000025 * TEST CASE TOTAL : 5
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiewd771da72002-12-27 18:41:54 +000027 * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiewd771da72002-12-27 18:41:54 +000029 * 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
subrata_modakbdbaec52009-02-26 12:14:51 +000044 *
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
robbiewd771da72002-12-27 18:41:54 +000046 * Setup:
47 * Setup signal handling.
48 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
robbiewd771da72002-12-27 18:41:54 +000050 * 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
subrata_modak4bb656a2009-02-26 12:02:09 +000057 *
robbiewd771da72002-12-27 18:41:54 +000058 * Cleanup:
59 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000060 *
robbiewd771da72002-12-27 18:41:54 +000061 * 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 ****************************************************************/
robbiewd771da72002-12-27 18:41:54 +000074
75#include <errno.h>
robbiewd771da72002-12-27 18:41:54 +000076#include "test.h"
77#include "usctest.h"
Garrett Cooper17b121b2010-12-17 11:05:05 -080078#include "linux_syscall_numbers.h"
robbiewe7757592003-10-01 17:23:49 +000079
80/**************************************************************************/
robbiewe7757592003-10-01 17:23:49 +000081/* */
82/* Some archs do not have the manpage documented sys/capability.h file, */
robbiew04a8ba52003-10-02 17:38:04 +000083/* and require the use of the line below */
84
85#include <linux/capability.h>
86
87/* If you are having issues with including this file and have the sys/ */
88/* version, then you may want to try switching to it. -Robbie W. */
robbiewe7757592003-10-01 17:23:49 +000089/**************************************************************************/
robbiewd771da72002-12-27 18:41:54 +000090
robbiewd771da72002-12-27 18:41:54 +000091static void setup();
92static void cleanup();
93static void test_setup(int);
94
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020095char *TCID = "capget02";
robbiewd771da72002-12-27 18:41:54 +000096
97static struct __user_cap_header_struct header;
98static struct __user_cap_data_struct data;
99
100struct test_case_t {
101 cap_user_header_t headerp;
102 cap_user_data_t datap;
103 int exp_errno;
104 char *errdesc;
105} test_cases[] = {
vapier7ec19d92006-02-27 04:38:56 +0000106#ifndef UCLINUX
107 /* Skip since uClinux does not implement memory protection */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 {
109 (cap_user_header_t) - 1, &data, EFAULT, "EFAULT"}, {
110 &header, (cap_user_data_t) - 1, EFAULT, "EFAULT"},
vapier7ec19d92006-02-27 04:38:56 +0000111#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 {
113 &header, &data, EINVAL, "EINVAL"}, {
114 &header, &data, EINVAL, "EINVAL"}, {
115 &header, &data, ESRCH, "ESRCH"}
robbiewd771da72002-12-27 18:41:54 +0000116};
117
118int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
119
subrata_modak56207ce2009-03-23 13:35:39 +0000120int main(int ac, char **av)
robbiewd771da72002-12-27 18:41:54 +0000121{
122
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200123 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200124 const char *msg;
robbiewd771da72002-12-27 18:41:54 +0000125
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800126 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800127 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd771da72002-12-27 18:41:54 +0000128
robbiewd771da72002-12-27 18:41:54 +0000129 setup();
130
robbiewd771da72002-12-27 18:41:54 +0000131 for (lc = 0; TEST_LOOPING(lc); lc++) {
132
Caspar Zhangd59a6592013-03-07 14:59:12 +0800133 tst_count = 0;
robbiewd771da72002-12-27 18:41:54 +0000134
135 for (i = 0; i < TST_TOTAL; ++i) {
136 test_setup(i);
Jan Stancek359980f2013-02-15 10:16:05 +0100137 TEST(ltp_syscall(__NR_capget, test_cases[i].headerp,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138 test_cases[i].datap));
robbiewd771da72002-12-27 18:41:54 +0000139
Garrett Cooper17b121b2010-12-17 11:05:05 -0800140 if (TEST_RETURN == -1 &&
141 TEST_ERRNO == test_cases[i].exp_errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 tst_resm(TPASS | TTERRNO,
143 "capget failed as expected");
robbiewd771da72002-12-27 18:41:54 +0000144 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 tst_resm(TFAIL | TTERRNO,
146 "capget failed unexpectedly (%ld)",
147 TEST_RETURN);
robbiewd771da72002-12-27 18:41:54 +0000148 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000149 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800150 }
robbiewd771da72002-12-27 18:41:54 +0000151
robbiewd771da72002-12-27 18:41:54 +0000152 cleanup();
153
Garrett Cooper53740502010-12-16 00:04:01 -0800154 tst_exit();
robbiewd771da72002-12-27 18:41:54 +0000155
Garrett Cooper2c282152010-12-16 00:55:50 -0800156}
robbiewd771da72002-12-27 18:41:54 +0000157
158/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400159void setup(void)
robbiewd771da72002-12-27 18:41:54 +0000160{
Garrett Cooper2c282152010-12-16 00:55:50 -0800161
robbiewd771da72002-12-27 18:41:54 +0000162 tst_sig(NOFORK, DEF_HANDLER, cleanup);
163
robbiewd771da72002-12-27 18:41:54 +0000164 TEST_PAUSE;
165
Garrett Cooper2c282152010-12-16 00:55:50 -0800166}
robbiewd771da72002-12-27 18:41:54 +0000167
168/*
169 *cleanup() - performs all ONE TIME cleanup for this test at
170 * completion or premature exit.
171 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400172void cleanup(void)
robbiewd771da72002-12-27 18:41:54 +0000173{
Garrett Cooper2c282152010-12-16 00:55:50 -0800174}
robbiewd771da72002-12-27 18:41:54 +0000175
subrata_modak56207ce2009-03-23 13:35:39 +0000176void test_setup(int i)
robbiewd771da72002-12-27 18:41:54 +0000177{
vapier7ec19d92006-02-27 04:38:56 +0000178#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000179 i = i + 2;
vapier7ec19d92006-02-27 04:38:56 +0000180#endif
robbiewd771da72002-12-27 18:41:54 +0000181 switch (i) {
182
subrata_modak56207ce2009-03-23 13:35:39 +0000183 case 0:
robbiewd771da72002-12-27 18:41:54 +0000184 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000185 case 1:
robbiewd771da72002-12-27 18:41:54 +0000186 header.version = _LINUX_CAPABILITY_VERSION;
187 header.pid = getpid();
188 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000189 case 2:
robbiewd771da72002-12-27 18:41:54 +0000190 header.version = 0;
191 header.pid = getpid();
192 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000193 case 3:
robbiewd771da72002-12-27 18:41:54 +0000194 header.version = _LINUX_CAPABILITY_VERSION;
195 header.pid = -1;
196 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000197 case 4:
robbiewd771da72002-12-27 18:41:54 +0000198 header.version = _LINUX_CAPABILITY_VERSION;
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +0400199 header.pid = tst_get_unused_pid(cleanup);
robbiewd771da72002-12-27 18:41:54 +0000200 break;
201 }
Chris Dearmanec6edca2012-10-17 19:54:01 -0700202}