Blame


1 499f8c59 2015-10-27 stephen.d package p9pnew
2 499f8c59 2015-10-27 stephen.d
3 499f8c59 2015-10-27 stephen.d import (
4 499f8c59 2015-10-27 stephen.d "bytes"
5 40d4a02d 2015-11-03 stephen.d "errors"
6 499f8c59 2015-10-27 stephen.d "reflect"
7 499f8c59 2015-10-27 stephen.d "testing"
8 d6198009 2015-10-28 stephen.d "time"
9 499f8c59 2015-10-27 stephen.d )
10 499f8c59 2015-10-27 stephen.d
11 499f8c59 2015-10-27 stephen.d func TestEncodeDecode(t *testing.T) {
12 499f8c59 2015-10-27 stephen.d for _, testcase := range []struct {
13 499f8c59 2015-10-27 stephen.d description string
14 499f8c59 2015-10-27 stephen.d target interface{}
15 499f8c59 2015-10-27 stephen.d marshaled []byte
16 499f8c59 2015-10-27 stephen.d }{
17 499f8c59 2015-10-27 stephen.d {
18 499f8c59 2015-10-27 stephen.d description: "string",
19 499f8c59 2015-10-27 stephen.d target: "asdf",
20 499f8c59 2015-10-27 stephen.d marshaled: []byte{0x4, 0x0, 0x61, 0x73, 0x64, 0x66},
21 499f8c59 2015-10-27 stephen.d },
22 499f8c59 2015-10-27 stephen.d {
23 e9f5e414 2015-10-27 stephen.d description: "[]string",
24 e9f5e414 2015-10-27 stephen.d target: []string{"asdf", "qwer", "zxcv"},
25 e9f5e414 2015-10-27 stephen.d marshaled: []byte{
26 e9f5e414 2015-10-27 stephen.d 0x3, 0x0, // len(target)
27 e9f5e414 2015-10-27 stephen.d 0x4, 0x0, 0x61, 0x73, 0x64, 0x66,
28 e9f5e414 2015-10-27 stephen.d 0x4, 0x0, 0x71, 0x77, 0x65, 0x72,
29 e9f5e414 2015-10-27 stephen.d 0x4, 0x0, 0x7a, 0x78, 0x63, 0x76},
30 e9f5e414 2015-10-27 stephen.d },
31 e6bcde66 2015-10-29 stephen.d // Dir
32 e6bcde66 2015-10-29 stephen.d // Qid
33 e9f5e414 2015-10-27 stephen.d {
34 499f8c59 2015-10-27 stephen.d description: "Tversion fcall",
35 499f8c59 2015-10-27 stephen.d target: &Fcall{
36 499f8c59 2015-10-27 stephen.d Type: Tversion,
37 499f8c59 2015-10-27 stephen.d Tag: 2255,
38 e6bcde66 2015-10-29 stephen.d Message: &MessageTversion{
39 499f8c59 2015-10-27 stephen.d MSize: uint32(1024),
40 499f8c59 2015-10-27 stephen.d Version: "9PTEST",
41 499f8c59 2015-10-27 stephen.d },
42 499f8c59 2015-10-27 stephen.d },
43 e9f5e414 2015-10-27 stephen.d marshaled: []byte{
44 e9f5e414 2015-10-27 stephen.d 0x64, 0xcf, 0x8, 0x0, 0x4, 0x0, 0x0,
45 e9f5e414 2015-10-27 stephen.d 0x6, 0x0, 0x39, 0x50, 0x54, 0x45, 0x53, 0x54},
46 499f8c59 2015-10-27 stephen.d },
47 e9f5e414 2015-10-27 stephen.d {
48 e6bcde66 2015-10-29 stephen.d description: "Rversion fcall",
49 e6bcde66 2015-10-29 stephen.d target: &Fcall{
50 e6bcde66 2015-10-29 stephen.d Type: Rversion,
51 e6bcde66 2015-10-29 stephen.d Tag: 2255,
52 e6bcde66 2015-10-29 stephen.d Message: &MessageRversion{
53 e6bcde66 2015-10-29 stephen.d MSize: uint32(1024),
54 e6bcde66 2015-10-29 stephen.d Version: "9PTEST",
55 e6bcde66 2015-10-29 stephen.d },
56 e6bcde66 2015-10-29 stephen.d },
57 e6bcde66 2015-10-29 stephen.d marshaled: []byte{
58 e6bcde66 2015-10-29 stephen.d 0x65, 0xcf, 0x8, 0x0, 0x4, 0x0, 0x0,
59 e6bcde66 2015-10-29 stephen.d 0x6, 0x0, 0x39, 0x50, 0x54, 0x45, 0x53, 0x54},
60 e6bcde66 2015-10-29 stephen.d },
61 e6bcde66 2015-10-29 stephen.d {
62 e9f5e414 2015-10-27 stephen.d description: "Twalk fcall",
63 e9f5e414 2015-10-27 stephen.d target: &Fcall{
64 e9f5e414 2015-10-27 stephen.d Type: Twalk,
65 e9f5e414 2015-10-27 stephen.d Tag: 5666,
66 e9f5e414 2015-10-27 stephen.d Message: &MessageTwalk{
67 e9f5e414 2015-10-27 stephen.d Fid: 1010,
68 e9f5e414 2015-10-27 stephen.d Newfid: 1011,
69 97423e8b 2015-10-29 stephen.d Wnames: []string{"a", "b", "c"},
70 e9f5e414 2015-10-27 stephen.d },
71 e9f5e414 2015-10-27 stephen.d },
72 e9f5e414 2015-10-27 stephen.d marshaled: []byte{
73 e9f5e414 2015-10-27 stephen.d 0x6e, 0x22, 0x16, 0xf2, 0x3, 0x0, 0x0, 0xf3, 0x3, 0x0, 0x0,
74 e9f5e414 2015-10-27 stephen.d 0x3, 0x0, // len(wnames)
75 e9f5e414 2015-10-27 stephen.d 0x1, 0x0, 0x61, // "a"
76 e9f5e414 2015-10-27 stephen.d 0x1, 0x0, 0x62, // "b"
77 e9f5e414 2015-10-27 stephen.d 0x1, 0x0, 0x63}, // "c"
78 e9f5e414 2015-10-27 stephen.d },
79 d6198009 2015-10-28 stephen.d {
80 d6198009 2015-10-28 stephen.d description: "Rwalk call",
81 d6198009 2015-10-28 stephen.d target: &Fcall{
82 d6198009 2015-10-28 stephen.d Type: Rwalk,
83 d6198009 2015-10-28 stephen.d Tag: 5556,
84 d6198009 2015-10-28 stephen.d Message: &MessageRwalk{
85 d6198009 2015-10-28 stephen.d Qids: []Qid{
86 d6198009 2015-10-28 stephen.d Qid{
87 d6198009 2015-10-28 stephen.d Type: QTDIR,
88 d6198009 2015-10-28 stephen.d Path: 1111,
89 d6198009 2015-10-28 stephen.d Version: 11112,
90 d6198009 2015-10-28 stephen.d },
91 d6198009 2015-10-28 stephen.d Qid{Type: QTFILE,
92 d6198009 2015-10-28 stephen.d Version: 1112,
93 d6198009 2015-10-28 stephen.d Path: 11114},
94 d6198009 2015-10-28 stephen.d },
95 d6198009 2015-10-28 stephen.d },
96 d6198009 2015-10-28 stephen.d },
97 d6198009 2015-10-28 stephen.d marshaled: []byte{
98 d6198009 2015-10-28 stephen.d 0x6f, 0xb4, 0x15,
99 d6198009 2015-10-28 stephen.d 0x2, 0x0,
100 d6198009 2015-10-28 stephen.d 0x80, 0x68, 0x2b, 0x0, 0x0, 0x57, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
101 d6198009 2015-10-28 stephen.d 0x0, 0x58, 0x4, 0x0, 0x0, 0x6a, 0x2b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
102 d6198009 2015-10-28 stephen.d },
103 d6198009 2015-10-28 stephen.d {
104 d6198009 2015-10-28 stephen.d description: "Rread fcall",
105 d6198009 2015-10-28 stephen.d target: &Fcall{
106 d6198009 2015-10-28 stephen.d Type: Rread,
107 d6198009 2015-10-28 stephen.d Tag: 5556,
108 d6198009 2015-10-28 stephen.d Message: &MessageRread{
109 d6198009 2015-10-28 stephen.d Data: []byte("a lot of byte data"),
110 d6198009 2015-10-28 stephen.d },
111 d6198009 2015-10-28 stephen.d },
112 d6198009 2015-10-28 stephen.d marshaled: []byte{
113 d6198009 2015-10-28 stephen.d 0x75, 0xb4, 0x15,
114 74ec7ac9 2015-10-30 stephen.d 0x12, 0x0, 0x0, 0x0,
115 d6198009 2015-10-28 stephen.d 0x61, 0x20, 0x6c, 0x6f, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x62, 0x79, 0x74, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61},
116 d6198009 2015-10-28 stephen.d },
117 d6198009 2015-10-28 stephen.d {
118 d6198009 2015-10-28 stephen.d description: "",
119 d6198009 2015-10-28 stephen.d target: &Fcall{
120 d6198009 2015-10-28 stephen.d Type: Rstat,
121 d6198009 2015-10-28 stephen.d Tag: 5556,
122 d6198009 2015-10-28 stephen.d Message: &MessageRstat{
123 d6198009 2015-10-28 stephen.d Stat: Dir{
124 d6198009 2015-10-28 stephen.d Type: ^uint16(0),
125 d6198009 2015-10-28 stephen.d Dev: ^uint32(0),
126 d6198009 2015-10-28 stephen.d Qid: Qid{
127 d6198009 2015-10-28 stephen.d Type: QTDIR,
128 d6198009 2015-10-28 stephen.d Version: ^uint32(0),
129 d6198009 2015-10-28 stephen.d Path: ^uint64(0),
130 d6198009 2015-10-28 stephen.d },
131 d6198009 2015-10-28 stephen.d Mode: DMDIR | DMREAD,
132 d6198009 2015-10-28 stephen.d AccessTime: time.Date(2006, 01, 02, 03, 04, 05, 0, time.UTC),
133 d6198009 2015-10-28 stephen.d ModTime: time.Date(2006, 01, 02, 03, 04, 05, 0, time.UTC),
134 d6198009 2015-10-28 stephen.d Length: ^uint64(0),
135 d6198009 2015-10-28 stephen.d Name: "somedir",
136 d6198009 2015-10-28 stephen.d UID: "uid",
137 d6198009 2015-10-28 stephen.d GID: "gid",
138 d6198009 2015-10-28 stephen.d MUID: "muid",
139 d6198009 2015-10-28 stephen.d },
140 d6198009 2015-10-28 stephen.d },
141 d6198009 2015-10-28 stephen.d },
142 d6198009 2015-10-28 stephen.d marshaled: []byte{
143 d6198009 2015-10-28 stephen.d 0x7d, 0xb4, 0x15,
144 74ec7ac9 2015-10-30 stephen.d 0x42, 0x0, // TODO(stevvooe): Include Dir size. Not straightforward.
145 74ec7ac9 2015-10-30 stephen.d 0x40, 0x0, // TODO(stevvooe): Include Dir size. Not straightforward.
146 d6198009 2015-10-28 stephen.d 0xff, 0xff, // type
147 d6198009 2015-10-28 stephen.d 0xff, 0xff, 0xff, 0xff, // dev
148 d6198009 2015-10-28 stephen.d 0x80, 0xff, 0xff, 0xff, 0xff, // qid.type, qid.version
149 d6198009 2015-10-28 stephen.d 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // qid.path
150 d6198009 2015-10-28 stephen.d 0x4, 0x0, 0x0, 0x80, // mode
151 d6198009 2015-10-28 stephen.d 0x25, 0x98, 0xb8, 0x43, // atime
152 d6198009 2015-10-28 stephen.d 0x25, 0x98, 0xb8, 0x43, // mtime
153 d6198009 2015-10-28 stephen.d 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // length
154 d6198009 2015-10-28 stephen.d 0x7, 0x0, 0x73, 0x6f, 0x6d, 0x65, 0x64, 0x69, 0x72,
155 d6198009 2015-10-28 stephen.d 0x3, 0x0, 0x75, 0x69, 0x64, // uid
156 d6198009 2015-10-28 stephen.d 0x3, 0x0, 0x67, 0x69, 0x64, // gid
157 d6198009 2015-10-28 stephen.d 0x4, 0x0, 0x6d, 0x75, 0x69, 0x64}, // muid
158 d6198009 2015-10-28 stephen.d },
159 40d4a02d 2015-11-03 stephen.d {
160 40d4a02d 2015-11-03 stephen.d description: "Rerror fcall",
161 40d4a02d 2015-11-03 stephen.d target: newErrorFcall(5556, errors.New("A serious error")),
162 40d4a02d 2015-11-03 stephen.d marshaled: []byte{
163 6350d279 2015-11-03 stephen.d 0x6b, // Rerror
164 6350d279 2015-11-03 stephen.d 0xb4, 0x15, // Tag
165 6350d279 2015-11-03 stephen.d 0xf, 0x0, // String size.
166 6350d279 2015-11-03 stephen.d 0x41, 0x20, 0x73, 0x65, 0x72, 0x69, 0x6f, 0x75, 0x73, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72},
167 40d4a02d 2015-11-03 stephen.d },
168 499f8c59 2015-10-27 stephen.d } {
169 499f8c59 2015-10-27 stephen.d t.Logf("target under test: %v", testcase.target)
170 499f8c59 2015-10-27 stephen.d fatalf := func(format string, args ...interface{}) {
171 499f8c59 2015-10-27 stephen.d t.Fatalf(testcase.description+": "+format, args...)
172 499f8c59 2015-10-27 stephen.d }
173 499f8c59 2015-10-27 stephen.d
174 e9f5e414 2015-10-27 stephen.d t.Logf("expecting message of %v bytes", len(testcase.marshaled))
175 e9f5e414 2015-10-27 stephen.d
176 499f8c59 2015-10-27 stephen.d var b bytes.Buffer
177 499f8c59 2015-10-27 stephen.d
178 499f8c59 2015-10-27 stephen.d enc := &encoder{&b}
179 499f8c59 2015-10-27 stephen.d dec := &decoder{&b}
180 499f8c59 2015-10-27 stephen.d
181 499f8c59 2015-10-27 stephen.d if err := enc.encode(testcase.target); err != nil {
182 499f8c59 2015-10-27 stephen.d fatalf("error writing fcall: %v", err)
183 499f8c59 2015-10-27 stephen.d }
184 499f8c59 2015-10-27 stephen.d
185 499f8c59 2015-10-27 stephen.d if !bytes.Equal(b.Bytes(), testcase.marshaled) {
186 d6198009 2015-10-28 stephen.d fatalf("unexpected bytes for fcall: \n%#v != \n%#v", b.Bytes(), testcase.marshaled)
187 499f8c59 2015-10-27 stephen.d }
188 499f8c59 2015-10-27 stephen.d
189 d6198009 2015-10-28 stephen.d // check that size9p is working correctly
190 d6198009 2015-10-28 stephen.d if int(size9p(testcase.target)) != len(testcase.marshaled) {
191 d6198009 2015-10-28 stephen.d fatalf("size not correct: %v != %v", int(size9p(testcase.target)), len(testcase.marshaled))
192 d6198009 2015-10-28 stephen.d }
193 d6198009 2015-10-28 stephen.d
194 499f8c59 2015-10-27 stephen.d var v interface{}
195 499f8c59 2015-10-27 stephen.d targetType := reflect.TypeOf(testcase.target)
196 499f8c59 2015-10-27 stephen.d
197 499f8c59 2015-10-27 stephen.d if targetType.Kind() == reflect.Ptr {
198 499f8c59 2015-10-27 stephen.d v = reflect.New(targetType.Elem()).Interface()
199 499f8c59 2015-10-27 stephen.d } else {
200 499f8c59 2015-10-27 stephen.d v = reflect.New(targetType).Interface()
201 499f8c59 2015-10-27 stephen.d }
202 499f8c59 2015-10-27 stephen.d
203 499f8c59 2015-10-27 stephen.d if err := dec.decode(v); err != nil {
204 e9f5e414 2015-10-27 stephen.d fatalf("error reading: %v", err)
205 499f8c59 2015-10-27 stephen.d }
206 499f8c59 2015-10-27 stephen.d
207 499f8c59 2015-10-27 stephen.d if targetType.Kind() != reflect.Ptr {
208 499f8c59 2015-10-27 stephen.d v = reflect.Indirect(reflect.ValueOf(v)).Interface()
209 499f8c59 2015-10-27 stephen.d }
210 499f8c59 2015-10-27 stephen.d
211 499f8c59 2015-10-27 stephen.d if !reflect.DeepEqual(v, testcase.target) {
212 6350d279 2015-11-03 stephen.d fatalf("not equal: %v != %v (\n%#v\n%#v\n)",
213 6350d279 2015-11-03 stephen.d v, testcase.target,
214 6350d279 2015-11-03 stephen.d v, testcase.target)
215 499f8c59 2015-10-27 stephen.d }
216 d6198009 2015-10-28 stephen.d
217 e9f5e414 2015-10-27 stephen.d t.Logf("%#v", v)
218 e9f5e414 2015-10-27 stephen.d
219 499f8c59 2015-10-27 stephen.d }
220 499f8c59 2015-10-27 stephen.d }