| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package safety_test
- import (
- "testing"
- hthv1 "hth/internal/gen/hth/api/v1"
- "hth/internal/ids"
- "hth/internal/profile"
- "hth/internal/protocol"
- "hth/internal/safety"
- "hth/internal/testutil"
- "google.golang.org/protobuf/proto"
- )
- func FuzzAssemble(f *testing.F) {
- p, err := profile.Load(testutil.ProfilePath(f))
- if err != nil {
- f.Fatal(err)
- }
- f.Add(ids.Fan1, 10.0, false)
- f.Add(ids.DoorX, 101.0, false)
- f.Add("not-an-id", 1.0, true)
- f.Add(ids.PTC1, 0.0, false)
- f.Fuzz(func(t *testing.T, id string, value float64, allow bool) {
- if len(id) > 64 {
- id = id[:64]
- }
- _ = safety.Assemble(protocol.SetAll{}, nil, p)
- _ = safety.Assemble(protocol.SetAll{}, &hthv1.ActionBatch{
- AllowClamp: allow,
- ContinuousTargets: []*hthv1.ContinuousTarget{nil, {Id: id, Value: value}},
- }, p)
- _ = safety.Assemble(protocol.SetAll{Fan: [4]uint8{10, 10, 10, 10}}, &hthv1.ActionBatch{
- AllowClamp: allow,
- ContinuousTargets: []*hthv1.ContinuousTarget{{Id: id, Value: value}},
- }, p)
- })
- }
- func FuzzAssembleProto(f *testing.F) {
- p, err := profile.Load(testutil.ProfilePath(f))
- if err != nil {
- f.Fatal(err)
- }
- seed, _ := proto.Marshal(&hthv1.ActionBatch{
- ContinuousTargets: []*hthv1.ContinuousTarget{{Id: ids.Fan1, Value: 10}},
- })
- f.Add(seed)
- f.Add([]byte{})
- f.Add([]byte{0xff, 0x00, 0x01})
- f.Fuzz(func(t *testing.T, data []byte) {
- if len(data) > 4096 {
- t.Skip()
- }
- batch := &hthv1.ActionBatch{}
- _ = proto.Unmarshal(data, batch)
- _ = safety.Assemble(protocol.SetAll{}, batch, p)
- _ = safety.RawFieldAudits(batch)
- _ = safety.LimitSlew(protocol.SetAll{}, protocol.SetAll{Fan: [4]uint8{40}}, p, safety.SlewOpts{
- Elapsed: 10, AllowClamp: true, Bypass: len(data)%2 == 0, First: len(data)%3 == 0,
- })
- })
- }
|