profile_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_CONTRACT_CHECK_PASS")) {
  58. t.Fatalf("missing PASS marker:\n%s", out)
  59. }
  60. }
  61. func TestMissingSafetyNotesRejected(t *testing.T) {
  62. dir := t.TempDir()
  63. path := filepath.Join(dir, "p.json")
  64. raw := []byte(`{"schema":"hth-device-profile-v1","profile_id":"x","profile_revision":"1","safety":{"fan":10,"ptc":0,"door_close_percent":0}}`)
  65. if err := os.WriteFile(path, raw, 0o644); err != nil {
  66. t.Fatal(err)
  67. }
  68. if _, err := profile.Load(path); err == nil {
  69. t.Fatal("expected validation error")
  70. }
  71. }
  72. func TestTrailingJSONRejected(t *testing.T) {
  73. dir := t.TempDir()
  74. path := filepath.Join(dir, "p.json")
  75. raw, err := os.ReadFile(testutil.ProfilePath(t))
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. if err := os.WriteFile(path, append(raw, []byte("\n{}")...), 0o644); err != nil {
  80. t.Fatal(err)
  81. }
  82. if _, err := profile.LoadStrict(path); err == nil {
  83. t.Fatal("expected trailing JSON error")
  84. }
  85. }
  86. func TestDuplicateJSONKeyRejected(t *testing.T) {
  87. dir := t.TempDir()
  88. path := filepath.Join(dir, "p.json")
  89. raw, err := os.ReadFile(testutil.ProfilePath(t))
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. dup := bytes.Replace(raw, []byte(`"profile_id"`), []byte(`"profile_id"`), 1)
  94. // Force a duplicate key at the object root.
  95. bad := []byte(`{"schema":"hth-device-profile-v1","schema":"dup",` + string(raw[1:]))
  96. if err := os.WriteFile(path, bad, 0o644); err != nil {
  97. t.Fatal(err)
  98. }
  99. if _, err := profile.LoadStrict(path); err == nil {
  100. t.Fatal("duplicate JSON key must fail closed")
  101. }
  102. _ = dup
  103. }
  104. func TestDuplicateSetAllOffsetRejected(t *testing.T) {
  105. p, err := profile.LoadStrict(testutil.ProfilePath(t))
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. if len(p.Actuators) < 2 {
  110. t.Fatal("need two actuators")
  111. }
  112. p.Actuators[1].SetAllOffset = p.Actuators[0].SetAllOffset
  113. if err := p.Validate(); err == nil {
  114. t.Fatal("duplicate SET_ALL offset must fail")
  115. }
  116. }
  117. func mutateProfile(t *testing.T, mut func([]byte) []byte) string {
  118. t.Helper()
  119. raw, err := os.ReadFile(testutil.ProfilePath(t))
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. path := filepath.Join(t.TempDir(), "p.json")
  124. if err := os.WriteFile(path, mut(raw), 0o644); err != nil {
  125. t.Fatal(err)
  126. }
  127. return path
  128. }
  129. func TestProd2UMissingCRCNameRejected(t *testing.T) {
  130. path := mutateProfile(t, func(raw []byte) []byte {
  131. return bytes.Replace(raw, []byte(`"name": "CRC-16/CCITT-FALSE"`), []byte(`"name": ""`), 1)
  132. })
  133. if _, err := profile.LoadStrict(path); err == nil {
  134. t.Fatal("empty CRC name must fail Prod2U validator")
  135. }
  136. }
  137. func TestProd2UWrongPolynomialRejected(t *testing.T) {
  138. path := mutateProfile(t, func(raw []byte) []byte {
  139. return bytes.Replace(raw, []byte(`"polynomial": 4129`), []byte(`"polynomial": 1`), 1)
  140. })
  141. if _, err := profile.LoadStrict(path); err == nil {
  142. t.Fatal("wrong CRC polynomial must fail")
  143. }
  144. }
  145. func TestProd2UMissingCommandRejected(t *testing.T) {
  146. path := mutateProfile(t, func(raw []byte) []byte {
  147. return bytes.Replace(raw, []byte(`"set_all": 1,`), []byte(``), 1)
  148. })
  149. if _, err := profile.LoadStrict(path); err == nil {
  150. t.Fatal("missing SET_ALL command must fail")
  151. }
  152. }
  153. func TestProd2UDuplicateCommandTypeRejected(t *testing.T) {
  154. path := mutateProfile(t, func(raw []byte) []byte {
  155. return bytes.Replace(raw, []byte(`"get_temperature": 2,`), []byte(`"get_temperature": 1,`), 1)
  156. })
  157. if _, err := profile.LoadStrict(path); err == nil {
  158. t.Fatal("duplicate command type must fail")
  159. }
  160. }
  161. func TestProd2UMissingHeaderRejected(t *testing.T) {
  162. path := mutateProfile(t, func(raw []byte) []byte {
  163. return bytes.Replace(raw, []byte(`"header_hex": "A55A"`), []byte(`"header_hex": ""`), 1)
  164. })
  165. if _, err := profile.LoadStrict(path); err == nil {
  166. t.Fatal("missing header must fail")
  167. }
  168. }
  169. func TestProd2UWrongUARTRejected(t *testing.T) {
  170. path := mutateProfile(t, func(raw []byte) []byte {
  171. return bytes.Replace(raw, []byte(`"baud": 115200`), []byte(`"baud": 9600`), 1)
  172. })
  173. if _, err := profile.LoadStrict(path); err == nil {
  174. t.Fatal("wrong UART baud must fail")
  175. }
  176. }