|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
| 2 | + |
| 3 | +/* |
| 4 | + * HID driver for UC-Logic devices not fully compliant with HID standard |
| 5 | + * |
| 6 | + * Copyright (c) 2022 José Expósito <[email protected]> |
| 7 | + */ |
| 8 | + |
| 9 | +#include <kunit/test.h> |
| 10 | +#include "./hid-uclogic-rdesc.h" |
| 11 | + |
| 12 | +struct uclogic_template_case { |
| 13 | + const char *name; |
| 14 | + const __u8 *template; |
| 15 | + size_t template_size; |
| 16 | + const s32 *param_list; |
| 17 | + size_t param_num; |
| 18 | + const __u8 *expected; |
| 19 | +}; |
| 20 | + |
| 21 | +static const s32 params_pen_all[UCLOGIC_RDESC_PH_ID_NUM] = { |
| 22 | + [UCLOGIC_RDESC_PEN_PH_ID_X_LM] = 0xAA, |
| 23 | + [UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0xBB, |
| 24 | + [UCLOGIC_RDESC_PEN_PH_ID_Y_LM] = 0xCC, |
| 25 | + [UCLOGIC_RDESC_PEN_PH_ID_Y_PM] = 0xDD, |
| 26 | + [UCLOGIC_RDESC_PEN_PH_ID_PRESSURE_LM] = 0xEE, |
| 27 | +}; |
| 28 | + |
| 29 | +static const s32 params_pen_some[] = { |
| 30 | + [UCLOGIC_RDESC_PEN_PH_ID_X_LM] = 0xAA, |
| 31 | + [UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0xBB, |
| 32 | +}; |
| 33 | + |
| 34 | +static const __u8 template_empty[] = { }; |
| 35 | +static const __u8 template_small[] = { 0x00 }; |
| 36 | +static const __u8 template_no_ph[] = { 0xAA, 0xFE, 0xAA, 0xED, 0x1D }; |
| 37 | + |
| 38 | +static const __u8 template_pen_ph_end[] = { |
| 39 | + 0xAA, 0xBB, UCLOGIC_RDESC_PEN_PH_HEAD |
| 40 | +}; |
| 41 | + |
| 42 | +static const __u8 template_pen_all_params[] = { |
| 43 | + UCLOGIC_RDESC_PEN_PH(X_LM), |
| 44 | + 0x47, UCLOGIC_RDESC_PEN_PH(X_PM), |
| 45 | + 0x27, UCLOGIC_RDESC_PEN_PH(Y_LM), |
| 46 | + UCLOGIC_RDESC_PEN_PH(Y_PM), |
| 47 | + 0x00, UCLOGIC_RDESC_PEN_PH(PRESSURE_LM), |
| 48 | +}; |
| 49 | + |
| 50 | +static const __u8 expected_pen_all_params[] = { |
| 51 | + 0xAA, 0x00, 0x00, 0x00, |
| 52 | + 0x47, 0xBB, 0x00, 0x00, 0x00, |
| 53 | + 0x27, 0xCC, 0x00, 0x00, 0x00, |
| 54 | + 0xDD, 0x00, 0x00, 0x00, |
| 55 | + 0x00, 0xEE, 0x00, 0x00, 0x00, |
| 56 | +}; |
| 57 | + |
| 58 | +static const __u8 template_pen_some_params[] = { |
| 59 | + 0x01, 0x02, |
| 60 | + UCLOGIC_RDESC_PEN_PH(X_LM), |
| 61 | + 0x03, UCLOGIC_RDESC_PEN_PH(X_PM), |
| 62 | + 0x04, 0x05, |
| 63 | +}; |
| 64 | + |
| 65 | +static const __u8 expected_pen_some_params[] = { |
| 66 | + 0x01, 0x02, |
| 67 | + 0xAA, 0x00, 0x00, 0x00, |
| 68 | + 0x03, 0xBB, 0x00, 0x00, 0x00, |
| 69 | + 0x04, 0x05, |
| 70 | +}; |
| 71 | + |
| 72 | +static const __u8 template_params_none[] = { |
| 73 | + 0x27, UCLOGIC_RDESC_PEN_PH(Y_LM), |
| 74 | + UCLOGIC_RDESC_PEN_PH(Y_PM), |
| 75 | + 0x00, UCLOGIC_RDESC_PEN_PH(PRESSURE_LM), |
| 76 | +}; |
| 77 | + |
| 78 | +static struct uclogic_template_case uclogic_template_cases[] = { |
| 79 | + { |
| 80 | + .name = "Empty template", |
| 81 | + .template = template_empty, |
| 82 | + .template_size = sizeof(template_empty), |
| 83 | + .param_list = params_pen_all, |
| 84 | + .param_num = ARRAY_SIZE(params_pen_all), |
| 85 | + .expected = template_empty, |
| 86 | + }, |
| 87 | + { |
| 88 | + .name = "Template smaller than the placeholder", |
| 89 | + .template = template_small, |
| 90 | + .template_size = sizeof(template_small), |
| 91 | + .param_list = params_pen_all, |
| 92 | + .param_num = ARRAY_SIZE(params_pen_all), |
| 93 | + .expected = template_small, |
| 94 | + }, |
| 95 | + { |
| 96 | + .name = "No placeholder", |
| 97 | + .template = template_no_ph, |
| 98 | + .template_size = sizeof(template_no_ph), |
| 99 | + .param_list = params_pen_all, |
| 100 | + .param_num = ARRAY_SIZE(params_pen_all), |
| 101 | + .expected = template_no_ph, |
| 102 | + }, |
| 103 | + { |
| 104 | + .name = "Pen placeholder at the end, without ID", |
| 105 | + .template = template_pen_ph_end, |
| 106 | + .template_size = sizeof(template_pen_ph_end), |
| 107 | + .param_list = params_pen_all, |
| 108 | + .param_num = ARRAY_SIZE(params_pen_all), |
| 109 | + .expected = template_pen_ph_end, |
| 110 | + }, |
| 111 | + { |
| 112 | + .name = "All params present in the pen template", |
| 113 | + .template = template_pen_all_params, |
| 114 | + .template_size = sizeof(template_pen_all_params), |
| 115 | + .param_list = params_pen_all, |
| 116 | + .param_num = ARRAY_SIZE(params_pen_all), |
| 117 | + .expected = expected_pen_all_params, |
| 118 | + }, |
| 119 | + { |
| 120 | + .name = "Some params present in the pen template (complete param list)", |
| 121 | + .template = template_pen_some_params, |
| 122 | + .template_size = sizeof(template_pen_some_params), |
| 123 | + .param_list = params_pen_all, |
| 124 | + .param_num = ARRAY_SIZE(params_pen_all), |
| 125 | + .expected = expected_pen_some_params, |
| 126 | + }, |
| 127 | + { |
| 128 | + .name = "Some params present in the pen template (incomplete param list)", |
| 129 | + .template = template_pen_some_params, |
| 130 | + .template_size = sizeof(template_pen_some_params), |
| 131 | + .param_list = params_pen_some, |
| 132 | + .param_num = ARRAY_SIZE(params_pen_some), |
| 133 | + .expected = expected_pen_some_params, |
| 134 | + }, |
| 135 | + { |
| 136 | + .name = "No params present in the template", |
| 137 | + .template = template_params_none, |
| 138 | + .template_size = sizeof(template_params_none), |
| 139 | + .param_list = params_pen_some, |
| 140 | + .param_num = ARRAY_SIZE(params_pen_some), |
| 141 | + .expected = template_params_none, |
| 142 | + }, |
| 143 | +}; |
| 144 | + |
| 145 | +static void uclogic_template_case_desc(struct uclogic_template_case *t, |
| 146 | + char *desc) |
| 147 | +{ |
| 148 | + strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE); |
| 149 | +} |
| 150 | + |
| 151 | +KUNIT_ARRAY_PARAM(uclogic_template, uclogic_template_cases, |
| 152 | + uclogic_template_case_desc); |
| 153 | + |
| 154 | +static void uclogic_template_test(struct kunit *test) |
| 155 | +{ |
| 156 | + __u8 *res; |
| 157 | + const struct uclogic_template_case *params = test->param_value; |
| 158 | + |
| 159 | + res = uclogic_rdesc_template_apply(params->template, |
| 160 | + params->template_size, |
| 161 | + params->param_list, |
| 162 | + params->param_num); |
| 163 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res); |
| 164 | + KUNIT_EXPECT_EQ(test, 0, |
| 165 | + memcmp(res, params->expected, params->template_size)); |
| 166 | + kfree(res); |
| 167 | +} |
| 168 | + |
| 169 | +static struct kunit_case hid_uclogic_rdesc_test_cases[] = { |
| 170 | + KUNIT_CASE_PARAM(uclogic_template_test, uclogic_template_gen_params), |
| 171 | + {} |
| 172 | +}; |
| 173 | + |
| 174 | +static struct kunit_suite hid_uclogic_rdesc_test_suite = { |
| 175 | + .name = "hid-uclogic-rdesc-test", |
| 176 | + .test_cases = hid_uclogic_rdesc_test_cases, |
| 177 | +}; |
| 178 | + |
| 179 | +kunit_test_suite(hid_uclogic_rdesc_test_suite); |
| 180 | + |
| 181 | +MODULE_DESCRIPTION("KUnit tests for the UC-Logic driver"); |
| 182 | +MODULE_LICENSE("GPL"); |
| 183 | +MODULE_AUTHOR( "José Expósito <[email protected]>"); |
0 commit comments