All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ossama Othman <ossama.othman@intel.com>
To: ell@lists.01.org
Subject: [PATCH] unit: Test generic netlink family registration
Date: Wed, 12 Apr 2017 13:04:17 -0700	[thread overview]
Message-ID: <1492027457-28188-1-git-send-email-ossama.othman@intel.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 4866 bytes --]

Verify that genl family registration works by attempting to set a
family_appeared watch for the "nlctrl" family, and registering a
callback for it.  The "nlctrl" generic netlink family always exists in
the kernel so it is a suitable family to use for testing the
family_appeared watch and related family registration operations.
---
 unit/test-genl.c | 114 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 93 insertions(+), 21 deletions(-)

diff --git a/unit/test-genl.c b/unit/test-genl.c
index 44fbbb0..632eceb 100644
--- a/unit/test-genl.c
+++ b/unit/test-genl.c
@@ -29,6 +29,15 @@
 
 #include <ell/ell.h>
 
+struct test_data
+{
+	struct l_genl_family *appeared_family;
+	struct l_genl_family *vanished_family;
+
+	unsigned int group_id;
+	bool vanished_called;
+};
+
 static void do_debug(const char *str, void *user_data)
 {
 	const char *prefix = user_data;
@@ -36,38 +45,102 @@ static void do_debug(const char *str, void *user_data)
 	l_info("%s%s", prefix, str);
 }
 
+static void notify_callback(struct l_genl_msg * msg, void * user_data)
+{
+}
+
+static void family_appeared(void *user_data)
+{
+	struct test_data *data = user_data;
+
+	data->group_id = l_genl_family_register(data->appeared_family,
+						"notify",
+						notify_callback,
+						NULL,
+						NULL);
+}
+
 static void family_vanished(void *user_data)
 {
-	bool *vanished_called = user_data;
+	struct test_data *data = user_data;
+
+	data->vanished_called = true;
+}
+
+static bool prep_family_appeared(struct l_genl *genl,
+					struct test_data *data)
+{
+	/*
+	 * Set a family_appeared watch for the "nlctrl" family.
+	 *
+	 * The "nlctrl" generic netlink family always exists in the
+	 * kernel so it is a suitable family to use for testing
+	 * the family_appeared watch and related family registration
+	 * operations.
+	 */
+	data->appeared_family = l_genl_family_new(genl, "nlctrl");
+
+	return l_genl_family_set_watches(data->appeared_family,
+						family_appeared, NULL,
+						data, NULL);
+}
+
+static bool prep_family_vanished(struct l_genl *genl,
+					struct test_data *data)
+{
+	/*
+	 * Use a bogus family name to trigger the vanished watch to
+	 * be called during the ELL event loop run.
+	 */
+	static const char BOGUS_GENL_NAME[] = "bogus_genl_family";
+
+	data->vanished_family = l_genl_family_new(genl, BOGUS_GENL_NAME);
+	return l_genl_family_set_watches(data->vanished_family,
+						NULL, family_vanished,
+						data, NULL);
+}
 
-	*vanished_called = true;
+static bool check_test_data(struct test_data *data)
+{
+    return data->group_id != 0 && data->vanished_called;
 }
 
 static void idle_callback(struct l_idle *idle, void *user_data)
 {
+	struct test_data *data = user_data;
 	static int count = 0;
 
 	/*
-	 * Allow the main loop to iterate at least twice to allow the
-	 * generic netlink watches to be called.
+	 * Exit the event loop if the desired results have been
+	 * obtained, but limit the number of iterations to prevent the
+	 * loop from running indefinetely if the conditions for
+	 * success are never reached.
+	 *
+	 * Allow the main loop to iterate at least four times to allow
+	 * the generic netlink watches and family registration to be
+	 * called and completed, respectively.
 	 */
-	if (++count > 1)
+	if (check_test_data(data) || ++count > 3)
 		l_main_quit();
 }
 
+static bool destroy_test_data(struct test_data *data)
+{
+	bool unregistered =
+	    l_genl_family_unregister(data->appeared_family,
+					data->group_id);
+
+	l_genl_family_unref(data->vanished_family);
+	l_genl_family_unref(data->appeared_family);
+
+	return unregistered;
+}
+
 int main(int argc, char *argv[])
 {
 	struct l_genl *genl;
-	struct l_genl_family *family;
 	struct l_idle *idle;
-
-	/*
-	 * Use a bogus family name to trigger the vanished watch to
-	 * be called.
-	 */
-	static const char BOGUS_GENL_NAME[] = "bogus_genl_family";
-
-	bool vanished_called = false;
+	struct test_data data = { .group_id = 0 };
 
 	if (!l_main_init())
 		return -1;
@@ -78,22 +151,21 @@ int main(int argc, char *argv[])
 
 	l_genl_set_debug(genl, do_debug, "[GENL] ", NULL);
 
-	family = l_genl_family_new(genl, BOGUS_GENL_NAME);
-	l_genl_family_set_watches(family, NULL, family_vanished,
-					&vanished_called, NULL);
+	assert(prep_family_appeared(genl, &data));
+	assert(prep_family_vanished(genl, &data));
 
-	idle = l_idle_create(idle_callback, NULL, NULL);
+	idle = l_idle_create(idle_callback, &data, NULL);
 
 	l_main_run();
 
 	l_idle_remove(idle);
 
-	l_genl_family_unref(family);
+	assert(check_test_data(&data));
+	assert(destroy_test_data(&data));
+
 	l_genl_unref(genl);
 
 	l_main_exit();
 
-	assert(vanished_called);
-
 	return 0;
 }
-- 
2.7.4


             reply	other threads:[~2017-04-12 20:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-12 20:04 Ossama Othman [this message]
2017-04-12 20:09 ` [PATCH v2] unit: Test generic netlink family registration Ossama Othman
2017-04-13 19:18   ` Denis Kenzior

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=1492027457-28188-1-git-send-email-ossama.othman@intel.com \
    --to=ossama.othman@intel.com \
    --cc=ell@lists.01.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.