package rpc
import (
"bytes"
"testing"
)
type encoderTest struct {
b *bytes.Buffer
enc *Encoder
vs []*Var
h []byte
encd []byte
}
var protoBuf = bytes.NewBuffer([]byte{})
var protoEncoderTest = &encoderTest{
b: protoBuf,
enc: NewEncoder(protoBuf),
vs: []*Var{
&Var{Key: "cmpfile", Value: ""},
&Var{Key: "client", Value: "76"},
&Var{Key: "api", Value: "99999"},
&Var{Key: "enableStreams", Value: ""},
&Var{Key: "host", Value: "brett-mint"},
&Var{Key: "port", Value: "localhost:8192"},
&Var{Key: "sndbuf", Value: "98303"},
&Var{Key: "rcvbuf", Value: "796356"},
&Var{Key: "func", Value: "protocol"},
},
h: []byte{157, 157, 0, 0, 0},
encd: []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 infoBuf = bytes.NewBuffer([]byte{})
var infoEncoderTest = &encoderTest{
b: infoBuf,
enc: NewEncoder(infoBuf),
vs: []*Var{
&Var{Key: "prog", Value: "p4"},
&Var{Key: "version", Value: "2014.1/LINUX26X86_64/821990"},
&Var{Key: "client", Value: "brett-mint"},
&Var{Key: "cwd", Value: "/home/brett/go/src/p4"},
&Var{Key: "host", Value: "brett-mint"},
&Var{Key: "os", Value: "UNIX"},
&Var{Key: "user", Value: "brett"},
&Var{Key: "charset", Value: "1"},
&Var{Key: "func", Value: "user-info"},
},
h: []byte{184, 184, 0, 0, 0},
encd: []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 encoderTests = []*encoderTest{
protoEncoderTest,
infoEncoderTest,
}
func TestHeader(t *testing.T) {
for _, et := range encoderTests {
et.b.Reset()
for _, v := range et.vs {
d, _ := v.MarshalBinary()
et.b.Write(d)
}
h := et.enc.header(et.b.Len())
for i, e := range et.h {
if (*h)[i] != e {
t.Errorf("Expected header of %+v to be %+v got %+v", et.vs, et.h, h)
}
}
}
}
func TestEncode(t *testing.T) {
for _, et := range encoderTests {
et.b.Reset()
err := et.enc.Encode(et.vs)
if err != nil {
t.Errorf("Expected nil error encoding %+v, got %s", et.vs, err)
}
bout := make([]byte, 0x1fffffff)
et.b.Read(bout)
for i, e := range et.encd {
if bout[i] != e {
t.Errorf("Expected encoder output of %+v to be %+v got %+v", et.vs, et.encd, bout)
}
}
}
}