|
| 1 | +// SiYuan - From thought to insight, with agents |
| 2 | +// Copyright (c) 2020-present, b3log.org |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +// TestSensitivePathAliases 验证工作空间及家目录别名使用一致的敏感路径规则。 |
| 26 | +func TestSensitivePathAliases(t *testing.T) { |
| 27 | + realHome, err := filepath.EvalSymlinks(t.TempDir()) |
| 28 | + if err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + alias := filepath.Join(t.TempDir(), "home") |
| 32 | + origHome, origWorkspace := HomeDir, WorkspaceDir |
| 33 | + t.Cleanup(func() { HomeDir, WorkspaceDir = origHome, origWorkspace }) |
| 34 | + if err := os.MkdirAll(filepath.Join(realHome, "home"), 0755); err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + for _, layout := range []string{"SiYuan", ".config/SiYuan", "siyuan"} { |
| 38 | + for _, rel := range []string{"data/emojis/robot.svg", "conf/conf.json", "temp/private.txt", "temp/export/doc.html"} { |
| 39 | + p := filepath.Join(realHome, filepath.FromSlash(layout), filepath.FromSlash(rel)) |
| 40 | + if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + if err := os.WriteFile(p, []byte("fixture"), 0600); err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + if err := os.Symlink(realHome, alias); err != nil { |
| 49 | + t.Skipf("create directory symlink failed: %s", err) |
| 50 | + } |
| 51 | + for _, layout := range []string{"SiYuan", ".config/SiYuan", "siyuan"} { |
| 52 | + t.Run(layout, func(t *testing.T) { |
| 53 | + HomeDir = alias |
| 54 | + if layout == "siyuan" { |
| 55 | + HomeDir = filepath.Join(alias, "home") |
| 56 | + } |
| 57 | + WorkspaceDir = filepath.Join(alias, filepath.FromSlash(layout)) |
| 58 | + resolved, err := filepath.EvalSymlinks(WorkspaceDir) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("resolve %q: %v", WorkspaceDir, err) |
| 61 | + } |
| 62 | + // 新导出目标尚不存在时也应保留相同的目录访问规则。 |
| 63 | + for _, rel := range []string{"data/emojis/robot.svg", "conf/conf.json", "temp/private.txt", "temp/export/doc.html", "conf/new.json", "temp/export/new.html"} { |
| 64 | + want := layout == ".config/SiYuan" || rel == "conf/conf.json" || rel == "conf/new.json" || rel == "temp/private.txt" |
| 65 | + for _, home := range []string{alias, realHome} { |
| 66 | + HomeDir = home |
| 67 | + if layout == "siyuan" { |
| 68 | + HomeDir = filepath.Join(home, "home") |
| 69 | + } |
| 70 | + for _, root := range []string{WorkspaceDir, resolved} { |
| 71 | + p := filepath.Join(root, filepath.FromSlash(rel)) |
| 72 | + if got := IsSensitivePath(p); got != want { |
| 73 | + t.Errorf("IsSensitivePath(%q) = %v, want %v (home %q)", p, got, want, home) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
0 commit comments