|
| 1 | +package storage |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +const extraChars = "_.-" |
| 6 | + |
| 7 | +// TestRemoveInvalidChars validates invalid characters |
| 8 | +// are removed from filename strings. |
| 9 | +func TestRemoveInvalidChars(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + input string |
| 12 | + allow string |
| 13 | + want string |
| 14 | + }{ |
| 15 | + {"filename.txt", extraChars, "filename.txt"}, |
| 16 | + {"file@name!.txt", extraChars, "filename.txt"}, |
| 17 | + {"_file-name.txt", extraChars, "_file-name.txt"}, |
| 18 | + {"123-abc_ABC.txt", extraChars, "123-abc_ABC.txt"}, |
| 19 | + {"chars@#$name.txt", extraChars, "charsname.txt"}, |
| 20 | + {"afile", extraChars, "afile"}, |
| 21 | + {".....", extraChars, "....."}, |
| 22 | + {"!@#$%^&()", extraChars, ""}, |
| 23 | + {"", extraChars, ""}, |
| 24 | + } |
| 25 | + for _, test := range tests { |
| 26 | + result := removeInvalidChars(test.input, test.allow) |
| 27 | + if result != test.want { |
| 28 | + t.Fatalf("name: '%s' ('%s' allowed): '%s'; want: '%s'", |
| 29 | + test.input, test.allow, result, test.want) |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// TestTruncateName validates filenames are truncated |
| 35 | +// to the desired length. |
| 36 | +func TestTruncateName(t *testing.T) { |
| 37 | + tests := []struct { |
| 38 | + base string |
| 39 | + ext string |
| 40 | + length int |
| 41 | + want string |
| 42 | + }{ |
| 43 | + {"shrt", ".exceedinglylongext", 10, "shrt.exce"}, |
| 44 | + {"base", ".longextension", 12, "base.long"}, |
| 45 | + {"exactfit", ".jpeg", 13, "exactfit.jpeg"}, |
| 46 | + {"longfilename", ".txt", 10, "longfi.txt"}, |
| 47 | + {"truncatebase", ".png", 8, "trun.png"}, |
| 48 | + {"onlybase", ".toolong", 8, "onl.tool"}, |
| 49 | + {"short", ".dat", 9, "short.dat"}, |
| 50 | + {"example", "", 5, "examp"}, |
| 51 | + {"", ".zip", 5, ".zip"}, |
| 52 | + {"", "", 0, ""}, |
| 53 | + } |
| 54 | + for _, test := range tests { |
| 55 | + result := truncateName(test.base, test.ext, test.length) |
| 56 | + if result != test.want { |
| 57 | + t.Fatalf("base: '%s', ext: '%s' (%d); got: '%s', want: '%s'", |
| 58 | + test.base, test.ext, test.length, result, test.want) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// TestSanitizeName validates filenames do not exceed |
| 64 | +// length nor contain disallowed special characters. |
| 65 | +func TestSanitizeName(t *testing.T) { |
| 66 | + tests := []struct { |
| 67 | + input string |
| 68 | + maxLength int |
| 69 | + extraChars string |
| 70 | + want string |
| 71 | + }{ |
| 72 | + {"my@invalid#name?.txt", 20, extraChars, "myinvalidname.txt"}, |
| 73 | + {"averylongfilename.png", 15, extraChars, "averylongfi.png"}, |
| 74 | + {"my.file.name.txt", 20, extraChars, "my.file.name.txt"}, |
| 75 | + {".hiddenfile", 15, extraChars, defaultName + ".hidd"}, |
| 76 | + {"myfilename.png", 20, extraChars, "myfilename.png"}, |
| 77 | + {"/path/to/file.txt", 20, extraChars, "file.txt"}, |
| 78 | + {"myfilenames", 10, extraChars, "myfilename"}, |
| 79 | + {".@#$%^&*.png", 15, extraChars, "..png"}, |
| 80 | + {"@#$%^&*", 10, extraChars, defaultName}, |
| 81 | + } |
| 82 | + for _, test := range tests { |
| 83 | + result := SanitizeName(test.input, test.maxLength, test.extraChars) |
| 84 | + if result != test.want { |
| 85 | + t.Fatalf("name: '%s', length: %d, special: '%s', got: '%s', want: '%s'", |
| 86 | + test.input, test.maxLength, test.extraChars, result, test.want) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments