blob: 2b7043e0cf0be5941faf30ee19008be50a8cf0e1 [file] [log] [blame]
subrata_modak5c8fc012009-05-21 18:17:32 +00001/******************************************************************************/
yaberauneyafaf07482009-11-14 06:36:06 +00002/* Copyright (c) Crackerjack Project., 2007 */
3/* */
subrata_modak5c8fc012009-05-21 18:17:32 +00004/* This program is free software; you can redistribute it and/or modify */
5/* it under the terms of the GNU General Public License as published by */
yaberauneyafaf07482009-11-14 06:36:06 +00006/* the Free Software Foundation; either version 2 of the License, or */
7/* (at your option) any later version. */
8/* */
9/* This program is distributed in the hope that it will be useful, */
10/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
11/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
12/* the GNU General Public License for more details. */
13/* */
14/* You should have received a copy of the GNU General Public License */
15/* along with this program; if not, write to the Free Software */
Wanlong Gao4548c6c2012-10-19 18:03:36 +080016/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
yaberauneyafaf07482009-11-14 06:36:06 +000017/* */
subrata_modak5c8fc012009-05-21 18:17:32 +000018/******************************************************************************/
19/******************************************************************************/
yaberauneyafaf07482009-11-14 06:36:06 +000020/* */
21/* File: keyctl01.c */
22/* */
23/* Description: This tests the keyctl() syscall */
24/* Manipulate the kernel's key management facility */
25/* Usage: <for command-line> */
26/* keyctl01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */
27/* where, -c n : Run n copies concurrently. */
28/* -e : Turn on errno logging. */
29/* -i n : Execute test n times. */
30/* -I x : Execute test for x seconds. */
31/* -P x : Pause for x seconds between iterations. */
32/* -t : Turn on syscall timing. */
33/* */
34/* Total Tests: 2 */
35/* */
36/* Test Name: keyctl01 */
37/* History: Porting from Crackerjack to LTP is done by */
38/* Manas Kumar Nayak maknayak@in.ibm.com> */
subrata_modak5c8fc012009-05-21 18:17:32 +000039/******************************************************************************/
40
Garrett Cooper020a4da2010-11-03 20:57:21 -070041#include "config.h"
Garrett Cooper6340c2a2010-11-01 00:16:19 -070042#include <sys/types.h>
Garrett Coopere99ebbc2010-11-03 10:15:10 -070043#if HAVE_KEYCTL_SYSCALL
subrata_modak5c8fc012009-05-21 18:17:32 +000044#include <linux/keyctl.h>
Garrett Coopere99ebbc2010-11-03 10:15:10 -070045#endif
Garrett Cooperda908052010-10-27 06:05:00 -070046#include <errno.h>
Garrett Coopere99ebbc2010-11-03 10:15:10 -070047#if HAVE_KEYCTL_SYSCALL
Garrett Cooper4e772642010-10-31 20:58:03 -070048#include <keyutils.h>
Garrett Coopere99ebbc2010-11-03 10:15:10 -070049#endif
Garrett Cooperda908052010-10-27 06:05:00 -070050#include <limits.h>
51#include <stdio.h>
Garrett Cooper4e772642010-10-31 20:58:03 -070052
subrata_modak5c8fc012009-05-21 18:17:32 +000053/* Harness Specific Include Files. */
54#include "test.h"
55#include "usctest.h"
56#include "linux_syscall_numbers.h"
57
58/* Extern Global Variables */
subrata_modak5c8fc012009-05-21 18:17:32 +000059
60/* Global Variables */
Wanlong Gao354ebb42012-12-07 10:10:04 +080061char *TCID = "keyctl01"; /* Test program identifier. */
62int testno;
63int TST_TOTAL = 2; /* total number of tests in this file. */
subrata_modak5c8fc012009-05-21 18:17:32 +000064
Garrett Coopere99ebbc2010-11-03 10:15:10 -070065#if HAVE_KEYCTL_SYSCALL
subrata_modak5c8fc012009-05-21 18:17:32 +000066/* Extern Global Functions */
67/******************************************************************************/
yaberauneyafaf07482009-11-14 06:36:06 +000068/* */
69/* Function: cleanup */
70/* */
subrata_modak5c8fc012009-05-21 18:17:32 +000071/* Description: Performs all one time clean up for this test on successful */
yaberauneyafaf07482009-11-14 06:36:06 +000072/* completion, premature exit or failure. Closes all temporary */
73/* files, removes all temporary directories exits the test with */
74/* appropriate return code by calling tst_exit() function. */
75/* */
76/* Input: None. */
77/* */
78/* Output: None. */
79/* */
subrata_modak5c8fc012009-05-21 18:17:32 +000080/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
yaberauneyafaf07482009-11-14 06:36:06 +000081/* On success - Exits calling tst_exit(). With '0' return code. */
82/* */
subrata_modak5c8fc012009-05-21 18:17:32 +000083/******************************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +080084extern void cleanup()
85{
Garrett Cooper2c282152010-12-16 00:55:50 -080086
yaberauneyafaf07482009-11-14 06:36:06 +000087 TEST_CLEANUP;
88 tst_rmdir();
subrata_modak5c8fc012009-05-21 18:17:32 +000089
subrata_modak5c8fc012009-05-21 18:17:32 +000090}
91
92/* Local Functions */
93/******************************************************************************/
yaberauneyafaf07482009-11-14 06:36:06 +000094/* */
95/* Function: setup */
96/* */
subrata_modak5c8fc012009-05-21 18:17:32 +000097/* Description: Performs all one time setup for this test. This function is */
yaberauneyafaf07482009-11-14 06:36:06 +000098/* typically used to capture signals, create temporary dirs */
99/* and temporary files that may be used in the course of this */
100/* test. */
101/* */
102/* Input: None. */
103/* */
104/* Output: None. */
105/* */
106/* Return: On failure - Exits by calling cleanup(). */
107/* On success - returns 0. */
108/* */
subrata_modak5c8fc012009-05-21 18:17:32 +0000109/******************************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110void setup()
111{
yaberauneyafaf07482009-11-14 06:36:06 +0000112 /* Capture signals if any */
113 /* Create temporary directories */
114 TEST_PAUSE;
115 tst_tmpdir();
subrata_modak5c8fc012009-05-21 18:17:32 +0000116}
117
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118int main(int ac, char **av)
119{
yaberauneyac2e88392009-11-24 20:36:37 +0000120 int ret;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200121 int lc;
Garrett Cooperda908052010-10-27 06:05:00 -0700122 key_serial_t ne_key;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200123 char *msg;
yaberauneyafaf07482009-11-14 06:36:06 +0000124
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800125 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800126 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
yaberauneyafaf07482009-11-14 06:36:06 +0000127 tst_exit();
128 }
129
130 setup();
131
yaberauneyafaf07482009-11-14 06:36:06 +0000132 for (lc = 0; TEST_LOOPING(lc); lc++) {
133
134 Tst_count = 0;
135
136 for (testno = 1; testno < TST_TOTAL; ++testno) {
Garrett Cooperda908052010-10-27 06:05:00 -0700137
yaberauneyafaf07482009-11-14 06:36:06 +0000138 /* Call keyctl() and ask for a keyring's ID. */
Jan Stancek359980f2013-02-15 10:16:05 +0100139 ret = ltp_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 KEY_SPEC_USER_SESSION_KEYRING);
yaberauneyac2e88392009-11-24 20:36:37 +0000141 if (ret != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 tst_resm(TPASS,
143 "KEYCTL_GET_KEYRING_ID succeeded");
yaberauneyafaf07482009-11-14 06:36:06 +0000144 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 tst_resm(TFAIL | TERRNO,
146 "KEYCTL_GET_KEYRING_ID");
Garrett Cooperda908052010-10-27 06:05:00 -0700147 }
148
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 for (ne_key = INT32_MAX; ne_key > INT32_MIN; ne_key--) {
Jan Stancek359980f2013-02-15 10:16:05 +0100150 ret = ltp_syscall(__NR_keyctl, KEYCTL_READ,
151 ne_key);
Garrett Cooperda908052010-10-27 06:05:00 -0700152 if (ret == -1 && errno == ENOKEY)
153 break;
yaberauneyafaf07482009-11-14 06:36:06 +0000154 }
subrata_modak5c8fc012009-05-21 18:17:32 +0000155
yaberauneyafaf07482009-11-14 06:36:06 +0000156 /* Call keyctl. */
Jan Stancek359980f2013-02-15 10:16:05 +0100157 ret = ltp_syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key);
yaberauneyac2e88392009-11-24 20:36:37 +0000158 if (ret != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 tst_resm(TFAIL | TERRNO,
160 "KEYCTL_REVOKE succeeded unexpectedly");
yaberauneyac2e88392009-11-24 20:36:37 +0000161 } else {
yaberauneyafaf07482009-11-14 06:36:06 +0000162 /* Check for the correct error num. */
yaberauneyac2e88392009-11-24 20:36:37 +0000163 if (errno == ENOKEY) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 tst_resm(TPASS | TERRNO,
165 "KEYCTL_REVOKE got expected "
166 "errno");
yaberauneyafaf07482009-11-14 06:36:06 +0000167 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 tst_resm(TFAIL | TERRNO,
169 "KEYCTL_REVOKE got unexpected "
170 "errno");
subrata_modak5c8fc012009-05-21 18:17:32 +0000171 }
subrata_modak5c8fc012009-05-21 18:17:32 +0000172
yaberauneyafaf07482009-11-14 06:36:06 +0000173 }
174
175 }
176
177 }
178 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800179
Garrett Cooperda908052010-10-27 06:05:00 -0700180 return (1);
subrata_modak5c8fc012009-05-21 18:17:32 +0000181}
Garrett Coopere99ebbc2010-11-03 10:15:10 -0700182#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183int main(void)
Garrett Coopere99ebbc2010-11-03 10:15:10 -0700184{
185 tst_resm(TCONF, "keyctl syscall support not available on system");
186 tst_exit();
187}
Chris Dearmanec6edca2012-10-17 19:54:01 -0700188#endif