frame_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package protocol6400
  2. import (
  3. "encoding/hex"
  4. "errors"
  5. "testing"
  6. )
  7. func mustHex(t *testing.T, value string) []byte {
  8. t.Helper()
  9. out, err := hex.DecodeString(value)
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. return out
  14. }
  15. func TestGeneratedGoldenFrames(t *testing.T) {
  16. cases := map[byte]string{0: GoldenSetTargetStop, 20: GoldenSetTarget20, 70: GoldenSetTarget70}
  17. for target, want := range cases {
  18. got, err := SetTargetRequest(target)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. if hex.EncodeToString(got) != want {
  23. t.Fatalf("target %d got %x want %s", target, got, want)
  24. }
  25. }
  26. if got := GetSnapshotRequest(); hex.EncodeToString(got) != GoldenGetSnapshot {
  27. t.Fatalf("GET got %x", got)
  28. }
  29. responses := map[byte]string{
  30. RspSetTargetAck: GoldenSetTargetAck,
  31. RspGetSnapshotAck: GoldenGetSnapshotAck,
  32. RspStimulusAck: GoldenStimulusAck,
  33. }
  34. for frameType, want := range responses {
  35. got, err := Encode(frameType, nil)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. if hex.EncodeToString(got) != want {
  40. t.Fatalf("response 0x%02x got %x want %s", frameType, got, want)
  41. }
  42. }
  43. }
  44. func TestTargetRangeAndCRC(t *testing.T) {
  45. for _, value := range []byte{1, 19, 71, 255} {
  46. if _, err := SetTargetRequest(value); !errors.Is(err, ErrTargetRange) {
  47. t.Fatalf("target %d error %v", value, err)
  48. }
  49. }
  50. raw := mustHex(t, GoldenSetTarget70)
  51. raw[len(raw)-1] ^= 1
  52. if _, err := Decode(raw); !errors.Is(err, ErrCRC) {
  53. t.Fatalf("CRC error %v", err)
  54. }
  55. }
  56. func TestProductionRejectsStimulus(t *testing.T) {
  57. if _, err := StimulusRequest(ProfileControl, Stimulus{DurationSeconds: 1}); !errors.Is(err, ErrNotSupported) {
  58. t.Fatalf("production stimulus error %v", err)
  59. }
  60. if _, err := StimulusRequest("identification", Stimulus{DurationSeconds: 1}); !errors.Is(err, ErrNotSupported) {
  61. t.Fatalf("legacy alias stimulus error %v", err)
  62. }
  63. if _, err := StimulusRequest("unknown", Stimulus{DurationSeconds: 1}); !errors.Is(err, ErrNotSupported) {
  64. t.Fatalf("unknown profile stimulus error %v", err)
  65. }
  66. value := Stimulus{DurationSeconds: 1}
  67. raw, err := StimulusRequest(ProfileIdentification, value)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if hex.EncodeToString(raw) != GoldenStimulusAllZeroOneSecond {
  72. t.Fatalf("stimulus got %x", raw)
  73. }
  74. if _, err := StimulusRequest(ProfileSimDebug, value); err != nil {
  75. t.Fatalf("sim-debug stimulus error %v", err)
  76. }
  77. }
  78. func TestSnapshotACKAndDataTypesAreDistinct(t *testing.T) {
  79. if RspGetSnapshotAck != 0x82 || RspSnapshotData != 0x42 {
  80. t.Fatalf("GET response types ACK=0x%02x DATA=0x%02x", RspGetSnapshotAck, RspSnapshotData)
  81. }
  82. if _, err := Encode(RspGetSnapshotAck, make([]byte, SnapshotPayloadLen)); !errors.Is(err, ErrLength) {
  83. t.Fatalf("0x82 with snapshot payload error %v", err)
  84. }
  85. }