1 /*
2 * Copyright © 2019 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
28 /* remove the main() from the included program so we can define our own */
29 #define main __disabled
30 int main(int argc, char **argv);
31 #include "libinput-fuzz-extract.c"
32 #undef main
33
START_TEST(test_parse_ev_abs)34 START_TEST(test_parse_ev_abs)
35 {
36 struct test {
37 uint32_t which;
38 const char *prop;
39 int min, max, res, fuzz, flat;
40
41 } tests[] = {
42 { .which = (MIN|MAX),
43 .prop = "1:2",
44 .min = 1, .max = 2 },
45 { .which = (MIN|MAX),
46 .prop = "1:2:",
47 .min = 1, .max = 2 },
48 { .which = (MIN|MAX|RES),
49 .prop = "10:20:30",
50 .min = 10, .max = 20, .res = 30 },
51 { .which = (RES),
52 .prop = "::100",
53 .res = 100 },
54 { .which = (MIN),
55 .prop = "10:",
56 .min = 10 },
57 { .which = (MAX|RES),
58 .prop = ":10:1001",
59 .max = 10, .res = 1001 },
60 { .which = (MIN|MAX|RES|FUZZ),
61 .prop = "1:2:3:4",
62 .min = 1, .max = 2, .res = 3, .fuzz = 4},
63 { .which = (MIN|MAX|RES|FUZZ|FLAT),
64 .prop = "1:2:3:4:5",
65 .min = 1, .max = 2, .res = 3, .fuzz = 4, .flat = 5},
66 { .which = (MIN|RES|FUZZ|FLAT),
67 .prop = "1::3:4:50",
68 .min = 1, .res = 3, .fuzz = 4, .flat = 50},
69 { .which = FUZZ|FLAT,
70 .prop = ":::5:60",
71 .fuzz = 5, .flat = 60},
72 { .which = FUZZ,
73 .prop = ":::5:",
74 .fuzz = 5 },
75 { .which = RES, .prop = "::12::",
76 .res = 12 },
77 /* Malformed property but parsing this one makes us more
78 * future proof */
79 { .which = (RES|FUZZ|FLAT), .prop = "::12:1:2:3:4:5:6",
80 .res = 12, .fuzz = 1, .flat = 2 },
81 { .which = 0, .prop = ":::::" },
82 { .which = 0, .prop = ":" },
83 { .which = 0, .prop = "" },
84 { .which = 0, .prop = ":asb::::" },
85 { .which = 0, .prop = "foo" },
86 };
87 struct test *t;
88
89 ARRAY_FOR_EACH(tests, t) {
90 struct input_absinfo abs;
91 uint32_t mask;
92
93 mask = parse_ev_abs_prop(t->prop, &abs);
94 ck_assert_int_eq(mask, t->which);
95
96 if (t->which & MIN)
97 ck_assert_int_eq(abs.minimum, t->min);
98 if (t->which & MAX)
99 ck_assert_int_eq(abs.maximum, t->max);
100 if (t->which & RES)
101 ck_assert_int_eq(abs.resolution, t->res);
102 if (t->which & FUZZ)
103 ck_assert_int_eq(abs.fuzz, t->fuzz);
104 if (t->which & FLAT)
105 ck_assert_int_eq(abs.flat, t->flat);
106 }
107 }
108 END_TEST
109
main(int argc,char ** argv)110 int main(int argc, char **argv) {
111
112 SRunner *sr = srunner_create(NULL);
113 Suite *s = suite_create("fuzz-override");
114 TCase *tc = tcase_create("parser");
115 int nfailed;
116
117 tcase_add_test(tc, test_parse_ev_abs);
118 suite_add_tcase(s, tc);
119 srunner_add_suite(sr, s);
120
121 srunner_run_all(sr, CK_NORMAL);
122 nfailed = srunner_ntests_failed(sr);
123 srunner_free(sr);
124
125 return nfailed;
126 }
127