| 1 | //go:build unix |
| 2 | |
| 3 | package rebase |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "syscall" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | // Only a regular file is a cache entry. A FIFO, which an open to read |
| 15 | // would wait on for a writer, a symlink to one, and a symlink to an entry |
| 16 | // that reads, each in place of a landing entry and of a tree entry, is a |
| 17 | // miss, and the read returns at once. |
| 18 | func TestACacheEntryThatIsNotARegularFileIsAMiss(t *testing.T) { |
| 19 | c := cache{dir: t.TempDir()} |
| 20 | text, oid, digest := "the-valley transfer landing\n", strings.Repeat("ab", 20), strings.Repeat("cd", 32) |
| 21 | c.putLanding(text) |
| 22 | c.putTree(oid, digest) |
| 23 | reads := map[string]func() bool{ |
| 24 | filepath.Join(c.dir, "landings", hexSum(text)): func() bool { return c.landing(text) }, |
| 25 | filepath.Join(c.dir, "trees", oid): func() bool { _, ok := c.tree(oid); return ok }, |
| 26 | } |
| 27 | for path, read := range reads { |
| 28 | if !read() { |
| 29 | t.Fatalf("the entry at %s does not read", path) |
| 30 | } |
| 31 | kept, fifo := filepath.Join(t.TempDir(), "kept"), filepath.Join(t.TempDir(), "fifo") |
| 32 | if err := os.Rename(path, kept); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if err := syscall.Mkfifo(fifo, 0o600); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | for what, plant := range map[string]func() error{ |
| 39 | "a FIFO": func() error { return syscall.Mkfifo(path, 0o600) }, |
| 40 | "a symlink to a FIFO": func() error { return os.Symlink(fifo, path) }, |
| 41 | "a symlink to an entry that reads": func() error { return os.Symlink(kept, path) }, |
| 42 | } { |
| 43 | os.Remove(path) |
| 44 | if err := plant(); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | found := make(chan bool, 1) |
| 48 | go func() { found <- read() }() |
| 49 | select { |
| 50 | case ok := <-found: |
| 51 | if ok { |
| 52 | t.Errorf("%s at %s is found", what, path) |
| 53 | } |
| 54 | case <-time.After(time.Minute): |
| 55 | t.Fatalf("reading %s at %s did not return", what, path) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |