All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] unit: Test generic netlink family registration
@ 2017-04-12 20:04 Ossama Othman
  2017-04-12 20:09 ` [PATCH v2] " Ossama Othman
  0 siblings, 1 reply; 3+ messages in thread
From: Ossama Othman @ 2017-04-12 20:04 UTC (permalink / raw)
  To: ell

[-- 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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v2] unit: Test generic netlink family registration
  2017-04-12 20:04 [PATCH] unit: Test generic netlink family registration Ossama Othman
@ 2017-04-12 20:09 ` Ossama Othman
  2017-04-13 19:18   ` Denis Kenzior
  0 siblings, 1 reply; 3+ messages in thread
From: Ossama Othman @ 2017-04-12 20:09 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 4864 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..7faaa2d 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 indefinitely 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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] unit: Test generic netlink family registration
  2017-04-12 20:09 ` [PATCH v2] " Ossama Othman
@ 2017-04-13 19:18   ` Denis Kenzior
  0 siblings, 0 replies; 3+ messages in thread
From: Denis Kenzior @ 2017-04-13 19:18 UTC (permalink / raw)
  To: ell

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

Hi Ossama,

On 04/12/2017 03:09 PM, Ossama Othman wrote:
> 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(-)
>

Applied, thanks.

Regards,
-Denis


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-04-13 19:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-12 20:04 [PATCH] unit: Test generic netlink family registration Ossama Othman
2017-04-12 20:09 ` [PATCH v2] " Ossama Othman
2017-04-13 19:18   ` Denis Kenzior

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.