mirror of
https://github.com/torvalds/linux.git
synced 2026-05-22 22:22:08 +02:00
selftests/hid: add tests for hid_bpf_input_report
Usual way of testing, we call the function and ensures we receive the event Link: https://lore.kernel.org/r/20240315-b4-hid-bpf-new-funcs-v4-6-079c282469d3@kernel.org Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
This commit is contained in:
parent
9be50ac30a
commit
2c0e8ced7d
|
|
@ -749,6 +749,52 @@ TEST_F(hid_bpf, test_hid_change_report)
|
|||
ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
|
||||
}
|
||||
|
||||
/*
|
||||
* Call hid_bpf_input_report against the given uhid device,
|
||||
* check that the program is called and does the expected.
|
||||
*/
|
||||
TEST_F(hid_bpf, test_hid_user_input_report_call)
|
||||
{
|
||||
struct hid_hw_request_syscall_args args = {
|
||||
.retval = -1,
|
||||
.size = 10,
|
||||
};
|
||||
DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
|
||||
.ctx_in = &args,
|
||||
.ctx_size_in = sizeof(args),
|
||||
);
|
||||
__u8 buf[10] = {0};
|
||||
int err, prog_fd;
|
||||
|
||||
LOAD_BPF;
|
||||
|
||||
args.hid = self->hid_id;
|
||||
args.data[0] = 1; /* report ID */
|
||||
args.data[1] = 2; /* report ID */
|
||||
args.data[2] = 42; /* report ID */
|
||||
|
||||
prog_fd = bpf_program__fd(self->skel->progs.hid_user_input_report);
|
||||
|
||||
/* check that there is no data to read from hidraw */
|
||||
memset(buf, 0, sizeof(buf));
|
||||
err = read(self->hidraw_fd, buf, sizeof(buf));
|
||||
ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
|
||||
|
||||
err = bpf_prog_test_run_opts(prog_fd, &tattrs);
|
||||
|
||||
ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
|
||||
|
||||
ASSERT_EQ(args.retval, 0);
|
||||
|
||||
/* read the data from hidraw */
|
||||
memset(buf, 0, sizeof(buf));
|
||||
err = read(self->hidraw_fd, buf, sizeof(buf));
|
||||
ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
|
||||
ASSERT_EQ(buf[0], 1);
|
||||
ASSERT_EQ(buf[1], 2);
|
||||
ASSERT_EQ(buf[2], 42);
|
||||
}
|
||||
|
||||
/*
|
||||
* Call hid_bpf_hw_output_report against the given uhid device,
|
||||
* check that the program is called and does the expected.
|
||||
|
|
@ -797,8 +843,7 @@ TEST_F(hid_bpf, test_hid_user_output_report_call)
|
|||
}
|
||||
|
||||
/*
|
||||
* Attach hid_user_raw_request to the given uhid device,
|
||||
* call the bpf program from userspace
|
||||
* Call hid_hw_raw_request against the given uhid device,
|
||||
* check that the program is called and does the expected.
|
||||
*/
|
||||
TEST_F(hid_bpf, test_hid_user_raw_request_call)
|
||||
|
|
|
|||
|
|
@ -125,6 +125,28 @@ int hid_user_output_report(struct hid_hw_request_syscall_args *args)
|
|||
return 0;
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int hid_user_input_report(struct hid_hw_request_syscall_args *args)
|
||||
{
|
||||
struct hid_bpf_ctx *ctx;
|
||||
const size_t size = args->size;
|
||||
int i, ret = 0;
|
||||
|
||||
if (size > sizeof(args->data))
|
||||
return -7; /* -E2BIG */
|
||||
|
||||
ctx = hid_bpf_allocate_context(args->hid);
|
||||
if (!ctx)
|
||||
return -1; /* EPERM check */
|
||||
|
||||
ret = hid_bpf_input_report(ctx, HID_INPUT_REPORT, args->data, size);
|
||||
args->retval = ret;
|
||||
|
||||
hid_bpf_release_context(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const __u8 rdesc[] = {
|
||||
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
|
||||
0x09, 0x32, /* USAGE (Z) */
|
||||
|
|
|
|||
|
|
@ -96,5 +96,9 @@ extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,
|
|||
enum hid_class_request reqtype) __ksym;
|
||||
extern int hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx,
|
||||
__u8 *buf, size_t buf__sz) __ksym;
|
||||
extern int hid_bpf_input_report(struct hid_bpf_ctx *ctx,
|
||||
enum hid_report_type type,
|
||||
__u8 *data,
|
||||
size_t buf__sz) __ksym;
|
||||
|
||||
#endif /* __HID_BPF_HELPERS_H */
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user