1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// 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 <https://www.gnu.org/licenses/>.

#[cfg(test)]
mod test_string_resolver {
    use s_macro::s;

    use crate::plugins::dfs::handler::aux_resolve_path;

    #[test]
    fn test_ok_not_absolute() {
        let root = std::path::PathBuf::from("/");
        let path = s!("a/b/c");
        let res = aux_resolve_path(&path, root);
        assert!(res.is_ok(), "res: {res:?}");
    }

    #[test]
    fn test_rel_ok_dot() {
        let root = std::path::PathBuf::from("/");
        let path = s!("./a/b/c");
        let res = aux_resolve_path(&path, root);
        assert!(res.is_ok(), "res: {res:?}");
    }

    #[test]
    fn test_ok_starts_with_dotdot() {
        let root = std::path::PathBuf::from("/");
        let path = s!("../a/b/c");
        let res = aux_resolve_path(&path, root);
        assert!(res.is_ok(), "res: {res:?}");
    }

    #[test]
    fn test_abs_ok_dot() {
        let root = std::path::PathBuf::from("/");
        let path = s!("/./a/b/c");
        let res = aux_resolve_path(&path, root).unwrap();
        assert!(res == "/a/b/c", "res: {res:?}");
    }
    #[test]
    fn test_abs_ok_dotdot() {
        let root = std::path::PathBuf::from("/");
        let path = s!("/../a/b/c");
        let res = aux_resolve_path(&path, root);
        assert!(res.is_ok(), "res: {res:?}");
    }

    #[test]
    fn test_abs_weird_above_root() {
        let root = std::path::PathBuf::from("/");
        let path = s!("/../../..//..///.../.../../a/b.c/");
        let res = aux_resolve_path(&path, root);
        assert!(res.is_ok(), "res: {res:?}");
    }

    #[test]
    fn test_root_ok_mult_redirect() {
        let root = std::path::PathBuf::from("/");
        let path = s!("/a/../a/../a/../a/../a/../a/../a");
        let res = aux_resolve_path(&path, root).unwrap();
        assert!(res == "/a", "res: {res:?}");
    }

    #[test]
    fn test_root_ok_up_before_root() {
        let root = std::path::PathBuf::from("/");
        let path = s!("/../a/b/c");
        let res = aux_resolve_path(&path, root);
        assert!(res.is_ok(), "res: {res:?}");
    }

    #[test]
    fn test_plain_doubledot() {
        let root = std::path::PathBuf::from("/");
        let path = s!("..");
        let res = aux_resolve_path(&path, root);
        assert!(res.is_ok(), "res: {res:?}");
        assert!(!res.unwrap().is_empty());
    }

    #[test]
    fn test_root_ok_dotdotnames() {
        let root = std::path::PathBuf::from("/");
        let path = s!("/a/..c");
        let res = aux_resolve_path(&path, root).unwrap();
        assert!(res == "/a/..c", "res: {res:?}");
    }

    #[test]
    fn test_root_ok_dotnames() {
        let root = std::path::PathBuf::from("/");
        let path = s!("/a/../.c");
        let res = aux_resolve_path(&path, root).unwrap();
        assert!(res == "/.c", "res: {res:?}");
    }
}

#[cfg(test)]
mod test_tilda {
    use s_macro::s;

    use crate::plugins::dfs::handler::aux_resolve_local;

    #[test]
    fn test_ok_tilda() {
        let root = std::path::PathBuf::from("/");
        let path = s!("~/a/b/c");
        let res = aux_resolve_local(&path, root);
        assert!(res.is_ok(), "res: {res:?}");
    }

    #[test]
    fn test_nook_tilda() {
        let root = std::path::PathBuf::from("/");
        let path = s!("/~/a/b/c");
        let res = aux_resolve_local(&path, root);
        assert!(res.is_err(), "res: {res:?}");
    }

    #[test]
    fn test_ok_multiple_tildas() {
        let root = std::path::PathBuf::from("/");
        let path = s!("~/a/b/c/~/d/e/f");
        let res = aux_resolve_local(&path, root);
        assert!(res.is_err(), "res: {res:?}");
    }
}