keyctl01.c revision 89af32a63ce8a780ea39337339e14caae244b5a4
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	       */
16/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
17/*									    */
18/******************************************************************************/
19/******************************************************************************/
20/*									    */
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>			*/
39/******************************************************************************/
40
41#include "config.h"
42#include <sys/types.h>
43#if HAVE_KEYCTL_SYSCALL
44#include <linux/keyctl.h>
45#endif
46#include <errno.h>
47#if HAVE_KEYCTL_SYSCALL
48#include <keyutils.h>
49#endif
50#include <limits.h>
51#include <stdio.h>
52
53/* Harness Specific Include Files. */
54#include "test.h"
55#include "usctest.h"
56#include "linux_syscall_numbers.h"
57
58/* Extern Global Variables */
59
60/* Global Variables */
61char *TCID = "keyctl01";/* Test program identifier.*/
62int  testno;
63int  TST_TOTAL = 2;	/* total number of tests in this file.   */
64
65#if HAVE_KEYCTL_SYSCALL
66/* Extern Global Functions */
67/******************************************************************************/
68/*									    */
69/* Function:    cleanup						       */
70/*									    */
71/* Description: Performs all one time clean up for this test on successful    */
72/*	      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/*									    */
80/* Return:      On failure - Exits calling tst_exit(). Non '0' return code.   */
81/*	      On success - Exits calling tst_exit(). With '0' return code.  */
82/*									    */
83/******************************************************************************/
84extern void cleanup() {
85
86	TEST_CLEANUP;
87	tst_rmdir();
88
89}
90
91/* Local  Functions */
92/******************************************************************************/
93/*									    */
94/* Function:    setup							 */
95/*									    */
96/* Description: Performs all one time setup for this test. This function is   */
97/*	      typically used to capture signals, create temporary dirs      */
98/*	      and temporary files that may be used in the course of this    */
99/*	      test.							 */
100/*									    */
101/* Input:       None.							 */
102/*									    */
103/* Output:      None.							 */
104/*									    */
105/* Return:      On failure - Exits by calling cleanup().		      */
106/*	      On success - returns 0.				       */
107/*									    */
108/******************************************************************************/
109void setup() {
110	/* Capture signals if any */
111	/* Create temporary directories */
112	TEST_PAUSE;
113	tst_tmpdir();
114}
115
116int main(int ac, char **av) {
117	int ret;
118	int lc;
119	key_serial_t ne_key;
120	char *msg;
121
122	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
123		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
124		tst_exit();
125	}
126
127	setup();
128
129	for (lc = 0; TEST_LOOPING(lc); lc++) {
130
131		Tst_count = 0;
132
133		for (testno = 1; testno < TST_TOTAL; ++testno) {
134
135			/* Call keyctl() and ask for a keyring's ID. */
136			ret = syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
137					KEY_SPEC_USER_SESSION_KEYRING);
138			if (ret != -1) {
139				tst_resm(TPASS,"KEYCTL_GET_KEYRING_ID succeeded");
140			} else {
141		 		tst_resm(TFAIL|TERRNO, "KEYCTL_GET_KEYRING_ID");
142			}
143
144			for (ne_key = INT32_MAX; ne_key > INT32_MIN;
145			    ne_key--) {
146				ret = syscall(__NR_keyctl, KEYCTL_READ,
147					ne_key);
148				if (ret == -1 && errno == ENOKEY)
149					break;
150			}
151
152			/* Call keyctl. */
153			ret = syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key);
154			if (ret != -1) {
155				tst_resm(TFAIL|TERRNO,
156					"KEYCTL_REVOKE succeeded unexpectedly");
157			} else {
158				/* Check for the correct error num. */
159				if (errno == ENOKEY) {
160					tst_resm(TPASS|TERRNO,
161						"KEYCTL_REVOKE got expected "
162						"errno");
163				} else {
164					tst_resm(TFAIL|TERRNO,
165						"KEYCTL_REVOKE got unexpected "
166						"errno");
167				}
168
169			}
170
171		}
172
173	}
174	cleanup();
175
176	return (1);
177}
178#else
179int
180main(void)
181{
182	tst_resm(TCONF, "keyctl syscall support not available on system");
183	tst_exit();
184}
185#endif
186