package rpc import ( "bytes" "testing" ) type varTest struct { v Var encd []byte l []byte } type varTests struct { b *bytes.Buffer vts []*varTest } var protoVarTests = &varTests{ b: protoBuf, vts: []*varTest{ &varTest{ v: Var{Key: "cmpfile", Value: ""}, encd: []byte{ 99, 109, 112, 102, 105, 108, 101, 0, 0, 0, 0, 0, 0}, l: []byte{0, 0, 0, 0}, }, &varTest{ v: Var{Key: "client", Value: "76"}, encd: []byte{ 99, 108, 105, 101, 110, 116, 0, 2, 0, 0, 0, 55, 54, 0}, l: []byte{2, 0, 0, 0}, }, &varTest{ v: Var{Key: "api", Value: "99999"}, encd: []byte{ 97, 112, 105, 0, 5, 0, 0, 0, 57, 57, 57, 57, 57, 0}, l: []byte{5, 0, 0, 0}, }, &varTest{ v: Var{Key: "enableStreams", Value: ""}, encd: []byte{ 101, 110, 97, 98, 108, 101, 83, 116, 114, 101, 97, 109, 115, 0, 0, 0, 0, 0, 0}, l: []byte{0, 0, 0, 0}, }, &varTest{ v: Var{Key: "host", Value: "brett-mint"}, encd: []byte{ 104, 111, 115, 116, 0, 10, 0, 0, 0, 98, 114, 101, 116, 116, 45, 109, 105, 110, 116, 0}, l: []byte{10, 0, 0, 0}, }, &varTest{ v: Var{Key: "port", Value: "localhost:8192"}, encd: []byte{ 112, 111, 114, 116, 0, 14, 0, 0, 0, 108, 111, 99, 97, 108, 104, 111, 115, 116, 58, 56, 49, 57, 50, 0}, l: []byte{14, 0, 0, 0}, }, &varTest{ v: Var{Key: "sndbuf", Value: "98303"}, encd: []byte{ 115, 110, 100, 98, 117, 102, 0, 5, 0, 0, 0, 57, 56, 51, 48, 51, 0}, l: []byte{5, 0, 0, 0}, }, &varTest{ v: Var{Key: "rcvbuf", Value: "796356"}, encd: []byte{ 114, 99, 118, 98, 117, 102, 0, 6, 0, 0, 0, 55, 57, 54, 51, 53, 54, 0}, l: []byte{6, 0, 0, 0}, }, &varTest{ v: Var{Key: "func", Value: "protocol"}, encd: []byte{ 102, 117, 110, 99, 0, 8, 0, 0, 0, 112, 114, 111, 116, 111, 99, 111, 108, 0}, l: []byte{8, 0, 0, 0}, }, }, } func TestLen(t *testing.T) { for _, vt := range protoVarTests.vts { bout := vt.v.Len() for i, b := range bout { if b != vt.l[i] { t.Errorf("Expected Len of %+v to be %+v got %+v", vt.v, vt.encd, bout) } } } } func TestMarshalBinary(t *testing.T) { for _, vt := range protoVarTests.vts { bout, err := vt.v.MarshalBinary() if err != nil { t.Errorf("Got err %s Marshaling binary from %+v", err, vt.v) } for i, b := range bout { if b != vt.encd[i] { t.Errorf("Expected MarshalBinary of %+v to be %+v got %+v", vt.v, vt.encd, bout) } } } }