| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package backend_test
- import (
- "context"
- "errors"
- "os"
- "path/filepath"
- "testing"
- "time"
- "google.golang.org/protobuf/types/known/timestamppb"
- "hth/internal/backend"
- "hth/internal/capture"
- hthv1 "hth/internal/gen/hth/api/v1"
- )
- func writeCapture(t *testing.T, dir string, recs []capture.Record, capHash string) {
- t.Helper()
- f, err := os.Create(capture.RecordsPath(dir))
- if err != nil {
- t.Fatal(err)
- }
- for _, rec := range recs {
- if err := capture.AppendRecord(f, rec); err != nil {
- t.Fatal(err)
- }
- }
- f.Close()
- man := capture.Manifest{
- APIVersion: "hth.api.v1", DeviceID: "d0", Status: capture.StatusComplete,
- ProfileHash: "profile-test", CapabilityHash: capHash,
- }
- if err := capture.Seal(dir, man); err != nil {
- t.Fatal(err)
- }
- }
- func TestReplayEmptyCaptureRejected(t *testing.T) {
- dir := t.TempDir()
- if err := os.WriteFile(capture.RecordsPath(dir), []byte(""), 0o644); err != nil {
- t.Fatal(err)
- }
- _ = capture.WriteManifest(dir, capture.Manifest{
- APIVersion: "hth.api.v1", Status: capture.StatusComplete,
- ProfileHash: "p", CapabilityHash: "c", RecordCount: 0, RecordsSHA256: "x",
- })
- r := backend.NewReplay(dir, 1, true, 0)
- if err := r.Open(context.Background()); err == nil {
- t.Fatal("empty records must fail")
- }
- }
- func TestReplayCapabilityMismatch(t *testing.T) {
- dir := t.TempDir()
- s := &hthv1.Snapshot{DeviceId: "d0", EventId: 1, WallTime: timestamppb.New(time.Unix(1, 0))}
- rec, _ := capture.SnapshotRecord(s)
- writeCapture(t, dir, []capture.Record{rec}, "cap-a")
- r := backend.NewReplay(dir, 1, true, 0)
- r.SetContract("hth.api.v1", "cap-b")
- if err := r.Open(context.Background()); err == nil {
- t.Fatal("cap mismatch must fail")
- }
- }
- func TestReplayContextCancelDoesNotAdvance(t *testing.T) {
- dir := t.TempDir()
- s1 := &hthv1.Snapshot{DeviceId: "d0", EventId: 1, WallTime: timestamppb.New(time.Unix(1, 0))}
- s2 := &hthv1.Snapshot{DeviceId: "d0", EventId: 2, WallTime: timestamppb.New(time.Unix(1, 0).Add(80 * time.Millisecond))}
- r1, _ := capture.SnapshotRecord(s1)
- r2, _ := capture.SnapshotRecord(s2)
- writeCapture(t, dir, []capture.Record{r1, r2}, "cap-test")
- rp := backend.NewReplay(dir, 1, false, 0)
- if err := rp.Open(context.Background()); err != nil {
- t.Fatal(err)
- }
- item, err := rp.Next(context.Background())
- if err != nil || item.EventID != 1 {
- t.Fatalf("first %v %v", item.EventID, err)
- }
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
- defer cancel()
- _, err = rp.Next(ctx)
- if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, context.Canceled) {
- t.Fatalf("want cancel during wait, got %v", err)
- }
- item, err = rp.Next(context.Background())
- if err != nil {
- t.Fatal(err)
- }
- if item.EventID != 2 {
- t.Fatalf("cancel must not skip record, got %d", item.EventID)
- }
- }
- func TestReplayEOF(t *testing.T) {
- dir := t.TempDir()
- s := &hthv1.Snapshot{DeviceId: "d0", EventId: 9, WallTime: timestamppb.New(time.Unix(1, 0))}
- rec, _ := capture.SnapshotRecord(s)
- writeCapture(t, dir, []capture.Record{rec}, "cap-test")
- rp := backend.NewReplay(dir, 1000, true, 0)
- if err := rp.Open(context.Background()); err != nil {
- t.Fatal(err)
- }
- if _, err := rp.Next(context.Background()); err != nil {
- t.Fatal(err)
- }
- _, err := rp.Next(context.Background())
- if !errors.Is(err, backend.ErrReplayEOF) {
- t.Fatalf("EOF: %v", err)
- }
- }
- func TestReplayTruncatedRejected(t *testing.T) {
- dir := t.TempDir()
- p := capture.RecordsPath(dir)
- if err := os.WriteFile(p, []byte("{\"v\":1,\"kind\":\"event\"}"), 0o644); err != nil {
- t.Fatal(err)
- }
- _ = capture.WriteManifest(dir, capture.Manifest{
- APIVersion: "hth.api.v1", Status: capture.StatusComplete,
- ProfileHash: "p", CapabilityHash: "c", RecordCount: 1, RecordsSHA256: "x",
- })
- r := backend.NewReplay(dir, 1, true, 0)
- if err := r.Open(context.Background()); err == nil {
- t.Fatal("truncated jsonl must fail")
- }
- }
- func TestReplayLargeFileLimit(t *testing.T) {
- dir := t.TempDir()
- p := capture.RecordsPath(dir)
- // Stat size check uses MaxRecordsBytes; write a small file and monkey via Open path
- if err := os.WriteFile(p, []byte("{}\n"), 0o644); err != nil {
- t.Fatal(err)
- }
- _ = capture.WriteManifest(dir, capture.Manifest{
- APIVersion: "hth.api.v1", Status: capture.StatusComplete,
- ProfileHash: "p", CapabilityHash: "c", RecordCount: 1, RecordsSHA256: "x",
- })
- r := backend.NewReplay(dir, 1, true, 0)
- if err := r.Open(context.Background()); err == nil {
- t.Fatal("bad record/hash must fail")
- }
- _ = filepath.Separator
- }
|