Debian Codegraph @

test/test-log.c

raw
1 /*
2 * Copyright © 2014 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <config.h>
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <libinput.h>
29 #include <stdarg.h>
30 #include <unistd.h>
31
32 #include "litest.h"
33
34 static int log_handler_called;
35 static struct libinput *log_handler_context;
36
37 static void
38 simple_log_handler(struct libinput *libinput,
39 enum libinput_log_priority priority,
40 const char *format,
41 va_list args)
42 {
43 log_handler_called++;
44 if (log_handler_context)
45 litest_assert_ptr_eq(libinput, log_handler_context);
46 litest_assert_notnull(format);
47 }
48
49 static int
50 open_restricted(const char *path, int flags, void *data)
51 {
52 int fd;
53 fd = open(path, flags);
54 return fd < 0 ? -errno : fd;
55 }
56 static void
57 close_restricted(int fd, void *data)
58 {
59 close(fd);
60 }
61
62 static const struct libinput_interface simple_interface = {
63 .open_restricted = open_restricted,
64 .close_restricted = close_restricted,
65 };
66
67 START_TEST(log_default_priority)
68 {
69 enum libinput_log_priority pri;
70
71 _unref_(libinput) *li = libinput_path_create_context(&simple_interface, NULL);
72 pri = libinput_log_get_priority(li);
73
74 litest_assert_enum_eq(pri, LIBINPUT_LOG_PRIORITY_ERROR);
75 }
76 END_TEST
77
78 START_TEST(log_handler_invoked)
79 {
80 log_handler_context = NULL;
81 log_handler_called = 0;
82
83 _litest_context_destroy_ struct libinput *li = litest_create_context();
84
85 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
86 libinput_log_set_handler(li, simple_log_handler);
87 log_handler_context = li;
88
89 libinput_path_add_device(li, "/tmp");
90
91 litest_assert_int_gt(log_handler_called, 0);
92
93 log_handler_context = NULL;
94 log_handler_called = 0;
95 }
96 END_TEST
97
98 START_TEST(log_handler_NULL)
99 {
100 log_handler_called = 0;
101
102 _litest_context_destroy_ struct libinput *li = litest_create_context();
103 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
104 libinput_log_set_handler(li, NULL);
105
106 libinput_path_add_device(li, "/tmp");
107
108 litest_assert_int_eq(log_handler_called, 0);
109
110 log_handler_called = 0;
111 }
112 END_TEST
113
114 START_TEST(log_priority)
115 {
116 log_handler_context = NULL;
117 log_handler_called = 0;
118
119 _litest_context_destroy_ struct libinput *li = litest_create_context();
120 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_ERROR);
121 libinput_log_set_handler(li, simple_log_handler);
122 log_handler_context = li;
123
124 libinput_path_add_device(li, "/tmp");
125
126 litest_assert_int_eq(log_handler_called, 1);
127
128 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
129 /* event0 exists on any box we care to run the test suite on and we
130 * currently prints *something* for each device */
131 libinput_path_add_device(li, "/dev/input/event0");
132 litest_assert_int_gt(log_handler_called, 1);
133
134 log_handler_context = NULL;
135 log_handler_called = 0;
136 }
137 END_TEST
138
139 static int axisrange_log_handler_called = 0;
140
141 static void
142 axisrange_warning_log_handler(struct libinput *libinput,
143 enum libinput_log_priority priority,
144 const char *format,
145 va_list args)
146 {
147 axisrange_log_handler_called++;
148 litest_assert_notnull(format);
149
150 litest_assert_str_in("is outside expected range", format);
151 }
152
153 START_TEST(log_axisrange_warning)
154 {
155 struct litest_device *dev = litest_current_device();
156 struct libinput *li = dev->libinput;
157 const struct input_absinfo *abs;
158 int axis = litest_test_param_get_i32(test_env->params, "axis");
159
160 litest_touch_down(dev, 0, 90, 100);
161 litest_drain_events(li);
162
163 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
164 libinput_log_set_handler(li, axisrange_warning_log_handler);
165
166 abs = libevdev_get_abs_info(dev->evdev, axis);
167
168 for (int i = 0; i < 100; i++) {
169 litest_event(dev,
170 EV_ABS,
171 ABS_MT_POSITION_X + axis,
172 abs->maximum * 2 + i);
173 litest_event(dev, EV_ABS, axis, abs->maximum * 2);
174 litest_event(dev, EV_SYN, SYN_REPORT, 0);
175 litest_dispatch(li);
176 }
177
178 /* Expect only one message per 5 min */
179 litest_assert_int_eq(axisrange_log_handler_called, 1);
180
181 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_ERROR);
182 litest_restore_log_handler(li);
183 axisrange_log_handler_called = 0;
184 }
185 END_TEST
186
187 TEST_COLLECTION(log)
188 {
189 /* clang-format off */
190 litest_add_deviceless(log_default_priority);
191 litest_add_deviceless(log_handler_invoked);
192 litest_add_deviceless(log_handler_NULL);
193 litest_add_no_device(log_priority);
194
195 litest_with_parameters(params, "axis", 'I', 2, litest_named_i32(ABS_X), litest_named_i32(ABS_Y)) {
196 /* mtdev clips to axis ranges */
197 litest_add_parametrized(log_axisrange_warning, LITEST_TOUCH, LITEST_PROTOCOL_A, params);
198 litest_add_parametrized(log_axisrange_warning, LITEST_TOUCHPAD, LITEST_ANY, params);
199 }
200 /* clang-format on */
201 }