Add test case to verify proper handling of Netlink sockets in procfs:
- Tests /proc/net/netlink file accessibility - Validates socket count changes on creation/deletion - Uses kernel's netlink_seq_ops mechanism
Checks that socket entries are correctly added/removed from procfs.
Signed-off-by: Yana Bashlykova yana2bsh@gmail.com --- tools/testing/selftests/net/genetlink.c | 74 +++++++++++++++++++++++++ 1 file changed, 74 insertions(+)
diff --git a/tools/testing/selftests/net/genetlink.c b/tools/testing/selftests/net/genetlink.c index 5be9ca68accd..f8231a302c36 100644 --- a/tools/testing/selftests/net/genetlink.c +++ b/tools/testing/selftests/net/genetlink.c @@ -81,6 +81,25 @@
#define LARGE_GENL_FAMILY_NAME "LARGE_GENL"
+struct nl_sock *socket_alloc_and_conn(void) +{ + struct nl_sock *socket; + + socket = nl_socket_alloc(); + if (!socket) { + fprintf(stderr, "Failed to allocate socket\n"); + return NULL; + } + + if (genl_connect(socket)) { + fprintf(stderr, + "Failed to connect to generic netlink through socket\n"); + nl_socket_free(socket); + return NULL; + } + return socket; +} + /* * Test cases */ @@ -143,6 +162,61 @@ TEST(capture_start) printf("Starting Netlink tests...\n"); }
+/** + * TEST(open_netlink_file) - Verifies correct reading of Netlink socket information + * + * Tests the /proc/net/netlink interface by: + * 1. Creating a test Netlink socket + * 2. Reading the proc file before and after socket creation + * 3. Verifying the socket count changes as expected + * + * The test checks that: + * - /proc/net/netlink is accessible + * - Entries are properly added/removed + * - Uses kernel's netlink_seq_ops mechanism + */ + +TEST(open_netlink_file) +{ + FILE *file; + char line[256]; + int cnt = 0; + + printf("Running Test: opening and reading /proc/net/netlink file...\n"); + + struct nl_sock *sock; + + sock = socket_alloc_and_conn(); + + file = fopen("/proc/net/netlink", "r"); + ASSERT_NE(NULL, file); + if (file == NULL) { + perror("fopen"); + return; + } + + while (fgets(line, sizeof(line), file) != NULL) + cnt++; + + nl_socket_free(sock); + + fclose(file); + + file = fopen("/proc/net/netlink", "r"); + ASSERT_NE(NULL, file); + if (file == NULL) { + perror("fopen"); + return; + } + + while (fgets(line, sizeof(line), file) != NULL) + cnt--; + + EXPECT_EQ(cnt, 1); + + fclose(file); +} + /** * TEST(capture_end) - Terminates Netlink traffic monitoring session *