xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/keyctl/keyctl01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Crackerjack Project., 2007
4  * Copyright (c) 2017 Fujitsu Ltd.
5  * Copyright (c) Linux Test Project, 2009-2024
6  * Ported by Manas Kumar Nayak [email protected]>
7  * Modified by Guangwen Feng <[email protected]>
8  */
9 
10 /*\
11  * [Description]
12  *
13  * Tests the keyctl(2) syscall.
14  *
15  * Manipulate the kernel's key management facility.
16  */
17 
18 #include <errno.h>
19 #include <stdint.h>
20 
21 #include "tst_test.h"
22 #include "lapi/keyctl.h"
23 
do_test(void)24 static void do_test(void)
25 {
26 	key_serial_t key;
27 
28 	TEST(keyctl(KEYCTL_GET_KEYRING_ID, KEY_SPEC_USER_SESSION_KEYRING));
29 	if (TST_RET != -1)
30 		tst_res(TPASS, "KEYCTL_GET_KEYRING_ID succeeded");
31 	else
32 		tst_res(TFAIL | TTERRNO, "KEYCTL_GET_KEYRING_ID failed");
33 
34 	for (key = INT32_MAX; key > INT32_MIN; key--) {
35 		TEST(keyctl(KEYCTL_READ, key));
36 		if (TST_RET == -1 && TST_ERR == ENOKEY)
37 			break;
38 	}
39 
40 	TEST(keyctl(KEYCTL_REVOKE, key));
41 	if (TST_RET != -1) {
42 		tst_res(TFAIL, "KEYCTL_REVOKE succeeded unexpectedly");
43 		return;
44 	}
45 
46 	if (TST_ERR != ENOKEY) {
47 		tst_res(TFAIL | TTERRNO, "KEYCTL_REVOKE failed unexpectedly");
48 		return;
49 	}
50 
51 	tst_res(TPASS | TTERRNO, "KEYCTL_REVOKE failed as expected");
52 }
53 
54 static struct tst_test test = {
55 	.test_all = do_test,
56 };
57