@@ -470,6 +470,11 @@ static void detach_bpf(FIXTURE_DATA(hid_bpf) * self)
470
470
close (self -> hidraw_fd );
471
471
self -> hidraw_fd = 0 ;
472
472
473
+ if (!self -> skel )
474
+ return ;
475
+
476
+ hid__detach (self -> skel );
477
+
473
478
for (i = 0 ; i < ARRAY_SIZE (self -> hid_links ); i ++ ) {
474
479
if (self -> hid_links [i ])
475
480
bpf_link__destroy (self -> hid_links [i ]);
@@ -575,6 +580,8 @@ static void load_programs(const struct test_program programs[],
575
580
programs [i ].name + 4 );
576
581
}
577
582
583
+ hid__attach (self -> skel );
584
+
578
585
self -> hidraw_fd = open_hidraw (self -> dev_id );
579
586
ASSERT_GE (self -> hidraw_fd , 0 ) TH_LOG ("open_hidraw" );
580
587
}
@@ -919,6 +926,108 @@ TEST_F(hid_bpf, test_hid_user_raw_request_call)
919
926
ASSERT_EQ (args .data [1 ], 2 );
920
927
}
921
928
929
+ /*
930
+ * Call hid_hw_raw_request against the given uhid device,
931
+ * check that the program is called and prevents the
932
+ * call to uhid.
933
+ */
934
+ TEST_F (hid_bpf , test_hid_filter_raw_request_call )
935
+ {
936
+ const struct test_program progs [] = {
937
+ { .name = "hid_test_filter_raw_request" },
938
+ };
939
+ __u8 buf [10 ] = {0 };
940
+ int err ;
941
+
942
+ LOAD_PROGRAMS (progs );
943
+
944
+ /* first check that we did not attach to device_event */
945
+
946
+ /* inject one event */
947
+ buf [0 ] = 1 ;
948
+ buf [1 ] = 42 ;
949
+ uhid_send_event (_metadata , self -> uhid_fd , buf , 6 );
950
+
951
+ /* read the data from hidraw */
952
+ memset (buf , 0 , sizeof (buf ));
953
+ err = read (self -> hidraw_fd , buf , sizeof (buf ));
954
+ ASSERT_EQ (err , 6 ) TH_LOG ("read_hidraw" );
955
+ ASSERT_EQ (buf [0 ], 1 );
956
+ ASSERT_EQ (buf [1 ], 42 );
957
+ ASSERT_EQ (buf [2 ], 0 ) TH_LOG ("leftovers_from_previous_test" );
958
+
959
+ /* now check that our program is preventing hid_hw_raw_request() */
960
+
961
+ /* emit hid_hw_raw_request from hidraw */
962
+ /* Get Feature */
963
+ memset (buf , 0 , sizeof (buf ));
964
+ buf [0 ] = 0x1 ; /* Report Number */
965
+ err = ioctl (self -> hidraw_fd , HIDIOCGFEATURE (sizeof (buf )), buf );
966
+ ASSERT_LT (err , 0 ) TH_LOG ("unexpected success while reading HIDIOCGFEATURE: %d" , err );
967
+ ASSERT_EQ (errno , 20 ) TH_LOG ("unexpected error code while reading HIDIOCGFEATURE: %d" ,
968
+ errno );
969
+
970
+ /* remove our bpf program and check that we can now emit commands */
971
+
972
+ /* detach the program */
973
+ detach_bpf (self );
974
+
975
+ self -> hidraw_fd = open_hidraw (self -> dev_id );
976
+ ASSERT_GE (self -> hidraw_fd , 0 ) TH_LOG ("open_hidraw" );
977
+
978
+ err = ioctl (self -> hidraw_fd , HIDIOCGFEATURE (sizeof (buf )), buf );
979
+ ASSERT_GE (err , 0 ) TH_LOG ("error while reading HIDIOCGFEATURE: %d" , err );
980
+ }
981
+
982
+ /*
983
+ * Call hid_hw_raw_request against the given uhid device,
984
+ * check that the program is called and can issue the call
985
+ * to uhid and transform the answer.
986
+ */
987
+ TEST_F (hid_bpf , test_hid_change_raw_request_call )
988
+ {
989
+ const struct test_program progs [] = {
990
+ { .name = "hid_test_hidraw_raw_request" },
991
+ };
992
+ __u8 buf [10 ] = {0 };
993
+ int err ;
994
+
995
+ LOAD_PROGRAMS (progs );
996
+
997
+ /* emit hid_hw_raw_request from hidraw */
998
+ /* Get Feature */
999
+ memset (buf , 0 , sizeof (buf ));
1000
+ buf [0 ] = 0x1 ; /* Report Number */
1001
+ err = ioctl (self -> hidraw_fd , HIDIOCGFEATURE (sizeof (buf )), buf );
1002
+ ASSERT_EQ (err , 3 ) TH_LOG ("unexpected returned size while reading HIDIOCGFEATURE: %d" , err );
1003
+
1004
+ ASSERT_EQ (buf [0 ], 2 );
1005
+ ASSERT_EQ (buf [1 ], 3 );
1006
+ ASSERT_EQ (buf [2 ], 4 );
1007
+ }
1008
+
1009
+ /*
1010
+ * Call hid_hw_raw_request against the given uhid device,
1011
+ * check that the program is not making infinite loops.
1012
+ */
1013
+ TEST_F (hid_bpf , test_hid_infinite_loop_raw_request_call )
1014
+ {
1015
+ const struct test_program progs [] = {
1016
+ { .name = "hid_test_infinite_loop_raw_request" },
1017
+ };
1018
+ __u8 buf [10 ] = {0 };
1019
+ int err ;
1020
+
1021
+ LOAD_PROGRAMS (progs );
1022
+
1023
+ /* emit hid_hw_raw_request from hidraw */
1024
+ /* Get Feature */
1025
+ memset (buf , 0 , sizeof (buf ));
1026
+ buf [0 ] = 0x1 ; /* Report Number */
1027
+ err = ioctl (self -> hidraw_fd , HIDIOCGFEATURE (sizeof (buf )), buf );
1028
+ ASSERT_EQ (err , 3 ) TH_LOG ("unexpected returned size while reading HIDIOCGFEATURE: %d" , err );
1029
+ }
1030
+
922
1031
/*
923
1032
* Attach hid_insert{0,1,2} to the given uhid device,
924
1033
* retrieve and open the matching hidraw node,
0 commit comments