| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- package profile_test
- import (
- "bytes"
- "os"
- "os/exec"
- "path/filepath"
- "testing"
- "hth/internal/ids"
- "hth/internal/profile"
- "hth/internal/testutil"
- )
- func TestLoadProd2UProfile(t *testing.T) {
- p, err := profile.LoadStrict(testutil.ProfilePath(t))
- if err != nil {
- t.Fatal(err)
- }
- if p.ProfileID != "prod2u-normal-multiproject-115200-v1" {
- t.Fatalf("profile_id %s", p.ProfileID)
- }
- if p.LookupActuator(ids.DoorX) == nil || p.LookupSensor(ids.TempChannel(26)) == nil {
- t.Fatal("missing door/temp ids")
- }
- if p.Honesty.FirmwareVersion != "NOT_SUPPORTED" {
- t.Fatal("firmware must be NOT_SUPPORTED")
- }
- s := p.SafeSetAll()
- if s.Fan[0] != 10 || s.PTC[0] != 0 || s.DoorX != 0 || s.DoorY != 0 || s.DoorZ != 0 {
- t.Fatalf("safe set_all %+v", s)
- }
- if p.ProfileIdentityDigest() == "" || p.FileSHA256 == "" {
- t.Fatal("hashes")
- }
- }
- func TestLoadStrictUnknownField(t *testing.T) {
- dir := t.TempDir()
- path := filepath.Join(dir, "p.json")
- raw, err := os.ReadFile(testutil.ProfilePath(t))
- if err != nil {
- t.Fatal(err)
- }
- // Insert an unknown field after the opening brace.
- bad := []byte(`{"not_a_real_field": true,` + string(raw[1:]))
- if err := os.WriteFile(path, bad, 0o644); err != nil {
- t.Fatal(err)
- }
- if _, err := profile.LoadStrict(path); err == nil {
- t.Fatal("expected unknown field error")
- }
- }
- func TestGenProfileNoDrift(t *testing.T) {
- script := filepath.Join(testutil.HostRoot(t), "tools", "l0_contract_check.py")
- cmd := exec.Command(testutil.Python(t), script)
- out, err := cmd.CombinedOutput()
- if err != nil {
- t.Fatalf("l0_contract_check: %v\n%s", err, out)
- }
- if !bytes.Contains(out, []byte("L0_CONTRACT_CHECK_PASS")) {
- t.Fatalf("missing PASS marker:\n%s", out)
- }
- }
- func TestMissingSafetyNotesRejected(t *testing.T) {
- dir := t.TempDir()
- path := filepath.Join(dir, "p.json")
- raw := []byte(`{"schema":"hth-device-profile-v1","profile_id":"x","profile_revision":"1","safety":{"fan":10,"ptc":0,"door_close_percent":0}}`)
- if err := os.WriteFile(path, raw, 0o644); err != nil {
- t.Fatal(err)
- }
- if _, err := profile.Load(path); err == nil {
- t.Fatal("expected validation error")
- }
- }
- func TestTrailingJSONRejected(t *testing.T) {
- dir := t.TempDir()
- path := filepath.Join(dir, "p.json")
- raw, err := os.ReadFile(testutil.ProfilePath(t))
- if err != nil {
- t.Fatal(err)
- }
- if err := os.WriteFile(path, append(raw, []byte("\n{}")...), 0o644); err != nil {
- t.Fatal(err)
- }
- if _, err := profile.LoadStrict(path); err == nil {
- t.Fatal("expected trailing JSON error")
- }
- }
- func TestDuplicateJSONKeyRejected(t *testing.T) {
- dir := t.TempDir()
- path := filepath.Join(dir, "p.json")
- raw, err := os.ReadFile(testutil.ProfilePath(t))
- if err != nil {
- t.Fatal(err)
- }
- dup := bytes.Replace(raw, []byte(`"profile_id"`), []byte(`"profile_id"`), 1)
- // Force a duplicate key at the object root.
- bad := []byte(`{"schema":"hth-device-profile-v1","schema":"dup",` + string(raw[1:]))
- if err := os.WriteFile(path, bad, 0o644); err != nil {
- t.Fatal(err)
- }
- if _, err := profile.LoadStrict(path); err == nil {
- t.Fatal("duplicate JSON key must fail closed")
- }
- _ = dup
- }
- func TestDuplicateSetAllOffsetRejected(t *testing.T) {
- p, err := profile.LoadStrict(testutil.ProfilePath(t))
- if err != nil {
- t.Fatal(err)
- }
- if len(p.Actuators) < 2 {
- t.Fatal("need two actuators")
- }
- p.Actuators[1].SetAllOffset = p.Actuators[0].SetAllOffset
- if err := p.Validate(); err == nil {
- t.Fatal("duplicate SET_ALL offset must fail")
- }
- }
- func mutateProfile(t *testing.T, mut func([]byte) []byte) string {
- t.Helper()
- raw, err := os.ReadFile(testutil.ProfilePath(t))
- if err != nil {
- t.Fatal(err)
- }
- path := filepath.Join(t.TempDir(), "p.json")
- if err := os.WriteFile(path, mut(raw), 0o644); err != nil {
- t.Fatal(err)
- }
- return path
- }
- func TestProd2UMissingCRCNameRejected(t *testing.T) {
- path := mutateProfile(t, func(raw []byte) []byte {
- return bytes.Replace(raw, []byte(`"name": "CRC-16/CCITT-FALSE"`), []byte(`"name": ""`), 1)
- })
- if _, err := profile.LoadStrict(path); err == nil {
- t.Fatal("empty CRC name must fail Prod2U validator")
- }
- }
- func TestProd2UWrongPolynomialRejected(t *testing.T) {
- path := mutateProfile(t, func(raw []byte) []byte {
- return bytes.Replace(raw, []byte(`"polynomial": 4129`), []byte(`"polynomial": 1`), 1)
- })
- if _, err := profile.LoadStrict(path); err == nil {
- t.Fatal("wrong CRC polynomial must fail")
- }
- }
- func TestProd2UMissingCommandRejected(t *testing.T) {
- path := mutateProfile(t, func(raw []byte) []byte {
- return bytes.Replace(raw, []byte(`"set_all": 1,`), []byte(``), 1)
- })
- if _, err := profile.LoadStrict(path); err == nil {
- t.Fatal("missing SET_ALL command must fail")
- }
- }
- func TestProd2UDuplicateCommandTypeRejected(t *testing.T) {
- path := mutateProfile(t, func(raw []byte) []byte {
- return bytes.Replace(raw, []byte(`"get_temperature": 2,`), []byte(`"get_temperature": 1,`), 1)
- })
- if _, err := profile.LoadStrict(path); err == nil {
- t.Fatal("duplicate command type must fail")
- }
- }
- func TestProd2UMissingHeaderRejected(t *testing.T) {
- path := mutateProfile(t, func(raw []byte) []byte {
- return bytes.Replace(raw, []byte(`"header_hex": "A55A"`), []byte(`"header_hex": ""`), 1)
- })
- if _, err := profile.LoadStrict(path); err == nil {
- t.Fatal("missing header must fail")
- }
- }
- func TestProd2UWrongUARTRejected(t *testing.T) {
- path := mutateProfile(t, func(raw []byte) []byte {
- return bytes.Replace(raw, []byte(`"baud": 115200`), []byte(`"baud": 9600`), 1)
- })
- if _, err := profile.LoadStrict(path); err == nil {
- t.Fatal("wrong UART baud must fail")
- }
- }
|