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