xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setdomainname/setdomainname02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2019 Petr Vorel <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Saji Kumar.V.R <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker #include "setdomainname.h"
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker #define ERRNO_DESC(x) .exp_errno = x, .errno_desc = #x
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #define MAX_NAME_LENGTH _UTSNAME_DOMAIN_LENGTH - 1
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker struct test_case {
15*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
16*49cdfc7eSAndroid Build Coastguard Worker 	char *name;
17*49cdfc7eSAndroid Build Coastguard Worker 	int len;
18*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
19*49cdfc7eSAndroid Build Coastguard Worker 	char *errno_desc;
20*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
21*49cdfc7eSAndroid Build Coastguard Worker 	{ "len == -1", TST_VALID_DOMAIN_NAME, -1, ERRNO_DESC(EINVAL) },
22*49cdfc7eSAndroid Build Coastguard Worker 	{ "len > allowed maximum", TST_VALID_DOMAIN_NAME, MAX_NAME_LENGTH + 1, ERRNO_DESC(EINVAL) },
23*49cdfc7eSAndroid Build Coastguard Worker 	{ "name == NULL", NULL, MAX_NAME_LENGTH, ERRNO_DESC(EFAULT) }
24*49cdfc7eSAndroid Build Coastguard Worker };
25*49cdfc7eSAndroid Build Coastguard Worker 
verify_setdomainname(unsigned int nr)26*49cdfc7eSAndroid Build Coastguard Worker void verify_setdomainname(unsigned int nr)
27*49cdfc7eSAndroid Build Coastguard Worker {
28*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tcase = &tcases[nr];
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker 	TEST(do_setdomainname(tcase->name, (size_t) tcase->len));
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "testing %s", tcase->desc);
33*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
34*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "unexpected exit code: %ld", TST_RET);
35*49cdfc7eSAndroid Build Coastguard Worker 		return;
36*49cdfc7eSAndroid Build Coastguard Worker 	}
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR != tcase->exp_errno) {
39*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "unexpected errno: %d, expected: %d",
40*49cdfc7eSAndroid Build Coastguard Worker 			TST_ERR, tcase->exp_errno);
41*49cdfc7eSAndroid Build Coastguard Worker 		return;
42*49cdfc7eSAndroid Build Coastguard Worker 	}
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS | TTERRNO, "expected failure");
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
48*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
49*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
50*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
51*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
52*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_setdomainname,
53*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = TEST_VARIANTS,
54*49cdfc7eSAndroid Build Coastguard Worker };
55