fuzz_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package safety_test
  2. import (
  3. "testing"
  4. hthv1 "hth/internal/gen/hth/api/v1"
  5. "hth/internal/ids"
  6. "hth/internal/profile"
  7. "hth/internal/protocol"
  8. "hth/internal/safety"
  9. "hth/internal/testutil"
  10. "google.golang.org/protobuf/proto"
  11. )
  12. func FuzzAssemble(f *testing.F) {
  13. p, err := profile.Load(testutil.ProfilePath(f))
  14. if err != nil {
  15. f.Fatal(err)
  16. }
  17. f.Add(ids.Fan1, 10.0, false)
  18. f.Add(ids.DoorX, 101.0, false)
  19. f.Add("not-an-id", 1.0, true)
  20. f.Add(ids.PTC1, 0.0, false)
  21. f.Fuzz(func(t *testing.T, id string, value float64, allow bool) {
  22. if len(id) > 64 {
  23. id = id[:64]
  24. }
  25. _ = safety.Assemble(protocol.SetAll{}, nil, p)
  26. _ = safety.Assemble(protocol.SetAll{}, &hthv1.ActionBatch{
  27. AllowClamp: allow,
  28. ContinuousTargets: []*hthv1.ContinuousTarget{nil, {Id: id, Value: value}},
  29. }, p)
  30. _ = safety.Assemble(protocol.SetAll{Fan: [4]uint8{10, 10, 10, 10}}, &hthv1.ActionBatch{
  31. AllowClamp: allow,
  32. ContinuousTargets: []*hthv1.ContinuousTarget{{Id: id, Value: value}},
  33. }, p)
  34. })
  35. }
  36. func FuzzAssembleProto(f *testing.F) {
  37. p, err := profile.Load(testutil.ProfilePath(f))
  38. if err != nil {
  39. f.Fatal(err)
  40. }
  41. seed, _ := proto.Marshal(&hthv1.ActionBatch{
  42. ContinuousTargets: []*hthv1.ContinuousTarget{{Id: ids.Fan1, Value: 10}},
  43. })
  44. f.Add(seed)
  45. f.Add([]byte{})
  46. f.Add([]byte{0xff, 0x00, 0x01})
  47. f.Fuzz(func(t *testing.T, data []byte) {
  48. if len(data) > 4096 {
  49. t.Skip()
  50. }
  51. batch := &hthv1.ActionBatch{}
  52. _ = proto.Unmarshal(data, batch)
  53. _ = safety.Assemble(protocol.SetAll{}, batch, p)
  54. _ = safety.RawFieldAudits(batch)
  55. _ = safety.LimitSlew(protocol.SetAll{}, protocol.SetAll{Fan: [4]uint8{40}}, p, safety.SlewOpts{
  56. Elapsed: 10, AllowClamp: true, Bypass: len(data)%2 == 0, First: len(data)%3 == 0,
  57. })
  58. })
  59. }