demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 39 minutes ago
History
files_test.go · 1.3 KB · 50 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestSafeName(t *testing.T) { |
| 10 | for in, want := range map[string]string{ |
| 11 | "report.pdf": "report.pdf", |
| 12 | "../../etc/passwd": "passwd", |
| 13 | `C:\Users\me\a b.txt`: "a b.txt", |
| 14 | ".bashrc": "bashrc", |
| 15 | "..": "", |
| 16 | ".": "", |
| 17 | "": "", |
| 18 | "/": "", |
| 19 | "a\nb\x00c.txt": "abc.txt", |
| 20 | " photo 1.jpg ": "photo 1.jpg", |
| 21 | "日本語.txt": "日本語.txt", |
| 22 | } { |
| 23 | if got := safeName(in); got != want { |
| 24 | t.Errorf("safeName(%q) = %q, want %q", in, got, want) |
| 25 | } |
| 26 | } |
| 27 | long := safeName(string(make([]rune, 0)) + repeat("é", 150) + ".txt") |
| 28 | if len(long) > 200 || filepath.Ext(long) != ".txt" { |
| 29 | t.Errorf("long name not trimmed well: %d bytes, %q", len(long), filepath.Ext(long)) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func repeat(s string, n int) (r string) { |
| 34 | for i := 0; i < n; i++ { |
| 35 | r += s |
| 36 | } |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | func TestUniquePath(t *testing.T) { |
| 41 | dir := t.TempDir() |
| 42 | if got := uniquePath(dir, "a.txt"); got != filepath.Join(dir, "a.txt") { |
| 43 | t.Fatal(got) |
| 44 | } |
| 45 | os.WriteFile(filepath.Join(dir, "a.txt"), nil, 0o644) |
| 46 | os.WriteFile(filepath.Join(dir, "a (2).txt"), nil, 0o644) |
| 47 | if got := uniquePath(dir, "a.txt"); got != filepath.Join(dir, "a (3).txt") { |
| 48 | t.Fatal(got) |
| 49 | } |
| 50 | } |