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 <check.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <libinput.h>
30 #include <unistd.h>
31 #include <stdarg.h>
32
33 #include "litest.h"
34
35 static int log_handler_called;
36 static struct libinput *log_handler_context;
37
38 static void
simple_log_handler(struct libinput * libinput,enum libinput_log_priority priority,const char * format,va_list args)39 simple_log_handler(struct libinput *libinput,
40 enum libinput_log_priority priority,
41 const char *format,
42 va_list args)
43 {
44 log_handler_called++;
45 if (log_handler_context)
46 litest_assert_ptr_eq(libinput, log_handler_context);
47 litest_assert_notnull(format);
48 }
49
START_TEST(log_default_priority)50 START_TEST(log_default_priority)
51 {
52 enum libinput_log_priority pri;
53 struct libinput *li;
54
55 li = litest_create_context();
56 pri = libinput_log_get_priority(li);
57
58 ck_assert_int_eq(pri, LIBINPUT_LOG_PRIORITY_ERROR);
59
60 litest_destroy_context(li);
61 }
62 END_TEST
63
START_TEST(log_handler_invoked)64 START_TEST(log_handler_invoked)
65 {
66 struct libinput *li;
67
68 log_handler_context = NULL;
69 log_handler_called = 0;
70
71 li = litest_create_context();
72
73 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
74 libinput_log_set_handler(li, simple_log_handler);
75 log_handler_context = li;
76
77 libinput_path_add_device(li, "/tmp");
78
79 ck_assert_int_gt(log_handler_called, 0);
80
81 litest_destroy_context(li);
82
83 log_handler_context = NULL;
84 log_handler_called = 0;
85 }
86 END_TEST
87
START_TEST(log_handler_NULL)88 START_TEST(log_handler_NULL)
89 {
90 struct libinput *li;
91
92 log_handler_called = 0;
93
94 li = litest_create_context();
95 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
96 libinput_log_set_handler(li, NULL);
97
98 libinput_path_add_device(li, "/tmp");
99
100 ck_assert_int_eq(log_handler_called, 0);
101
102 litest_destroy_context(li);
103
104 log_handler_called = 0;
105 }
106 END_TEST
107
START_TEST(log_priority)108 START_TEST(log_priority)
109 {
110 struct libinput *li;
111
112 log_handler_context = NULL;
113 log_handler_called = 0;
114
115 li = litest_create_context();
116 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_ERROR);
117 libinput_log_set_handler(li, simple_log_handler);
118 log_handler_context = li;
119
120 libinput_path_add_device(li, "/tmp");
121
122 ck_assert_int_eq(log_handler_called, 1);
123
124 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
125 /* event0 exists on any box we care to run the test suite on and we
126 * currently prints *something* for each device */
127 libinput_path_add_device(li, "/dev/input/event0");
128 ck_assert_int_gt(log_handler_called, 1);
129
130 litest_destroy_context(li);
131
132 log_handler_context = NULL;
133 log_handler_called = 0;
134 }
135 END_TEST
136
137 static int axisrange_log_handler_called = 0;
138
139 static void
axisrange_warning_log_handler(struct libinput * libinput,enum libinput_log_priority priority,const char * format,va_list args)140 axisrange_warning_log_handler(struct libinput *libinput,
141 enum libinput_log_priority priority,
142 const char *format,
143 va_list args)
144 {
145 const char *substr;
146
147 axisrange_log_handler_called++;
148 litest_assert_notnull(format);
149
150 substr = strstr(format, "is outside expected range");
151 litest_assert_notnull(substr);
152 }
153
START_TEST(log_axisrange_warning)154 START_TEST(log_axisrange_warning)
155 {
156 struct litest_device *dev = litest_current_device();
157 struct libinput *li = dev->libinput;
158 const struct input_absinfo *abs;
159 int axis = _i; /* looped test */
160
161 litest_touch_down(dev, 0, 90, 100);
162 litest_drain_events(li);
163
164 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_INFO);
165 libinput_log_set_handler(li, axisrange_warning_log_handler);
166
167 abs = libevdev_get_abs_info(dev->evdev, axis);
168
169 for (int i = 0; i < 100; i++) {
170 litest_event(dev, 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 libinput_dispatch(li);
176 }
177
178 /* Expect only one message per 5 min */
179 ck_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
TEST_COLLECTION(log)187 TEST_COLLECTION(log)
188 {
189 struct range axes = { ABS_X, ABS_Y + 1};
190
191 litest_add_deviceless("log:defaults", log_default_priority);
192 litest_add_deviceless("log:logging", log_handler_invoked);
193 litest_add_deviceless("log:logging", log_handler_NULL);
194 litest_add_no_device("log:logging", log_priority);
195
196 /* mtdev clips to axis ranges */
197 litest_add_ranged("log:warnings", log_axisrange_warning, LITEST_TOUCH, LITEST_PROTOCOL_A, &axes);
198 litest_add_ranged("log:warnings", log_axisrange_warning, LITEST_TOUCHPAD, LITEST_ANY, &axes);
199 }
200