carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package release
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestStateWritersRefuseSymlinkParents(t *testing.T) {
11 for _, tc := range []struct {
12 name, parent string
13 write func(string) error
14 }{
15 {"program", "programs", func(dir string) error { return RecordProgram(dir, "app.example", "example.org/app") }},
16 {"healthy", "healthy", func(dir string) error {
17 return RecordHealthy(dir, "bab", "app", HealthyBuild{SHA256: strings.Repeat("a", 64)})
18 }},
19 {"refusal", "refusals", func(dir string) error { return WriteRefusal(dir, Refusal{Host: "app.example"}) }},
20 } {
21 t.Run(tc.name, func(t *testing.T) {
22 dir, outside := t.TempDir(), t.TempDir()
23 if err := os.Symlink(outside, filepath.Join(dir, tc.parent)); err != nil {
24 t.Fatal(err)
25 }
26 if err := tc.write(dir); err == nil {
27 t.Fatal("write followed a symlink parent")
28 }
29 entries, err := os.ReadDir(outside)
30 if err != nil || len(entries) != 0 {
31 t.Fatalf("outside directory changed: %v %v", entries, err)
32 }
33 })
34 }
35}
36
37func TestRecordProgramIgnoresPlantedLegacyTemporarySymlink(t *testing.T) {
38 dir := t.TempDir()
39 victim := filepath.Join(dir, "victim")
40 if err := os.WriteFile(victim, []byte("untouched"), 0o600); err != nil {
41 t.Fatal(err)
42 }
43 p := programPath(dir, "app.example")
44 if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
45 t.Fatal(err)
46 }
47 if err := os.Symlink(victim, p+".tmp"); err != nil {
48 t.Fatal(err)
49 }
50 if err := RecordProgram(dir, "app.example", "example.org/app"); err != nil {
51 t.Fatal(err)
52 }
53 got, err := os.ReadFile(victim)
54 if err != nil || string(got) != "untouched" {
55 t.Fatalf("victim changed: %q %v", got, err)
56 }
57 if got := ReadPrograms(dir, "app.example"); len(got) != 1 || got[0] != "example.org/app" {
58 t.Fatalf("program not recorded: %v", got)
59 }
60}
61