a28182072ce64e1d6f8656202d950215750995b5
[platform/upstream/libinput.git] / test / log.c
1 /*
2  * Copyright © 2014 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24
25 #include <check.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <libinput.h>
29 #include <libudev.h>
30 #include <unistd.h>
31
32 #include "litest.h"
33
34 static int log_handler_called;
35 static void *log_handler_userdata;
36
37 static int open_restricted(const char *path, int flags, void *data)
38 {
39         int fd;
40         fd = open(path, flags);
41         return fd < 0 ? -errno : fd;
42 }
43 static void close_restricted(int fd, void *data)
44 {
45         close(fd);
46 }
47
48 const struct libinput_interface simple_interface = {
49         .open_restricted = open_restricted,
50         .close_restricted = close_restricted,
51 };
52
53 static void
54 simple_log_handler(enum libinput_log_priority priority,
55                    void *userdata,
56                    const char *format,
57                    va_list args)
58 {
59         log_handler_called++;
60         ck_assert(userdata == log_handler_userdata);
61         ck_assert(format != NULL);
62 }
63
64 START_TEST(log_default_priority)
65 {
66         enum libinput_log_priority pri;
67
68         pri = libinput_log_get_priority();
69
70         ck_assert_int_eq(pri, LIBINPUT_LOG_PRIORITY_ERROR);
71 }
72 END_TEST
73
74 START_TEST(log_handler_invoked)
75 {
76         struct libinput *li;
77         enum libinput_log_priority pri = libinput_log_get_priority();
78
79         libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
80         libinput_log_set_handler(simple_log_handler, NULL);
81         log_handler_userdata = NULL;
82
83         li = libinput_path_create_context(&simple_interface, NULL);
84         libinput_path_add_device(li, "/tmp");
85
86         ck_assert_int_gt(log_handler_called, 0);
87         log_handler_called = 0;
88
89         libinput_destroy(li);
90         libinput_log_set_priority(pri);
91 }
92 END_TEST
93
94 START_TEST(log_userdata_NULL)
95 {
96         struct libinput *li;
97         enum libinput_log_priority pri = libinput_log_get_priority();
98
99         libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
100         libinput_log_set_handler(simple_log_handler, NULL);
101         log_handler_userdata = NULL;
102
103         li = libinput_path_create_context(&simple_interface, NULL);
104         libinput_path_add_device(li, "/tmp");
105
106         ck_assert_int_gt(log_handler_called, 0);
107         log_handler_called = 0;
108
109         libinput_destroy(li);
110
111         libinput_log_set_priority(pri);
112 }
113 END_TEST
114
115 START_TEST(log_userdata)
116 {
117         struct libinput *li;
118         enum libinput_log_priority pri = libinput_log_get_priority();
119
120         libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
121         libinput_log_set_handler(simple_log_handler, &li);
122         log_handler_userdata = &li;
123
124         li = libinput_path_create_context(&simple_interface, NULL);
125         libinput_path_add_device(li, "/tmp");
126
127         ck_assert_int_gt(log_handler_called, 0);
128         log_handler_called = 0;
129
130         libinput_destroy(li);
131         libinput_log_set_priority(pri);
132 }
133 END_TEST
134
135 START_TEST(log_handler_NULL)
136 {
137         struct libinput *li;
138         enum libinput_log_priority pri = libinput_log_get_priority();
139
140         libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
141         libinput_log_set_handler(NULL, NULL);
142         log_handler_userdata = NULL;
143
144         li = libinput_path_create_context(&simple_interface, NULL);
145         libinput_path_add_device(li, "/tmp");
146
147         ck_assert_int_eq(log_handler_called, 0);
148         log_handler_called = 0;
149         libinput_log_set_handler(simple_log_handler, NULL);
150
151         libinput_destroy(li);
152         libinput_log_set_priority(pri);
153 }
154 END_TEST
155
156 START_TEST(log_priority)
157 {
158         struct libinput *li;
159         enum libinput_log_priority pri = libinput_log_get_priority();
160
161         libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_ERROR);
162         libinput_log_set_handler(simple_log_handler, NULL);
163         log_handler_userdata = NULL;
164
165         li = libinput_path_create_context(&simple_interface, NULL);
166         libinput_path_add_device(li, "/tmp");
167
168         ck_assert_int_eq(log_handler_called, 0);
169
170         libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_INFO);
171         libinput_path_add_device(li, "/tmp");
172         ck_assert_int_gt(log_handler_called, 0);
173
174         log_handler_called = 0;
175
176         libinput_destroy(li);
177         libinput_log_set_priority(pri);
178 }
179 END_TEST
180
181 int main (int argc, char **argv) {
182         litest_add_no_device("log:defaults", log_default_priority);
183         litest_add_no_device("log:logging", log_handler_invoked);
184         litest_add_no_device("log:logging", log_handler_NULL);
185         litest_add_no_device("log:logging", log_userdata);
186         litest_add_no_device("log:logging", log_userdata_NULL);
187         litest_add_no_device("log:logging", log_priority);
188
189         return litest_run(argc, argv);
190 }