package rpc import ( "bytes" "testing" ) type decoderTest struct { b *bytes.Buffer dec *Decoder vs Vars h []byte msg []byte } var protoDBuf = bytes.NewBuffer([]byte{}) var protoDecoderTest = &decoderTest{ b: protoDBuf, dec: NewDecoder(protoDBuf), vs: protoVars, h: []byte{157, 157, 0, 0, 0}, msg: []byte{157, 157, 0, 0, 0, 99, 109, 112, 102, 105, 108, 101, 0, 0, 0, 0, 0, 0, 99, 108, 105, 101, 110, 116, 0, 2, 0, 0, 0, 55, 54, 0, 97, 112, 105, 0, 5, 0, 0, 0, 57, 57, 57, 57, 57, 0, 101, 110, 97, 98, 108, 101, 83, 116, 114, 101, 97, 109, 115, 0, 0, 0, 0, 0, 0, 104, 111, 115, 116, 0, 10, 0, 0, 0, 98, 114, 101, 116, 116, 45, 109, 105, 110, 116, 0, 112, 111, 114, 116, 0, 14, 0, 0, 0, 108, 111, 99, 97, 108, 104, 111, 115, 116, 58, 56, 49, 57, 50, 0, 115, 110, 100, 98, 117, 102, 0, 5, 0, 0, 0, 57, 56, 51, 48, 51, 0, 114, 99, 118, 98, 117, 102, 0, 6, 0, 0, 0, 55, 57, 54, 51, 53, 54, 0, 102, 117, 110, 99, 0, 8, 0, 0, 0, 112, 114, 111, 116, 111, 99, 111, 108, 0}, } var infoDBuf = bytes.NewBuffer([]byte{}) var infoDecoderTest = &decoderTest{ b: infoDBuf, dec: NewDecoder(infoDBuf), vs: infoVars, h: []byte{184, 184, 0, 0, 0}, msg: []byte{184, 184, 0, 0, 0, 112, 114, 111, 103, 0, 2, 0, 0, 0, 112, 52, 0, 118, 101, 114, 115, 105, 111, 110, 0, 27, 0, 0, 0, 50, 48, 49, 52, 46, 49, 47, 76, 73, 78, 85, 88, 50, 54, 88, 56, 54, 95, 54, 52, 47, 56, 50, 49, 57, 57, 48, 0, 99, 108, 105, 101, 110, 116, 0, 10, 0, 0, 0, 98, 114, 101, 116, 116, 45, 109, 105, 110, 116, 0, 99, 119, 100, 0, 21, 0, 0, 0, 47, 104, 111, 109, 101, 47, 98, 114, 101, 116, 116, 47, 103, 111, 47, 115, 114, 99, 47, 112, 52, 0, 104, 111, 115, 116, 0, 10, 0, 0, 0, 98, 114, 101, 116, 116, 45, 109, 105, 110, 116, 0, 111, 115, 0, 4, 0, 0, 0, 85, 78, 73, 88, 0, 117, 115, 101, 114, 0, 5, 0, 0, 0, 98, 114, 101, 116, 116, 0, 99, 104, 97, 114, 115, 101, 116, 0, 1, 0, 0, 0, 49, 0, 102, 117, 110, 99, 0, 9, 0, 0, 0, 117, 115, 101, 114, 45, 105, 110, 102, 111, 0}, } var decoderTests = []*decoderTest{ protoDecoderTest, infoDecoderTest, } func TestDecode(t *testing.T) { for _, dt := range decoderTests { dt.b.Reset() vs := NewVars() dt.b.Write(dt.msg) err := dt.dec.Decode(vs) if err != nil { t.Errorf("Got err %s Decoding binary from %+v", err, dt.msg) } for k, v := range dt.vs { vo, ok := vs[k] if !ok { t.Errorf("Could not find key %s in %+v", k, vs) } else if vo != v { t.Errorf(`Expected Decode of %+v to create a Vars map with key: "%s" with value: "%s", got key: "%s" value: "%s"`, dt.msg, k, v, k, vo) } } } }