From: Oleksij Rempel <o.re…@pengutronix.de> Add a selftest for J1939 NAME binding and source addresses: a socket bound to a NAME reads it back with getsockname(), and a socket bound to a source address sends frames with that address in the CAN id. A stress test runs several TP sessions at the same time, each with its own payload. 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 | 1 + .../selftests/net/can/test_j1939_addr.c | 233 ++++++++++++++++++ .../selftests/net/can/test_j1939_addr.sh | 4 + 4 files changed, 239 insertions(+) create mode 100644 tools/testing/selftests/net/can/test_j1939_addr.c create mode 100755 tools/testing/selftests/net/can/test_j1939_addr.sh diff --git a/tools/testing/selftests/net/can/.gitignore b/tools/testing/selftests/net/can/.gitignore index 75c078a2492e..1a6e89cdceb4 100644 --- a/tools/testing/selftests/net/can/.gitignore +++ b/tools/testing/selftests/net/can/.gitignore @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only j1939_gw +test_j1939_addr test_j1939_base test_j1939_opts test_raw_filter diff --git a/tools/testing/selftests/net/can/Makefile b/tools/testing/selftests/net/can/Makefile index 996b5bbd715a..0ca1d1801ff5 100644 --- a/tools/testing/selftests/net/can/Makefile +++ b/tools/testing/selftests/net/can/Makefile @@ -7,6 +7,7 @@ CFLAGS += -Wall -Wl,--no-as-needed -O2 -g -I$(top_srcdir)/usr/include -I$(top_sr LOCAL_HDRS := j1939_helpers.h j1939_tests.h ../../clock-helpers.h TEST_PROGS := \ + test_j1939_addr.sh \ test_j1939_base.sh \ test_j1939_opts.sh \ test_raw_filter.sh \ diff --git a/tools/testing/selftests/net/can/test_j1939_addr.c b/tools/testing/selftests/net/can/test_j1939_addr.c new file mode 100644 index 000000000000..b73e4b59fbe5 --- /dev/null +++ b/tools/testing/selftests/net/can/test_j1939_addr.c @@ -0,0 +1,233 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) +/* + * J1939 addressing and concurrency selftests. + * + * Cover binding a NAME and reading it back, and the bound source address in + * the CAN id. Also run many concurrent transport sessions. + * + * Run by test_j1939_addr.sh, see j1939_tests.h. + */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/wait.h> + +#include "j1939_tests.h" + +/* fixed unicast addresses, arbitrary */ +#define SA_TX 0x80 +#define SA_RX 0x90 + +/* stress session i sends from SA_STRESS_TX + i to SA_STRESS_RX + i */ +#define SA_STRESS_TX 0x40 +#define SA_STRESS_RX 0x20 + +/* NAME, arbitrary */ +#define NAME_TX 0x00000000c0ffee01ULL + +/* PGN, arbitrary */ +#define PGN_TEST 0x0ab00 +static_assert(J1939_IS_PDU1(PGN_TEST), "PGN_TEST must be PDU1"); + +/* stress run, arbitrary tuning */ +#define STRESS_N 8 +#define STRESS_SIZE 128 /* a TP size */ +static_assert(STRESS_SIZE > J1939_SF_MAX_SIZE, "STRESS_SIZE must need TP"); +static_assert(STRESS_SIZE <= J1939_TP_MAX_SIZE, "STRESS_SIZE must fit TP"); +/* the stress addresses lie below the fixed ones, which differ */ +static_assert(SA_STRESS_RX + STRESS_N <= SA_STRESS_TX && + SA_STRESS_TX + STRESS_N <= SA_TX && + SA_STRESS_TX + STRESS_N <= SA_RX && SA_TX != SA_RX, + "test addresses overlap"); +static_assert((SA_TX <= J1939_MAX_UNICAST_ADDR) && + (SA_RX <= J1939_MAX_UNICAST_ADDR), + "the fixed addresses must be unicast"); + +/* ends of a pipe(2) fd pair */ +enum { PIPE_RD, PIPE_WR, PIPE_NUM }; + +static int j1939_open_name(int ifindex, name_t name, uint8_t sa, pgn_t pgn) +{ + return j1939_open_flags(ifindex, name, sa, pgn, J1939_OPEN_NONE); +} + +/* + * One TP transfer from sa_tx to sa_rx. The payload depends on sa_tx, so a + * receiver that gets the data of another session fails. After the setup, write + * one byte to ready, close it and wait until the parent closes go, so that all + * sessions start together. + * Returns the exit status of the child process. + */ +static int stress_child(int ifindex, uint8_t sa_tx, uint8_t sa_rx, int ready, + int go) +{ + uint8_t tx_buf[STRESS_SIZE]; + struct sockaddr_can src; + int rc = EXIT_FAILURE; + bool setup, signalled; + char c = 0; /* the sync byte, any value */ + int tx, rx; + size_t i; + + fill_pattern(tx_buf, sizeof(tx_buf)); + for (i = 0; i < sizeof(tx_buf); i++) + tx_buf[i] ^= sa_tx; + + rx = j1939_open(ifindex, sa_rx, PGN_TEST); + tx = j1939_open(ifindex, sa_tx, J1939_NO_PGN); + setup = rx >= 0 && tx >= 0; + if (setup) + setup = j1939_connect_ok(tx, ifindex, sa_rx, PGN_TEST); + + /* + * Signal even after a setup error, and close ready at once: then the + * parent gets EOF, not a hang, if a child dies before its write. + */ + signalled = write_all_ok(ready, &c, sizeof(c)); + close(ready); + + if (!signalled) + goto out; + /* read() returns 0 at EOF, when the parent closes go */ + if (read(go, &c, sizeof(c)) != 0) + goto out; + if (!setup) + goto out; + + if (!send_all_ok(tx, tx_buf, sizeof(tx_buf))) + goto out; + + if (recvfrom_verify_ok(rx, tx_buf, sizeof(tx_buf), + J1939_RECV_TIMEOUT_MS, &src) && + src.can_addr.j1939.addr == sa_tx) + rc = EXIT_SUCCESS; +out: + if (rx >= 0) + close(rx); + if (tx >= 0) + close(tx); + return rc; +} + +FIXTURE(j1939_addr) +{ + int ifindex; + int tx; + int raw; +}; + +FIXTURE_SETUP(j1939_addr) +{ + self->tx = -1; + self->raw = -1; + + J1939_SETUP_IFINDEX(self); +} + +FIXTURE_TEARDOWN(j1939_addr) +{ + if (self->tx >= 0) + close(self->tx); + if (self->raw >= 0) + close(self->raw); +} + +/* Bind with a NAME and read it back with getsockname(2). */ +TEST_F(j1939_addr, name_bind_getsockname) +{ + struct sockaddr_can addr = {}; + socklen_t len = sizeof(addr); + + self->tx = j1939_open_name(self->ifindex, NAME_TX, SA_TX, J1939_NO_PGN); + ASSERT_GE(self->tx, 0) + TH_LOG("bind with NAME failed: %d", errno); + + ASSERT_EQ(getsockname(self->tx, (struct sockaddr *)&addr, &len), 0); + EXPECT_EQ(addr.can_addr.j1939.name, NAME_TX); + EXPECT_EQ(addr.can_addr.j1939.addr, SA_TX); +} + +/* The bound source address is used as source address on the wire. */ +TEST_F(j1939_addr, source_sa_on_wire) +{ + uint8_t buf[CAN_MAX_DLEN] = {}; + struct can_frame frame; + + self->raw = open_raw(); + ASSERT_GE(self->raw, 0); + + /* static source address: no address claim needed to transmit */ + self->tx = j1939_open(self->ifindex, SA_TX, J1939_NO_PGN); + ASSERT_GE(self->tx, 0); + ASSERT_TRUE(j1939_connect_ok(self->tx, self->ifindex, SA_RX, PGN_TEST)); + + ASSERT_TRUE(send_all_ok(self->tx, buf, sizeof(buf))); + + ASSERT_TRUE(recv_frame_ok(self->raw, &frame, J1939_RECV_TIMEOUT_MS)); + ASSERT_TRUE(j1939_is_data_frame(frame.can_id)) + TH_LOG("CAN id 0x%08x is not a 29-bit data frame", + frame.can_id); + EXPECT_EQ(j1939_id_sa(frame.can_id), SA_TX) + TH_LOG("source address on wire is 0x%02x, expected 0x%02x", + j1939_id_sa(frame.can_id), SA_TX); +} + +/* Many concurrent transport-protocol sessions run without corruption. */ +TEST_F(j1939_addr, stress_concurrent_sessions) +{ + pid_t pids[STRESS_N]; + int ready[PIPE_NUM]; + int go[PIPE_NUM]; + int failed = 0; + int i, n; + char c; + + ASSERT_EQ(pipe(ready), 0); + ASSERT_EQ(pipe(go), 0); + + for (n = 0; n < STRESS_N; n++) { + pids[n] = fork(); + if (pids[n] < 0) + break; + if (pids[n] == 0) { + close(ready[PIPE_RD]); + close(go[PIPE_WR]); + _exit(stress_child(self->ifindex, SA_STRESS_TX + n, + SA_STRESS_RX + n, ready[PIPE_WR], + go[PIPE_RD])); + } + } + + close(ready[PIPE_WR]); + close(go[PIPE_RD]); + + /* wait until all children are set up, then start them */ + for (i = 0; i < n; i++) { + if (!read_all_ok(ready[PIPE_RD], &c, sizeof(c))) + break; + } + close(ready[PIPE_RD]); + close(go[PIPE_WR]); + + for (i = 0; i < n; i++) { + int status; + + if (waitpid(pids[i], &status, 0) < 0) { + failed++; + continue; + } + if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) + failed++; + } + + ASSERT_EQ(n, STRESS_N) + TH_LOG("fork failed after %d of %d children", n, STRESS_N); + EXPECT_EQ(failed, 0) + TH_LOG("%d of %d concurrent sessions failed", failed, STRESS_N); +} + +int main(int argc, char **argv) +{ + return j1939_test_main(argc, argv, J1939_NEEDS_RAW); +} diff --git a/tools/testing/selftests/net/can/test_j1939_addr.sh b/tools/testing/selftests/net/can/test_j1939_addr.sh new file mode 100755 index 000000000000..61f65365ec56 --- /dev/null +++ b/tools/testing/selftests/net/can/test_j1939_addr.sh @@ -0,0 +1,4 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +source "$(dirname "$0")/j1939_lib.sh"
-- 2.53.0