mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 01:10:54 +08:00
uniqueUploadPath treated every Stat error except "not exist" as "try the next suffix". A name the filesystem rejects never becomes "not exist", so the handler looped. The search now returns that error, and it also stops after 10000 existing suffixes.
34 lines
777 B
Go
34 lines
777 B
Go
package server
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestUniqueUploadPathSuffixesCollisions(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte("a"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "note-1.txt"), []byte("b"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := uniqueUploadPath(dir, "note.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if filepath.Base(got) != "note-2.txt" {
|
|
t.Fatalf("got %s", got)
|
|
}
|
|
}
|
|
|
|
func TestUniqueUploadPathStopsOnUnstorableName(t *testing.T) {
|
|
dir := t.TempDir()
|
|
name := strings.Repeat("a", 300) + ".txt"
|
|
if _, err := uniqueUploadPath(dir, name); err == nil {
|
|
t.Fatal("an overlong file name must not be searched forever")
|
|
}
|
|
}
|