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