2023-11-26 05:11:19 +00:00
|
|
|
package protocol_test
|
2023-11-23 00:18:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2023-11-26 05:11:19 +00:00
|
|
|
|
|
|
|
"github.com/CPunch/gopenfusion/internal/protocol"
|
2023-11-23 00:18:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestPacketData struct {
|
|
|
|
A int32
|
|
|
|
B int32
|
|
|
|
UTF16Str string `size:"32"`
|
|
|
|
Pad int16 `pad:"2"`
|
|
|
|
C int32
|
|
|
|
}
|
|
|
|
|
2023-11-23 02:04:53 +00:00
|
|
|
// this is the data we expect to get from encoding the above struct with:
|
|
|
|
//
|
|
|
|
// Encode(TestPacketData{
|
|
|
|
// A: 1,
|
|
|
|
// B: 2,
|
|
|
|
// UTF16Str: "hello world",
|
|
|
|
// C: 3,
|
|
|
|
// })
|
2023-11-26 05:11:19 +00:00
|
|
|
var testData = [...]byte{
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
|
|
|
|
0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00,
|
|
|
|
0x6f, 0x00, 0x20, 0x00, 0x77, 0x00, 0x6f, 0x00,
|
|
|
|
0x72, 0x00, 0x6c, 0x00, 0x64, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
|
|
|
|
}
|
2023-11-23 02:04:53 +00:00
|
|
|
|
|
|
|
func TestPacketEncodeDecode(t *testing.T) {
|
2023-11-26 05:11:19 +00:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
pkt := protocol.NewPacket(buf)
|
|
|
|
|
2023-11-23 00:18:22 +00:00
|
|
|
if err := pkt.Encode(TestPacketData{
|
|
|
|
A: 1,
|
|
|
|
B: 2,
|
|
|
|
UTF16Str: "hello world",
|
|
|
|
C: 3,
|
|
|
|
}); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2023-11-26 05:11:19 +00:00
|
|
|
if !bytes.Equal(buf.Bytes(), testData[:]) {
|
2023-11-23 02:04:53 +00:00
|
|
|
t.Error("packet data does not match!")
|
|
|
|
}
|
|
|
|
|
2023-11-23 00:18:22 +00:00
|
|
|
var test TestPacketData
|
|
|
|
if err := pkt.Decode(&test); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.A != 1 || test.B != 2 || test.C != 3 || test.UTF16Str != "hello world" {
|
2023-11-23 02:04:53 +00:00
|
|
|
t.Error("decoded packet data does not match!")
|
2023-11-23 00:18:22 +00:00
|
|
|
}
|
|
|
|
}
|