]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3: VFS: Add cmocka tests for pathname parsing in vfs_widelinks.
authorJeremy Allison <jra@samba.org>
Mon, 6 Apr 2020 19:18:50 +0000 (12:18 -0700)
committerRalph Boehme <slow@samba.org>
Thu, 9 Apr 2020 19:40:35 +0000 (19:40 +0000)
Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/modules/test_vfs_widelinks.c [new file with mode: 0644]
source3/modules/wscript_build
source3/selftest/tests.py

diff --git a/source3/modules/test_vfs_widelinks.c b/source3/modules/test_vfs_widelinks.c
new file mode 100644 (file)
index 0000000..c6055c8
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ *  Unix SMB/CIFS implementation.
+ *
+ *  Unit test for widelinks path validator.
+ *
+ *  Copyright (C) Jeremy Allison 2020
+ *
+ *  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/>.
+ */
+
+/* Needed for static build to complete... */
+#include "includes.h"
+#include "smbd/smbd.h"
+NTSTATUS vfs_widelinks_init(TALLOC_CTX *ctx);
+
+#include "vfs_widelinks.c"
+#include <cmocka.h>
+
+struct str_test_values {
+       const char *src_str;
+       const char *dst_str;
+} ;
+
+/* As many nasty edge cases as I can think of.. */
+
+static struct str_test_values examples[] = {
+       { "/", "/" },
+       { "/../../", "/" },
+       { "/foo/../", "/" },
+       { "/./././", "/" },
+       { "/./././.", "/" },
+       { "/.../././.", "/..." },
+       { "/./././.foo", "/.foo" },
+       { "/./././.foo.", "/.foo." },
+       { "/./././foo.", "/foo." },
+       { "/foo/bar/..", "/foo" },
+       { "/foo/bar/../baz/", "/foo/baz" },
+       { "////////////////", "/" },
+       { "/////////./././././.", "/" },
+       { "/./.././../.boo/../baz", "/baz" },
+       { "/a/component/path", "/a/component/path" },
+       { "/a/component/path/", "/a/component/path" },
+       { "/a/component/path/..", "/a/component" },
+       { "/a/component/../path/", "/a/path" },
+       { "///a/./././///component/../////path/", "/a/path" }
+};
+
+/*
+ * Test our realpath resolution code.
+ */
+static void test_resolve_realpath_name(void **state)
+{
+       unsigned i;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       for (i = 0; i < ARRAY_SIZE(examples); i++) {
+               char *test_dst = resolve_realpath_name(frame,
+                                       examples[i].src_str);
+               if (test_dst == NULL) {
+                       fail();
+               }
+               assert_string_equal(test_dst, examples[i].dst_str);
+               TALLOC_FREE(test_dst);
+       }
+       TALLOC_FREE(frame);
+}
+
+int main(int argc, char **argv)
+{
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_resolve_realpath_name),
+       };
+
+       cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
+
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}
index 57a1bfb59feab064e15fe557de6907cbbf5323fe..2d81357c513eb1d701fbd724446ae5b46fade3a2 100644 (file)
@@ -623,3 +623,8 @@ bld.SAMBA3_MODULE('vfs_widelinks',
                  init_function='',
                  internal_module=bld.SAMBA3_IS_STATIC_MODULE('vfs_widelinks'),
                  enabled=bld.SAMBA3_IS_ENABLED_MODULE('vfs_widelinks'))
+
+bld.SAMBA3_BINARY('test_vfs_widelinks',
+                  source='test_vfs_widelinks.c',
+                  deps='smbd_base cmocka',
+                  for_selftest=True)
index 50e77ef805581f6876e4a5e11ac5212acf9d0cc7..1ceb0a5ad5f9427149c156f2d8c22da79eab27ae 100755 (executable)
@@ -502,6 +502,10 @@ plantestsuite("samba3.test_nfs4_acl", "none",
               [os.path.join(bindir(), "test_nfs4_acls"),
                "$SMB_CONF_PATH"])
 
+plantestsuite("samba3.test_vfs_widelinks", "none",
+              [os.path.join(bindir(), "test_vfs_widelinks"),
+               "$SMB_CONF_PATH"])
+
 plantestsuite(
     "samba3.resolvconf", "none",
     [os.path.join(samba3srcdir, "script/tests/test_resolvconf.sh")])