package service import ( "regexp" "testing" ) func TestRandomHostNameIsOpaqueAndDNSCompatible(t *testing.T) { pattern := regexp.MustCompile(`^sv[0-9a-f]{10}$`) seen := make(map[string]bool, 128) for range 128 { name, err := randomHostName() if err != nil { t.Fatal(err) } if !pattern.MatchString(name) { t.Fatalf("unexpected host name %q", name) } if seen[name] { t.Fatalf("duplicate host name %q", name) } seen[name] = true } } func TestRandomPanelPasswordMeetsAaPanelContract(t *testing.T) { pattern := regexp.MustCompile(`^[A-Za-z0-9]{20}$`) seen := make(map[string]bool, 128) for range 128 { password, err := randomPanelPassword() if err != nil { t.Fatal(err) } if !pattern.MatchString(password) { t.Fatalf("unexpected panel password %q", password) } if seen[password] { t.Fatalf("duplicate panel password %q", password) } seen[password] = true } } func TestNormalizeRunPathKeepsProjectInsideHostRoot(t *testing.T) { for input, want := range map[string]string{"": "/", "/": "/", "public": "/public", "/apps/web/dist": "/apps/web/dist", "/.output/public": "/.output/public"} { got, err := normalizeRunPath(input) if err != nil || got != want { t.Fatalf("normalizeRunPath(%q) = %q, %v; want %q", input, got, err, want) } } for _, input := range []string{"/../etc", "/public/../etc", "/public//assets", `C:\\Windows`, "/has space", "/public/"} { if _, err := normalizeRunPath(input); err == nil { t.Fatalf("normalizeRunPath(%q) accepted an unsafe path", input) } } } func TestNormalizeDeploymentEntry(t *testing.T) { for runtime, expected := range map[string]string{"Node.js": "package.json", "Python": "app.py", "Go": "app", "Java": "app.jar"} { got, err := normalizeDeploymentEntry(runtime, "") if err != nil || got != expected { t.Fatalf("normalizeDeploymentEntry(%q) = %q, %v", runtime, got, err) } } for _, test := range []struct{ runtime, entry string }{ {"Java", "../../etc/passwd"}, {"Java", "/app.jar"}, {"Java", "app.sh"}, {"Node.js", `..\\server.js`}, {"Python", "app.txt"}, {"Go", "app.exe"}, } { if _, err := normalizeDeploymentEntry(test.runtime, test.entry); err == nil { t.Fatalf("normalizeDeploymentEntry(%q, %q) accepted unsafe input", test.runtime, test.entry) } } } func TestRunPathsOverlap(t *testing.T) { for _, pair := range [][2]string{{"/", "/public"}, {"/app", "/app"}, {"/app", "/app/public"}, {"/app/public", "/app"}} { if !runPathsOverlap(pair[0], pair[1]) { t.Fatalf("expected %q and %q to overlap", pair[0], pair[1]) } } for _, pair := range [][2]string{{"/app", "/application"}, {"/site-a", "/site-b"}, {"/apps/a", "/apps/b"}} { if runPathsOverlap(pair[0], pair[1]) { t.Fatalf("did not expect %q and %q to overlap", pair[0], pair[1]) } } }