replay_strict_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package backend_test
  2. import (
  3. "context"
  4. "errors"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "google.golang.org/protobuf/types/known/timestamppb"
  10. "hth/internal/backend"
  11. "hth/internal/capture"
  12. hthv1 "hth/internal/gen/hth/api/v1"
  13. )
  14. func writeCapture(t *testing.T, dir string, recs []capture.Record, capHash string) {
  15. t.Helper()
  16. f, err := os.Create(capture.RecordsPath(dir))
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. for _, rec := range recs {
  21. if err := capture.AppendRecord(f, rec); err != nil {
  22. t.Fatal(err)
  23. }
  24. }
  25. f.Close()
  26. man := capture.Manifest{
  27. APIVersion: "hth.api.v1", DeviceID: "d0", Status: capture.StatusComplete,
  28. ProfileHash: "profile-test", CapabilityHash: capHash,
  29. }
  30. if err := capture.Seal(dir, man); err != nil {
  31. t.Fatal(err)
  32. }
  33. }
  34. func TestReplayEmptyCaptureRejected(t *testing.T) {
  35. dir := t.TempDir()
  36. if err := os.WriteFile(capture.RecordsPath(dir), []byte(""), 0o644); err != nil {
  37. t.Fatal(err)
  38. }
  39. _ = capture.WriteManifest(dir, capture.Manifest{
  40. APIVersion: "hth.api.v1", Status: capture.StatusComplete,
  41. ProfileHash: "p", CapabilityHash: "c", RecordCount: 0, RecordsSHA256: "x",
  42. })
  43. r := backend.NewReplay(dir, 1, true, 0)
  44. if err := r.Open(context.Background()); err == nil {
  45. t.Fatal("empty records must fail")
  46. }
  47. }
  48. func TestReplayCapabilityMismatch(t *testing.T) {
  49. dir := t.TempDir()
  50. s := &hthv1.Snapshot{DeviceId: "d0", EventId: 1, WallTime: timestamppb.New(time.Unix(1, 0))}
  51. rec, _ := capture.SnapshotRecord(s)
  52. writeCapture(t, dir, []capture.Record{rec}, "cap-a")
  53. r := backend.NewReplay(dir, 1, true, 0)
  54. r.SetContract("hth.api.v1", "cap-b")
  55. if err := r.Open(context.Background()); err == nil {
  56. t.Fatal("cap mismatch must fail")
  57. }
  58. }
  59. func TestReplayContextCancelDoesNotAdvance(t *testing.T) {
  60. dir := t.TempDir()
  61. s1 := &hthv1.Snapshot{DeviceId: "d0", EventId: 1, WallTime: timestamppb.New(time.Unix(1, 0))}
  62. s2 := &hthv1.Snapshot{DeviceId: "d0", EventId: 2, WallTime: timestamppb.New(time.Unix(1, 0).Add(80 * time.Millisecond))}
  63. r1, _ := capture.SnapshotRecord(s1)
  64. r2, _ := capture.SnapshotRecord(s2)
  65. writeCapture(t, dir, []capture.Record{r1, r2}, "cap-test")
  66. rp := backend.NewReplay(dir, 1, false, 0)
  67. if err := rp.Open(context.Background()); err != nil {
  68. t.Fatal(err)
  69. }
  70. item, err := rp.Next(context.Background())
  71. if err != nil || item.EventID != 1 {
  72. t.Fatalf("first %v %v", item.EventID, err)
  73. }
  74. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
  75. defer cancel()
  76. _, err = rp.Next(ctx)
  77. if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, context.Canceled) {
  78. t.Fatalf("want cancel during wait, got %v", err)
  79. }
  80. item, err = rp.Next(context.Background())
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if item.EventID != 2 {
  85. t.Fatalf("cancel must not skip record, got %d", item.EventID)
  86. }
  87. }
  88. func TestReplayEOF(t *testing.T) {
  89. dir := t.TempDir()
  90. s := &hthv1.Snapshot{DeviceId: "d0", EventId: 9, WallTime: timestamppb.New(time.Unix(1, 0))}
  91. rec, _ := capture.SnapshotRecord(s)
  92. writeCapture(t, dir, []capture.Record{rec}, "cap-test")
  93. rp := backend.NewReplay(dir, 1000, true, 0)
  94. if err := rp.Open(context.Background()); err != nil {
  95. t.Fatal(err)
  96. }
  97. if _, err := rp.Next(context.Background()); err != nil {
  98. t.Fatal(err)
  99. }
  100. _, err := rp.Next(context.Background())
  101. if !errors.Is(err, backend.ErrReplayEOF) {
  102. t.Fatalf("EOF: %v", err)
  103. }
  104. }
  105. func TestReplayTruncatedRejected(t *testing.T) {
  106. dir := t.TempDir()
  107. p := capture.RecordsPath(dir)
  108. if err := os.WriteFile(p, []byte("{\"v\":1,\"kind\":\"event\"}"), 0o644); err != nil {
  109. t.Fatal(err)
  110. }
  111. _ = capture.WriteManifest(dir, capture.Manifest{
  112. APIVersion: "hth.api.v1", Status: capture.StatusComplete,
  113. ProfileHash: "p", CapabilityHash: "c", RecordCount: 1, RecordsSHA256: "x",
  114. })
  115. r := backend.NewReplay(dir, 1, true, 0)
  116. if err := r.Open(context.Background()); err == nil {
  117. t.Fatal("truncated jsonl must fail")
  118. }
  119. }
  120. func TestReplayLargeFileLimit(t *testing.T) {
  121. dir := t.TempDir()
  122. p := capture.RecordsPath(dir)
  123. // Stat size check uses MaxRecordsBytes; write a small file and monkey via Open path
  124. if err := os.WriteFile(p, []byte("{}\n"), 0o644); err != nil {
  125. t.Fatal(err)
  126. }
  127. _ = capture.WriteManifest(dir, capture.Manifest{
  128. APIVersion: "hth.api.v1", Status: capture.StatusComplete,
  129. ProfileHash: "p", CapabilityHash: "c", RecordCount: 1, RecordsSHA256: "x",
  130. })
  131. r := backend.NewReplay(dir, 1, true, 0)
  132. if err := r.Open(context.Background()); err == nil {
  133. t.Fatal("bad record/hash must fail")
  134. }
  135. _ = filepath.Separator
  136. }