profile_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package profile_test
  2. import (
  3. "bytes"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "testing"
  8. "hth/internal/ids"
  9. "hth/internal/profile"
  10. "hth/internal/testutil"
  11. )
  12. func TestLoadProd2UProfile(t *testing.T) {
  13. p, err := profile.LoadStrict(testutil.ProfilePath(t))
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. if p.ProfileID != "prod2u-normal-multiproject-115200-v1" {
  18. t.Fatalf("profile_id %s", p.ProfileID)
  19. }
  20. if p.LookupActuator(ids.DoorX) == nil || p.LookupSensor(ids.TempChannel(26)) == nil {
  21. t.Fatal("missing door/temp ids")
  22. }
  23. if p.Honesty.FirmwareVersion != "NOT_SUPPORTED" {
  24. t.Fatal("firmware must be NOT_SUPPORTED")
  25. }
  26. s := p.SafeSetAll()
  27. if s.Fan[0] != 10 || s.PTC[0] != 0 || s.DoorX != 0 || s.DoorY != 0 || s.DoorZ != 0 {
  28. t.Fatalf("safe set_all %+v", s)
  29. }
  30. if p.ProfileIdentityDigest() == "" || p.FileSHA256 == "" {
  31. t.Fatal("hashes")
  32. }
  33. }
  34. func TestLoadStrictUnknownField(t *testing.T) {
  35. dir := t.TempDir()
  36. path := filepath.Join(dir, "p.json")
  37. raw, err := os.ReadFile(testutil.ProfilePath(t))
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. // Insert an unknown field after the opening brace.
  42. bad := []byte(`{"not_a_real_field": true,` + string(raw[1:]))
  43. if err := os.WriteFile(path, bad, 0o644); err != nil {
  44. t.Fatal(err)
  45. }
  46. if _, err := profile.LoadStrict(path); err == nil {
  47. t.Fatal("expected unknown field error")
  48. }
  49. }
  50. func TestGenProfileNoDrift(t *testing.T) {
  51. script := filepath.Join(testutil.HostRoot(t), "tools", "l0_contract_check.py")
  52. cmd := exec.Command(testutil.Python(t), script)
  53. out, err := cmd.CombinedOutput()
  54. if err != nil {
  55. t.Fatalf("l0_contract_check: %v\n%s", err, out)
  56. }
  57. if bytes.Contains(out, []byte("L0_FAIL")) {
  58. t.Fatalf("l0 failed:\n%s", out)
  59. }
  60. // Offline L0 is allowed to be PARTIAL when buf is not on PATH; buf lint/breaking
  61. // are separate recorded gates. Unit tests must not fail-closed on that.
  62. if bytes.Contains(out, []byte("L0_CONTRACT_CHECK_PASS")) ||
  63. bytes.Contains(out, []byte("L0_OFFLINE_EVIDENCE_CHECK_PARTIAL")) {
  64. return
  65. }
  66. t.Fatalf("missing PASS marker:\n%s", out)
  67. }
  68. func TestMissingSafetyNotesRejected(t *testing.T) {
  69. dir := t.TempDir()
  70. path := filepath.Join(dir, "p.json")
  71. raw := []byte(`{"schema":"hth-device-profile-v1","profile_id":"x","profile_revision":"1","safety":{"fan":10,"ptc":0,"door_close_percent":0}}`)
  72. if err := os.WriteFile(path, raw, 0o644); err != nil {
  73. t.Fatal(err)
  74. }
  75. if _, err := profile.Load(path); err == nil {
  76. t.Fatal("expected validation error")
  77. }
  78. }
  79. func TestTrailingJSONRejected(t *testing.T) {
  80. dir := t.TempDir()
  81. path := filepath.Join(dir, "p.json")
  82. raw, err := os.ReadFile(testutil.ProfilePath(t))
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. if err := os.WriteFile(path, append(raw, []byte("\n{}")...), 0o644); err != nil {
  87. t.Fatal(err)
  88. }
  89. if _, err := profile.LoadStrict(path); err == nil {
  90. t.Fatal("expected trailing JSON error")
  91. }
  92. }
  93. func TestDuplicateJSONKeyRejected(t *testing.T) {
  94. dir := t.TempDir()
  95. path := filepath.Join(dir, "p.json")
  96. raw, err := os.ReadFile(testutil.ProfilePath(t))
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. dup := bytes.Replace(raw, []byte(`"profile_id"`), []byte(`"profile_id"`), 1)
  101. // Force a duplicate key at the object root.
  102. bad := []byte(`{"schema":"hth-device-profile-v1","schema":"dup",` + string(raw[1:]))
  103. if err := os.WriteFile(path, bad, 0o644); err != nil {
  104. t.Fatal(err)
  105. }
  106. if _, err := profile.LoadStrict(path); err == nil {
  107. t.Fatal("duplicate JSON key must fail closed")
  108. }
  109. _ = dup
  110. }
  111. func TestDuplicateSetAllOffsetRejected(t *testing.T) {
  112. p, err := profile.LoadStrict(testutil.ProfilePath(t))
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. if len(p.Actuators) < 2 {
  117. t.Fatal("need two actuators")
  118. }
  119. p.Actuators[1].SetAllOffset = p.Actuators[0].SetAllOffset
  120. if err := p.Validate(); err == nil {
  121. t.Fatal("duplicate SET_ALL offset must fail")
  122. }
  123. }
  124. func mutateProfile(t *testing.T, mut func([]byte) []byte) string {
  125. t.Helper()
  126. raw, err := os.ReadFile(testutil.ProfilePath(t))
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. path := filepath.Join(t.TempDir(), "p.json")
  131. if err := os.WriteFile(path, mut(raw), 0o644); err != nil {
  132. t.Fatal(err)
  133. }
  134. return path
  135. }
  136. func TestProd2UMissingCRCNameRejected(t *testing.T) {
  137. path := mutateProfile(t, func(raw []byte) []byte {
  138. return bytes.Replace(raw, []byte(`"name": "CRC-16/CCITT-FALSE"`), []byte(`"name": ""`), 1)
  139. })
  140. if _, err := profile.LoadStrict(path); err == nil {
  141. t.Fatal("empty CRC name must fail Prod2U validator")
  142. }
  143. }
  144. func TestProd2UWrongPolynomialRejected(t *testing.T) {
  145. path := mutateProfile(t, func(raw []byte) []byte {
  146. return bytes.Replace(raw, []byte(`"polynomial": 4129`), []byte(`"polynomial": 1`), 1)
  147. })
  148. if _, err := profile.LoadStrict(path); err == nil {
  149. t.Fatal("wrong CRC polynomial must fail")
  150. }
  151. }
  152. func TestProd2UMissingCommandRejected(t *testing.T) {
  153. path := mutateProfile(t, func(raw []byte) []byte {
  154. return bytes.Replace(raw, []byte(`"set_all": 1,`), []byte(``), 1)
  155. })
  156. if _, err := profile.LoadStrict(path); err == nil {
  157. t.Fatal("missing SET_ALL command must fail")
  158. }
  159. }
  160. func TestProd2UDuplicateCommandTypeRejected(t *testing.T) {
  161. path := mutateProfile(t, func(raw []byte) []byte {
  162. return bytes.Replace(raw, []byte(`"get_temperature": 2,`), []byte(`"get_temperature": 1,`), 1)
  163. })
  164. if _, err := profile.LoadStrict(path); err == nil {
  165. t.Fatal("duplicate command type must fail")
  166. }
  167. }
  168. func TestProd2UMissingHeaderRejected(t *testing.T) {
  169. path := mutateProfile(t, func(raw []byte) []byte {
  170. return bytes.Replace(raw, []byte(`"header_hex": "A55A"`), []byte(`"header_hex": ""`), 1)
  171. })
  172. if _, err := profile.LoadStrict(path); err == nil {
  173. t.Fatal("missing header must fail")
  174. }
  175. }
  176. func TestProd2UWrongUARTRejected(t *testing.T) {
  177. path := mutateProfile(t, func(raw []byte) []byte {
  178. return bytes.Replace(raw, []byte(`"baud": 115200`), []byte(`"baud": 9600`), 1)
  179. })
  180. if _, err := profile.LoadStrict(path); err == nil {
  181. t.Fatal("wrong UART baud must fail")
  182. }
  183. }