xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/socketcall/socketcall02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
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) 2020 Yang Xu <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Sowmya Adiga <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  * This is a error test for the socketcall(2) system call.
8*49cdfc7eSAndroid Build Coastguard Worker  */
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <linux/net.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static unsigned long args_valid[3] = {PF_INET, SOCK_STREAM, 0};
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
24*49cdfc7eSAndroid Build Coastguard Worker 	int call;
25*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long *args;
26*49cdfc7eSAndroid Build Coastguard Worker 	int exp_err;
27*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
28*49cdfc7eSAndroid Build Coastguard Worker } TC[] = {
29*49cdfc7eSAndroid Build Coastguard Worker 	{0, args_valid, EINVAL, "invalid call(<1)"},
30*49cdfc7eSAndroid Build Coastguard Worker 	{21, args_valid, EINVAL, "invalid call(>20)"},
31*49cdfc7eSAndroid Build Coastguard Worker 	{SYS_SOCKET, NULL, EFAULT, "invalid args address"},
32*49cdfc7eSAndroid Build Coastguard Worker };
33*49cdfc7eSAndroid Build Coastguard Worker 
verify_socketcall(unsigned int i)34*49cdfc7eSAndroid Build Coastguard Worker static void verify_socketcall(unsigned int i)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "%s", TC[i].desc);
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_socketcall, TC[i].call, TC[i].args));
39*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
40*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "socketcall() succeeded unexpectedly");
41*49cdfc7eSAndroid Build Coastguard Worker 		return;
42*49cdfc7eSAndroid Build Coastguard Worker 	}
43*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == TC[i].exp_err)
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "socketcall() failed as expected ");
45*49cdfc7eSAndroid Build Coastguard Worker 	else
46*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "socketcall fail expected %s got", tst_strerrno(TC[i].exp_err));
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)49*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(TC); i++) {
54*49cdfc7eSAndroid Build Coastguard Worker 		if (!TC[i].args)
55*49cdfc7eSAndroid Build Coastguard Worker 			TC[i].args = tst_get_bad_addr(NULL);
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
60*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
61*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_socketcall,
62*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(TC),
63*49cdfc7eSAndroid Build Coastguard Worker };
64*49cdfc7eSAndroid Build Coastguard Worker 
65