package nvda2cthulhu import ( "bytes" "testing" "github.com/gorilla/websocket" ) func TestDecodeMessagePackRequests(t *testing.T) { tests := []struct { name string payload []byte want bridgeRequest }{ {name: "fix string", payload: []byte{0xa4, 'G', 'a', 'm', 'e'}, want: bridgeRequest{kind: "SpeakText", text: "Game"}}, {name: "map with string", payload: []byte{0x81, 0xa9, 'S', 'p', 'e', 'a', 'k', 'T', 'e', 'x', 't', 0xa4, 'G', 'a', 'm', 'e'}, want: bridgeRequest{kind: "SpeakText", text: "Game"}}, {name: "array cancel", payload: []byte{0x91, 0xac, 'C', 'a', 'n', 'c', 'e', 'l', 'S', 'p', 'e', 'e', 'c', 'h'}, want: bridgeRequest{kind: "CancelSpeech"}}, {name: "bin value", payload: []byte{0x81, 0xab, 'B', 'r', 'a', 'i', 'l', 'l', 'e', 'T', 'e', 'x', 't', 0xc4, 0x02, 'O', 'K'}, want: bridgeRequest{kind: "BrailleText", text: "OK"}}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { got, err := decodeRequest(websocket.BinaryMessage, test.payload) if err != nil { t.Fatalf("decodeRequest() error = %v", err) } if got.kind != test.want.kind || got.text != test.want.text { t.Fatalf("decodeRequest() = %+v, want %+v", got, test.want) } }) } } func TestDecodeMessagePackRejectsMalformedContainers(t *testing.T) { for _, payload := range [][]byte{ {0x81, 0x90, 0xc0}, // An array key cannot be inserted into a Go map. append(append([]byte{}, bytes.Repeat([]byte{0x91}, 66)...), 0xc0), {0xa2, 'O', 'K', 0xc0}, // One WebSocket frame contains exactly one request. } { if _, err := decodeRequest(websocket.BinaryMessage, payload); err == nil { t.Fatalf("accepted malformed MessagePack payload %x", payload) } } }