[pve-devel] [PATCH v3 pve-common 10/12] test: add tests for path_normalize of PVE::Path

Max Carrara m.carrara at proxmox.com
Thu Jan 9 15:48:16 CET 2025


Add these tests solely to ensure that the behaviour of path_normalize
stays consistent / stable in case we ever decide to provide our own
implementation instead of wrapping File::Spec->canonpath().

Signed-off-by: Max Carrara <m.carrara at proxmox.com>
---
Changes v2 --> v3:
  * None

Changes v1 --> v2:
  * NEW: Split from patch 02

 test/Path/Makefile                |   1 +
 test/Path/path_normalize_tests.pl | 176 ++++++++++++++++++++++++++++++
 2 files changed, 177 insertions(+)
 create mode 100755 test/Path/path_normalize_tests.pl

diff --git a/test/Path/Makefile b/test/Path/Makefile
index 9dcb878..8afed17 100644
--- a/test/Path/Makefile
+++ b/test/Path/Makefile
@@ -4,6 +4,7 @@ TESTS = \
 	path_file_ops_tests.pl					\
 	path_is_absolute_relative_tests.pl			\
 	path_join_tests.pl					\
+	path_normalize_tests.pl					\
 	path_parent_tests.pl					\
 	path_push_tests.pl					\
 
diff --git a/test/Path/path_normalize_tests.pl b/test/Path/path_normalize_tests.pl
new file mode 100755
index 0000000..4d1de24
--- /dev/null
+++ b/test/Path/path_normalize_tests.pl
@@ -0,0 +1,176 @@
+#!/usr/bin/env perl
+
+use lib '../../src';
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use PVE::Path;
+
+# Note: These tests exist solely to ensure that the behaviour of path_normalize
+# stays consistent in case we ever decide to provide our own implementation
+# instead of wrapping File::Spec->canonpath().
+
+my $path_normalize_cases = [
+    {
+	name => "empty path",
+	path => "",
+	normalized => "",
+    },
+    {
+	name => "root",
+	path => "/",
+	normalized => "/",
+    },
+    {
+	name => "current path reference",
+	path => ".",
+	normalized => ".",
+    },
+    {
+	name => "parent directory reference",
+	path => "..",
+	normalized => "..",
+    },
+    {
+	name => "single path component",
+	path => "foo",
+	normalized => "foo",
+    },
+    {
+	name => "single path component, absolute",
+	path => "/foo",
+	normalized => "/foo",
+    },
+    {
+	name => "single path component, starting with current path reference",
+	path => "./foo",
+	normalized => "foo",
+    },
+    {
+	name => "parent directory reference, starting with current path reference",
+	path => "./..",
+	normalized => "..",
+    },
+    {
+	name => "multiple components, with redundant path separators",
+	path => "foo//bar///baz////quo/////qux//////",
+	normalized => "foo/bar/baz/quo/qux",
+    },
+    {
+	name => "multiple components, with redundant current path references",
+	path => "foo/./bar/././baz/./././quo/././././qux/././././.",
+	normalized => "foo/bar/baz/quo/qux",
+    },
+    {
+	name => "multiple components, with parent directory references",
+	path => "foo/../bar/../../baz/../../../quo/../../../../qux/../../../../..",
+	normalized => "foo/../bar/../../baz/../../../quo/../../../../qux/../../../../..",
+    },
+    {
+	name => "root path, with redundant path separators",
+	path => "///////////",
+	normalized => "/",
+    },
+    {
+	name => "root path, with redundant current path references",
+	path => "/./././././././././.",
+	normalized => "/",
+    },
+    {
+	name => "root with parent directory reference",
+	path => "/..",
+	normalized => "/",
+    },
+    {
+	name => "root with multiple parent directory references",
+	path => "/../../../../../../../../..",
+	normalized => "/",
+    },
+    {
+	name => "current path reference, with redundant path separators",
+	path => ".///////////",
+	normalized => ".",
+    },
+    {
+	name => "current path reference, with redundant current path references",
+	path => "./././././././././.",
+	normalized => ".",
+    },
+    {
+	name => "current path reference, with parent directory references",
+	path => "./../../..",
+	normalized => "../../..",
+    },
+    {
+	name => "multiple components,"
+	    . " with redundant path separators,"
+	    . " with redundant current path references,"
+	    . " with parent directory references",
+	path => "foo//././//bar///./././//.//baz/.././..///quo/..////../qux/././//..",
+	normalized => "foo/bar/baz/../../quo/../../qux/..",
+    },
+    {
+	name => "multiple components, with odd component names",
+	path => ".../  \t/\t/\\/........../.~.^./.+\$={}[]()<>.../!/\"/'",
+	normalized => ".../  \t/\t/\\/........../.~.^./.+\$={}[]()<>.../!/\"/'",
+    },
+    # Diverging from File::Spec->canonpath() here -- canonpath() doesn't throw
+    # if it gets undef, but path_normalize() does in order to stay consistent
+    # with all the other functions of PVE::Path.
+    {
+	name => "path is undef",
+	path => undef,
+	normalized => undef,
+	should_throw => 1,
+    },
+];
+
+sub test_path_normalize : prototype($) {
+    my ($case) = @_;
+
+    my $name = "path_normalize: " . $case->{name};
+
+    my $normalized = eval { PVE::Path::path_normalize($case->{path}); };
+
+    if ($@) {
+	if ($case->{should_throw}) {
+	    pass($name);
+	    return;
+	}
+
+	fail($name);
+	diag("Failed to normalize path:\n$@");
+	return;
+    }
+
+    # Note: `!is()` isn't the same as `isnt()` -- we want extra output here
+    # if the check fails; can't do that with `isnt()`
+    if (!is($normalized, $case->{normalized}, $name)) {
+	diag("path       = " . $case->{path});
+	diag("normalized = " . $case->{normalized});
+    }
+
+    return;
+}
+
+sub main : prototype() {
+    plan(tests => scalar($path_normalize_cases->@*));
+
+    for my $case ($path_normalize_cases->@*) {
+	eval {
+	    # suppress warnings here to make output less noisy for certain tests if necessary
+	    # local $SIG{__WARN__} = sub {};
+	    test_path_normalize($case);
+	};
+	warn "$@\n" if $@;
+    }
+
+    done_testing();
+
+    return;
+}
+
+main();
-- 
2.39.5





More information about the pve-devel mailing list