strict_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package capture_test
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "time"
  8. "google.golang.org/protobuf/types/known/timestamppb"
  9. "hth/internal/capture"
  10. hthv1 "hth/internal/gen/hth/api/v1"
  11. )
  12. func TestEmptyHashesRejected(t *testing.T) {
  13. err := capture.ValidateManifest(capture.Manifest{
  14. Status: capture.StatusComplete, APIVersion: "hth.api.v1", RecordCount: 1,
  15. }, "hth.api.v1", "", true)
  16. if err == nil || !strings.Contains(err.Error(), "empty capability_hash") {
  17. t.Fatalf("empty hash: %v", err)
  18. }
  19. }
  20. func TestUnknownManifestFieldRejected(t *testing.T) {
  21. dir := t.TempDir()
  22. if err := os.WriteFile(filepath.Join(dir, "manifest.json"), []byte(`{"v":1,"kind":"manifest","bonus":true}`+"\n"), 0o644); err != nil {
  23. t.Fatal(err)
  24. }
  25. if _, err := capture.ReadManifest(dir); err == nil {
  26. t.Fatal("unknown field must fail")
  27. }
  28. }
  29. func TestRecordCountMismatch(t *testing.T) {
  30. dir := t.TempDir()
  31. f, err := os.Create(capture.RecordsPath(dir))
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. s := &hthv1.Snapshot{DeviceId: "d0", EventId: 1, WallTime: timestamppb.New(time.Unix(1, 0))}
  36. rec, err := capture.SnapshotRecord(s)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if err := capture.AppendRecord(f, rec); err != nil {
  41. t.Fatal(err)
  42. }
  43. f.Close()
  44. man := capture.Manifest{
  45. APIVersion: "hth.api.v1", DeviceID: "d0", Status: capture.StatusComplete,
  46. ProfileHash: "p", CapabilityHash: "c",
  47. }
  48. if err := capture.Seal(dir, man); err != nil {
  49. t.Fatal(err)
  50. }
  51. got, err := capture.ReadManifest(dir)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. got.RecordCount = 99
  56. recs, err := capture.ReadRecords(capture.RecordsPath(dir))
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if err := capture.ValidateRecordsAgainstManifest(got, recs, got.RecordsSHA256); err == nil {
  61. t.Fatal("count mismatch must fail")
  62. }
  63. }
  64. func TestWrongRecordsHash(t *testing.T) {
  65. m := capture.Manifest{RecordCount: 1, RecordsSHA256: "deadbeef"}
  66. err := capture.ValidateRecordsAgainstManifest(m, []capture.Record{{Kind: "event", EventID: 1}}, "ffff")
  67. if err == nil {
  68. t.Fatal("hash mismatch must fail")
  69. }
  70. }
  71. func TestTrailingJSONAndNonMonotonicRejected(t *testing.T) {
  72. dir := t.TempDir()
  73. if err := os.WriteFile(filepath.Join(dir, "manifest.json"), []byte(`{"v":1,"kind":"manifest","status":"incomplete"}`+"\n{}\n"), 0o644); err != nil {
  74. t.Fatal(err)
  75. }
  76. if _, err := capture.ReadManifest(dir); err == nil {
  77. t.Fatal("second JSON object must fail")
  78. }
  79. err := capture.ValidateRecordsAgainstManifest(
  80. capture.Manifest{RecordCount: 2, Status: capture.StatusComplete, FirstSourceEventID: 5, LastSourceEventID: 3},
  81. []capture.Record{{Kind: "event", EventID: 5}, {Kind: "event", EventID: 3}},
  82. "",
  83. )
  84. if err == nil {
  85. t.Fatal("non-monotonic source ids must fail")
  86. }
  87. }
  88. func TestZeroRecordCompleteRejected(t *testing.T) {
  89. err := capture.ValidateManifest(capture.Manifest{
  90. Status: capture.StatusComplete, APIVersion: "hth.api.v1", RecordCount: 0,
  91. CapabilityHash: "c", RecordsSHA256: "a", ProfileHash: "p",
  92. }, "hth.api.v1", "c", true)
  93. if err == nil {
  94. t.Fatal("complete empty capture must fail")
  95. }
  96. }