109 lines
2.0 KiB
Go
109 lines
2.0 KiB
Go
package protocol
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestUxSerializationRoundtrip(t *testing.T) {
|
|
w := NewUxWriter()
|
|
w.U8(42).
|
|
Bool(true).
|
|
I16(-1234).
|
|
U16(5678).
|
|
I32(-999999).
|
|
U32(12345678).
|
|
I64(-9876543210).
|
|
U64(18446744073709551615).
|
|
F32(3.14159).
|
|
F64(2.718281828459).
|
|
Int7(10).
|
|
Raw7(25).
|
|
UxString("Hello Ananta Go Protocol")
|
|
|
|
data := w.Bytes()
|
|
r := NewUxReader(data)
|
|
|
|
u8, _ := r.U8()
|
|
if u8 != 42 {
|
|
t.Errorf("U8 mismatch: %v", u8)
|
|
}
|
|
|
|
b, _ := r.Bool()
|
|
if !b {
|
|
t.Errorf("Bool mismatch: %v", b)
|
|
}
|
|
|
|
i16, _ := r.I16()
|
|
if i16 != -1234 {
|
|
t.Errorf("I16 mismatch: %v", i16)
|
|
}
|
|
|
|
u16, _ := r.U16()
|
|
if u16 != 5678 {
|
|
t.Errorf("U16 mismatch: %v", u16)
|
|
}
|
|
|
|
i32, _ := r.I32()
|
|
if i32 != -999999 {
|
|
t.Errorf("I32 mismatch: %v", i32)
|
|
}
|
|
|
|
u32, _ := r.U32()
|
|
if u32 != 12345678 {
|
|
t.Errorf("U32 mismatch: %v", u32)
|
|
}
|
|
|
|
i64, _ := r.I64()
|
|
if i64 != -9876543210 {
|
|
t.Errorf("I64 mismatch: %v", i64)
|
|
}
|
|
|
|
u64, _ := r.U64()
|
|
if u64 != 18446744073709551615 {
|
|
t.Errorf("U64 mismatch: %v", u64)
|
|
}
|
|
|
|
f32, _ := r.F32()
|
|
if f32 < 3.14 || f32 > 3.15 {
|
|
t.Errorf("F32 mismatch: %v", f32)
|
|
}
|
|
|
|
f64, _ := r.F64()
|
|
if f64 < 2.71 || f64 > 2.72 {
|
|
t.Errorf("F64 mismatch: %v", f64)
|
|
}
|
|
|
|
int7, _ := r.Int7()
|
|
if int7 != 10 {
|
|
t.Errorf("Int7 mismatch: got %v, want 10", int7)
|
|
}
|
|
|
|
raw7, _ := r.Raw7()
|
|
if raw7 != 25 {
|
|
t.Errorf("Raw7 mismatch: got %v, want 25", raw7)
|
|
}
|
|
|
|
str, _ := r.UxString()
|
|
if str != "Hello Ananta Go Protocol" {
|
|
t.Errorf("UxString mismatch: got %v", str)
|
|
}
|
|
}
|
|
|
|
func TestRpcPacketRoundtrip(t *testing.T) {
|
|
notify := BuildNotify(MethodCheckVersion, []byte{0x01, 0x02, 0x03})
|
|
p := ParseRpcPacket(notify)
|
|
if !p.IsNotify() || p.MethodID != MethodCheckVersion {
|
|
t.Errorf("Notify packet mismatch: %+v", p)
|
|
}
|
|
if !bytes.Equal(p.Body, []byte{0x01, 0x02, 0x03}) {
|
|
t.Errorf("Body mismatch: %v", p.Body)
|
|
}
|
|
|
|
ret := BuildReturn(MethodAskEnterWorld, 100, 0, []byte("OK"))
|
|
p2 := ParseRpcPacket(ret)
|
|
if !p2.IsReturn() || p2.MethodID != MethodAskEnterWorld || p2.InvokeID != 100 || p2.Error != 0 {
|
|
t.Errorf("Return packet mismatch: %+v", p2)
|
|
}
|
|
}
|