27 lines
663 B
Go
27 lines
663 B
Go
package protocol
|
|
|
|
import "testing"
|
|
|
|
func TestEncodeDecodeRoundTrip(t *testing.T) {
|
|
in := Frame{Version: 1, Command: CmdSendMessage, Flags: FlagCompress, RequestID: 9, SequenceID: 3, Body: []byte("hi")}
|
|
raw, err := Encode(in)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := Decode(raw, 65536)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out.Command != in.Command || out.RequestID != in.RequestID || string(out.Body) != "hi" {
|
|
t.Fatalf("round trip mismatch: %+v", out)
|
|
}
|
|
}
|
|
|
|
func TestDecodeRejectsBadMagic(t *testing.T) {
|
|
raw, _ := Encode(Frame{Body: []byte("x")})
|
|
raw[0] = 0
|
|
if _, err := Decode(raw, 65536); err == nil {
|
|
t.Fatal("expected invalid frame")
|
|
}
|
|
}
|