• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19  /* Description:
20  *   Verify that:
21  *		1) kcmp fails with bad pid
22  *		2) kcmp fails with invalid flag
23  *		3) kcmp fails with invalid flag
24  *		4) kcmp fails with invalid flag
25  *		5) kcmp fails with invalid flag
26  *		6) kcmp fails with invalid fd
27  */
28 
29 #define _GNU_SOURCE
30 
31 #include "test.h"
32 #include "safe_macros.h"
33 #include "lapi/fcntl.h"
34 #include "kcmp.h"
35 
36 #define TEST_FILE "test_file"
37 #define TEST_FILE2 "test_file2"
38 
39 static int fd1;
40 static int fd2;
41 static int fd_fake;
42 static int pid1;
43 static int pid_unused;
44 static int fd_fake = -1;
45 
46 char *TCID = "kcmp02";
47 
48 #include <sys/types.h>
49 #include <sys/wait.h>
50 
51 static struct test_case {
52 	int *pid1;
53 	int *pid2;
54 	int type;
55 	int *fd1;
56 	int *fd2;
57 	int exp_errno;
58 } test_cases[] = {
59 	{&pid1, &pid_unused, KCMP_FILE, &fd1, &fd2, ESRCH},
60 	{&pid1, &pid1, KCMP_TYPES + 1, &fd1, &fd2, EINVAL},
61 	{&pid1, &pid1, -1, &fd1, &fd2, EINVAL},
62 	{&pid1, &pid1, INT_MIN, &fd1, &fd2, EINVAL},
63 	{&pid1, &pid1, INT_MAX, &fd1, &fd2, EINVAL},
64 	{&pid1, &pid1, KCMP_FILE, &fd1, &fd_fake, EBADF}
65 };
66 
67 int TST_TOTAL = ARRAY_SIZE(test_cases);
68 
69 static void cleanup(void);
70 static void setup(void);
71 static void kcmp_verify(const struct test_case *test);
72 
main(int ac,char ** av)73 int main(int ac, char **av)
74 {
75 	int lc;
76 	int i;
77 
78 	tst_parse_opts(ac, av, NULL, NULL);
79 	setup();
80 
81 	for (lc = 0; TEST_LOOPING(lc); lc++) {
82 		tst_count = 0;
83 
84 		for (i = 0; i < TST_TOTAL; i++)
85 			kcmp_verify(&test_cases[i]);
86 
87 	}
88 
89 	cleanup();
90 	tst_exit();
91 }
92 
kcmp_verify(const struct test_case * test)93 static void kcmp_verify(const struct test_case *test)
94 {
95 	TEST(kcmp(*(test->pid1), *(test->pid2), test->type,
96 			  *(test->fd1), *(test->fd2)));
97 
98 	if (TEST_RETURN != -1) {
99 		tst_resm(TFAIL, "kcmp() succeeded unexpectedly");
100 		return;
101 	}
102 
103 	if (test->exp_errno == TEST_ERRNO) {
104 		tst_resm(TPASS | TTERRNO, "kcmp() returned the expected value");
105 		return;
106 	}
107 
108 	tst_resm(TFAIL | TTERRNO,
109 		"kcmp() got unexpected return value: expected: %d - %s",
110 			test->exp_errno, tst_strerrno(test->exp_errno));
111 }
112 
setup(void)113 static void setup(void)
114 {
115 	if ((tst_kvercmp(3, 5, 0)) < 0) {
116 		tst_brkm(TCONF, NULL,
117 			"This test can only run on kernels that are 3.5. and higher");
118 	}
119 
120 	tst_tmpdir();
121 
122 	pid1 = getpid();
123 	pid_unused = tst_get_unused_pid(cleanup);
124 	fd1 = SAFE_OPEN(cleanup, TEST_FILE, O_CREAT | O_RDWR | O_TRUNC);
125 	fd2 = SAFE_OPEN(cleanup, TEST_FILE2, O_CREAT | O_RDWR | O_TRUNC);
126 
127 }
128 
cleanup(void)129 static void cleanup(void)
130 {
131 	if (fd1 > 0 && close(fd1) < 0)
132 		tst_resm(TWARN | TERRNO, "close fd1 failed");
133 
134 	if (fd2 > 0 && close(fd2) < 0)
135 		tst_resm(TWARN | TERRNO, "close fd2 failed");
136 	tst_rmdir();
137 
138 }
139