config_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package config_test
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "time"
  7. "hth/internal/config"
  8. "hth/internal/profile"
  9. "hth/internal/testutil"
  10. )
  11. func writeYAML(t *testing.T, body string) string {
  12. t.Helper()
  13. dir := t.TempDir()
  14. p := filepath.Join(dir, "hthd.yaml")
  15. if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
  16. t.Fatal(err)
  17. }
  18. return p
  19. }
  20. func baseYAML(profilePath string) string {
  21. return `
  22. api_version: hth.api.v1
  23. device_id: prod2u-0
  24. backend: mock
  25. profile: ` + profilePath + `
  26. runtime_root: /tmp/hth-cfgtest
  27. sockets:
  28. observe: /tmp/hth-cfgtest/observe/observe.sock
  29. control: /tmp/hth-cfgtest/control/control.sock
  30. observe_perm: "0660"
  31. control_perm: "0660"
  32. poll:
  33. temperature_ms: 200
  34. motor_status_ms: 100
  35. watchdog:
  36. heartbeat_period_ms: 5000
  37. lease:
  38. min_ttl_ms: 1000
  39. max_ttl_ms: 30000
  40. safety:
  41. fan: 10
  42. ptc: 0
  43. door_close_percent: 0
  44. ring_buffer_size: 64
  45. grpc:
  46. reflection: true
  47. mock:
  48. seed: 1
  49. startup_complete: true
  50. first_order_thermal: true
  51. `
  52. }
  53. func TestLoadAndValidate(t *testing.T) {
  54. prof := testutil.ProfilePath(t)
  55. path := writeYAML(t, baseYAML(prof))
  56. c, err := config.Load(path)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. p, err := profile.Load(c.Profile)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if err := c.Validate(p); err != nil {
  65. t.Fatal(err)
  66. }
  67. }
  68. func TestExtraYAMLDocumentRejected(t *testing.T) {
  69. prof := testutil.ProfilePath(t)
  70. path := writeYAML(t, baseYAML(prof)+"\n---\napi_version: extra\n")
  71. if _, err := config.Load(path); err == nil {
  72. t.Fatal("second YAML document must be rejected")
  73. }
  74. }
  75. func TestUnknownFieldError(t *testing.T) {
  76. prof := testutil.ProfilePath(t)
  77. path := writeYAML(t, baseYAML(prof)+"\nnot_a_field: 1\n")
  78. if _, err := config.Load(path); err == nil {
  79. t.Fatal("expected unknown field error")
  80. }
  81. }
  82. func TestSerialMissingSafetyRejected(t *testing.T) {
  83. prof := testutil.ProfilePath(t)
  84. body := `
  85. api_version: hth.api.v1
  86. device_id: prod2u-0
  87. backend: serial
  88. profile: ` + prof + `
  89. serial:
  90. path: /dev/hth/controller0
  91. runtime_root: /tmp/hth-cfgtest
  92. sockets:
  93. observe: /tmp/hth-cfgtest/observe/a.sock
  94. control: /tmp/hth-cfgtest/control/b.sock
  95. poll:
  96. temperature_ms: 200
  97. motor_status_ms: 100
  98. watchdog:
  99. heartbeat_period_ms: 5000
  100. lease:
  101. min_ttl_ms: 1000
  102. max_ttl_ms: 30000
  103. `
  104. path := writeYAML(t, body)
  105. c, err := config.Load(path)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. p, err := profile.Load(c.Profile)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. if err := c.Validate(p); err == nil {
  114. t.Fatal("serial mode must require explicit safety values")
  115. }
  116. }
  117. func TestHeartbeatMustBeBelowFirmwareTimeout(t *testing.T) {
  118. prof := testutil.ProfilePath(t)
  119. path := writeYAML(t, `
  120. api_version: hth.api.v1
  121. device_id: prod2u-0
  122. backend: mock
  123. profile: `+prof+`
  124. runtime_root: /tmp/hth-cfgtest
  125. sockets:
  126. observe: /tmp/hth-cfgtest/observe/a.sock
  127. control: /tmp/hth-cfgtest/control/b.sock
  128. poll:
  129. temperature_ms: 200
  130. motor_status_ms: 100
  131. watchdog:
  132. heartbeat_period_ms: 30000
  133. lease:
  134. min_ttl_ms: 1000
  135. max_ttl_ms: 30000
  136. safety:
  137. fan: 10
  138. ptc: 0
  139. door_close_percent: 0
  140. `)
  141. c, err := config.Load(path)
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. p, err := profile.Load(c.Profile)
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. if err := c.Validate(p); err == nil {
  150. t.Fatal("heartbeat == firmware timeout must fail")
  151. }
  152. }
  153. func TestSamplePeriodMin(t *testing.T) {
  154. prof := testutil.ProfilePath(t)
  155. path := writeYAML(t, `
  156. api_version: hth.api.v1
  157. device_id: prod2u-0
  158. backend: mock
  159. profile: `+prof+`
  160. runtime_root: /tmp/hth-cfgtest
  161. sockets:
  162. observe: /tmp/hth-cfgtest/observe/a.sock
  163. control: /tmp/hth-cfgtest/control/b.sock
  164. poll:
  165. temperature_ms: 1
  166. motor_status_ms: 100
  167. watchdog:
  168. heartbeat_period_ms: 5000
  169. lease:
  170. min_ttl_ms: 1000
  171. max_ttl_ms: 30000
  172. safety:
  173. fan: 10
  174. ptc: 0
  175. door_close_percent: 0
  176. `)
  177. c, err := config.Load(path)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. p, err := profile.Load(c.Profile)
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. if err := c.Validate(p); err == nil {
  186. t.Fatal("expected sample period too small")
  187. }
  188. }
  189. func TestGetTempTimeoutBelowScanRejected(t *testing.T) {
  190. prof := testutil.ProfilePath(t)
  191. path := writeYAML(t, `
  192. api_version: hth.api.v1
  193. device_id: prod2u-0
  194. backend: mock
  195. profile: `+prof+`
  196. runtime_root: /tmp/hth-cfgtest
  197. sockets:
  198. observe: /tmp/hth-cfgtest/observe/a.sock
  199. control: /tmp/hth-cfgtest/control/b.sock
  200. serial:
  201. get_temp_timeout_ms: 100
  202. poll:
  203. temperature_ms: 200
  204. motor_status_ms: 100
  205. watchdog:
  206. heartbeat_period_ms: 5000
  207. lease:
  208. min_ttl_ms: 1000
  209. max_ttl_ms: 30000
  210. safety:
  211. fan: 10
  212. ptc: 0
  213. door_close_percent: 0
  214. `)
  215. c, err := config.Load(path)
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. p, err := profile.Load(c.Profile)
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. if err := c.Validate(p); err == nil {
  224. t.Fatal("GET_TEMPERATURE timeout below scan busy must fail")
  225. }
  226. }
  227. func TestGetTempTimeoutCoversScan(t *testing.T) {
  228. prof := testutil.ProfilePath(t)
  229. path := writeYAML(t, baseYAML(prof))
  230. c, err := config.Load(path)
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. p, err := profile.Load(c.Profile)
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. if err := c.Validate(p); err != nil {
  239. t.Fatal(err)
  240. }
  241. got := c.GetTempTimeout(p)
  242. min := time.Duration(p.Timeouts.TemperatureScanBusyMS)*time.Millisecond + 300*time.Millisecond
  243. if got < min {
  244. t.Fatalf("derived GET timeout %s < min %s", got, min)
  245. }
  246. }
  247. func FuzzLoadYAML(f *testing.F) {
  248. f.Add([]byte("api_version: hth.api.v1\n"))
  249. f.Add([]byte("{}\n"))
  250. f.Add([]byte("a: 1\n---\nb: 2\n"))
  251. f.Fuzz(func(t *testing.T, data []byte) {
  252. p := filepath.Join(t.TempDir(), "c.yaml")
  253. if err := os.WriteFile(p, data, 0o644); err != nil {
  254. t.Fatal(err)
  255. }
  256. _, _ = config.Load(p)
  257. })
  258. }
  259. func FuzzSocketPaths(f *testing.F) {
  260. f.Add("/tmp/hth-cfgtest", "/tmp/hth-cfgtest/observe/observe.sock", "/tmp/hth-cfgtest/control/control.sock")
  261. f.Add("/run/hth", "/etc/passwd", "/run/hth/control.sock")
  262. f.Add("..", "../o.sock", "../c.sock")
  263. f.Add("/tmp/hth-cfgtest", "/tmp/hth-cfgtest/o.sock", "/tmp/hth-cfgtest/o.sock")
  264. f.Fuzz(func(t *testing.T, root, obs, ctl string) {
  265. if len(root) > 512 || len(obs) > 512 || len(ctl) > 512 {
  266. return
  267. }
  268. fan, ptc, door := 10, 0, 0
  269. c := &config.Config{
  270. APIVersion: "hth.api.v1",
  271. DeviceID: "prod2u-0",
  272. Backend: config.BackendMock,
  273. Profile: "profile.json",
  274. RuntimeRoot: root,
  275. Sockets: config.Sockets{
  276. Observe: obs, Control: ctl,
  277. ObservePerm: "0660", ControlPerm: "0660",
  278. },
  279. Poll: config.Poll{TemperatureMS: 200, MotorStatusMS: 100},
  280. Watchdog: config.Watchdog{HeartbeatPeriodMS: 5000},
  281. Lease: config.Lease{MinTTLMS: 50, MaxTTLMS: 5000},
  282. Safety: config.Safety{Fan: &fan, PTC: &ptc, DoorClosePercent: &door},
  283. Mock: config.Mock{Seed: 1, StartupComplete: true},
  284. }
  285. _ = c.Validate(nil)
  286. })
  287. }