keyctl01.c revision 988aabfb05cbe808b5110eda4feab5a2ac8a7703
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 "usctest.h"
39#include "linux_syscall_numbers.h"
40
41char *TCID = "keyctl01";
42int testno;
43int TST_TOTAL = 2;
44
45#ifdef HAVE_LINUX_KEYCTL_H
46
47static void cleanup(void)
48{
49	TEST_CLEANUP;
50	tst_rmdir();
51}
52
53static void setup(void)
54{
55	TEST_PAUSE;
56	tst_tmpdir();
57}
58
59int main(int ac, char **av)
60{
61	int ret;
62	int lc;
63	int32_t ne_key;
64	const char *msg;
65
66	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
67		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
68
69	setup();
70
71	for (lc = 0; TEST_LOOPING(lc); lc++) {
72
73		tst_count = 0;
74
75		for (testno = 1; testno < TST_TOTAL; ++testno) {
76
77			/* Call keyctl() and ask for a keyring's ID. */
78			ret = ltp_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
79				      KEY_SPEC_USER_SESSION_KEYRING);
80			if (ret != -1) {
81				tst_resm(TPASS,
82					 "KEYCTL_GET_KEYRING_ID succeeded");
83			} else {
84				tst_resm(TFAIL | TERRNO,
85					 "KEYCTL_GET_KEYRING_ID");
86			}
87
88			for (ne_key = INT32_MAX; ne_key > INT32_MIN; ne_key--) {
89				ret = ltp_syscall(__NR_keyctl, KEYCTL_READ,
90					ne_key);
91				if (ret == -1 && errno == ENOKEY)
92					break;
93			}
94
95			/* Call keyctl. */
96			ret = ltp_syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key);
97			if (ret != -1) {
98				tst_resm(TFAIL | TERRNO,
99					 "KEYCTL_REVOKE succeeded unexpectedly");
100			} else {
101				/* Check for the correct error num. */
102				if (errno == ENOKEY) {
103					tst_resm(TPASS | TERRNO,
104						 "KEYCTL_REVOKE got expected "
105						 "errno");
106				} else {
107					tst_resm(TFAIL | TERRNO,
108						 "KEYCTL_REVOKE got unexpected "
109						 "errno");
110				}
111
112			}
113
114		}
115
116	}
117	cleanup();
118	tst_exit();
119}
120#else
121int main(void)
122{
123	tst_brkm(TCONF, NULL, "keyctl syscall support not available on system");
124}
125#endif /* HAVE_LINUX_KEYCTL_H */
126