hopp/metadapta_test.go

69 lines
1.8 KiB
Go

package hopp
import "io"
import "bytes"
import "errors"
import "slices"
import "testing"
func TestEncodeMessageA(test *testing.T) {
buffer := new(bytes.Buffer)
payload := []byte { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }
err := encodeMessageA(buffer, 0x5800FEABC3104F04, 0x6B12, payload)
correct := []byte {
0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04,
0x6B, 0x12,
0x00, 0x06,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
}
if err != nil {
test.Fatal(err)
}
if got, correct := buffer.Bytes(), correct; !slices.Equal(got, correct) {
test.Fatalf("not equal: %v %v", got, correct)
}
}
func TestEncodeMessageAErr(test *testing.T) {
buffer := new(bytes.Buffer)
payload := make([]byte, 0x10000)
err := encodeMessageA(buffer, 0x5800FEABC3104F04, 0x6B12, payload)
if !errors.Is(err, ErrPayloadTooLarge) {
test.Fatalf("wrong error: %v", err)
}
}
func TestDecodeMessageA(test *testing.T) {
transID, method, payload, err := decodeMessageA(bytes.NewReader([]byte {
0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04,
0x6B, 0x12,
0x00, 0x06,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
}))
if err != nil {
test.Fatal(err)
}
if got, correct := transID, int64(0x5800FEABC3104F04); got != correct {
test.Fatalf("not equal: %v %v", got, correct)
}
if got, correct := method, uint16(0x6B12); got != correct {
test.Fatalf("not equal: %v %v", got, correct)
}
correctPayload := []byte { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }
if got, correct := payload, correctPayload; !slices.Equal(got, correct) {
test.Fatalf("not equal: %v %v", got, correct)
}
}
func TestDecodeMessageAErr(test *testing.T) {
_, _, _, err := decodeMessageA(bytes.NewReader([]byte {
0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04,
0x6B, 0x12,
0x01, 0x06,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
}))
if !errors.Is(err, io.ErrUnexpectedEOF) {
test.Fatalf("wrong error: %v", err)
}
}