| 1 | package memory |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestStoreSavePreservesUnreadableIndex(t *testing.T) { |
| 10 | s := Store{Dir: t.TempDir()} |
| 11 | if _, err := s.Save(Memory{Name: "alpha", Description: "keep", Type: TypeProject, Body: "body"}); err != nil { |
| 12 | t.Fatal(err) |
| 13 | } |
| 14 | indexPath := filepath.Join(s.Dir, indexFile) |
| 15 | before := mustReadString(t, indexPath) |
| 16 | if err := os.Chmod(indexPath, 0); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | t.Cleanup(func() { _ = os.Chmod(indexPath, 0o644) }) |
| 20 | if _, err := os.ReadFile(indexPath); err == nil { |
| 21 | t.Skip("this user can read files without read permission") |
| 22 | } |
| 23 | if _, err := s.Save(Memory{Name: "beta", Description: "new", Type: TypeProject, Body: "body"}); err == nil { |
| 24 | t.Fatal("Save should report the index read error") |
| 25 | } |
| 26 | if err := os.Chmod(indexPath, 0o644); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if got := mustReadString(t, indexPath); got != before { |
| 30 | t.Fatalf("Save overwrote an unreadable index:\n%s", got) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestStoreArchivePreservesUnreadableIndex(t *testing.T) { |
| 35 | s := Store{Dir: t.TempDir()} |
| 36 | for _, name := range []string{"alpha", "beta"} { |
| 37 | if _, err := s.Save(Memory{Name: name, Description: "keep", Type: TypeProject, Body: "body"}); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | } |
| 41 | indexPath := filepath.Join(s.Dir, indexFile) |
| 42 | before := mustReadString(t, indexPath) |
| 43 | if err := os.Chmod(indexPath, 0); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | t.Cleanup(func() { _ = os.Chmod(indexPath, 0o644) }) |
| 47 | if _, err := os.ReadFile(indexPath); err == nil { |
| 48 | t.Skip("this user can read files without read permission") |
| 49 | } |
| 50 | if _, err := s.Archive("alpha"); err == nil { |
| 51 | t.Fatal("Archive should report the index read error") |
| 52 | } |
| 53 | if err := os.Chmod(indexPath, 0o644); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if got := mustReadString(t, indexPath); got != before { |
| 57 | t.Fatalf("Archive overwrote an unreadable index:\n%s", got) |
| 58 | } |
| 59 | if _, err := os.Stat(filepath.Join(s.Dir, "alpha.md")); err != nil { |
| 60 | t.Fatalf("Archive moved the fact before checking the index: %v", err) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestFlushIndexPreservesUnreadableFile(t *testing.T) { |
| 65 | dir := t.TempDir() |
| 66 | indexPath := filepath.Join(dir, indexFile) |
| 67 | before := "# Memory\n\nHand-written note.\n" |
| 68 | if err := os.WriteFile(indexPath, []byte(before), 0o644); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | if err := os.Chmod(indexPath, 0); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | t.Cleanup(func() { _ = os.Chmod(indexPath, 0o644) }) |
| 75 | if _, err := os.ReadFile(indexPath); err == nil { |
| 76 | t.Skip("this user can read files without read permission") |
| 77 | } |
| 78 | if err := flushIndexIn(dir, map[string]string{"beta": "- [beta](beta.md) — new"}); err == nil { |
| 79 | t.Fatal("flushIndexIn should report the index read error") |
| 80 | } |
| 81 | if err := os.Chmod(indexPath, 0o644); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if got := mustReadString(t, indexPath); got != before { |
| 85 | t.Fatalf("flushIndexIn overwrote hand-written content:\n%s", got) |
| 86 | } |
| 87 | } |
| 88 |