329 lines
12 KiB
Go
329 lines
12 KiB
Go
|
package wireproto
|
||
|
|
||
|
import (
|
||
|
`bytes`
|
||
|
`encoding/hex`
|
||
|
`encoding/json`
|
||
|
`fmt`
|
||
|
"testing"
|
||
|
|
||
|
// `github.com/davecgh/go-spew/spew`
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
testSimpleReqEncoded []byte = []byte{
|
||
|
0x01, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00,
|
||
|
0x00, 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00,
|
||
|
0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x31, 0x76, 0x61, 0x6c, 0x75,
|
||
|
0x65, 0x31, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x32,
|
||
|
0x76, 0x61, 0x6c, 0x75, 0x65, 0x32, 0x03, 0x04,
|
||
|
}
|
||
|
testSimpleRespEncoded []byte = []byte{
|
||
|
0x06, 0x1b, 0xce, 0xfd, 0x07, 0x20, 0x01, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x01,
|
||
|
0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x01,
|
||
|
0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x10,
|
||
|
0x64, 0x61, 0x74, 0x61, 0x31, 0x3c, 0x61, 0x72, 0x62, 0x69, 0x74, 0x72, 0x61, 0x72, 0x79, 0x20,
|
||
|
0x64, 0x61, 0x74, 0x61, 0x3e, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
|
||
|
0x06, 0x00, 0x00, 0x00, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x31, 0x76, 0x61, 0x6c, 0x75, 0x65,
|
||
|
0x31, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x32, 0x76,
|
||
|
0x61, 0x6c, 0x75, 0x65, 0x32, 0x03, 0x04,
|
||
|
}
|
||
|
testMultiReqEncoded []byte = []byte{
|
||
|
0x01, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00,
|
||
|
0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
|
||
|
0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x41, 0x31, 0x41, 0x76, 0x61,
|
||
|
0x6c, 0x75, 0x65, 0x41, 0x31, 0x41, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69,
|
||
|
0x65, 0x6c, 0x64, 0x41, 0x31, 0x42, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x41, 0x31, 0x42, 0x00, 0x00,
|
||
|
0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69,
|
||
|
0x65, 0x6c, 0x64, 0x41, 0x32, 0x41, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x41, 0x32, 0x41, 0x00, 0x00,
|
||
|
0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x41, 0x32, 0x42, 0x76, 0x61,
|
||
|
0x6c, 0x75, 0x65, 0x41, 0x32, 0x42, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00,
|
||
|
0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69,
|
||
|
0x65, 0x6c, 0x64, 0x42, 0x31, 0x41, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x31, 0x41, 0x00, 0x00,
|
||
|
0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x42, 0x31, 0x42, 0x76, 0x61,
|
||
|
0x6c, 0x75, 0x65, 0x42, 0x31, 0x42, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
|
||
|
0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x42, 0x32, 0x41, 0x76, 0x61,
|
||
|
0x6c, 0x75, 0x65, 0x42, 0x32, 0x41, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69,
|
||
|
0x65, 0x6c, 0x64, 0x42, 0x32, 0x42, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x32, 0x42, 0x03, 0x04,
|
||
|
}
|
||
|
testMultiRespEncoded []byte = []byte{
|
||
|
0x06, 0x1b, 0xae, 0x88, 0xbe, 0xd2, 0x01, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x02,
|
||
|
0x00, 0x00, 0x01, 0x98, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01,
|
||
|
0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10,
|
||
|
0x64, 0x61, 0x74, 0x61, 0x41, 0x31, 0x3c, 0x61, 0x72, 0x62, 0x69, 0x74, 0x72, 0x61, 0x72, 0x79,
|
||
|
0x20, 0x64, 0x61, 0x74, 0x61, 0x3e, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
|
||
|
0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x41, 0x31, 0x41, 0x76, 0x61,
|
||
|
0x6c, 0x75, 0x65, 0x41, 0x31, 0x41, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69,
|
||
|
0x65, 0x6c, 0x64, 0x41, 0x31, 0x42, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x41, 0x31, 0x42, 0x00, 0x00,
|
||
|
0x00, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
|
||
|
0x00, 0x10, 0x64, 0x61, 0x74, 0x61, 0x41, 0x32, 0x3c, 0x61, 0x72, 0x62, 0x69, 0x74, 0x72, 0x61,
|
||
|
0x72, 0x79, 0x20, 0x64, 0x61, 0x74, 0x61, 0x3e, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30,
|
||
|
0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x41, 0x32, 0x41,
|
||
|
0x76, 0x61, 0x6c, 0x75, 0x65, 0x41, 0x32, 0x41, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08,
|
||
|
0x66, 0x69, 0x65, 0x6c, 0x64, 0x41, 0x32, 0x42, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x41, 0x32, 0x42,
|
||
|
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1e,
|
||
|
0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10, 0x64, 0x61, 0x74, 0x61,
|
||
|
0x42, 0x31, 0x3c, 0x61, 0x72, 0x62, 0x69, 0x74, 0x72, 0x61, 0x72, 0x79, 0x20, 0x64, 0x61, 0x74,
|
||
|
0x61, 0x3e, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
|
||
|
0x00, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x42, 0x31, 0x41, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42,
|
||
|
0x31, 0x41, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x42,
|
||
|
0x31, 0x42, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x31, 0x42, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
||
|
0x00, 0x1e, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10, 0x64, 0x61,
|
||
|
0x74, 0x61, 0x42, 0x32, 0x3c, 0x61, 0x72, 0x62, 0x69, 0x74, 0x72, 0x61, 0x72, 0x79, 0x20, 0x64,
|
||
|
0x61, 0x74, 0x61, 0x3e, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x08,
|
||
|
0x00, 0x00, 0x00, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x42, 0x32, 0x41, 0x76, 0x61, 0x6c, 0x75,
|
||
|
0x65, 0x42, 0x32, 0x41, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x66, 0x69, 0x65, 0x6c,
|
||
|
0x64, 0x42, 0x32, 0x42, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x32, 0x42, 0x03, 0x04,
|
||
|
}
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
testPrintModel bool = true
|
||
|
)
|
||
|
|
||
|
func TestSimpleRequestMarshal(t *testing.T) {
|
||
|
|
||
|
var err error
|
||
|
var b []byte
|
||
|
|
||
|
testSimpleReq.Resolve()
|
||
|
|
||
|
if b, err = testSimpleReq.MarshalBinary(); err != nil {
|
||
|
t.Fatalf("Failed marshaling request: %v", err)
|
||
|
}
|
||
|
|
||
|
t.Logf("Target:\n%v", hex.EncodeToString(testSimpleReqEncoded))
|
||
|
t.Logf("Marshaled:\n%v", hex.EncodeToString(b))
|
||
|
if !bytes.Equal(b, testSimpleReqEncoded) {
|
||
|
t.Fatal("Marshaled request binary does not match")
|
||
|
}
|
||
|
|
||
|
_ = b
|
||
|
}
|
||
|
|
||
|
func TestSimpleRequestUnmarshal(t *testing.T) {
|
||
|
|
||
|
var err error
|
||
|
var b []byte
|
||
|
var b2 []byte
|
||
|
var req *Request = new(Request)
|
||
|
|
||
|
testSimpleReq.Resolve()
|
||
|
|
||
|
if err = req.UnmarshalBinary(testSimpleReqEncoded); err != nil {
|
||
|
t.Fatalf("Error when unmarshaling: %v", err)
|
||
|
}
|
||
|
|
||
|
// if !reflect.DeepEqual(req, testSimpleReq) {
|
||
|
// t.Fatalf("Unmarshaled request and canonical request do not match!")
|
||
|
// }
|
||
|
|
||
|
// t.Log(spew.Sdump(req))
|
||
|
if b, err = json.MarshalIndent(req, "", " "); err != nil {
|
||
|
t.Fatalf("Failed to marshal to JSON: %v", err)
|
||
|
}
|
||
|
if b2, err = json.MarshalIndent(testSimpleReq, "", " "); err != nil {
|
||
|
t.Fatalf("Failed to marshal to JSON: %v", err)
|
||
|
}
|
||
|
|
||
|
if !bytes.Equal(b, b2) {
|
||
|
// t.Logf("Unmarshaled JSON conversion:\n%v\nCanonical:\n%v", hex.EncodeToString(b), hex.EncodeToString(b2))
|
||
|
t.Logf("Unmarshaled JSON conversion:\n%v\nCanonical:\n%v", string(b), string(b2))
|
||
|
t.Fatalf("Unmarshaled request and canonical request do not match!")
|
||
|
}
|
||
|
|
||
|
t.Logf(string(b))
|
||
|
|
||
|
_, _ = b, b2
|
||
|
}
|
||
|
|
||
|
func TestSimpleResponseMarshal(t *testing.T) {
|
||
|
|
||
|
var err error
|
||
|
var b []byte
|
||
|
|
||
|
testSimpleResp.Resolve()
|
||
|
|
||
|
if b, err = testSimpleResp.MarshalBinary(); err != nil {
|
||
|
t.Fatalf("Failed marshaling request: %v", err)
|
||
|
}
|
||
|
|
||
|
t.Logf("Target:\n%v", hex.EncodeToString(testSimpleRespEncoded))
|
||
|
t.Logf("Marshaled:\n%v", hex.EncodeToString(b))
|
||
|
if !bytes.Equal(b, testSimpleRespEncoded) {
|
||
|
t.Fatal("Marshaled request binary does not match")
|
||
|
}
|
||
|
|
||
|
_ = b
|
||
|
}
|
||
|
|
||
|
func TestSimpleResponseUnmarshal(t *testing.T) {
|
||
|
|
||
|
var err error
|
||
|
var b []byte
|
||
|
var b2 []byte
|
||
|
var resp *Response = new(Response)
|
||
|
|
||
|
testSimpleResp.Resolve()
|
||
|
|
||
|
if err = resp.UnmarshalBinary(testSimpleRespEncoded); err != nil {
|
||
|
t.Fatalf("Error when unmarshaling: %v", err)
|
||
|
}
|
||
|
|
||
|
// if !reflect.DeepEqual(resp, testSimpleResp) {
|
||
|
// t.Fatalf("Unmarshaled response and canonical response do not match!")
|
||
|
// }
|
||
|
|
||
|
// t.Log(spew.Sdump(resp))
|
||
|
if b, err = json.MarshalIndent(resp, "", " "); err != nil {
|
||
|
t.Fatalf("Failed to marshal to JSON: %v", err)
|
||
|
}
|
||
|
if b2, err = json.MarshalIndent(testSimpleResp, "", " "); err != nil {
|
||
|
t.Fatalf("Failed to marshal to JSON: %v", err)
|
||
|
}
|
||
|
|
||
|
if !bytes.Equal(b, b2) {
|
||
|
// t.Logf("Unmarshaled JSON conversion:\n%v\nCanonical:\n%v", hex.EncodeToString(b), hex.EncodeToString(b2))
|
||
|
t.Logf("Unmarshaled JSON conversion:\n%v\nCanonical:\n%v", string(b), string(b2))
|
||
|
t.Fatalf("Unmarshaled response and canonical response do not match!")
|
||
|
}
|
||
|
|
||
|
t.Logf(string(b))
|
||
|
|
||
|
_, _ = b, b2
|
||
|
}
|
||
|
|
||
|
func TestMultiRequestMarshal(t *testing.T) {
|
||
|
|
||
|
var err error
|
||
|
var b []byte
|
||
|
|
||
|
testMultiReq.Resolve()
|
||
|
|
||
|
if b, err = testMultiReq.MarshalBinary(); err != nil {
|
||
|
t.Fatalf("Failed marshaling request: %v", err)
|
||
|
}
|
||
|
|
||
|
t.Logf("Target:\n%v", hex.EncodeToString(testMultiReqEncoded))
|
||
|
t.Logf("Marshaled:\n%v", hex.EncodeToString(b))
|
||
|
if !bytes.Equal(b, testMultiReqEncoded) {
|
||
|
t.Fatal("Marshaled request binary does not match")
|
||
|
}
|
||
|
|
||
|
_ = b
|
||
|
}
|
||
|
|
||
|
func TestMultiRequestUnmarshal(t *testing.T) {
|
||
|
|
||
|
var err error
|
||
|
var b []byte
|
||
|
var b2 []byte
|
||
|
var req *Request = new(Request)
|
||
|
|
||
|
testMultiReq.Resolve()
|
||
|
|
||
|
if err = req.UnmarshalBinary(testMultiReqEncoded); err != nil {
|
||
|
t.Fatalf("Error when unmarshaling: %v", err)
|
||
|
}
|
||
|
|
||
|
// if !reflect.DeepEqual(req, testMultiReq) {
|
||
|
// t.Fatalf("Unmarshaled request and canonical request do not match!")
|
||
|
// }
|
||
|
|
||
|
// t.Log(spew.Sdump(req))
|
||
|
if b, err = json.MarshalIndent(req, "", " "); err != nil {
|
||
|
t.Fatalf("Failed to marshal to JSON: %v", err)
|
||
|
}
|
||
|
if b2, err = json.MarshalIndent(testMultiReq, "", " "); err != nil {
|
||
|
t.Fatalf("Failed to marshal to JSON: %v", err)
|
||
|
}
|
||
|
|
||
|
if !bytes.Equal(b, b2) {
|
||
|
// t.Logf("Unmarshaled JSON conversion:\n%v\nCanonical:\n%v", hex.EncodeToString(b), hex.EncodeToString(b2))
|
||
|
t.Logf("Unmarshaled JSON conversion:\n%v\nCanonical:\n%v", string(b), string(b2))
|
||
|
t.Fatalf("Unmarshaled request and canonical request do not match!")
|
||
|
}
|
||
|
|
||
|
t.Logf(string(b))
|
||
|
|
||
|
_, _ = b, b2
|
||
|
}
|
||
|
|
||
|
func TestMultiResponseMarshal(t *testing.T) {
|
||
|
|
||
|
var err error
|
||
|
var b []byte
|
||
|
|
||
|
testMultiResp.Resolve()
|
||
|
|
||
|
if b, err = testMultiResp.MarshalBinary(); err != nil {
|
||
|
t.Fatalf("Failed marshaling request: %v", err)
|
||
|
}
|
||
|
|
||
|
t.Logf("Target:\n%v", hex.EncodeToString(testMultiRespEncoded))
|
||
|
t.Logf("Marshaled:\n%v", hex.EncodeToString(b))
|
||
|
if !bytes.Equal(b, testMultiRespEncoded) {
|
||
|
t.Fatal("Marshaled request binary does not match")
|
||
|
}
|
||
|
|
||
|
_ = b
|
||
|
}
|
||
|
|
||
|
func TestMultiResponseUnmarshal(t *testing.T) {
|
||
|
|
||
|
var err error
|
||
|
var b []byte
|
||
|
var b2 []byte
|
||
|
var resp *Response = new(Response)
|
||
|
|
||
|
testMultiResp.Resolve()
|
||
|
|
||
|
if err = resp.UnmarshalBinary(testMultiRespEncoded); err != nil {
|
||
|
t.Fatalf("Error when unmarshaling: %v", err)
|
||
|
}
|
||
|
|
||
|
// if !reflect.DeepEqual(resp, testMultiResp) {
|
||
|
// t.Fatalf("Unmarshaled response and canonical response do not match!")
|
||
|
// }
|
||
|
|
||
|
// t.Log(spew.Sdump(resp))
|
||
|
if b, err = json.MarshalIndent(resp, "", " "); err != nil {
|
||
|
t.Fatalf("Failed to marshal to JSON: %v", err)
|
||
|
}
|
||
|
if b2, err = json.MarshalIndent(testMultiResp, "", " "); err != nil {
|
||
|
t.Fatalf("Failed to marshal to JSON: %v", err)
|
||
|
}
|
||
|
|
||
|
if !bytes.Equal(b, b2) {
|
||
|
// t.Logf("Unmarshaled JSON conversion:\n%v\nCanonical:\n%v", hex.EncodeToString(b), hex.EncodeToString(b2))
|
||
|
t.Logf("Unmarshaled JSON conversion:\n%v\nCanonical:\n%v", string(b), string(b2))
|
||
|
t.Fatalf("Unmarshaled response and canonical response do not match!")
|
||
|
}
|
||
|
|
||
|
t.Logf(string(b))
|
||
|
|
||
|
_, _ = b, b2
|
||
|
}
|
||
|
|
||
|
func TestModels(t *testing.T) {
|
||
|
|
||
|
testSimpleReq.Resolve()
|
||
|
testSimpleResp.Resolve()
|
||
|
testMultiReq.Resolve()
|
||
|
testMultiResp.Resolve()
|
||
|
|
||
|
if testPrintModel {
|
||
|
fmt.Println("// REQUEST (Simple)")
|
||
|
fmt.Println(testSimpleReq.Model())
|
||
|
|
||
|
fmt.Println("// RESPONSE (Simple)")
|
||
|
fmt.Println(testSimpleResp.Model())
|
||
|
|
||
|
fmt.Println("// REQUEST (Complex)")
|
||
|
fmt.Println(testMultiReq.Model())
|
||
|
|
||
|
fmt.Println("// RESPONSE (Complex)")
|
||
|
fmt.Println(testMultiResp.Model())
|
||
|
}
|
||
|
}
|