@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
+/*
+ * J1939 wire-format selftests: a J1939 socket against CAN_RAW.
+ *
+ * Run by test_j1939_wire.sh, see j1939_tests.h.
+ */
+#include <errno.h>
+
+#include "kselftest_harness.h"
+#include "j1939_tests.h"
+
+/* arbitrary addresses and PGNs; only PDU class and DP bit matter */
+#define SA_LOCAL 0x90 /* the J1939 socket under test */
+#define SA_PEER 0x20 /* the CAN_RAW peer */
+/* a destination-specific PGN with DP set, and a broadcast PGN */
+#define PGN_PDU1 (J1939_PGN_DP | 0x0ab00)
+#define PGN_PDU2 0x0feee
+_Static_assert(J1939_IS_PDU1(PGN_PDU1) && !J1939_IS_PDU1(PGN_PDU2),
+ "PGN_PDU1 must be PDU1 and PGN_PDU2 must be PDU2");
+
+#define PRIO J1939_PRIO_DEFAULT
+
+FIXTURE(j1939_wire)
+{
+ int ifindex;
+ int j1939;
+ int raw;
+};
+
+FIXTURE_SETUP(j1939_wire)
+{
+ self->j1939 = -1;
+ self->raw = -1;
+
+ J1939_SETUP_IFINDEX(self);
+
+ J1939_SETUP_RAW(self, J1939_RAW_RCVBUF_DEFAULT);
+}
+
+FIXTURE_TEARDOWN(j1939_wire)
+{
+ if (self->j1939 >= 0)
+ close(self->j1939);
+ if (self->raw >= 0)
+ close(self->raw);
+}
+
+/*
+ * Inject one CAN frame from the raw peer and check that the J1939 socket
+ * receives the same CAN_MAX_DLEN-byte payload, from SA_PEER and with PGN @pgn.
+ * Opens the J1939 RX socket into self->j1939 (the fixture teardown closes it).
+ * @bcast selects a broadcast (PDU2) socket and frame; @pgn and @da address the
+ * injected frame.
+ */
+static bool rx_single_frame_ok(FIXTURE_DATA(j1939_wire) *self, bool bcast,
+ pgn_t pgn, uint8_t da,
+ const uint8_t payload[CAN_MAX_DLEN])
+{
+ struct sockaddr_can src;
+
+ if (bcast)
+ self->j1939 = j1939_open_flags(self->ifindex, J1939_NO_NAME,
+ SA_LOCAL, pgn, J1939_OPEN_BCAST);
+ else
+ self->j1939 = j1939_open(self->ifindex, SA_LOCAL, pgn);
+ if (self->j1939 < 0)
+ return false;
+
+ if (!send_raw8_ok(self->raw, PRIO, pgn, da, SA_PEER, payload))
+ return false;
+
+ if (!recvfrom_verify_ok(self->j1939, payload, CAN_MAX_DLEN,
+ J1939_RECV_TIMEOUT_MS, &src)) {
+ fprintf(stderr, "# payload missing or not the one sent\n");
+ return false;
+ }
+
+ if (src.can_addr.j1939.addr != SA_PEER ||
+ src.can_addr.j1939.pgn != pgn) {
+ fprintf(stderr,
+ "# from SA 0x%02x PGN 0x%05x, want 0x%02x 0x%05x\n",
+ src.can_addr.j1939.addr, src.can_addr.j1939.pgn,
+ SA_PEER, pgn);
+ return false;
+ }
+ return true;
+}
+
+/*
+ * Send one CAN_MAX_DLEN-byte payload from the J1939 socket to the raw peer and
+ * return the CAN frame seen on the wire via *frame, so each caller can assert
+ * the id fields it cares about. Opens the J1939 TX socket into self->j1939
+ * (the fixture teardown closes it) and connects it to (@da, @pgn); @bcast
+ * opens a broadcast (PDU2) socket.
+ */
+static bool tx_to_raw_send_recv_ok(FIXTURE_DATA(j1939_wire) *self, bool bcast,
+ pgn_t pgn, uint8_t da,
+ const uint8_t payload[CAN_MAX_DLEN],
+ struct can_frame *frame)
+{
+ if (bcast)
+ self->j1939 = j1939_open_flags(self->ifindex, J1939_NO_NAME,
+ SA_LOCAL, J1939_NO_PGN,
+ J1939_OPEN_BCAST);
+ else
+ self->j1939 = j1939_open(self->ifindex, SA_LOCAL, J1939_NO_PGN);
+ if (self->j1939 < 0)
+ return false;
+
+ if (!j1939_connect_ok(self->j1939, self->ifindex, da, pgn))
+ return false;
+
+ if (!send_all_ok(self->j1939, payload, CAN_MAX_DLEN))
+ return false;
+
+ return recv_frame_ok(self->raw, frame, J1939_RECV_TIMEOUT_MS);
+}
+
+/* J1939 TX -> CAN_RAW: destination-specific (PDU1) single frame. */
+TEST_F(j1939_wire, tx_pdu1_to_raw)
+{
+ uint8_t payload[CAN_MAX_DLEN];
+ struct can_frame frame;
+
+ fill_pattern(payload, sizeof(payload));
+ ASSERT_TRUE(tx_to_raw_send_recv_ok(self, false, PGN_PDU1, SA_PEER,
+ payload, &frame));
+ 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_prio(frame.can_id), PRIO);
+ EXPECT_EQ(j1939_id_sa(frame.can_id), SA_LOCAL);
+ EXPECT_EQ(j1939_id_ps(frame.can_id), SA_PEER);
+ /* PGN, with the PS (the destination) masked off */
+ EXPECT_EQ(j1939_id_pgn(frame.can_id) & J1939_PGN_PDU1_MAX, PGN_PDU1);
+
+ ASSERT_EQ(frame.can_dlc, sizeof(payload));
+ EXPECT_EQ(memcmp(frame.data, payload, sizeof(payload)), 0);
+}
+
+/* CAN_RAW -> J1939 RX: destination-specific (PDU1) single frame. */
+TEST_F(j1939_wire, rx_pdu1_from_raw)
+{
+ uint8_t payload[CAN_MAX_DLEN];
+
+ fill_pattern(payload, sizeof(payload));
+ ASSERT_TRUE(rx_single_frame_ok(self, false, PGN_PDU1, SA_LOCAL,
+ payload));
+}
+
+/* J1939 TX -> CAN_RAW: broadcast (PDU2) single frame. */
+TEST_F(j1939_wire, tx_pdu2_to_raw)
+{
+ uint8_t payload[CAN_MAX_DLEN];
+ struct can_frame frame;
+
+ fill_pattern(payload, sizeof(payload));
+ ASSERT_TRUE(tx_to_raw_send_recv_ok(self, true, PGN_PDU2, J1939_NO_ADDR,
+ payload, &frame));
+ 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_prio(frame.can_id), PRIO);
+ EXPECT_EQ(j1939_id_sa(frame.can_id), SA_LOCAL);
+ EXPECT_EQ(j1939_id_pgn(frame.can_id), PGN_PDU2);
+
+ ASSERT_EQ(frame.can_dlc, sizeof(payload));
+ EXPECT_EQ(memcmp(frame.data, payload, sizeof(payload)), 0);
+}
+
+/* CAN_RAW -> J1939 RX: broadcast (PDU2) single frame. */
+TEST_F(j1939_wire, rx_pdu2_from_raw)
+{
+ uint8_t payload[CAN_MAX_DLEN];
+
+ fill_pattern(payload, sizeof(payload));
+ ASSERT_TRUE(rx_single_frame_ok(self, true, PGN_PDU2, J1939_NO_ADDR,
+ payload));
+}
+
+int main(int argc, char **argv)
+{
+ return j1939_test_main(argc, argv);
+}