//go:build unix package servertoken import ( "os" "path/filepath" "strings" "testing" ) func TestReadAcceptsOnlyAPrivateRegularFile(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "server-token") token := strings.Repeat("t", 48) if err := os.WriteFile(path, []byte(token+"\n"), 0o600); err != nil { t.Fatal(err) } if got, err := Read(path); err != nil || got != token { t.Fatalf("Read = %q, %v", got, err) } if err := os.Chmod(path, 0o640); err != nil { t.Fatal(err) } if _, err := Read(path); err == nil { t.Fatal("group-readable token accepted") } os.Chmod(path, 0o600) link := filepath.Join(dir, "link") os.Symlink(path, link) if _, err := Read(link); err == nil { t.Fatal("symlinked token accepted") } short := filepath.Join(dir, "short") os.WriteFile(short, []byte("short"), 0o600) if _, err := Read(short); err == nil { t.Fatal("short token accepted") } } func TestEqualIsExact(t *testing.T) { if Equal("", "") || Equal("a", "") || !Equal("abc", "abc") || Equal("abc", "abd") { t.Fatal("Equal mismatch") } }