Download this file
| 1 | //go:build linux |
| 2 | |
| 3 | package layout |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "golang.org/x/sys/unix" |
| 13 | ) |
| 14 | |
| 15 | // TestReadJournalRefusesASymlinkAndAFIFO_FIFO is the FIFO half of |
| 16 | // TestReadJournalRefusesASymlinkAndAFIFO: /data/layout is agent-owned, |
| 17 | // but a flat tenant shares the agent's uid during the transition and can |
| 18 | // plant a FIFO there in place of a journal. ReadJournal must refuse it |
| 19 | // rather than block on the open. Run with a timeout so a regression here |
| 20 | // fails the test instead of wedging it. |
| 21 | func TestReadJournalRefusesASymlinkAndAFIFO_FIFO(t *testing.T) { |
| 22 | dd := t.TempDir() |
| 23 | dir := filepath.Join(dd, "layout") |
| 24 | if err := os.MkdirAll(dir, 0o700); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if err := unix.Mkfifo(filepath.Join(dir, "h.example.com.json"), 0o600); err != nil { |
| 28 | t.Fatalf("Mkfifo: %v", err) |
| 29 | } |
| 30 | done := make(chan error, 1) |
| 31 | go func() { |
| 32 | _, _, err := ReadJournal(dd, "h.example.com") |
| 33 | done <- err |
| 34 | }() |
| 35 | select { |
| 36 | case err := <-done: |
| 37 | if err == nil { |
| 38 | t.Error("a FIFO planted as a journal was read") |
| 39 | } |
| 40 | if errors.Is(err, os.ErrNotExist) { |
| 41 | t.Errorf("FIFO reported as absent rather than refused: %v", err) |
| 42 | } |
| 43 | case <-time.After(2 * time.Second): |
| 44 | t.Fatal("ReadJournal blocked on a FIFO instead of refusing it") |
| 45 | } |
| 46 | } |
| 47 | |