1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci /* Description:
7f08c3bdfSopenharmony_ci *   Verify that:
8f08c3bdfSopenharmony_ci *		1) kcmp fails with bad pid
9f08c3bdfSopenharmony_ci *		2) kcmp fails with invalid flag
10f08c3bdfSopenharmony_ci *		3) kcmp fails with invalid flag
11f08c3bdfSopenharmony_ci *		4) kcmp fails with invalid flag
12f08c3bdfSopenharmony_ci *		5) kcmp fails with invalid flag
13f08c3bdfSopenharmony_ci *		6) kcmp fails with invalid fd
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#define _GNU_SOURCE
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
20f08c3bdfSopenharmony_ci#include "lapi/kcmp.h"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#define TEST_FILE "test_file"
23f08c3bdfSopenharmony_ci#define TEST_FILE2 "test_file2"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic int fd1;
26f08c3bdfSopenharmony_cistatic int fd2;
27f08c3bdfSopenharmony_cistatic int fd_fake;
28f08c3bdfSopenharmony_cistatic int pid1;
29f08c3bdfSopenharmony_cistatic int pid_unused;
30f08c3bdfSopenharmony_cistatic int fd_fake = -1;
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci#include <sys/types.h>
33f08c3bdfSopenharmony_ci#include <sys/wait.h>
34f08c3bdfSopenharmony_ci#include <limits.h>
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic struct test_case {
37f08c3bdfSopenharmony_ci	int *pid1;
38f08c3bdfSopenharmony_ci	int *pid2;
39f08c3bdfSopenharmony_ci	int type;
40f08c3bdfSopenharmony_ci	int *fd1;
41f08c3bdfSopenharmony_ci	int *fd2;
42f08c3bdfSopenharmony_ci	int exp_errno;
43f08c3bdfSopenharmony_ci} test_cases[] = {
44f08c3bdfSopenharmony_ci	{&pid1, &pid_unused, KCMP_FILE, &fd1, &fd2, ESRCH},
45f08c3bdfSopenharmony_ci	{&pid1, &pid1, KCMP_TYPES + 1, &fd1, &fd2, EINVAL},
46f08c3bdfSopenharmony_ci	{&pid1, &pid1, -1, &fd1, &fd2, EINVAL},
47f08c3bdfSopenharmony_ci	{&pid1, &pid1, INT_MIN, &fd1, &fd2, EINVAL},
48f08c3bdfSopenharmony_ci	{&pid1, &pid1, INT_MAX, &fd1, &fd2, EINVAL},
49f08c3bdfSopenharmony_ci	{&pid1, &pid1, KCMP_FILE, &fd1, &fd_fake, EBADF}
50f08c3bdfSopenharmony_ci};
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void setup(void)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	pid1 = getpid();
55f08c3bdfSopenharmony_ci	pid_unused = tst_get_unused_pid();
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	fd1 = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR | O_TRUNC, 0644);
58f08c3bdfSopenharmony_ci	fd2 = SAFE_OPEN(TEST_FILE2, O_CREAT | O_RDWR | O_TRUNC, 0644);
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic void cleanup(void)
62f08c3bdfSopenharmony_ci{
63f08c3bdfSopenharmony_ci	if (fd1 > 0)
64f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd1);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	if (fd2 > 0)
67f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd2);
68f08c3bdfSopenharmony_ci}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic void verify_kcmp(unsigned int n)
71f08c3bdfSopenharmony_ci{
72f08c3bdfSopenharmony_ci	struct test_case *test = &test_cases[n];
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	TEST(kcmp(*(test->pid1), *(test->pid2), test->type,
75f08c3bdfSopenharmony_ci		  *(test->fd1), *(test->fd2)));
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
78f08c3bdfSopenharmony_ci		tst_res(TFAIL, "kcmp() succeeded unexpectedly");
79f08c3bdfSopenharmony_ci		return;
80f08c3bdfSopenharmony_ci	}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	if (test->exp_errno == TST_ERR) {
83f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "kcmp() returned the expected value");
84f08c3bdfSopenharmony_ci		return;
85f08c3bdfSopenharmony_ci	}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	tst_res(TFAIL | TTERRNO,
88f08c3bdfSopenharmony_ci		"kcmp() got unexpected return value: expected: %d - %s",
89f08c3bdfSopenharmony_ci			test->exp_errno, tst_strerrno(test->exp_errno));
90f08c3bdfSopenharmony_ci}
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_cistatic struct tst_test test = {
93f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(test_cases),
94f08c3bdfSopenharmony_ci	.setup = setup,
95f08c3bdfSopenharmony_ci	.cleanup = cleanup,
96f08c3bdfSopenharmony_ci	.test = verify_kcmp,
97f08c3bdfSopenharmony_ci	.needs_tmpdir = 1
98f08c3bdfSopenharmony_ci};
99