| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package protocol6400
- import (
- "encoding/hex"
- "errors"
- "testing"
- )
- func mustHex(t *testing.T, value string) []byte {
- t.Helper()
- out, err := hex.DecodeString(value)
- if err != nil {
- t.Fatal(err)
- }
- return out
- }
- func TestGeneratedGoldenFrames(t *testing.T) {
- cases := map[byte]string{0: GoldenSetTargetStop, 20: GoldenSetTarget20, 70: GoldenSetTarget70}
- for target, want := range cases {
- got, err := SetTargetRequest(target)
- if err != nil {
- t.Fatal(err)
- }
- if hex.EncodeToString(got) != want {
- t.Fatalf("target %d got %x want %s", target, got, want)
- }
- }
- if got := GetSnapshotRequest(); hex.EncodeToString(got) != GoldenGetSnapshot {
- t.Fatalf("GET got %x", got)
- }
- responses := map[byte]string{
- RspSetTargetAck: GoldenSetTargetAck,
- RspGetSnapshotAck: GoldenGetSnapshotAck,
- RspStimulusAck: GoldenStimulusAck,
- }
- for frameType, want := range responses {
- got, err := Encode(frameType, nil)
- if err != nil {
- t.Fatal(err)
- }
- if hex.EncodeToString(got) != want {
- t.Fatalf("response 0x%02x got %x want %s", frameType, got, want)
- }
- }
- }
- func TestTargetRangeAndCRC(t *testing.T) {
- for _, value := range []byte{1, 19, 71, 255} {
- if _, err := SetTargetRequest(value); !errors.Is(err, ErrTargetRange) {
- t.Fatalf("target %d error %v", value, err)
- }
- }
- raw := mustHex(t, GoldenSetTarget70)
- raw[len(raw)-1] ^= 1
- if _, err := Decode(raw); !errors.Is(err, ErrCRC) {
- t.Fatalf("CRC error %v", err)
- }
- }
- func TestProductionRejectsStimulus(t *testing.T) {
- if _, err := StimulusRequest(ProfileControl, Stimulus{DurationSeconds: 1}); !errors.Is(err, ErrNotSupported) {
- t.Fatalf("production stimulus error %v", err)
- }
- if _, err := StimulusRequest("identification", Stimulus{DurationSeconds: 1}); !errors.Is(err, ErrNotSupported) {
- t.Fatalf("legacy alias stimulus error %v", err)
- }
- if _, err := StimulusRequest("unknown", Stimulus{DurationSeconds: 1}); !errors.Is(err, ErrNotSupported) {
- t.Fatalf("unknown profile stimulus error %v", err)
- }
- value := Stimulus{DurationSeconds: 1}
- raw, err := StimulusRequest(ProfileIdentification, value)
- if err != nil {
- t.Fatal(err)
- }
- if hex.EncodeToString(raw) != GoldenStimulusAllZeroOneSecond {
- t.Fatalf("stimulus got %x", raw)
- }
- if _, err := StimulusRequest(ProfileSimDebug, value); err != nil {
- t.Fatalf("sim-debug stimulus error %v", err)
- }
- }
- func TestSnapshotACKAndDataTypesAreDistinct(t *testing.T) {
- if RspGetSnapshotAck != 0x82 || RspSnapshotData != 0x42 {
- t.Fatalf("GET response types ACK=0x%02x DATA=0x%02x", RspGetSnapshotAck, RspSnapshotData)
- }
- if _, err := Encode(RspGetSnapshotAck, make([]byte, SnapshotPayloadLen)); !errors.Is(err, ErrLength) {
- t.Fatalf("0x82 with snapshot payload error %v", err)
- }
- }
|