blob: 1c471951958e71a9d167fe77e0cdefb3adc6e127 [file] [log] [blame]
Zeng Linggang9ac868c2014-06-27 10:02:52 +08001/*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17/*
18 * DESCRIPTION
19 * Verify that,
20 * 1. fd is not a valid file descriptor, EBADF would return.
21 * 2. old_value is not valid a pointer, EFAULT would return.
22 * 3. fd is not a valid timerfd file descriptor, EINVAL would return.
23 * 4. flags is invalid, EINVAL would return.
24 */
25
26#define _GNU_SOURCE
27
28#include <errno.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32
33#include "test.h"
Zeng Linggang9ac868c2014-06-27 10:02:52 +080034#include "safe_macros.h"
35#include "lapi/timerfd.h"
36
37char *TCID = "timerfd_settime01";
38
39static int bad_clockfd = -1;
40static int clockfd;
41static int fd;
42
43static struct test_case_t {
44 int *fd;
45 int flags;
46 struct itimerspec *old_value;
47 int exp_errno;
48} test_cases[] = {
49 {&bad_clockfd, 0, NULL, EBADF},
50 {&clockfd, 0, (struct itimerspec *)-1, EFAULT},
51 {&fd, 0, NULL, EINVAL},
52 {&clockfd, -1, NULL, EINVAL},
53};
54
55int TST_TOTAL = ARRAY_SIZE(test_cases);
56static void setup(void);
57static void timerfd_settime_verify(const struct test_case_t *);
58static void cleanup(void);
Jan Stancekd9c95092014-08-20 10:35:03 +020059static struct itimerspec new_value;
Zeng Linggang9ac868c2014-06-27 10:02:52 +080060
61int main(int argc, char *argv[])
62{
63 int lc;
64 const char *msg;
65 int i;
66
67 msg = parse_opts(argc, argv, NULL, NULL);
68 if (msg != NULL)
69 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
70
71 setup();
72
73 for (lc = 0; TEST_LOOPING(lc); lc++) {
74 tst_count = 0;
75 for (i = 0; i < TST_TOTAL; i++)
76 timerfd_settime_verify(&test_cases[i]);
77 }
78
79 cleanup();
80 tst_exit();
81}
82
83static void setup(void)
84{
85 if ((tst_kvercmp(2, 6, 25)) < 0)
86 tst_brkm(TCONF, NULL, "This test needs kernel 2.6.25 or newer");
87
88 tst_sig(NOFORK, DEF_HANDLER, cleanup);
89
90 TEST_PAUSE;
91
Zeng Linggang9ac868c2014-06-27 10:02:52 +080092 tst_tmpdir();
93
94 clockfd = timerfd_create(CLOCK_REALTIME, 0);
95 if (clockfd == -1)
96 tst_brkm(TBROK | TERRNO, cleanup, "timerfd_create() fail");
97
98 fd = SAFE_OPEN(cleanup, "test_file", O_RDWR | O_CREAT, 0644);
99}
100
101static void timerfd_settime_verify(const struct test_case_t *test)
102{
Zeng Linggang9ac868c2014-06-27 10:02:52 +0800103 TEST(timerfd_settime(*test->fd, test->flags, &new_value,
104 test->old_value));
105
106 if (TEST_RETURN != -1) {
107 tst_resm(TFAIL, "timerfd_settime() succeeded unexpectedly");
108 return;
109 }
110
111 if (TEST_ERRNO == test->exp_errno) {
112 tst_resm(TPASS | TTERRNO,
113 "timerfd_settime() failed as expected");
114 } else {
115 tst_resm(TFAIL | TTERRNO,
116 "timerfd_settime() failed unexpectedly; expected: "
117 "%d - %s", test->exp_errno, strerror(test->exp_errno));
118 }
119}
120
121static void cleanup(void)
122{
Zeng Linggang9ac868c2014-06-27 10:02:52 +0800123 if (clockfd > 0)
124 close(clockfd);
125
126 if (fd > 0)
127 close(fd);
128
129 tst_rmdir();
130}