| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package capture_test
- import (
- "os"
- "path/filepath"
- "strings"
- "testing"
- "time"
- "google.golang.org/protobuf/types/known/timestamppb"
- "hth/internal/capture"
- hthv1 "hth/internal/gen/hth/api/v1"
- )
- func TestEmptyHashesRejected(t *testing.T) {
- err := capture.ValidateManifest(capture.Manifest{
- Status: capture.StatusComplete, APIVersion: "hth.api.v1", RecordCount: 1,
- }, "hth.api.v1", "", true)
- if err == nil || !strings.Contains(err.Error(), "empty capability_hash") {
- t.Fatalf("empty hash: %v", err)
- }
- }
- func TestUnknownManifestFieldRejected(t *testing.T) {
- dir := t.TempDir()
- if err := os.WriteFile(filepath.Join(dir, "manifest.json"), []byte(`{"v":1,"kind":"manifest","bonus":true}`+"\n"), 0o644); err != nil {
- t.Fatal(err)
- }
- if _, err := capture.ReadManifest(dir); err == nil {
- t.Fatal("unknown field must fail")
- }
- }
- func TestRecordCountMismatch(t *testing.T) {
- dir := t.TempDir()
- f, err := os.Create(capture.RecordsPath(dir))
- if err != nil {
- t.Fatal(err)
- }
- s := &hthv1.Snapshot{DeviceId: "d0", EventId: 1, WallTime: timestamppb.New(time.Unix(1, 0))}
- rec, err := capture.SnapshotRecord(s)
- if err != nil {
- t.Fatal(err)
- }
- 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: "p", CapabilityHash: "c",
- }
- if err := capture.Seal(dir, man); err != nil {
- t.Fatal(err)
- }
- got, err := capture.ReadManifest(dir)
- if err != nil {
- t.Fatal(err)
- }
- got.RecordCount = 99
- recs, err := capture.ReadRecords(capture.RecordsPath(dir))
- if err != nil {
- t.Fatal(err)
- }
- if err := capture.ValidateRecordsAgainstManifest(got, recs, got.RecordsSHA256); err == nil {
- t.Fatal("count mismatch must fail")
- }
- }
- func TestWrongRecordsHash(t *testing.T) {
- m := capture.Manifest{RecordCount: 1, RecordsSHA256: "deadbeef"}
- err := capture.ValidateRecordsAgainstManifest(m, []capture.Record{{Kind: "event", EventID: 1}}, "ffff")
- if err == nil {
- t.Fatal("hash mismatch must fail")
- }
- }
- func TestTrailingJSONAndNonMonotonicRejected(t *testing.T) {
- dir := t.TempDir()
- if err := os.WriteFile(filepath.Join(dir, "manifest.json"), []byte(`{"v":1,"kind":"manifest","status":"incomplete"}`+"\n{}\n"), 0o644); err != nil {
- t.Fatal(err)
- }
- if _, err := capture.ReadManifest(dir); err == nil {
- t.Fatal("second JSON object must fail")
- }
- err := capture.ValidateRecordsAgainstManifest(
- capture.Manifest{RecordCount: 2, Status: capture.StatusComplete, FirstSourceEventID: 5, LastSourceEventID: 3},
- []capture.Record{{Kind: "event", EventID: 5}, {Kind: "event", EventID: 3}},
- "",
- )
- if err == nil {
- t.Fatal("non-monotonic source ids must fail")
- }
- }
- func TestZeroRecordCompleteRejected(t *testing.T) {
- err := capture.ValidateManifest(capture.Manifest{
- Status: capture.StatusComplete, APIVersion: "hth.api.v1", RecordCount: 0,
- CapabilityHash: "c", RecordsSHA256: "a", ProfileHash: "p",
- }, "hth.api.v1", "c", true)
- if err == nil {
- t.Fatal("complete empty capture must fail")
- }
- }
|