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