Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Yana Bashlykova <yana2bsh@gmail.com>
To: "David S. Miller" <davem@davemloft.net>
Cc: Yana Bashlykova <yana2bsh@gmail.com>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org, lvc-project@linuxtesting.org
Subject: [PATCH 6.1 11/15] selftests: net: genetlink: add /proc/net/netlink test
Date: Fri, 12 Sep 2025 22:53:34 +0300	[thread overview]
Message-ID: <20250912195339.20635-12-yana2bsh@gmail.com> (raw)
In-Reply-To: <20250912195339.20635-1-yana2bsh@gmail.com>

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
  *
-- 
2.34.1


  parent reply	other threads:[~2025-09-12 19:54 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-12 19:53 [PATCH 6.1 00/15] genetlink: Test Netlink subsystem of Linux v6.1 Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 10/15] selftests: net: genetlink: add packet capture test infrastructure Yana Bashlykova
2025-09-12 19:53 ` Yana Bashlykova [this message]
2025-09-12 19:53 ` [PATCH 6.1 12/15] selftests: net: genetlink: add Generic Netlink controller tests Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 13/15] selftests: net: genetlink: add large family ID resolution test Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 14/15] selftests: net: genetlink: add Netlink and Generic Netlink test suite Yana Bashlykova
2025-09-12 19:53 ` [PATCH 6.1 15/15] selftests: net: genetlink: fix expectation for large family resolution Yana Bashlykova
2025-09-12 20:17 ` [PATCH 6.1 00/15] genetlink: Test Netlink subsystem of Linux v6.1 Jakub Kicinski
2025-10-15 14:49   ` Яна Башлыкова
2025-10-15 15:04     ` Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250912195339.20635-12-yana2bsh@gmail.com \
    --to=yana2bsh@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox