From: Oleksij Rempel <o.re…@pengutronix.de> Add a functional selftest for the SAE J1939 socket interface. It sends between CAN_J1939 sockets over a vcan interface and checks payload integrity for single frame, TP and ETP sizes, the boundaries between them, and single-frame and BAM broadcast. A single-frame broadcast reaches a receiver only with SO_BROADCAST. Enable CONFIG_CAN_J1939, CONFIG_CAN_RAW and CONFIG_CAN_GW in the test config: CAN_RAW for the CAN_RAW peer of the later tests, CAN_GW for the gateway that j1939_lib.sh sets up. This test opens no CAN_RAW socket and skips only without CAN_J1939. Signed-off-by: Oleksij Rempel <o.re…@pengutronix.de> Link: https://patch.msgid.link/2026…@pengutronix.de [mkl: switch to static_assert()] Signed-off-by: Marc Kleine-Budde <m…@pengutronix.de> --- tools/testing/selftests/net/can/.gitignore | 1 + tools/testing/selftests/net/can/Makefile | 14 +- tools/testing/selftests/net/can/config | 3 + .../testing/selftests/net/can/j1939_helpers.c | 11 + .../testing/selftests/net/can/j1939_helpers.h | 8 + .../selftests/net/can/test_j1939_base.c | 241 ++++++++++++++++++ .../selftests/net/can/test_j1939_base.sh | 7 + 7 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/net/can/test_j1939_base.c create mode 100755 tools/testing/selftests/net/can/test_j1939_base.sh diff --git a/tools/testing/selftests/net/can/.gitignore b/tools/testing/selftests/net/can/.gitignore index 30cd8b40d0f8..e566019425c8 100644 --- a/tools/testing/selftests/net/can/.gitignore +++ b/tools/testing/selftests/net/can/.gitignore @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only j1939_gw +test_j1939_base test_raw_filter diff --git a/tools/testing/selftests/net/can/Makefile b/tools/testing/selftests/net/can/Makefile index cf0b06974d54..b09887a0d31f 100644 --- a/tools/testing/selftests/net/can/Makefile +++ b/tools/testing/selftests/net/can/Makefile @@ -4,14 +4,24 @@ top_srcdir = ../../../../.. CFLAGS += -Wall -Wl,--no-as-needed -O2 -g -I$(top_srcdir)/usr/include -I$(top_srcdir)/tools/include $(KHDR_INCLUDES) -TEST_PROGS := test_raw_filter.sh +LOCAL_HDRS := j1939_helpers.h j1939_tests.h ../../clock-helpers.h + +TEST_PROGS := \ + test_j1939_base.sh \ + test_raw_filter.sh \ +# end of TEST_PROGS TEST_FILES := j1939_lib.sh TEST_INCLUDES := ../lib.sh $(wildcard ../lib/sh/*.sh) -TEST_GEN_FILES := test_raw_filter +# each .sh in TEST_PROGS runs the binary of the same name +TEST_GEN_FILES := $(TEST_PROGS:.sh=) + # sets up the CAN gateway for j1939_lib.sh TEST_GEN_FILES += j1939_gw include ../../lib.mk + +# the J1939 tests link the shared helper code +$(filter $(OUTPUT)/test_j1939_%,$(TEST_GEN_FILES)): j1939_helpers.c diff --git a/tools/testing/selftests/net/can/config b/tools/testing/selftests/net/can/config index 188f79796670..e124727f63b6 100644 --- a/tools/testing/selftests/net/can/config +++ b/tools/testing/selftests/net/can/config @@ -1,3 +1,6 @@ CONFIG_CAN=m CONFIG_CAN_DEV=m +CONFIG_CAN_GW=m +CONFIG_CAN_J1939=m +CONFIG_CAN_RAW=m CONFIG_CAN_VCAN=m diff --git a/tools/testing/selftests/net/can/j1939_helpers.c b/tools/testing/selftests/net/can/j1939_helpers.c index 38c584d9c286..1045575a8a4e 100644 --- a/tools/testing/selftests/net/can/j1939_helpers.c +++ b/tools/testing/selftests/net/can/j1939_helpers.c @@ -243,6 +243,17 @@ static ssize_t recv_timeout(int s, void *buf, size_t len, int timeout_ms) return recv(s, buf, len, 0); } +bool recv_none_ok(int s) +{ + struct pollfd pfd = { .fd = s, .events = POLLIN }; + uint8_t drop; + + if (poll(&pfd, 1, J1939_NEG_RECV_TIMEOUT_MS) == 0) + return true; + recv(s, &drop, sizeof(drop), MSG_DONTWAIT); + return false; +} + bool recvfrom_verify_ok(int s, const void *want, size_t len, int timeout_ms, struct sockaddr_can *src) { diff --git a/tools/testing/selftests/net/can/j1939_helpers.h b/tools/testing/selftests/net/can/j1939_helpers.h index 87961b5bbb15..0235d8ce2a8e 100644 --- a/tools/testing/selftests/net/can/j1939_helpers.h +++ b/tools/testing/selftests/net/can/j1939_helpers.h @@ -198,6 +198,14 @@ int open_raw_rcvbuf(int rcvbuf); /* open_raw_rcvbuf() with the default receive buffer. */ int open_raw(void); +/* + * Return true if poll() on s times out after J1939_NEG_RECV_TIMEOUT_MS. + * Otherwise (a message, a socket error or a poll() failure) do one recv() + * with MSG_DONTWAIT and return false. That recv() drops a waiting message + * or clears a pending socket error. + */ +bool recv_none_ok(int s); + /* * recvfrom() one message on s within timeout_ms, check that it is the len * bytes in want, and return the sender in *src unless src is NULL. The buffer diff --git a/tools/testing/selftests/net/can/test_j1939_base.c b/tools/testing/selftests/net/can/test_j1939_base.c new file mode 100644 index 000000000000..e36f5117a54e --- /dev/null +++ b/tools/testing/selftests/net/can/test_j1939_base.c @@ -0,0 +1,241 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) +/* + * J1939 base functional selftests. + * + * Send between SOCK_DGRAM/CAN_J1939 sockets over a vcan interface and verify + * payload integrity across the transport-size transitions: + * a single frame (PDU1), Transport Protocol, Extended Transport Protocol, the + * exact boundaries between them, and single-frame and BAM broadcast (PDU2). + * + * Run by test_j1939_base.sh, see j1939_tests.h. + */ +#include <errno.h> + +#include "j1939_tests.h" + +/* arbitrary addresses and PGNs, within the asserts below */ +#define SA_TX 0x80 +#define SA_RX 0x90 +#define PGN_PDU1 0x0ab00 /* destination specific */ +#define PGN_PDU2 0x0feee /* broadcast */ +static_assert(J1939_IS_PDU1(PGN_PDU1) && !J1939_IS_PDU1(PGN_PDU2), + "PGN_PDU1 must be PDU1 and PGN_PDU2 must be PDU2"); +static_assert((PGN_PDU1 & UINT8_MAX) == 0, + "the kernel rejects a PDU1 PGN whose PS byte is not 0"); +static_assert(PGN_PDU1 <= J1939_PGN_MAX && PGN_PDU2 <= J1939_PGN_MAX, + "PGN_PDU1 and PGN_PDU2 must be valid PGNs"); +static_assert(SA_TX != SA_RX && SA_TX <= J1939_MAX_UNICAST_ADDR && + SA_RX <= J1939_MAX_UNICAST_ADDR, + "SA_TX and SA_RX must be two different unicast addresses"); + +/* An arbitrary TP size inside the TP range, and one that needs ETP. */ +#define TP_SIZE 128 +#define ETP_SIZE 2000 +static_assert(TP_SIZE > J1939_SF_MAX_SIZE, "TP_SIZE must need TP"); +static_assert(TP_SIZE < J1939_TP_MAX_SIZE, "TP_SIZE must fit TP"); +static_assert(ETP_SIZE > J1939_TP_MAX_SIZE, "ETP_SIZE must need ETP"); + +/* + * Longest gap the spec allows before a BAM data packet (SAE J1939-21:2001 + * section 5.12.3). The kernel waits 50 ms, see j1939_session_tx_rts() and + * j1939_session_tx_dat(). + */ +#define BAM_PKT_GAP_MAX_MS 200 + +FIXTURE(j1939_base) +{ + int ifindex; + int tx; + int rx; + int rx_no_bcast; /* a receiver without SO_BROADCAST */ + bool bcast; +}; + +FIXTURE_SETUP(j1939_base) +{ + self->tx = -1; + self->rx = -1; + self->rx_no_bcast = -1; + J1939_SETUP_IFINDEX(self); +} + +FIXTURE_TEARDOWN(j1939_base) +{ + if (self->tx >= 0) + close(self->tx); + if (self->rx >= 0) + close(self->rx); + if (self->rx_no_bcast >= 0) + close(self->rx_no_bcast); +} + +/* + * Set up a receiver bound to (rx_addr, pgn) and a sender bound to SA_TX and + * connected to (tx_dst, pgn). Any pair opened by a previous call is closed + * first. bcast opens both sockets with J1939_OPEN_BCAST, for the PDU2 and BAM + * tests. + */ +static bool pair_up_ok(FIXTURE_DATA(j1939_base) *self, uint8_t rx_addr, + uint8_t tx_dst, pgn_t pgn, bool bcast) +{ + int flags; + + flags = bcast ? J1939_OPEN_BCAST : J1939_OPEN_NONE; + self->bcast = bcast; + if (self->tx >= 0) { + close(self->tx); + self->tx = -1; + } + if (self->rx >= 0) { + close(self->rx); + self->rx = -1; + } + + self->rx = j1939_open_flags(self->ifindex, J1939_NO_NAME, rx_addr, pgn, + flags); + if (self->rx < 0) { + fprintf(stderr, "# open rx failed: %d\n", errno); + return false; + } + + self->tx = j1939_open_flags(self->ifindex, J1939_NO_NAME, SA_TX, + J1939_NO_PGN, flags); + if (self->tx < 0) { + fprintf(stderr, "# open tx failed: %d\n", errno); + return false; + } + + if (!j1939_connect_ok(self->tx, self->ifindex, tx_dst, pgn)) { + fprintf(stderr, "# connect tx failed: %d\n", errno); + return false; + } + + return true; +} + +/* + * Send a deterministic payload of the given size on tx and verify it is + * received byte for byte on rx. + */ +static bool roundtrip_ok(FIXTURE_DATA(j1939_base) *self, size_t size) +{ + int timeout_ms = J1939_RECV_TIMEOUT_MS; + uint8_t *tx_buf; + bool ok; + + /* a BAM message arrives only after all its packet gaps */ + if (self->bcast && size > J1939_SF_MAX_SIZE) + timeout_ms += J1939_NUM_PKTS(size) * BAM_PKT_GAP_MAX_MS; + + tx_buf = malloc(size); + if (!tx_buf) { + fprintf(stderr, "# alloc failed\n"); + return false; + } + fill_pattern(tx_buf, size); + + errno = 0; + ok = send_recv_verify_ok(self->tx, self->rx, tx_buf, size, timeout_ms); + if (!ok) { + fprintf(stderr, "# round-trip failed at size %zu: %s\n", size, + errno ? strerror(errno) : "no or wrong data"); + } + + free(tx_buf); + return ok; +} + +/* Largest single frame, unicast PDU1. */ +TEST_F(j1939_base, single_frame_pdu1) +{ + ASSERT_TRUE(pair_up_ok(self, SA_RX, SA_RX, PGN_PDU1, false)); + EXPECT_TRUE(roundtrip_ok(self, J1939_SF_MAX_SIZE)); +} + +/* Transport Protocol, unicast. */ +TEST_F(j1939_base, tp_roundtrip) +{ + ASSERT_TRUE(pair_up_ok(self, SA_RX, SA_RX, PGN_PDU1, false)); + EXPECT_TRUE(roundtrip_ok(self, TP_SIZE)); +} + +/* Extended Transport Protocol, unicast. */ +TEST_F(j1939_base, etp_roundtrip) +{ + ASSERT_TRUE(pair_up_ok(self, SA_RX, SA_RX, PGN_PDU1, false)); + EXPECT_TRUE(roundtrip_ok(self, ETP_SIZE)); +} + +/* + * Sizes on both sides of the single frame / TP and TP / ETP limits. Only the + * payload is checked here, not which transport carried it. + */ +TEST_F(j1939_base, size_boundaries) +{ + static const size_t sizes[] = { + J1939_SF_MAX_SIZE, J1939_SF_MAX_SIZE + 1, + J1939_TP_MAX_SIZE, J1939_TP_MAX_SIZE + 1, + }; + size_t k; + + for (k = 0; k < ARRAY_SIZE(sizes); k++) { + ASSERT_TRUE(pair_up_ok(self, SA_RX, SA_RX, PGN_PDU1, false)); + EXPECT_TRUE(roundtrip_ok(self, sizes[k])) + TH_LOG("failed at boundary size %zu", sizes[k]); + } +} + +/* Single-frame broadcast, PDU2. */ +TEST_F(j1939_base, single_frame_broadcast_pdu2) +{ + ASSERT_TRUE(pair_up_ok(self, SA_RX, J1939_NO_ADDR, PGN_PDU2, true)); + EXPECT_TRUE(roundtrip_ok(self, J1939_SF_MAX_SIZE)); +} + +/* BAM (broadcast transport). */ +TEST_F(j1939_base, bam_broadcast) +{ + ASSERT_TRUE(pair_up_ok(self, SA_RX, J1939_NO_ADDR, PGN_PDU2, true)); + EXPECT_TRUE(roundtrip_ok(self, TP_SIZE)); +} + +/* A single-frame broadcast reaches only a receiver with SO_BROADCAST. */ +TEST_F(j1939_base, bcast_needs_so_broadcast) +{ + uint8_t second_buf[J1939_SF_MAX_SIZE]; + uint8_t tx_buf[J1939_SF_MAX_SIZE]; + int enable = 1; + + fill_pattern(tx_buf, sizeof(tx_buf)); + memcpy(second_buf, tx_buf, sizeof(second_buf)); + second_buf[0] ^= UINT8_MAX; + + ASSERT_TRUE(pair_up_ok(self, SA_RX, J1939_NO_ADDR, PGN_PDU2, true)); + self->rx_no_bcast = j1939_open(self->ifindex, SA_RX, PGN_PDU2); + ASSERT_GE(self->rx_no_bcast, 0) + TH_LOG("open rx_no_bcast failed: %d", errno); + + /* the receiver with SO_BROADCAST shows that the frame went out */ + ASSERT_TRUE(send_recv_verify_ok(self->tx, self->rx, tx_buf, + sizeof(tx_buf), J1939_RECV_TIMEOUT_MS)); + EXPECT_TRUE(recv_none_ok(self->rx_no_bcast)) + TH_LOG("a receiver without SO_BROADCAST got a frame or error"); + + /* + * Set SO_BROADCAST and send a second frame. It must arrive now. + * That shows the first frame was dropped for the missing flag, + * not for another reason. The second frame has its own payload, + * so a late first frame cannot be mistaken for it. + */ + ASSERT_EQ(setsockopt(self->rx_no_bcast, SOL_SOCKET, SO_BROADCAST, + &enable, sizeof(enable)), 0); + EXPECT_TRUE(send_recv_verify_ok(self->tx, self->rx_no_bcast, + second_buf, sizeof(second_buf), + J1939_RECV_TIMEOUT_MS)) + TH_LOG("second broadcast missing or not the expected payload"); +} + +int main(int argc, char **argv) +{ + return j1939_test_main(argc, argv, J1939_NEEDS_NONE); +} diff --git a/tools/testing/selftests/net/can/test_j1939_base.sh b/tools/testing/selftests/net/can/test_j1939_base.sh new file mode 100755 index 000000000000..dd19cd469651 --- /dev/null +++ b/tools/testing/selftests/net/can/test_j1939_base.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +# test_j1939_base opens no CAN_RAW socket +# shellcheck disable=SC2034 # read by j1939_lib.sh +J1939_NO_PEER=1 +source "$(dirname "$0")/j1939_lib.sh"
-- 2.53.0