1818#include  <linux/can.h> 
1919#include  <linux/can/raw.h> 
2020
21+ #include  "../../kselftest_harness.h" 
22+ 
2123#define  ID  0x123
2224#define  TC  18 /* # of testcases */ 
2325
@@ -53,7 +55,38 @@ canid_t calc_mask(int testcase)
5355	return  mask ;
5456}
5557
56- int  main (int  argc , char  * * argv )
58+ int  send_can_frames (int  sock , int  testcase )
59+ {
60+ 	struct  can_frame  frame ;
61+ 
62+ 	frame .can_dlc  =  1 ;
63+ 	frame .data [0 ] =  testcase ;
64+ 
65+ 	frame .can_id  =  ID ;
66+ 	if  (write (sock , & frame , sizeof (frame )) <  0 ) {
67+ 		perror ("write" );
68+ 		return  1 ;
69+ 	}
70+ 	frame .can_id  =  (ID  | CAN_RTR_FLAG );
71+ 	if  (write (sock , & frame , sizeof (frame )) <  0 ) {
72+ 		perror ("write" );
73+ 		return  1 ;
74+ 	}
75+ 	frame .can_id  =  (ID  | CAN_EFF_FLAG );
76+ 	if  (write (sock , & frame , sizeof (frame )) <  0 ) {
77+ 		perror ("write" );
78+ 		return  1 ;
79+ 	}
80+ 	frame .can_id  =  (ID  | CAN_EFF_FLAG  | CAN_RTR_FLAG );
81+ 	if  (write (sock , & frame , sizeof (frame )) <  0 ) {
82+ 		perror ("write" );
83+ 		return  1 ;
84+ 	}
85+ 
86+ 	return  0 ;
87+ }
88+ 
89+ TEST (can_filter )
5790{
5891	fd_set  rdfs ;
5992	struct  timeval  tv ;
@@ -67,34 +100,26 @@ int main(int argc, char **argv)
67100	int  rxbits , rxbitval ;
68101	int  ret ;
69102	int  recv_own_msgs  =  1 ;
70- 	int  err  =  0 ;
71103	struct  ifreq  ifr ;
72104
73- 	if  ((s  =  socket (PF_CAN , SOCK_RAW , CAN_RAW )) <  0 ) {
74- 		perror ("socket" );
75- 		err  =  1 ;
76- 		goto out ;
77- 	}
105+ 	s  =  socket (PF_CAN , SOCK_RAW , CAN_RAW );
106+ 	ASSERT_LT (0 , s )
107+ 		TH_LOG ("failed to create CAN_RAW socket (%d)" , errno );
78108
79109	strcpy (ifr .ifr_name , VCANIF );
80- 	if  (ioctl (s , SIOCGIFINDEX , & ifr ) <  0 ) {
81- 		perror ("SIOCGIFINDEX" );
82- 		err  =  1 ;
83- 		goto out_socket ;
84- 	}
110+ 	ret  =  ioctl (s , SIOCGIFINDEX , & ifr );
111+ 	ASSERT_LE (0 , ret )
112+ 		TH_LOG ("failed SIOCGIFINDEX (%d)" , errno );
113+ 
85114	addr .can_family  =  AF_CAN ;
86115	addr .can_ifindex  =  ifr .ifr_ifindex ;
87116
88117	setsockopt (s , SOL_CAN_RAW , CAN_RAW_RECV_OWN_MSGS ,
89118		   & recv_own_msgs , sizeof (recv_own_msgs ));
90119
91- 	if  (bind (s , (struct  sockaddr  * )& addr , sizeof (addr )) <  0 ) {
92- 		perror ("bind" );
93- 		err  =  1 ;
94- 		goto out_socket ;
95- 	}
96- 
97- 	printf ("---\n" );
120+ 	ret  =  bind (s , (struct  sockaddr  * )& addr , sizeof (addr ));
121+ 	ASSERT_EQ (0 , ret )
122+ 		TH_LOG ("failed bind socket (%d)" , errno );
98123
99124	for  (testcase  =  0 ; testcase  <  TC ; testcase ++ ) {
100125
@@ -103,36 +128,14 @@ int main(int argc, char **argv)
103128		setsockopt (s , SOL_CAN_RAW , CAN_RAW_FILTER ,
104129			   & rfilter , sizeof (rfilter ));
105130
106- 		printf ("testcase %2d filters : can_id = 0x%08X can_mask = 0x%08X\n " ,
131+ 		TH_LOG ("testcase %2d filters : can_id = 0x%08X can_mask = 0x%08X" ,
107132		       testcase , rfilter .can_id , rfilter .can_mask );
108133
109- 		printf ("testcase %2d sending patterns ... " , testcase );
110- 
111- 		frame .can_dlc  =  1 ;
112- 		frame .data [0 ] =  testcase ;
113- 
114- 		frame .can_id  =  ID ;
115- 		if  (write (s , & frame , sizeof (frame )) <  0 ) {
116- 			perror ("write" );
117- 			exit (1 );
118- 		}
119- 		frame .can_id  =  (ID  | CAN_RTR_FLAG );
120- 		if  (write (s , & frame , sizeof (frame )) <  0 ) {
121- 			perror ("write" );
122- 			exit (1 );
123- 		}
124- 		frame .can_id  =  (ID  | CAN_EFF_FLAG );
125- 		if  (write (s , & frame , sizeof (frame )) <  0 ) {
126- 			perror ("write" );
127- 			exit (1 );
128- 		}
129- 		frame .can_id  =  (ID  | CAN_EFF_FLAG  | CAN_RTR_FLAG );
130- 		if  (write (s , & frame , sizeof (frame )) <  0 ) {
131- 			perror ("write" );
132- 			exit (1 );
133- 		}
134+ 		TH_LOG ("testcase %2d sending patterns..." , testcase );
134135
135- 		printf ("ok\n" );
136+ 		ret  =  send_can_frames (s , testcase );
137+ 		ASSERT_EQ (0 , ret )
138+ 			TH_LOG ("failed to send CAN frames" );
136139
137140		have_rx  =  1 ;
138141		rx  =  0 ;
@@ -147,58 +150,45 @@ int main(int argc, char **argv)
147150			tv .tv_usec  =  50000 ; /* 50ms timeout */ 
148151
149152			ret  =  select (s + 1 , & rdfs , NULL , NULL , & tv );
150- 			if  (ret  <  0 ) {
151- 				perror ("select" );
152- 				exit (1 );
153- 			}
153+ 			ASSERT_LE (0 , ret )
154+ 				TH_LOG ("failed select for frame %d (%d)" , rx , errno );
154155
155156			if  (FD_ISSET (s , & rdfs )) {
156157				have_rx  =  1 ;
157158				ret  =  read (s , & frame , sizeof (struct  can_frame ));
158- 				if  (ret  <  0 ) {
159- 					perror ("read" );
160- 					exit (1 );
161- 				}
162- 				if  ((frame .can_id  &  CAN_SFF_MASK ) !=  ID ) {
163- 					fprintf (stderr , "received wrong can_id!\n" );
164- 					exit (1 );
165- 				}
166- 				if  (frame .data [0 ] !=  testcase ) {
167- 					fprintf (stderr , "received wrong testcase!\n" );
168- 					exit (1 );
169- 				}
159+ 				ASSERT_LE (0 , ret )
160+ 					TH_LOG ("failed to read frame %d (%d)" , rx , errno );
161+ 
162+ 				ASSERT_EQ (ID , frame .can_id  &  CAN_SFF_MASK )
163+ 					TH_LOG ("received wrong can_id" );
164+ 				ASSERT_EQ (testcase , frame .data [0 ])
165+ 					TH_LOG ("received wrong test case" );
170166
171167				/* test & calc rxbits */ 
172168				rxbitval  =  1  << ((frame .can_id  &  (CAN_EFF_FLAG |CAN_RTR_FLAG |CAN_ERR_FLAG )) >> 28 );
173169
174170				/* only receive a rxbitval once */ 
175- 				if  ((rxbits  &  rxbitval ) ==  rxbitval ) {
176- 					fprintf (stderr , "received rxbitval %d twice!\n" , rxbitval );
177- 					exit (1 );
178- 				}
171+ 				ASSERT_NE (rxbitval , rxbits  &  rxbitval )
172+ 					TH_LOG ("received rxbitval %d twice" , rxbitval );
179173				rxbits  |= rxbitval ;
180174				rx ++ ;
181175
182- 				printf ("testcase %2d rx : can_id = 0x%08X rx = %d rxbits = %d\n " ,
176+ 				TH_LOG ("testcase %2d rx : can_id = 0x%08X rx = %d rxbits = %d" ,
183177				       testcase , frame .can_id , rx , rxbits );
184178			}
185179		}
186180		/* rx timed out -> check the received results */ 
187- 		if  (rx_res [testcase ] !=  rx ) {
188- 			fprintf (stderr , "wrong rx value in testcase %d : %d (expected %d)\n" ,
189- 				testcase , rx , rx_res [testcase ]);
190- 			exit (1 );
191- 		}
192- 		if  (rxbits_res [testcase ] !=  rxbits ) {
193- 			fprintf (stderr , "wrong rxbits value in testcase %d : %d (expected %d)\n" ,
194- 				testcase , rxbits , rxbits_res [testcase ]);
195- 			exit (1 );
196- 		}
197- 		printf ("testcase %2d ok\n---\n" , testcase );
181+ 		ASSERT_EQ (rx_res [testcase ], rx )
182+ 			TH_LOG ("wrong number of received frames %d" , testcase );
183+ 		ASSERT_EQ (rxbits_res [testcase ], rxbits )
184+ 			TH_LOG ("wrong rxbits value in testcase %d" , testcase );
185+ 
186+ 		TH_LOG ("testcase %2d ok" , testcase );
187+ 		TH_LOG ("---" );
198188	}
199189
200- out_socket :
201190	close (s );
202- out :
203- 	return  err ;
191+ 	return ;
204192}
193+ 
194+ TEST_HARNESS_MAIN 
0 commit comments