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