test: fix a few scan-build errors about dead storage
[platform/upstream/libevdev.git] / test / test-common.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24 #include <check.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28
29 #include "test-common.h"
30
31 void test_logfunc_abort_on_error(enum libevdev_log_priority priority,
32                                  void *data,
33                                  const char *file, int line,
34                                  const char *func,
35                                  const char *format, va_list args)
36 {
37         vprintf(format, args);
38         ck_abort();
39 }
40
41 void test_logfunc_ignore_error(enum libevdev_log_priority priority,
42                                void *data,
43                                const char *file, int line,
44                                const char *func,
45                                const char *format, va_list args)
46 {
47 }
48
49 void test_create_device(struct uinput_device **uidev_return,
50                         struct libevdev **dev_return,
51                         ...)
52 {
53         int rc, fd;
54         struct uinput_device *uidev;
55         struct libevdev *dev;
56         va_list args;
57
58         va_start(args, dev_return);
59
60         rc = uinput_device_new_with_events_v(&uidev, TEST_DEVICE_NAME, DEFAULT_IDS, args);
61         va_end(args);
62
63         ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
64
65         fd = uinput_device_get_fd(uidev);
66
67         rc = libevdev_new_from_fd(fd, &dev);
68         ck_assert_msg(rc == 0, "Failed to init device device: %s", strerror(-rc));
69         rc = fcntl(fd, F_SETFL, O_NONBLOCK);
70         ck_assert_msg(rc == 0, "fcntl failed: %s", strerror(errno));
71
72         *uidev_return = uidev;
73         *dev_return = dev;
74 }
75
76 void test_create_abs_device(struct uinput_device **uidev_return,
77                             struct libevdev **dev_return,
78                             int nabs,
79                             const struct input_absinfo *abs,
80                             ...)
81 {
82         int rc, fd;
83         struct uinput_device *uidev;
84         struct libevdev *dev;
85         va_list args;
86
87         uidev = uinput_device_new(TEST_DEVICE_NAME);
88         ck_assert(uidev != NULL);
89
90         va_start(args, abs);
91         rc = uinput_device_set_event_bits_v(uidev, args);
92         ck_assert_msg(rc == 0, "Failed to set uinput bits");
93         va_end(args);
94
95         while (--nabs >= 0) {
96                 int code;
97                 struct input_absinfo a;
98
99                 code = abs[nabs].value;
100                 a = abs[nabs];
101                 a.value = 0;
102
103                 rc = uinput_device_set_abs_bit(uidev, code, &a);
104                 ck_assert_msg(rc == 0, "for abs field %d\n", nabs);
105         }
106
107         rc = uinput_device_create(uidev);
108         ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
109
110         fd = uinput_device_get_fd(uidev);
111
112         rc = libevdev_new_from_fd(fd, &dev);
113         ck_assert_msg(rc == 0, "Failed to init device device: %s", strerror(-rc));
114         rc = fcntl(fd, F_SETFL, O_NONBLOCK);
115         ck_assert_msg(rc == 0, "fcntl failed: %s", strerror(errno));
116
117         *uidev_return = uidev;
118         *dev_return = dev;
119 }