All of lore.kernel.org
 help / color / mirror / Atom feed
From: mwilck@suse.com
To: Christophe Varoqui <christophe.varoqui@opensvc.com>,
	Benjamin Marzinski <bmarzins@redhat.com>
Cc: dm-devel@redhat.com, Martin Wilck <mwilck@suse.com>
Subject: [PATCH 06/35] multipath-tools tests: add test for devt2devname
Date: Thu,  9 Jul 2020 12:15:51 +0200	[thread overview]
Message-ID: <20200709101620.6786-7-mwilck@suse.com> (raw)
In-Reply-To: <20200709101620.6786-1-mwilck@suse.com>

From: Martin Wilck <mwilck@suse.com>

Two tests skipped because they fail, will be fixed with the following
patch.

Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 tests/Makefile |   3 +-
 tests/devt.c   | 194 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 196 insertions(+), 1 deletion(-)
 create mode 100644 tests/devt.c

diff --git a/tests/Makefile b/tests/Makefile
index 125553b..5f00a3a 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -13,7 +13,7 @@ CFLAGS += $(BIN_CFLAGS) -I$(multipathdir) -I$(mpathcmddir) \
 LIBDEPS += -L$(multipathdir) -L$(mpathcmddir) -lmultipath -lmpathcmd -lcmocka
 
 TESTS := uevent parser util dmevents hwtable blacklist unaligned vpd pgpolicy \
-	 alias directio valid
+	 alias directio valid devt
 
 .SILENT: $(TESTS:%=%.o)
 .PRECIOUS: $(TESTS:%=%-test)
@@ -52,6 +52,7 @@ alias-test_TESTDEPS := test-log.o
 alias-test_LIBDEPS := -lpthread -ldl
 valid-test_OBJDEPS := ../libmultipath/valid.o
 valid-test_LIBDEPS := -ludev -lpthread -ldl
+devt-test_LIBDEPS := -ludev
 ifneq ($(DIO_TEST_DEV),)
 directio-test_LIBDEPS := -laio
 endif
diff --git a/tests/devt.c b/tests/devt.c
new file mode 100644
index 0000000..4be6d75
--- /dev/null
+++ b/tests/devt.c
@@ -0,0 +1,194 @@
+/*
+ * Copyright (c) 2020 Martin Wilck, SUSE
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <stdbool.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdlib.h>
+#include <cmocka.h>
+#include <libudev.h>
+#include <sys/sysmacros.h>
+#include "util.h"
+#include "debug.h"
+
+#include "globals.c"
+
+static int get_one_devt(char *devt, size_t len)
+{
+	struct udev_enumerate *enm;
+	int r, ret = -1;
+	struct udev_list_entry *first;
+	struct udev_device *u_dev;
+	const char *path;
+	dev_t devnum;
+
+	enm = udev_enumerate_new(udev);
+	if (!enm)
+		return -1;
+	r = udev_enumerate_add_match_subsystem(enm, "block");
+	r = udev_enumerate_scan_devices(enm);
+	if (r < 0)
+		goto out;
+	first = udev_enumerate_get_list_entry(enm);
+	if (!first)
+		goto out;
+	path = udev_list_entry_get_name(first);
+	u_dev = udev_device_new_from_syspath(udev, path);
+	if (!u_dev)
+		goto out;
+	devnum = udev_device_get_devnum(u_dev);
+	snprintf(devt, len, "%d:%d",
+		 major(devnum), minor(devnum));
+	udev_device_unref(u_dev);
+	condlog(3, "found block device: %s", devt);
+	ret = 0;
+out:
+	udev_enumerate_unref(enm);
+	return ret;
+}
+
+int setup(void **state)
+{
+	static char dev_t[BLK_DEV_SIZE];
+
+	udev = udev_new();
+	if (udev == NULL)
+		return -1;
+	*state = dev_t;
+	return get_one_devt(dev_t, sizeof(dev_t));
+}
+
+int teardown(void **state)
+{
+	udev_unref(udev);
+	return 0;
+}
+
+static void test_devt2devname_devt_good(void **state)
+{
+	char dummy[BLK_DEV_SIZE];
+
+	assert_int_equal(devt2devname(dummy, sizeof(dummy), *state), 0);
+}
+
+static void test_devt2devname_devname_null(void **state)
+{
+	assert_int_equal(devt2devname(NULL, 0, ""), 1);
+}
+
+/* buffer length 0 */
+static void test_devt2devname_length_0(void **state)
+{
+	char dummy[] = "";
+
+	assert_int_equal(devt2devname(dummy, 0, ""), 1);
+}
+
+/* buffer too small */
+static void test_devt2devname_length_1(void **state)
+{
+	char dummy[] = "";
+
+	skip();
+	assert_int_equal(devt2devname(dummy, sizeof(dummy), *state), 1);
+}
+
+static void test_devt2devname_devt_null(void **state)
+{
+	char dummy[32];
+
+	skip();
+	assert_int_equal(devt2devname(dummy, sizeof(dummy), NULL), 1);
+}
+
+static void test_devt2devname_devt_empty(void **state)
+{
+	char dummy[32];
+
+	assert_int_equal(devt2devname(dummy, sizeof(dummy), ""), 1);
+}
+
+static void test_devt2devname_devt_invalid_1(void **state)
+{
+	char dummy[32];
+
+	assert_int_equal(devt2devname(dummy, sizeof(dummy), "foo"), 1);
+}
+
+static void test_devt2devname_devt_invalid_2(void **state)
+{
+	char dummy[32];
+
+	assert_int_equal(devt2devname(dummy, sizeof(dummy), "1234"), 1);
+}
+
+static void test_devt2devname_devt_invalid_3(void **state)
+{
+	char dummy[32];
+
+	assert_int_equal(devt2devname(dummy, sizeof(dummy), "0:0"), 1);
+}
+
+static void test_devt2devname_real(void **state)
+{
+	struct udev_enumerate *enm;
+	int r;
+	struct udev_list_entry *first, *item;
+	unsigned int i = 0;
+
+	enm = udev_enumerate_new(udev);
+	assert_non_null(enm);
+	r = udev_enumerate_add_match_subsystem(enm, "block");
+	assert_in_range(r, 0, INT_MAX);
+	r = udev_enumerate_scan_devices(enm);
+	first = udev_enumerate_get_list_entry(enm);
+	udev_list_entry_foreach(item, first) {
+		const char *path = udev_list_entry_get_name(item);
+		struct udev_device *u_dev;
+		dev_t devnum;
+		char devt[BLK_DEV_SIZE];
+		char devname[FILE_NAME_SIZE];
+
+		u_dev = udev_device_new_from_syspath(udev, path);
+		assert_non_null(u_dev);
+		devnum = udev_device_get_devnum(u_dev);
+		snprintf(devt, sizeof(devt), "%d:%d",
+			 major(devnum), minor(devnum));
+		r = devt2devname(devname, sizeof(devname), devt);
+		assert_int_equal(r, 0);
+		assert_string_equal(devname, udev_device_get_sysname(u_dev));
+		i++;
+		udev_device_unref(u_dev);
+	}
+	udev_enumerate_unref(enm);
+	condlog(2, "devt2devname test passed for %u block devices", i);
+}
+
+static int devt2devname_tests(void)
+{
+	const struct CMUnitTest tests[] = {
+		cmocka_unit_test(test_devt2devname_devt_good),
+		cmocka_unit_test(test_devt2devname_devname_null),
+		cmocka_unit_test(test_devt2devname_length_0),
+		cmocka_unit_test(test_devt2devname_length_1),
+		cmocka_unit_test(test_devt2devname_devt_null),
+		cmocka_unit_test(test_devt2devname_devt_empty),
+		cmocka_unit_test(test_devt2devname_devt_invalid_1),
+		cmocka_unit_test(test_devt2devname_devt_invalid_2),
+		cmocka_unit_test(test_devt2devname_devt_invalid_3),
+		cmocka_unit_test(test_devt2devname_real),
+	};
+
+	return cmocka_run_group_tests(tests, setup, teardown);
+}
+
+int main(void)
+{
+	int ret = 0;
+
+	ret += devt2devname_tests();
+	return ret;
+}
-- 
2.26.2

  parent reply	other threads:[~2020-07-09 10:15 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-09 10:15 [PATCH 00/35] multipath-tools series part I: minor changes mwilck
2020-07-09 10:15 ` [PATCH 01/35] multipath-tools tests/util: separate group for bitmask tests mwilck
2020-07-09 10:15 ` [PATCH 02/35] multipath-tools tests/directio: fix missing initializers mwilck
2020-07-09 10:15 ` [PATCH 03/35] tests: __wrap_dlog: use check_expected() mwilck
2020-07-09 10:15 ` [PATCH 04/35] multipath tools tests: add strchop() test mwilck
2020-07-09 10:15 ` [PATCH 05/35] libmultipath: improve strchop() mwilck
2020-07-09 10:15 ` mwilck [this message]
2020-07-09 10:15 ` [PATCH 07/35] libmultipath: devt2devname(): simplify using libudev mwilck
2020-07-09 10:15 ` [PATCH 08/35] libmultipath: create bitfield abstraction mwilck
2020-07-16 21:17   ` Benjamin Marzinski
2020-08-04 15:04     ` Martin Wilck
2020-08-04 15:18       ` Martin Wilck
2020-08-04 16:26         ` Benjamin Marzinski
2020-08-04 19:35           ` Martin Wilck
2020-08-10 18:05             ` Benjamin Marzinski
2020-08-10 18:59               ` Martin Wilck
2020-07-09 10:15 ` [PATCH 09/35] libmultipath: use bitfields in group_by_match() mwilck
2020-07-09 10:15 ` [PATCH 10/35] libmultipath: util: constify function arguments mwilck
2020-07-09 10:15 ` [PATCH 11/35] multipath-tools tests: add unit tests for strlcat mwilck
2020-07-09 10:15 ` [PATCH 12/35] libmultipath: strlcpy()/strlcat(): use restrict qualifier mwilck
2020-07-16 22:18   ` Benjamin Marzinski
2020-08-04 15:36     ` Martin Wilck
2020-08-04 17:29       ` Benjamin Marzinski
2020-08-04 20:15         ` Martin Wilck
2020-07-09 10:15 ` [PATCH 13/35] libmultipath: constify blacklist code mwilck
2020-07-09 10:15 ` [PATCH 14/35] libmultipath: rlookup_binding(): remove newline in log message mwilck
2020-07-09 10:16 ` [PATCH 15/35] libmultipath: fix missing initializer warning from clang 3.9 mwilck
2020-07-09 10:16 ` [PATCH 16/35] libmultipath: fix gcc -Wstringop-overflow warning mwilck
2020-07-09 10:16 ` [PATCH 17/35] libmultipath: remove uevent listener failback mwilck
2020-07-09 10:16 ` [PATCH 18/35] libmultipath: uevent: use static linkage where possible mwilck
2020-07-09 10:16 ` [PATCH 19/35] libmultipath: uevent: inline trivial functions mwilck
2020-07-09 10:16 ` [PATCH 20/35] libmultipath: decrease log level of "SCSI target" log message mwilck
2020-07-09 10:16 ` [PATCH 21/35] libmultipath: get_udev_uid(): more appropriate error code mwilck
2020-07-09 10:16 ` [PATCH 22/35] libmultipath: get_uid(): improve log message on udev failure mwilck
2020-07-09 10:16 ` [PATCH 23/35] libmultipath: make sysfs_pathinfo() static and use const mwilck
2020-07-09 10:16 ` [PATCH 24/35] libmultipath: pathinfo(): improve a log message mwilck
2020-07-09 10:16 ` [PATCH 25/35] libmultipath: pathinfo(): don't filter emtpy devnode names mwilck
2020-07-09 10:16 ` [PATCH 26/35] libmultipath: io_err_stat_handle_pathfail(): less error conditions mwilck
2020-07-09 10:16 ` [PATCH 27/35] libmultipath: improve libdm logging mwilck
2020-07-09 10:16 ` [PATCH 28/35] libmultipath: snprint_devices(): use udev_enumerate mwilck
2020-07-09 10:16 ` [PATCH 29/35] libmultipath: snprint_devices(): print hidden/foreign status mwilck
2020-07-09 10:16 ` [PATCH 30/35] libmultipath: alloc_path(): initialize pp->initialized mwilck
2020-07-09 10:16 ` [PATCH 31/35] libmultipath: alloc_path_with_pathinfo(): treat devname overflow as error mwilck
2020-07-09 10:16 ` [PATCH 32/35] libmultipath: log table params in addmap() mwilck
2020-07-09 10:16 ` [PATCH 33/35] multipathd: remove set_multipath_wwid() mwilck
2020-07-09 10:16 ` [PATCH 34/35] kpartx: print an error message if removing a partition fails mwilck
2020-07-09 10:16 ` [PATCH 35/35] kpartx: add missing newline mwilck
2020-07-20 21:09 ` [PATCH 00/35] multipath-tools series part I: minor changes Benjamin Marzinski

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=20200709101620.6786-7-mwilck@suse.com \
    --to=mwilck@suse.com \
    --cc=bmarzins@redhat.com \
    --cc=christophe.varoqui@opensvc.com \
    --cc=dm-devel@redhat.com \
    /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.