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