| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- package config_test
- import (
- "os"
- "path/filepath"
- "testing"
- "time"
- "hth/internal/config"
- "hth/internal/profile"
- "hth/internal/testutil"
- )
- func writeYAML(t *testing.T, body string) string {
- t.Helper()
- dir := t.TempDir()
- p := filepath.Join(dir, "hthd.yaml")
- if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
- t.Fatal(err)
- }
- return p
- }
- func baseYAML(profilePath string) string {
- return `
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: mock
- profile: ` + profilePath + `
- runtime_root: /tmp/hth-cfgtest
- sockets:
- observe: /tmp/hth-cfgtest/observe/observe.sock
- control: /tmp/hth-cfgtest/control/control.sock
- observe_perm: "0660"
- control_perm: "0660"
- poll:
- temperature_ms: 200
- motor_status_ms: 100
- watchdog:
- heartbeat_period_ms: 5000
- lease:
- min_ttl_ms: 1000
- max_ttl_ms: 30000
- safety:
- fan: 10
- ptc: 0
- door_close_percent: 0
- ring_buffer_size: 64
- grpc:
- reflection: true
- mock:
- seed: 1
- startup_complete: true
- first_order_thermal: true
- `
- }
- func TestLoadAndValidate(t *testing.T) {
- prof := testutil.ProfilePath(t)
- path := writeYAML(t, baseYAML(prof))
- c, err := config.Load(path)
- if err != nil {
- t.Fatal(err)
- }
- p, err := profile.Load(c.Profile)
- if err != nil {
- t.Fatal(err)
- }
- if err := c.Validate(p); err != nil {
- t.Fatal(err)
- }
- }
- func TestExtraYAMLDocumentRejected(t *testing.T) {
- prof := testutil.ProfilePath(t)
- path := writeYAML(t, baseYAML(prof)+"\n---\napi_version: extra\n")
- if _, err := config.Load(path); err == nil {
- t.Fatal("second YAML document must be rejected")
- }
- }
- func TestUnknownFieldError(t *testing.T) {
- prof := testutil.ProfilePath(t)
- path := writeYAML(t, baseYAML(prof)+"\nnot_a_field: 1\n")
- if _, err := config.Load(path); err == nil {
- t.Fatal("expected unknown field error")
- }
- }
- func TestSerialMissingSafetyRejected(t *testing.T) {
- prof := testutil.ProfilePath(t)
- body := `
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: serial
- profile: ` + prof + `
- serial:
- path: /dev/hth/controller0
- runtime_root: /tmp/hth-cfgtest
- sockets:
- observe: /tmp/hth-cfgtest/observe/a.sock
- control: /tmp/hth-cfgtest/control/b.sock
- poll:
- temperature_ms: 200
- motor_status_ms: 100
- watchdog:
- heartbeat_period_ms: 5000
- lease:
- min_ttl_ms: 1000
- max_ttl_ms: 30000
- `
- path := writeYAML(t, body)
- c, err := config.Load(path)
- if err != nil {
- t.Fatal(err)
- }
- p, err := profile.Load(c.Profile)
- if err != nil {
- t.Fatal(err)
- }
- if err := c.Validate(p); err == nil {
- t.Fatal("serial mode must require explicit safety values")
- }
- }
- func TestHeartbeatMustBeBelowFirmwareTimeout(t *testing.T) {
- prof := testutil.ProfilePath(t)
- path := writeYAML(t, `
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: mock
- profile: `+prof+`
- runtime_root: /tmp/hth-cfgtest
- sockets:
- observe: /tmp/hth-cfgtest/observe/a.sock
- control: /tmp/hth-cfgtest/control/b.sock
- poll:
- temperature_ms: 200
- motor_status_ms: 100
- watchdog:
- heartbeat_period_ms: 30000
- lease:
- min_ttl_ms: 1000
- max_ttl_ms: 30000
- safety:
- fan: 10
- ptc: 0
- door_close_percent: 0
- `)
- c, err := config.Load(path)
- if err != nil {
- t.Fatal(err)
- }
- p, err := profile.Load(c.Profile)
- if err != nil {
- t.Fatal(err)
- }
- if err := c.Validate(p); err == nil {
- t.Fatal("heartbeat == firmware timeout must fail")
- }
- }
- func TestSamplePeriodMin(t *testing.T) {
- prof := testutil.ProfilePath(t)
- path := writeYAML(t, `
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: mock
- profile: `+prof+`
- runtime_root: /tmp/hth-cfgtest
- sockets:
- observe: /tmp/hth-cfgtest/observe/a.sock
- control: /tmp/hth-cfgtest/control/b.sock
- poll:
- temperature_ms: 1
- motor_status_ms: 100
- watchdog:
- heartbeat_period_ms: 5000
- lease:
- min_ttl_ms: 1000
- max_ttl_ms: 30000
- safety:
- fan: 10
- ptc: 0
- door_close_percent: 0
- `)
- c, err := config.Load(path)
- if err != nil {
- t.Fatal(err)
- }
- p, err := profile.Load(c.Profile)
- if err != nil {
- t.Fatal(err)
- }
- if err := c.Validate(p); err == nil {
- t.Fatal("expected sample period too small")
- }
- }
- func TestGetTempTimeoutBelowScanRejected(t *testing.T) {
- prof := testutil.ProfilePath(t)
- path := writeYAML(t, `
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: mock
- profile: `+prof+`
- runtime_root: /tmp/hth-cfgtest
- sockets:
- observe: /tmp/hth-cfgtest/observe/a.sock
- control: /tmp/hth-cfgtest/control/b.sock
- serial:
- get_temp_timeout_ms: 100
- poll:
- temperature_ms: 200
- motor_status_ms: 100
- watchdog:
- heartbeat_period_ms: 5000
- lease:
- min_ttl_ms: 1000
- max_ttl_ms: 30000
- safety:
- fan: 10
- ptc: 0
- door_close_percent: 0
- `)
- c, err := config.Load(path)
- if err != nil {
- t.Fatal(err)
- }
- p, err := profile.Load(c.Profile)
- if err != nil {
- t.Fatal(err)
- }
- if err := c.Validate(p); err == nil {
- t.Fatal("GET_TEMPERATURE timeout below scan busy must fail")
- }
- }
- func TestGetTempTimeoutCoversScan(t *testing.T) {
- prof := testutil.ProfilePath(t)
- path := writeYAML(t, baseYAML(prof))
- c, err := config.Load(path)
- if err != nil {
- t.Fatal(err)
- }
- p, err := profile.Load(c.Profile)
- if err != nil {
- t.Fatal(err)
- }
- if err := c.Validate(p); err != nil {
- t.Fatal(err)
- }
- got := c.GetTempTimeout(p)
- min := time.Duration(p.Timeouts.TemperatureScanBusyMS)*time.Millisecond + 300*time.Millisecond
- if got < min {
- t.Fatalf("derived GET timeout %s < min %s", got, min)
- }
- }
- func FuzzLoadYAML(f *testing.F) {
- f.Add([]byte("api_version: hth.api.v1\n"))
- f.Add([]byte("{}\n"))
- f.Add([]byte("a: 1\n---\nb: 2\n"))
- f.Fuzz(func(t *testing.T, data []byte) {
- p := filepath.Join(t.TempDir(), "c.yaml")
- if err := os.WriteFile(p, data, 0o644); err != nil {
- t.Fatal(err)
- }
- _, _ = config.Load(p)
- })
- }
- func FuzzSocketPaths(f *testing.F) {
- f.Add("/tmp/hth-cfgtest", "/tmp/hth-cfgtest/observe/observe.sock", "/tmp/hth-cfgtest/control/control.sock")
- f.Add("/run/hth", "/etc/passwd", "/run/hth/control.sock")
- f.Add("..", "../o.sock", "../c.sock")
- f.Add("/tmp/hth-cfgtest", "/tmp/hth-cfgtest/o.sock", "/tmp/hth-cfgtest/o.sock")
- f.Fuzz(func(t *testing.T, root, obs, ctl string) {
- if len(root) > 512 || len(obs) > 512 || len(ctl) > 512 {
- return
- }
- fan, ptc, door := 10, 0, 0
- c := &config.Config{
- APIVersion: "hth.api.v1",
- DeviceID: "prod2u-0",
- Backend: config.BackendMock,
- Profile: "profile.json",
- RuntimeRoot: root,
- Sockets: config.Sockets{
- Observe: obs, Control: ctl,
- ObservePerm: "0660", ControlPerm: "0660",
- },
- Poll: config.Poll{TemperatureMS: 200, MotorStatusMS: 100},
- Watchdog: config.Watchdog{HeartbeatPeriodMS: 5000},
- Lease: config.Lease{MinTTLMS: 50, MaxTTLMS: 5000},
- Safety: config.Safety{Fan: &fan, PTC: &ptc, DoorClosePercent: &door},
- Mock: config.Mock{Seed: 1, StartupComplete: true},
- }
- _ = c.Validate(nil)
- })
- }
|