action_record_fuzz_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package device
  2. import (
  3. "testing"
  4. "time"
  5. hthv1 "hth/internal/gen/hth/api/v1"
  6. "hth/internal/protocol"
  7. )
  8. func FuzzActionRecordConversion(f *testing.F) {
  9. f.Add("cmd-1", "client:apply", uint64(1), uint64(1), int32(1000), int32(1000), int32(42))
  10. f.Add("hb", "daemon/internal:heartbeat", uint64(2), uint64(3), int32(0), int32(0), int32(0))
  11. f.Fuzz(func(t *testing.T, cmd, origin string, epoch, gen uint64, uid, gid, pid int32) {
  12. if len(cmd) > 128 {
  13. cmd = cmd[:128]
  14. }
  15. if len(origin) > 128 {
  16. origin = origin[:128]
  17. }
  18. ax := ActionExecutionContext{
  19. CommandID: cmd, Origin: origin, Mode: hthv1.ActionMode_ACTION_MODE_NORMAL,
  20. DeviceEpoch: epoch, PortGeneration: gen, JobID: 7,
  21. PeerUID: uid, PeerGID: gid, PeerPID: pid,
  22. RequestedAt: time.Unix(1, 0), QueuedAt: time.Unix(1, 1),
  23. EffectiveTarget: protocol.SetAll{Fan: [4]uint8{10, 10, 10, 10}},
  24. }
  25. rec := recordFromAction(ax, time.Unix(2, 0), hthv1.ConfirmationStatus_CONFIRMATION_STATUS_UNCONFIRMED,
  26. hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_UNCONFIRMED)
  27. if rec == nil {
  28. return
  29. }
  30. if rec.Origin != origin {
  31. t.Fatalf("origin %q != %q", rec.Origin, origin)
  32. }
  33. if rec.ActionEpoch != epoch || rec.PortGeneration != gen {
  34. t.Fatalf("epoch/gen %d/%d != %d/%d", rec.ActionEpoch, rec.PortGeneration, epoch, gen)
  35. }
  36. if rec.PeerUid != uid || rec.PeerGid != gid || rec.PeerPid != pid {
  37. t.Fatalf("peer mutated")
  38. }
  39. })
  40. }