18
18
#include <linux/can.h>
19
19
#include <linux/can/raw.h>
20
20
21
+ #define TH_LOG_ENABLED 0
21
22
#include "../../kselftest_harness.h"
22
23
23
24
#define ID 0x123
24
- #define TC 18 /* # of testcases */
25
-
26
- const int rx_res [TC ] = {4 , 4 , 4 , 4 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 1 , 1 , 1 , 1 , 1 , 1 };
27
- const int rxbits_res [TC ] = {4369 , 4369 , 4369 , 4369 , 17 , 4352 , 17 , 4352 , 257 , 257 , 4112 , 4112 , 1 , 256 , 16 , 4096 , 1 , 256 };
28
25
29
26
#define VCANIF "vcan0"
30
27
31
- canid_t calc_id (int testcase )
32
- {
33
- canid_t id = ID ;
34
-
35
- if (testcase & 1 )
36
- id |= CAN_EFF_FLAG ;
37
- if (testcase & 2 )
38
- id |= CAN_RTR_FLAG ;
39
-
40
- return id ;
41
- }
42
-
43
- canid_t calc_mask (int testcase )
44
- {
45
- canid_t mask = CAN_SFF_MASK ;
46
-
47
- if (testcase > 15 )
48
- return (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG );
49
-
50
- if (testcase & 4 )
51
- mask |= CAN_EFF_FLAG ;
52
- if (testcase & 8 )
53
- mask |= CAN_RTR_FLAG ;
54
-
55
- return mask ;
56
- }
57
-
58
28
int send_can_frames (int sock , int testcase )
59
29
{
60
30
struct can_frame frame ;
@@ -86,21 +56,16 @@ int send_can_frames(int sock, int testcase)
86
56
return 0 ;
87
57
}
88
58
89
- TEST (can_filter )
59
+ FIXTURE (can_filters ) {
60
+ int sock ;
61
+ };
62
+
63
+ FIXTURE_SETUP (can_filters )
90
64
{
91
- fd_set rdfs ;
92
- struct timeval tv ;
93
- int s ;
94
65
struct sockaddr_can addr ;
95
- struct can_filter rfilter ;
96
- struct can_frame frame ;
97
- int testcase ;
98
- int have_rx ;
99
- int rx ;
100
- int rxbits , rxbitval ;
101
- int ret ;
102
- int recv_own_msgs = 1 ;
103
66
struct ifreq ifr ;
67
+ int recv_own_msgs = 1 ;
68
+ int s , ret ;
104
69
105
70
s = socket (PF_CAN , SOCK_RAW , CAN_RAW );
106
71
ASSERT_LT (0 , s )
@@ -121,74 +86,220 @@ TEST(can_filter)
121
86
ASSERT_EQ (0 , ret )
122
87
TH_LOG ("failed bind socket (%d)" , errno );
123
88
124
- for (testcase = 0 ; testcase < TC ; testcase ++ ) {
89
+ self -> sock = s ;
90
+ }
91
+
92
+ FIXTURE_TEARDOWN (can_filters )
93
+ {
94
+ close (self -> sock );
95
+ }
125
96
126
- rfilter .can_id = calc_id (testcase );
127
- rfilter .can_mask = calc_mask (testcase );
128
- setsockopt (s , SOL_CAN_RAW , CAN_RAW_FILTER ,
129
- & rfilter , sizeof (rfilter ));
97
+ FIXTURE_VARIANT (can_filters ) {
98
+ int testcase ;
99
+ canid_t id ;
100
+ canid_t mask ;
101
+ int exp_num_rx ;
102
+ int exp_rxbits ;
103
+ };
104
+
105
+ FIXTURE_VARIANT_ADD (can_filters , base ) {
106
+ .testcase = 1 ,
107
+ .id = ID ,
108
+ .mask = CAN_SFF_MASK ,
109
+ .exp_num_rx = 4 ,
110
+ .exp_rxbits = 4369 ,
111
+ };
112
+ FIXTURE_VARIANT_ADD (can_filters , base_eff ) {
113
+ .testcase = 2 ,
114
+ .id = ID | CAN_EFF_FLAG ,
115
+ .mask = CAN_SFF_MASK ,
116
+ .exp_num_rx = 4 ,
117
+ .exp_rxbits = 4369 ,
118
+ };
119
+ FIXTURE_VARIANT_ADD (can_filters , base_rtr ) {
120
+ .testcase = 3 ,
121
+ .id = ID | CAN_RTR_FLAG ,
122
+ .mask = CAN_SFF_MASK ,
123
+ .exp_num_rx = 4 ,
124
+ .exp_rxbits = 4369 ,
125
+ };
126
+ FIXTURE_VARIANT_ADD (can_filters , base_effrtr ) {
127
+ .testcase = 4 ,
128
+ .id = ID | CAN_EFF_FLAG | CAN_RTR_FLAG ,
129
+ .mask = CAN_SFF_MASK ,
130
+ .exp_num_rx = 4 ,
131
+ .exp_rxbits = 4369 ,
132
+ };
133
+
134
+ FIXTURE_VARIANT_ADD (can_filters , filter_eff ) {
135
+ .testcase = 5 ,
136
+ .id = ID ,
137
+ .mask = CAN_SFF_MASK | CAN_EFF_FLAG ,
138
+ .exp_num_rx = 2 ,
139
+ .exp_rxbits = 17 ,
140
+ };
141
+ FIXTURE_VARIANT_ADD (can_filters , filter_eff_eff ) {
142
+ .testcase = 6 ,
143
+ .id = ID | CAN_EFF_FLAG ,
144
+ .mask = CAN_SFF_MASK | CAN_EFF_FLAG ,
145
+ .exp_num_rx = 2 ,
146
+ .exp_rxbits = 4352 ,
147
+ };
148
+ FIXTURE_VARIANT_ADD (can_filters , filter_eff_rtr ) {
149
+ .testcase = 7 ,
150
+ .id = ID | CAN_RTR_FLAG ,
151
+ .mask = CAN_SFF_MASK | CAN_EFF_FLAG ,
152
+ .exp_num_rx = 2 ,
153
+ .exp_rxbits = 17 ,
154
+ };
155
+ FIXTURE_VARIANT_ADD (can_filters , filter_eff_effrtr ) {
156
+ .testcase = 8 ,
157
+ .id = ID | CAN_EFF_FLAG | CAN_RTR_FLAG ,
158
+ .mask = CAN_SFF_MASK | CAN_EFF_FLAG ,
159
+ .exp_num_rx = 2 ,
160
+ .exp_rxbits = 4352 ,
161
+ };
162
+
163
+ FIXTURE_VARIANT_ADD (can_filters , filter_rtr ) {
164
+ .testcase = 9 ,
165
+ .id = ID ,
166
+ .mask = CAN_SFF_MASK | CAN_RTR_FLAG ,
167
+ .exp_num_rx = 2 ,
168
+ .exp_rxbits = 257 ,
169
+ };
170
+ FIXTURE_VARIANT_ADD (can_filters , filter_rtr_eff ) {
171
+ .testcase = 10 ,
172
+ .id = ID | CAN_EFF_FLAG ,
173
+ .mask = CAN_SFF_MASK | CAN_RTR_FLAG ,
174
+ .exp_num_rx = 2 ,
175
+ .exp_rxbits = 257 ,
176
+ };
177
+ FIXTURE_VARIANT_ADD (can_filters , filter_rtr_rtr ) {
178
+ .testcase = 11 ,
179
+ .id = ID | CAN_RTR_FLAG ,
180
+ .mask = CAN_SFF_MASK | CAN_RTR_FLAG ,
181
+ .exp_num_rx = 2 ,
182
+ .exp_rxbits = 4112 ,
183
+ };
184
+ FIXTURE_VARIANT_ADD (can_filters , filter_rtr_effrtr ) {
185
+ .testcase = 12 ,
186
+ .id = ID | CAN_EFF_FLAG | CAN_RTR_FLAG ,
187
+ .mask = CAN_SFF_MASK | CAN_RTR_FLAG ,
188
+ .exp_num_rx = 2 ,
189
+ .exp_rxbits = 4112 ,
190
+ };
191
+
192
+ FIXTURE_VARIANT_ADD (can_filters , filter_effrtr ) {
193
+ .testcase = 13 ,
194
+ .id = ID ,
195
+ .mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG ,
196
+ .exp_num_rx = 1 ,
197
+ .exp_rxbits = 1 ,
198
+ };
199
+ FIXTURE_VARIANT_ADD (can_filters , filter_effrtr_eff ) {
200
+ .testcase = 14 ,
201
+ .id = ID | CAN_EFF_FLAG ,
202
+ .mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG ,
203
+ .exp_num_rx = 1 ,
204
+ .exp_rxbits = 256 ,
205
+ };
206
+ FIXTURE_VARIANT_ADD (can_filters , filter_effrtr_rtr ) {
207
+ .testcase = 15 ,
208
+ .id = ID | CAN_RTR_FLAG ,
209
+ .mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG ,
210
+ .exp_num_rx = 1 ,
211
+ .exp_rxbits = 16 ,
212
+ };
213
+ FIXTURE_VARIANT_ADD (can_filters , filter_effrtr_effrtr ) {
214
+ .testcase = 16 ,
215
+ .id = ID | CAN_EFF_FLAG | CAN_RTR_FLAG ,
216
+ .mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG ,
217
+ .exp_num_rx = 1 ,
218
+ .exp_rxbits = 4096 ,
219
+ };
220
+
221
+ FIXTURE_VARIANT_ADD (can_filters , eff ) {
222
+ .testcase = 17 ,
223
+ .id = ID ,
224
+ .mask = CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG ,
225
+ .exp_num_rx = 1 ,
226
+ .exp_rxbits = 1 ,
227
+ };
228
+ FIXTURE_VARIANT_ADD (can_filters , eff_eff ) {
229
+ .testcase = 18 ,
230
+ .id = ID | CAN_EFF_FLAG ,
231
+ .mask = CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG ,
232
+ .exp_num_rx = 1 ,
233
+ .exp_rxbits = 256 ,
234
+ };
235
+
236
+ TEST_F (can_filters , test_filter )
237
+ {
238
+ fd_set rdfs ;
239
+ struct timeval tv ;
240
+ struct can_filter rfilter ;
241
+ struct can_frame frame ;
242
+ int have_rx ;
243
+ int rx ;
244
+ int rxbits , rxbitval ;
245
+ int ret ;
130
246
131
- TH_LOG ("testcase %2d filters : can_id = 0x%08X can_mask = 0x%08X" ,
132
- testcase , rfilter .can_id , rfilter .can_mask );
247
+ rfilter .can_id = variant -> id ;
248
+ rfilter .can_mask = variant -> mask ;
249
+ setsockopt (self -> sock , SOL_CAN_RAW , CAN_RAW_FILTER ,
250
+ & rfilter , sizeof (rfilter ));
133
251
134
- TH_LOG ("testcase %2d sending patterns..." , testcase );
252
+ TH_LOG ("filters: can_id = 0x%08X can_mask = 0x%08X" ,
253
+ rfilter .can_id , rfilter .can_mask );
135
254
136
- ret = send_can_frames (s , testcase );
137
- ASSERT_EQ (0 , ret )
138
- TH_LOG ("failed to send CAN frames" );
255
+ ret = send_can_frames (self -> sock , variant -> testcase );
256
+ ASSERT_EQ (0 , ret )
257
+ TH_LOG ("failed to send CAN frames" );
139
258
140
- have_rx = 1 ;
141
- rx = 0 ;
142
- rxbits = 0 ;
259
+ rx = 0 ;
260
+ rxbits = 0 ;
143
261
144
- while (have_rx ) {
262
+ do {
263
+ have_rx = 0 ;
264
+ FD_ZERO (& rdfs );
265
+ FD_SET (self -> sock , & rdfs );
266
+ tv .tv_sec = 0 ;
267
+ tv .tv_usec = 50000 ; /* 50ms timeout */
145
268
146
- have_rx = 0 ;
147
- FD_ZERO (& rdfs );
148
- FD_SET (s , & rdfs );
149
- tv .tv_sec = 0 ;
150
- tv .tv_usec = 50000 ; /* 50ms timeout */
269
+ ret = select (self -> sock + 1 , & rdfs , NULL , NULL , & tv );
270
+ ASSERT_LE (0 , ret )
271
+ TH_LOG ("failed select for frame %d (%d)" , rx , errno );
151
272
152
- ret = select (s + 1 , & rdfs , NULL , NULL , & tv );
273
+ if (FD_ISSET (self -> sock , & rdfs )) {
274
+ have_rx = 1 ;
275
+ ret = read (self -> sock , & frame , sizeof (struct can_frame ));
153
276
ASSERT_LE (0 , ret )
154
- TH_LOG ("failed select for frame %d (%d)" , rx , errno );
155
-
156
- if (FD_ISSET (s , & rdfs )) {
157
- have_rx = 1 ;
158
- ret = read (s , & frame , sizeof (struct can_frame ));
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" );
166
-
167
- /* test & calc rxbits */
168
- rxbitval = 1 << ((frame .can_id & (CAN_EFF_FLAG |CAN_RTR_FLAG |CAN_ERR_FLAG )) >> 28 );
169
-
170
- /* only receive a rxbitval once */
171
- ASSERT_NE (rxbitval , rxbits & rxbitval )
172
- TH_LOG ("received rxbitval %d twice" , rxbitval );
173
- rxbits |= rxbitval ;
174
- rx ++ ;
175
-
176
- TH_LOG ("testcase %2d rx : can_id = 0x%08X rx = %d rxbits = %d" ,
177
- testcase , frame .can_id , rx , rxbits );
178
- }
277
+ TH_LOG ("failed to read frame %d (%d)" , rx , errno );
278
+
279
+ ASSERT_EQ (ID , frame .can_id & CAN_SFF_MASK )
280
+ TH_LOG ("received wrong can_id" );
281
+ ASSERT_EQ (variant -> testcase , frame .data [0 ])
282
+ TH_LOG ("received wrong test case" );
283
+
284
+ /* test & calc rxbits */
285
+ rxbitval = 1 << ((frame .can_id & (CAN_EFF_FLAG |CAN_RTR_FLAG |CAN_ERR_FLAG )) >> 28 );
286
+
287
+ /* only receive a rxbitval once */
288
+ ASSERT_NE (rxbitval , rxbits & rxbitval )
289
+ TH_LOG ("received rxbitval %d twice" , rxbitval );
290
+ rxbits |= rxbitval ;
291
+ rx ++ ;
292
+
293
+ TH_LOG ("rx: can_id = 0x%08X rx = %d rxbits = %d" ,
294
+ frame .can_id , rx , rxbits );
179
295
}
180
- /* rx timed out -> check the received results */
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 ("---" );
188
- }
296
+ } while (have_rx );
189
297
190
- close (s );
191
- return ;
298
+ /* rx timed out -> check the received results */
299
+ ASSERT_EQ (variant -> exp_num_rx , rx )
300
+ TH_LOG ("wrong number of received frames" );
301
+ ASSERT_EQ (variant -> exp_rxbits , rxbits )
302
+ TH_LOG ("wrong rxbits value" );
192
303
}
193
304
194
305
TEST_HARNESS_MAIN
0 commit comments