1 /*
2 * Copyright (c) 2016 Fujitsu Ltd.
3 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License
14 * alone with this program.
15 */
16
17 /*
18 * Test Name: request_key02
19 *
20 * Description:
21 * 1) request_key(2) fails if no matching key was found.
22 * 2) request_key(2) fails if A revoked key was found.
23 * 3) request_key(2) fails if An expired key was found.
24 *
25 * Expected Result:
26 * 1) request_key(2) should return -1 and set errno to ENOKEY.
27 * 2) request_key(2) should return -1 and set errno to EKEYREVOKED.
28 * 3) request_key(2) should return -1 and set errno to EKEYEXPIRED.
29 *
30 */
31
32 #include "config.h"
33 #include <errno.h>
34 #include <sys/types.h>
35 #ifdef HAVE_KEYUTILS_H
36 # include <keyutils.h>
37 #endif
38 #include "tst_test.h"
39
40 #ifdef HAVE_KEYUTILS_H
41
42 static int key1;
43 static int key2;
44 static int key3;
45
46 static struct test_case {
47 const char *des;
48 int exp_err;
49 int *id;
50 } tcases[] = {
51 {"ltp1", ENOKEY, &key1},
52 {"ltp2", EKEYREVOKED, &key2},
53 {"ltp3", EKEYEXPIRED, &key3}
54 };
55
verify_request_key(unsigned int n)56 static void verify_request_key(unsigned int n)
57 {
58 struct test_case *tc = tcases + n;
59
60 TEST(request_key("keyring", tc->des, NULL, *tc->id));
61 if (TEST_RETURN != -1) {
62 tst_res(TFAIL, "request_key() succeed unexpectly");
63 return;
64 }
65
66 if (TEST_ERRNO == tc->exp_err) {
67 tst_res(TPASS | TTERRNO, "request_key() failed expectly");
68 return;
69 }
70
71 tst_res(TFAIL | TTERRNO, "request_key() failed unexpectly, "
72 "expected %s", tst_strerrno(tc->exp_err));
73 }
74
init_key(char * name,int cmd)75 static int init_key(char *name, int cmd)
76 {
77 int n;
78 int sec = 1;
79
80 n = add_key("keyring", name, NULL, 0, KEY_SPEC_THREAD_KEYRING);
81 if (n == -1)
82 tst_brk(TBROK | TERRNO, "add_key() failed");
83
84 if (cmd == KEYCTL_REVOKE) {
85 if (keyctl(cmd, n) == -1) {
86 tst_brk(TBROK | TERRNO, "failed to revoke a key");
87 }
88 }
89
90 if (cmd == KEYCTL_SET_TIMEOUT) {
91 if (keyctl(cmd, n, sec) == -1) {
92 tst_brk(TBROK | TERRNO,
93 "failed to set timeout for a key");
94 }
95
96 sleep(sec + 1);
97 }
98
99 return n;
100 }
101
setup(void)102 static void setup(void)
103 {
104 key1 = KEY_REQKEY_DEFL_DEFAULT;
105 key2 = init_key("ltp2", KEYCTL_REVOKE);
106 key3 = init_key("ltp3", KEYCTL_SET_TIMEOUT);
107 }
108
109 static struct tst_test test = {
110 .tid = "request_key02",
111 .setup = setup,
112 .tcnt = ARRAY_SIZE(tcases),
113 .test = verify_request_key,
114 };
115
116 #else
117
118 TST_TEST_TCONF("keyutils.h was missing at compilation");
119
120 #endif /* HAVE_LINUX_KEYCTL_H */
121