[PATCH can-next] selftests: can: cover CAN XL VCID getsockopt

From: WEI Qinggang

Date: Wed Nov 18 2026 - 22:14:35 EST


CAN_RAW_XL_VCID_OPTS returns the configured VCID options through
getsockopt(). When the supplied buffer is too small, it returns ERANGE
and updates optlen with the required size. Neither behavior is covered
by the CAN selftests.

Add two tests to a separate test_raw_sockopt executable. Set nonzero,
distinct VCID values and compare each returned field and the option
length. Pass a buffer length one byte below the structure size and
check the error and required length.

Use an unbound CAN_RAW socket so the tests need neither a CAN interface
nor vcan setup. Skip when CAN_RAW or the VCID option is unavailable.
Register the executable with kselftest and request CONFIG_CAN_RAW.

OpenAI Codex assisted with the implementation and commit message.

Signed-off-by: WEI Qinggang <stitchqgwei@xxxxxxx>
---
Tested with GCC 13.3.0 (-Werror). On Ubuntu 7.0.0-43-generic in QEMU,
both tests pass as root and as uid 65534 with only the loopback interface.
Both tests skip with CAN modules absent and on WSL 6.6 without the VCID
option. No vcan interface is created.

OpenAI Codex generated the implementation and commit message from a
request for these two cases and ran the build and runtime checks.

tools/testing/selftests/net/can/.gitignore | 1 +
tools/testing/selftests/net/can/Makefile | 2 +
tools/testing/selftests/net/can/config | 1 +
.../selftests/net/can/test_raw_sockopt.c | 80 +++++++++++++++++++
4 files changed, 84 insertions(+)
create mode 100644 tools/testing/selftests/net/can/test_raw_sockopt.c

diff --git a/tools/testing/selftests/net/can/.gitignore b/tools/testing/selftests/net/can/.gitignore
index 764a53fc8..f578c0821 100644
--- a/tools/testing/selftests/net/can/.gitignore
+++ b/tools/testing/selftests/net/can/.gitignore
@@ -1,2 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
test_raw_filter
+test_raw_sockopt
diff --git a/tools/testing/selftests/net/can/Makefile b/tools/testing/selftests/net/can/Makefile
index 5b82e60a0..43ea4efac 100644
--- a/tools/testing/selftests/net/can/Makefile
+++ b/tools/testing/selftests/net/can/Makefile
@@ -6,6 +6,8 @@ CFLAGS += -Wall -Wl,--no-as-needed -O2 -g -I$(top_srcdir)/usr/include $(KHDR_INC

TEST_PROGS := test_raw_filter.sh

+TEST_GEN_PROGS := test_raw_sockopt
+
TEST_GEN_FILES := test_raw_filter

include ../../lib.mk
diff --git a/tools/testing/selftests/net/can/config b/tools/testing/selftests/net/can/config
index 188f79796..2fb0a7a18 100644
--- a/tools/testing/selftests/net/can/config
+++ b/tools/testing/selftests/net/can/config
@@ -1,3 +1,4 @@
CONFIG_CAN=m
+CONFIG_CAN_RAW=m
CONFIG_CAN_DEV=m
CONFIG_CAN_VCAN=m
diff --git a/tools/testing/selftests/net/can/test_raw_sockopt.c b/tools/testing/selftests/net/can/test_raw_sockopt.c
new file mode 100644
index 000000000..e4175b8f6
--- /dev/null
+++ b/tools/testing/selftests/net/can/test_raw_sockopt.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* CAN_RAW socket option tests that do not require a CAN interface. */
+
+#include <errno.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <linux/can.h>
+#include <linux/can/raw.h>
+
+#include "kselftest_harness.h"
+
+static const struct can_raw_vcid_options vcid_opts = {
+ .flags = CAN_RAW_XL_VCID_TX_SET | CAN_RAW_XL_VCID_TX_PASS |
+ CAN_RAW_XL_VCID_RX_FILTER,
+ .tx_vcid = 0x5a,
+ .rx_vcid = 0xa5,
+ .rx_vcid_mask = 0xf0,
+};
+
+FIXTURE(can_raw_vcid)
+{
+ int sock;
+};
+
+FIXTURE_SETUP(can_raw_vcid)
+{
+ int ret;
+
+ self->sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+ if (self->sock < 0 &&
+ (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT))
+ SKIP(return, "CAN_RAW sockets are not supported");
+ ASSERT_GE(self->sock, 0);
+
+ ret = setsockopt(self->sock, SOL_CAN_RAW, CAN_RAW_XL_VCID_OPTS,
+ &vcid_opts, sizeof(vcid_opts));
+ if (ret < 0 && errno == ENOPROTOOPT) {
+ close(self->sock);
+ SKIP(return, "CAN_RAW_XL_VCID_OPTS is not supported");
+ }
+ ASSERT_EQ(0, ret);
+}
+
+FIXTURE_TEARDOWN(can_raw_vcid)
+{
+ close(self->sock);
+}
+
+TEST_F(can_raw_vcid, getsockopt_roundtrip)
+{
+ struct can_raw_vcid_options opts = { };
+ socklen_t len = sizeof(opts);
+ int ret;
+
+ ret = getsockopt(self->sock, SOL_CAN_RAW, CAN_RAW_XL_VCID_OPTS,
+ &opts, &len);
+ ASSERT_EQ(0, ret);
+ EXPECT_EQ(sizeof(opts), len);
+ EXPECT_EQ(vcid_opts.flags, opts.flags);
+ EXPECT_EQ(vcid_opts.tx_vcid, opts.tx_vcid);
+ EXPECT_EQ(vcid_opts.rx_vcid, opts.rx_vcid);
+ EXPECT_EQ(vcid_opts.rx_vcid_mask, opts.rx_vcid_mask);
+}
+
+TEST_F(can_raw_vcid, getsockopt_short_buffer)
+{
+ struct can_raw_vcid_options opts = { };
+ socklen_t len = sizeof(opts) - 1;
+ int ret, saved_errno;
+
+ ret = getsockopt(self->sock, SOL_CAN_RAW, CAN_RAW_XL_VCID_OPTS,
+ &opts, &len);
+ saved_errno = errno;
+ ASSERT_EQ(-1, ret);
+ EXPECT_EQ(ERANGE, saved_errno);
+ EXPECT_EQ(sizeof(opts), len);
+}
+
+TEST_HARNESS_MAIN

base-commit: 573af801552999954caa8347e4490b04cf4763f0
--
2.47.1.windows.1