Skip to content

Commit 8163892

Browse files
author
Benjamin Tissoires
committed
selftests/hid: Add initial hidraw tests skeleton
Largely inspired from hid_bpf.c for the fixture setup. Create a couple of tests for hidraw: - create a uhid device and check if the fixture is working properly - inject one uhid event and read it through hidraw These tests are not that useful for now, but will be once we start adding the ioctl and BPFs to revoke the hidraw node. Reviewed-by: Peter Hutterer <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent 375e9bd commit 8163892

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

tools/testing/selftests/hid/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ bpftool
22
*.skel.h
33
/tools
44
hid_bpf
5+
hidraw
56
results

tools/testing/selftests/hid/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ CFLAGS += -Wno-unused-command-line-argument
3232
endif
3333

3434
# Order correspond to 'make run_tests' order
35-
TEST_GEN_PROGS = hid_bpf
35+
TEST_GEN_PROGS = hid_bpf hidraw
3636

3737
# Emit succinct information message describing current building step
3838
# $1 - generic step name (e.g., CC, LINK, etc);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2022-2024 Red Hat */
3+
4+
#include "hid_common.h"
5+
6+
FIXTURE(hidraw) {
7+
int dev_id;
8+
int uhid_fd;
9+
int hidraw_fd;
10+
int hid_id;
11+
pthread_t tid;
12+
};
13+
static void close_hidraw(FIXTURE_DATA(hidraw) * self)
14+
{
15+
if (self->hidraw_fd)
16+
close(self->hidraw_fd);
17+
self->hidraw_fd = 0;
18+
}
19+
20+
FIXTURE_TEARDOWN(hidraw) {
21+
void *uhid_err;
22+
23+
uhid_destroy(_metadata, self->uhid_fd);
24+
25+
close_hidraw(self);
26+
pthread_join(self->tid, &uhid_err);
27+
}
28+
#define TEARDOWN_LOG(fmt, ...) do { \
29+
TH_LOG(fmt, ##__VA_ARGS__); \
30+
hidraw_teardown(_metadata, self, variant); \
31+
} while (0)
32+
33+
FIXTURE_SETUP(hidraw)
34+
{
35+
time_t t;
36+
int err;
37+
38+
/* initialize random number generator */
39+
srand((unsigned int)time(&t));
40+
41+
self->dev_id = rand() % 1024;
42+
43+
self->uhid_fd = setup_uhid(_metadata, self->dev_id);
44+
45+
/* locate the uev, self, variant);ent file of the created device */
46+
self->hid_id = get_hid_id(self->dev_id);
47+
ASSERT_GT(self->hid_id, 0)
48+
TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id);
49+
50+
err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd);
51+
ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err);
52+
53+
self->hidraw_fd = open_hidraw(self->dev_id);
54+
ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
55+
}
56+
57+
/*
58+
* A simple test to see if the fixture is working fine.
59+
* If this fails, none of the other tests will pass.
60+
*/
61+
TEST_F(hidraw, test_create_uhid)
62+
{
63+
}
64+
65+
/*
66+
* Inject one event in the uhid device,
67+
* check that we get the same data through hidraw
68+
*/
69+
TEST_F(hidraw, raw_event)
70+
{
71+
__u8 buf[10] = {0};
72+
int err;
73+
74+
/* inject one event */
75+
buf[0] = 1;
76+
buf[1] = 42;
77+
uhid_send_event(_metadata, self->uhid_fd, buf, 6);
78+
79+
/* read the data from hidraw */
80+
memset(buf, 0, sizeof(buf));
81+
err = read(self->hidraw_fd, buf, sizeof(buf));
82+
ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
83+
ASSERT_EQ(buf[0], 1);
84+
ASSERT_EQ(buf[1], 42);
85+
}
86+
87+
int main(int argc, char **argv)
88+
{
89+
return test_harness_run(argc, argv);
90+
}

0 commit comments

Comments
 (0)