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