1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <test_progs.h>
4 #include "timer.skel.h"
5 
timer(struct timer * timer_skel)6 static int timer(struct timer *timer_skel)
7 {
8 	int err, prog_fd;
9 	LIBBPF_OPTS(bpf_test_run_opts, topts);
10 
11 	err = timer__attach(timer_skel);
12 	if (!ASSERT_OK(err, "timer_attach"))
13 		return err;
14 
15 	ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1");
16 	ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1");
17 
18 	prog_fd = bpf_program__fd(timer_skel->progs.test1);
19 	err = bpf_prog_test_run_opts(prog_fd, &topts);
20 	ASSERT_OK(err, "test_run");
21 	ASSERT_EQ(topts.retval, 0, "test_run");
22 	timer__detach(timer_skel);
23 
24 	usleep(50); /* 10 usecs should be enough, but give it extra */
25 	/* check that timer_cb1() was executed 10+10 times */
26 	ASSERT_EQ(timer_skel->data->callback_check, 42, "callback_check2");
27 	ASSERT_EQ(timer_skel->data->callback2_check, 42, "callback2_check2");
28 
29 	/* check that timer_cb2() was executed twice */
30 	ASSERT_EQ(timer_skel->bss->bss_data, 10, "bss_data");
31 
32 	/* check that there were no errors in timer execution */
33 	ASSERT_EQ(timer_skel->bss->err, 0, "err");
34 
35 	/* check that code paths completed */
36 	ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok");
37 
38 	return 0;
39 }
40 
41 /* TODO: use pid filtering */
serial_test_timer(void)42 void serial_test_timer(void)
43 {
44 	struct timer *timer_skel = NULL;
45 	int err;
46 
47 	timer_skel = timer__open_and_load();
48 	if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load"))
49 		goto cleanup;
50 
51 	err = timer(timer_skel);
52 	ASSERT_OK(err, "timer");
53 cleanup:
54 	timer__destroy(timer_skel);
55 }
56