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