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