1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Crackerjack Project., 2007
4 * Porting from Crackerjack to LTP is done by
5 * Manas Kumar Nayak [email protected]>
6 *
7 * This case test various key type can support how many long
8 * bytes payload.
9 * keyring: 0 bytes
10 * user/logon: 32767 bytes
11 * big_key: 1M -1byte
12 *
13 * The tests needs root because larger keys are over limit for unpriviledged
14 * user by default.
15 */
16
17 #include "tst_test.h"
18 #include "lapi/keyctl.h"
19
20 static char *keyring_buf, *keyring_buf1;
21 static char *user_buf, *user_buf1;
22 static char *logon_buf, *logon_buf1;
23 static char *big_key_buf, *big_key_buf1;
24 static unsigned int logon_nsup, big_key_nsup;
25
26 struct tcase {
27 const char *type;
28 const char *desc;
29 char **buf;
30 size_t plen;
31 int pass_flag;
32 char *message;
33 } tcases[] = {
34 {"keyring", "abc", &keyring_buf, 0, 1,
35 "The key type is keyrings and plen is 0"},
36
37 {"keyring", "bcd", &keyring_buf, 1, 0,
38 "the key type is keyrings and plen is 1"},
39
40 {"user", "cde", &user_buf, 32767, 1,
41 "The key type is user and plen is 32767"},
42
43 {"user", "def", &user_buf1, 32768, 0,
44 "The key type is user and plen is 32768"},
45
46 {"logon", "ef:g", &logon_buf, 32767, 1,
47 "The key type is logon and plen is 32767"},
48
49 {"logon", "fg:h", &logon_buf1, 32768, 0,
50 "The key type is logon and plen is 32768"},
51
52 {"big_key", "ghi", &big_key_buf, (1 << 20) - 1, 1,
53 "The key type is big_key and plen is 1048575"},
54
55 {"big_key", "hij", &big_key_buf1, 1 << 20, 0,
56 "The key type is big_key and plen is 1048576"},
57 };
58
verify_add_key(unsigned int n)59 static void verify_add_key(unsigned int n)
60 {
61 struct tcase *tc = &tcases[n];
62
63 tst_res(TINFO, "%s", tc->message);
64
65 if (!strcmp(tc->type, "logon") && logon_nsup) {
66 tst_res(TCONF, "skipping unsupported logon key");
67 return;
68 }
69 if (!strcmp(tc->type, "big_key") && big_key_nsup) {
70 tst_res(TCONF, "skipping unsupported big_key key");
71 return;
72 }
73
74 TEST(add_key(tc->type, tc->desc, *tc->buf, tc->plen, KEY_SPEC_THREAD_KEYRING));
75 if (tc->pass_flag) {
76 if (TST_RET == -1)
77 tst_res(TFAIL | TTERRNO, "add_key call failed unexpectedly");
78 else
79 tst_res(TPASS, "add_key call succeeded as expected");
80 } else {
81 if (TST_RET == -1) {
82 if (TST_ERR == EINVAL)
83 tst_res(TPASS | TTERRNO, "add_key call failed as expected");
84 else
85 tst_res(TFAIL | TTERRNO, "add_key call failed expected EINVAL but got");
86 } else {
87 tst_res(TFAIL, "add_key call succeeded unexpectedly");
88 }
89 }
90 }
91
setup(void)92 static void setup(void)
93 {
94 char buf[64];
95
96 TEST(add_key("logon", "test:sup_logon", buf, sizeof(buf), KEY_SPEC_THREAD_KEYRING));
97 if (TST_RET == -1)
98 logon_nsup = 1;
99
100 TEST(add_key("big_key", "sup_big_key", buf, sizeof(buf), KEY_SPEC_THREAD_KEYRING));
101 if (TST_RET == -1)
102 big_key_nsup = 1;
103 }
104
105 static struct tst_test test = {
106 .setup = setup,
107 .tcnt = ARRAY_SIZE(tcases),
108 .test = verify_add_key,
109 .needs_root = 1,
110 .bufs = (struct tst_buffers []) {
111 {&keyring_buf, .size = 1},
112 {&keyring_buf1, .size = 1},
113 {&user_buf, .size = 32767},
114 {&user_buf1, .size = 32768},
115 {&logon_buf, .size = 32767},
116 {&logon_buf1, .size = 32768},
117 {&big_key_buf, .size = (1 << 20) - 1},
118 {&big_key_buf1, .size = 1 << 20},
119 {}
120 }
121 };
122