1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2016 Fujitsu Ltd. 4f08c3bdfSopenharmony_ci * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz> 5f08c3bdfSopenharmony_ci * 6f08c3bdfSopenharmony_ci * Author: Xiao Yang <yangx.jy@cn.fujitsu.com> 7f08c3bdfSopenharmony_ci */ 8f08c3bdfSopenharmony_ci 9f08c3bdfSopenharmony_ci/* 10f08c3bdfSopenharmony_ci * Test Name: request_key02 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * Description: 13f08c3bdfSopenharmony_ci * 1) request_key(2) fails if no matching key was found. 14f08c3bdfSopenharmony_ci * 2) request_key(2) fails if A revoked key was found. 15f08c3bdfSopenharmony_ci * 3) request_key(2) fails if An expired key was found. 16f08c3bdfSopenharmony_ci * 17f08c3bdfSopenharmony_ci * Expected Result: 18f08c3bdfSopenharmony_ci * 1) request_key(2) should return -1 and set errno to ENOKEY. 19f08c3bdfSopenharmony_ci * 2) request_key(2) should return -1 and set errno to EKEYREVOKED. 20f08c3bdfSopenharmony_ci * 3) request_key(2) should return -1 and set errno to EKEYEXPIRED. 21f08c3bdfSopenharmony_ci */ 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_ci#include <errno.h> 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#include "tst_test.h" 26f08c3bdfSopenharmony_ci#include "lapi/keyctl.h" 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic int key1; 29f08c3bdfSopenharmony_cistatic int key2; 30f08c3bdfSopenharmony_cistatic int key3; 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_cistatic struct test_case { 33f08c3bdfSopenharmony_ci const char *des; 34f08c3bdfSopenharmony_ci int exp_err; 35f08c3bdfSopenharmony_ci int *id; 36f08c3bdfSopenharmony_ci} tcases[] = { 37f08c3bdfSopenharmony_ci {"ltp1", ENOKEY, &key1}, 38f08c3bdfSopenharmony_ci {"ltp2", EKEYREVOKED, &key2}, 39f08c3bdfSopenharmony_ci {"ltp3", EKEYEXPIRED, &key3} 40f08c3bdfSopenharmony_ci}; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cistatic void verify_request_key(unsigned int n) 43f08c3bdfSopenharmony_ci{ 44f08c3bdfSopenharmony_ci struct test_case *tc = tcases + n; 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_ci TEST(request_key("keyring", tc->des, NULL, *tc->id)); 47f08c3bdfSopenharmony_ci if (TST_RET != -1) { 48f08c3bdfSopenharmony_ci tst_res(TFAIL, "request_key() succeed unexpectly"); 49f08c3bdfSopenharmony_ci return; 50f08c3bdfSopenharmony_ci } 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci if (TST_ERR == tc->exp_err) { 53f08c3bdfSopenharmony_ci tst_res(TPASS | TTERRNO, "request_key() failed expectly"); 54f08c3bdfSopenharmony_ci return; 55f08c3bdfSopenharmony_ci } 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "request_key() failed unexpectly, " 58f08c3bdfSopenharmony_ci "expected %s", tst_strerrno(tc->exp_err)); 59f08c3bdfSopenharmony_ci} 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_cistatic int init_key(char *name, int cmd) 62f08c3bdfSopenharmony_ci{ 63f08c3bdfSopenharmony_ci int n; 64f08c3bdfSopenharmony_ci int sec = 1; 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci n = add_key("keyring", name, NULL, 0, KEY_SPEC_THREAD_KEYRING); 67f08c3bdfSopenharmony_ci if (n == -1) 68f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "add_key() failed"); 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci if (cmd == KEYCTL_REVOKE) { 71f08c3bdfSopenharmony_ci if (keyctl(cmd, n) == -1) { 72f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "failed to revoke a key"); 73f08c3bdfSopenharmony_ci } 74f08c3bdfSopenharmony_ci } 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci if (cmd == KEYCTL_SET_TIMEOUT) { 77f08c3bdfSopenharmony_ci if (keyctl(cmd, n, sec) == -1) { 78f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, 79f08c3bdfSopenharmony_ci "failed to set timeout for a key"); 80f08c3bdfSopenharmony_ci } 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci sleep(sec + 1); 83f08c3bdfSopenharmony_ci } 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci return n; 86f08c3bdfSopenharmony_ci} 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_cistatic void setup(void) 89f08c3bdfSopenharmony_ci{ 90f08c3bdfSopenharmony_ci key1 = KEY_REQKEY_DEFL_DEFAULT; 91f08c3bdfSopenharmony_ci key2 = init_key("ltp2", KEYCTL_REVOKE); 92f08c3bdfSopenharmony_ci key3 = init_key("ltp3", KEYCTL_SET_TIMEOUT); 93f08c3bdfSopenharmony_ci} 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_cistatic struct tst_test test = { 96f08c3bdfSopenharmony_ci .setup = setup, 97f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 98f08c3bdfSopenharmony_ci .test = verify_request_key, 99f08c3bdfSopenharmony_ci}; 100