keyctl01.c revision aabb8340f63ed31afe995fd97795e542dc68b93c
1/******************************************************************************
2 * Copyright (c) Crackerjack Project., 2007				      *
3 *									      *
4 * 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       *
6 * 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 Foundation,   *
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA           *
17 *                                                                            *
18 ******************************************************************************/
19/*
20 * Description: This tests the keyctl() syscall
21 *		Manipulate the kernel's key management facility
22 *
23 * History:     Porting from Crackerjack to LTP is done by
24 *	      Manas Kumar Nayak maknayak@in.ibm.com>
25 */
26
27#include "config.h"
28#include <sys/types.h>
29#include <errno.h>
30#include <limits.h>
31#include <stdio.h>
32#include <stdint.h>
33#ifdef HAVE_LINUX_KEYCTL_H
34# include <linux/keyctl.h>
35#endif
36
37#include "test.h"
38#include "linux_syscall_numbers.h"
39
40char *TCID = "keyctl01";
41int testno;
42int TST_TOTAL = 2;
43
44#ifdef HAVE_LINUX_KEYCTL_H
45
46static void cleanup(void)
47{
48	tst_rmdir();
49}
50
51static void setup(void)
52{
53	TEST_PAUSE;
54	tst_tmpdir();
55}
56
57int main(int ac, char **av)
58{
59	int ret;
60	int lc;
61	int32_t ne_key;
62	const char *msg;
63
64	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
65		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
66
67	setup();
68
69	for (lc = 0; TEST_LOOPING(lc); lc++) {
70
71		tst_count = 0;
72
73		for (testno = 1; testno < TST_TOTAL; ++testno) {
74
75			/* Call keyctl() and ask for a keyring's ID. */
76			ret = ltp_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
77				      KEY_SPEC_USER_SESSION_KEYRING);
78			if (ret != -1) {
79				tst_resm(TPASS,
80					 "KEYCTL_GET_KEYRING_ID succeeded");
81			} else {
82				tst_resm(TFAIL | TERRNO,
83					 "KEYCTL_GET_KEYRING_ID");
84			}
85
86			for (ne_key = INT32_MAX; ne_key > INT32_MIN; ne_key--) {
87				ret = ltp_syscall(__NR_keyctl, KEYCTL_READ,
88					ne_key);
89				if (ret == -1 && errno == ENOKEY)
90					break;
91			}
92
93			/* Call keyctl. */
94			ret = ltp_syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key);
95			if (ret != -1) {
96				tst_resm(TFAIL | TERRNO,
97					 "KEYCTL_REVOKE succeeded unexpectedly");
98			} else {
99				/* Check for the correct error num. */
100				if (errno == ENOKEY) {
101					tst_resm(TPASS | TERRNO,
102						 "KEYCTL_REVOKE got expected "
103						 "errno");
104				} else {
105					tst_resm(TFAIL | TERRNO,
106						 "KEYCTL_REVOKE got unexpected "
107						 "errno");
108				}
109
110			}
111
112		}
113
114	}
115	cleanup();
116	tst_exit();
117}
118#else
119int main(void)
120{
121	tst_brkm(TCONF, NULL, "keyctl syscall support not available on system");
122}
123#endif /* HAVE_LINUX_KEYCTL_H */
124