Skip to content

Commit 9f6ac50

Browse files
rluboskartben
authored andcommitted
tests: net: socket: af_packet: Simplify test initialization
Test interface configuration doesn't change throught the test suite execution, therefore there's no need to reiterate the interfaces to set the interface pointers in each test case. Do it just once, when test suite is setup. Also remove the __test_packet_sockets() function which seemed no longer useful with all those simplifications. Signed-off-by: Robert Lubos <[email protected]>
1 parent 0a98a83 commit 9f6ac50

File tree

1 file changed

+20
-40
lines changed
  • tests/net/socket/af_packet/src

1 file changed

+20
-40
lines changed

tests/net/socket/af_packet/src/main.c

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ static void prepare_packet_socket(int *sock, struct net_if *iface, int type,
149149
struct user_data {
150150
struct net_if *first;
151151
struct net_if *second;
152-
};
152+
} ud;
153153

154154
static void iface_cb(struct net_if *iface, void *user_data)
155155
{
156-
struct user_data *ud = user_data;
156+
struct user_data *test_data = user_data;
157157
struct net_linkaddr *link_addr;
158158

159159
if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
@@ -166,12 +166,12 @@ static void iface_cb(struct net_if *iface, void *user_data)
166166
return;
167167
}
168168

169-
if (ud->first == NULL) {
170-
ud->first = iface;
169+
if (test_data->first == NULL) {
170+
test_data->first = iface;
171171
return;
172172
}
173173

174-
ud->second = iface;
174+
test_data->second = iface;
175175
}
176176

177177
#define SRC_PORT 4240
@@ -200,19 +200,6 @@ static void prepare_udp_socket(int *sock, struct sockaddr_in *sockaddr, uint16_t
200200
zassert_ok(ret, "setsockopt failed (%d)", errno);
201201
}
202202

203-
static void __test_packet_sockets(int *sock1, int *sock2)
204-
{
205-
struct user_data ud = { 0 };
206-
207-
net_if_foreach(iface_cb, &ud);
208-
209-
zassert_not_null(ud.first, "1st Ethernet interface not found");
210-
zassert_not_null(ud.second, "2nd Ethernet interface not found");
211-
212-
prepare_packet_socket(sock1, ud.first, SOCK_RAW, htons(ETH_P_ALL));
213-
prepare_packet_socket(sock2, ud.second, SOCK_RAW, htons(ETH_P_ALL));
214-
}
215-
216203
#define IP_HDR_SIZE 20
217204
#define UDP_HDR_SIZE 8
218205
#define HDR_SIZE (IP_HDR_SIZE + UDP_HDR_SIZE)
@@ -226,7 +213,8 @@ ZTEST(socket_packet, test_raw_packet_sockets)
226213
socklen_t addrlen;
227214
ssize_t sent = 0;
228215

229-
__test_packet_sockets(&packet_sock_1, &packet_sock_2);
216+
prepare_packet_socket(&packet_sock_1, ud.first, SOCK_RAW, htons(ETH_P_ALL));
217+
prepare_packet_socket(&packet_sock_2, ud.second, SOCK_RAW, htons(ETH_P_ALL));
230218

231219
/* Prepare UDP socket which will read data */
232220
prepare_udp_socket(&udp_sock_1, &sockaddr, DST_PORT);
@@ -278,23 +266,18 @@ ZTEST(socket_packet, test_raw_packet_sockets)
278266

279267
ZTEST(socket_packet, test_packet_sockets)
280268
{
281-
__test_packet_sockets(&packet_sock_1, &packet_sock_2);
269+
prepare_packet_socket(&packet_sock_1, ud.first, SOCK_RAW, htons(ETH_P_ALL));
270+
prepare_packet_socket(&packet_sock_2, ud.second, SOCK_RAW, htons(ETH_P_ALL));
282271
}
283272

284273
ZTEST(socket_packet, test_packet_sockets_dgram)
285274
{
286275
uint8_t data_to_send[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
287276
uint8_t data_to_receive[32];
288277
socklen_t addrlen = sizeof(struct sockaddr_ll);
289-
struct user_data ud = { 0 };
290278
struct sockaddr_ll dst, src;
291279
int ret;
292280

293-
net_if_foreach(iface_cb, &ud);
294-
295-
zassert_not_null(ud.first, "1st Ethernet interface not found");
296-
zassert_not_null(ud.second, "2nd Ethernet interface not found");
297-
298281
prepare_packet_socket(&packet_sock_1, ud.first, SOCK_DGRAM, htons(ETH_P_TSN));
299282
prepare_packet_socket(&packet_sock_2, ud.second, SOCK_DGRAM, htons(ETH_P_TSN));
300283

@@ -412,7 +395,6 @@ ZTEST(socket_packet, test_raw_and_dgram_socket_exchange)
412395
uint8_t data_to_send[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
413396
uint8_t data_to_receive[32];
414397
socklen_t addrlen = sizeof(struct sockaddr_ll);
415-
struct user_data ud = { 0 };
416398
struct sockaddr_ll dst, src;
417399
int ret;
418400
const uint8_t expected_payload_raw[] = {
@@ -428,12 +410,6 @@ ZTEST(socket_packet, test_raw_and_dgram_socket_exchange)
428410
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 /* Payload */
429411
};
430412

431-
432-
net_if_foreach(iface_cb, &ud);
433-
434-
zassert_not_null(ud.first, "1st Ethernet interface not found");
435-
zassert_not_null(ud.second, "2nd Ethernet interface not found");
436-
437413
prepare_packet_socket(&packet_sock_1, ud.first, SOCK_DGRAM, htons(ETH_P_ALL));
438414
prepare_packet_socket(&packet_sock_2, ud.second, SOCK_RAW, htons(ETH_P_ALL));
439415

@@ -488,7 +464,6 @@ ZTEST(socket_packet, test_raw_and_dgram_socket_recv)
488464
uint8_t data_to_send[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
489465
uint8_t data_to_receive[32];
490466
socklen_t addrlen = sizeof(struct sockaddr_ll);
491-
struct user_data ud = { 0 };
492467
struct sockaddr_ll dst, src;
493468
int ret;
494469
const uint8_t expected_payload_raw[] = {
@@ -498,11 +473,6 @@ ZTEST(socket_packet, test_raw_and_dgram_socket_recv)
498473
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 /* Payload */
499474
};
500475

501-
net_if_foreach(iface_cb, &ud);
502-
503-
zassert_not_null(ud.first, "1st Ethernet interface not found");
504-
zassert_not_null(ud.second, "2nd Ethernet interface not found");
505-
506476
prepare_packet_socket(&packet_sock_1, ud.first, SOCK_DGRAM, htons(ETH_P_ALL));
507477
prepare_packet_socket(&packet_sock_2, ud.second, SOCK_RAW, htons(ETH_P_ALL));
508478
prepare_packet_socket(&packet_sock_3, ud.second, SOCK_RAW, htons(ETH_P_ALL));
@@ -577,4 +547,14 @@ static void test_after(void *arg)
577547
test_sockets_close();
578548
}
579549

580-
ZTEST_SUITE(socket_packet, NULL, NULL, NULL, test_after, NULL);
550+
static void *test_setup(void)
551+
{
552+
net_if_foreach(iface_cb, &ud);
553+
554+
zassert_not_null(ud.first, "1st Ethernet interface not found");
555+
zassert_not_null(ud.second, "2nd Ethernet interface not found");
556+
557+
return NULL;
558+
}
559+
560+
ZTEST_SUITE(socket_packet, NULL, test_setup, NULL, test_after, NULL);

0 commit comments

Comments
 (0)