11 func TestEncodeDecode(t *testing.T) {
12 for _, testcase := range []struct {
18 description: "string",
20 marshaled: []byte{0x4, 0x0, 0x61, 0x73, 0x64, 0x66},
23 description: "[]string",
24 target: []string{"asdf", "qwer", "zxcv"},
26 0x3, 0x0, // len(target)
27 0x4, 0x0, 0x61, 0x73, 0x64, 0x66,
28 0x4, 0x0, 0x71, 0x77, 0x65, 0x72,
29 0x4, 0x0, 0x7a, 0x78, 0x63, 0x76},
34 description: "Tversion fcall",
38 Message: &MessageTversion{
44 0x64, 0xcf, 0x8, 0x0, 0x4, 0x0, 0x0,
45 0x6, 0x0, 0x39, 0x50, 0x54, 0x45, 0x53, 0x54},
48 description: "Rversion fcall",
52 Message: &MessageRversion{
58 0x65, 0xcf, 0x8, 0x0, 0x4, 0x0, 0x0,
59 0x6, 0x0, 0x39, 0x50, 0x54, 0x45, 0x53, 0x54},
62 description: "Twalk fcall",
66 Message: &MessageTwalk{
69 Wnames: []string{"a", "b", "c"},
73 0x6e, 0x22, 0x16, 0xf2, 0x3, 0x0, 0x0, 0xf3, 0x3, 0x0, 0x0,
74 0x3, 0x0, // len(wnames)
75 0x1, 0x0, 0x61, // "a"
76 0x1, 0x0, 0x62, // "b"
77 0x1, 0x0, 0x63}, // "c"
80 description: "Rwalk call",
84 Message: &MessageRwalk{
100 0x80, 0x68, 0x2b, 0x0, 0x0, 0x57, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
101 0x0, 0x58, 0x4, 0x0, 0x0, 0x6a, 0x2b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
104 description: "Rread fcall",
108 Message: &MessageRread{
109 Data: []byte("a lot of byte data"),
115 0x61, 0x20, 0x6c, 0x6f, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x62, 0x79, 0x74, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61},
122 Message: &MessageRstat{
131 Mode: DMDIR | DMREAD,
132 AccessTime: time.Date(2006, 01, 02, 03, 04, 05, 0, time.UTC),
133 ModTime: time.Date(2006, 01, 02, 03, 04, 05, 0, time.UTC),
144 0x42, 0x0, // TODO(stevvooe): Include Dir size. Not straightforward.
145 0x40, 0x0, // TODO(stevvooe): Include Dir size. Not straightforward.
147 0xff, 0xff, 0xff, 0xff, // dev
148 0x80, 0xff, 0xff, 0xff, 0xff, // qid.type, qid.version
149 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // qid.path
150 0x4, 0x0, 0x0, 0x80, // mode
151 0x25, 0x98, 0xb8, 0x43, // atime
152 0x25, 0x98, 0xb8, 0x43, // mtime
153 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // length
154 0x7, 0x0, 0x73, 0x6f, 0x6d, 0x65, 0x64, 0x69, 0x72,
155 0x3, 0x0, 0x75, 0x69, 0x64, // uid
156 0x3, 0x0, 0x67, 0x69, 0x64, // gid
157 0x4, 0x0, 0x6d, 0x75, 0x69, 0x64}, // muid
160 description: "Rerror fcall",
161 target: newErrorFcall(5556, errors.New("A serious error")),
165 0xf, 0x0, // String size.
166 0x41, 0x20, 0x73, 0x65, 0x72, 0x69, 0x6f, 0x75, 0x73, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72},
169 t.Logf("target under test: %v", testcase.target)
170 fatalf := func(format string, args ...interface{}) {
171 t.Fatalf(testcase.description+": "+format, args...)
174 t.Logf("expecting message of %v bytes", len(testcase.marshaled))
181 if err := enc.encode(testcase.target); err != nil {
182 fatalf("error writing fcall: %v", err)
185 if !bytes.Equal(b.Bytes(), testcase.marshaled) {
186 fatalf("unexpected bytes for fcall: \n%#v != \n%#v", b.Bytes(), testcase.marshaled)
189 // check that size9p is working correctly
190 if int(size9p(testcase.target)) != len(testcase.marshaled) {
191 fatalf("size not correct: %v != %v", int(size9p(testcase.target)), len(testcase.marshaled))
195 targetType := reflect.TypeOf(testcase.target)
197 if targetType.Kind() == reflect.Ptr {
198 v = reflect.New(targetType.Elem()).Interface()
200 v = reflect.New(targetType).Interface()
203 if err := dec.decode(v); err != nil {
204 fatalf("error reading: %v", err)
207 if targetType.Kind() != reflect.Ptr {
208 v = reflect.Indirect(reflect.ValueOf(v)).Interface()
211 if !reflect.DeepEqual(v, testcase.target) {
212 fatalf("not equal: %v != %v (\n%#v\n%#v\n)",