[PATCH] torture: add basic ms_fnmatch unit test
Andreas Schneider
asn at samba.org
Thu Mar 1 13:36:20 UTC 2018
On Thursday, 1 March 2018 13:34:57 CET David Disseldorp wrote:
> Hi,
>
> I wrote this while trying to understand how ms_fnmatch_core() uses its
> max_n parameter. Feel free to push if you think it'll be helpful to
> others.
What do you think about the attached patchset?
Andreas
--
Andreas Schneider GPG-ID: CC014E3D
Samba Team asn at samba.org
www.samba.org
-------------- next part --------------
>From 12a271151c9af238007e249814a7d52f38f4cd84 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Thu, 26 Oct 2017 09:47:57 +0200
Subject: [PATCH 1/2] util: Fix the logic in ms_fnmatch_protocol()
Make sure we always pass a valid max_n pointer to ms_fnmatch_core().
Signed-off-by: Andreas Schneider <asn at samba.org>
Reviewed-by: David Disseldorp <ddiss at samba.org>
---
lib/util/ms_fnmatch.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/lib/util/ms_fnmatch.c b/lib/util/ms_fnmatch.c
index c0f61ab04e7..636ac399f66 100644
--- a/lib/util/ms_fnmatch.c
+++ b/lib/util/ms_fnmatch.c
@@ -164,7 +164,8 @@ static int ms_fnmatch_core(const char *p, const char *n,
int ms_fnmatch_protocol(const char *pattern, const char *string, int protocol,
bool is_case_sensitive)
{
- int ret, count, i;
+ int ret = -1;
+ size_t count, i;
if (strcmp(string, "..") == 0) {
string = ".";
@@ -209,13 +210,17 @@ int ms_fnmatch_protocol(const char *pattern, const char *string, int protocol,
if (pattern[i] == '*' || pattern[i] == '<') count++;
}
- {
+ /* If the pattern includes '*' or '<' */
+ if (count >= 1) {
struct max_n max_n[count];
memset(max_n, 0, sizeof(struct max_n) * count);
ret = ms_fnmatch_core(pattern, string, max_n, strrchr(string, '.'),
is_case_sensitive);
+ } else {
+ ret = ms_fnmatch_core(pattern, string, NULL, strrchr(string, '.'),
+ is_case_sensitive);
}
return ret;
--
2.16.2
>From 416312f87d5d9acab17eb2410c3d3b7fc5fc99b3 Mon Sep 17 00:00:00 2001
From: David Disseldorp <ddiss at samba.org>
Date: Tue, 20 Feb 2018 11:08:47 +0100
Subject: [PATCH 2/2] tests: Add basic ms_fnmatch unit test
Pair-Programmed-With: Andreas Schneider <asn at samba.org>
Signed-off-by: David Disseldorp <ddiss at samba.org>
Signed-off-by: Andreas Schneider <asn at samba.org>
---
lib/util/tests/test_ms_fnmatch.c | 112 +++++++++++++++++++++++++++++++++++++++
lib/util/wscript_build | 5 ++
selftest/tests.py | 2 +
3 files changed, 119 insertions(+)
create mode 100644 lib/util/tests/test_ms_fnmatch.c
diff --git a/lib/util/tests/test_ms_fnmatch.c b/lib/util/tests/test_ms_fnmatch.c
new file mode 100644
index 00000000000..0c4d5bf2740
--- /dev/null
+++ b/lib/util/tests/test_ms_fnmatch.c
@@ -0,0 +1,112 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * Test suite for ldap client
+ *
+ * Copyright (C) 2018 Andreas Schneider <asn at samba.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <errno.h>
+
+#include "lib/util/ms_fnmatch.c"
+
+static void test_ms_fn_match_protocol_no_wildcard(void **state)
+{
+ int cmp;
+
+ /* no wildcards in pattern, a simple strcasecmp_m */
+ cmp = ms_fnmatch_protocol("pattern", "string", PROTOCOL_COREPLUS,
+ true); /* case sensitive */
+ assert_int_equal(cmp, -3);
+}
+
+static void test_ms_fn_match_protocol_pattern_upgraded(void **state)
+{
+ int cmp;
+
+ /* protocol < PROTOCOL_NT1 pattern is "upgraded" */
+ cmp = ms_fnmatch_protocol("??????", "string", PROTOCOL_COREPLUS,
+ false);
+ assert_int_equal(cmp, 0);
+}
+
+static void test_ms_fn_match_protocol_match_zero_or_more(void **state)
+{
+ int cmp;
+
+ /* '*' matches zero or more characters. handled via recursive calls */
+ cmp = ms_fnmatch_protocol("********", "string", PROTOCOL_COREPLUS,
+ true);
+ assert_int_equal(cmp, 0);
+}
+
+static void test_ms_fn_match_protocol_mapped_char(void **state)
+{
+ int cmp;
+
+ /* '?' is mapped to '>', which matches any char or a '\0' */
+ cmp = ms_fnmatch_protocol("???????", "string", PROTOCOL_COREPLUS,
+ false);
+ assert_int_equal(cmp, 0);
+}
+
+static void test_ms_fn_match_protocol_nt1_any_char(void **state)
+{
+ int cmp;
+
+ /* PROTOCOL_NT1 '?' matches any char, '\0' is not included */
+ cmp = ms_fnmatch_protocol("???????", "string", PROTOCOL_NT1,
+ false);
+ assert_int_equal(cmp, -1);
+}
+
+static void test_ms_fn_match_protocol_nt1_case_sensitive(void **state)
+{
+ int cmp;
+
+ cmp = ms_fnmatch_protocol("StRinG", "string", PROTOCOL_NT1,
+ true); /* case sensitive */
+ assert_int_equal(cmp, 0);
+
+ cmp = ms_fnmatch_protocol("StRin?", "string", PROTOCOL_NT1,
+ true); /* case sensitive */
+ assert_int_equal(cmp, -1);
+
+ cmp = ms_fnmatch_protocol("StRin?", "string", PROTOCOL_NT1,
+ false);
+ assert_int_equal(cmp, 0);
+ cmp = ms_fnmatch_protocol("strin?", "string", PROTOCOL_NT1,
+ true); /* case sensitive */
+ assert_int_equal(cmp, 0);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_ms_fn_match_protocol_no_wildcard),
+ cmocka_unit_test(test_ms_fn_match_protocol_pattern_upgraded),
+ cmocka_unit_test(test_ms_fn_match_protocol_match_zero_or_more),
+ cmocka_unit_test(test_ms_fn_match_protocol_mapped_char),
+ cmocka_unit_test(test_ms_fn_match_protocol_nt1_any_char),
+ cmocka_unit_test(test_ms_fn_match_protocol_nt1_case_sensitive),
+ };
+
+ cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/lib/util/wscript_build b/lib/util/wscript_build
index d49d4a2fc68..630848da759 100644
--- a/lib/util/wscript_build
+++ b/lib/util/wscript_build
@@ -222,3 +222,8 @@ else:
deps='cmocka replace samba-util',
local_include=False,
install=False)
+
+ bld.SAMBA_BINARY('test_ms_fnmatch',
+ source='tests/test_ms_fnmatch.c',
+ deps='samba-util cmocka',
+ install=False)
diff --git a/selftest/tests.py b/selftest/tests.py
index 1c6921af0ca..e69bc31b638 100644
--- a/selftest/tests.py
+++ b/selftest/tests.py
@@ -184,3 +184,5 @@ plantestsuite("samba.unittests.tldap", "none",
[os.path.join(bindir(), "default/source3/test_tldap")])
plantestsuite("samba.unittests.rfc1738", "none",
[os.path.join(bindir(), "default/lib/util/test_rfc1738")])
+plantestsuite("samba.unittests.ms_fnmatch", "none",
+ [os.path.join(bindir(), "default/lib/util/test_ms_fnmatch")])
--
2.16.2
More information about the samba-technical
mailing list