-
Notifications
You must be signed in to change notification settings - Fork 1
/
encdec_test.go
242 lines (215 loc) · 5.23 KB
/
encdec_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (C) 2014 Jakob Borg. All rights reserved.
// Copyright (C) 2018 Dario Castañé. All rights reserved. Use of this source code
// is governed by an MIT-style license that can be found in the LICENSE file.
package xdr_test
import (
"bytes"
"io"
"log"
"math/rand"
"reflect"
"testing"
"testing/quick"
"dario.cat/xdr"
)
// Contains all supported types
type TestStruct struct {
B bool
I int
I8 int8
UI8 uint8
I16 int16
UI16 uint16
I32 int32
UI32 uint32
I64 int64
UI64 uint64
BS []byte // max:1024
S string // max:1024
C Opaque
SS []string // max:1024
ES EmptyStruct
OS OtherStruct
OSs []OtherStruct
}
func (s1 TestStruct) TestEquals(s2 TestStruct) bool {
if s1.B != s2.B {
log.Printf("B differ; %v != %v", s1.B, s2.B)
return false
}
if s1.I != s2.I {
log.Printf("I differ; %d != %d", s1.I, s2.I)
return false
}
if s1.I8 != s2.I8 {
log.Printf("I8 differ; %d != %d", s1.I8, s2.I8)
return false
}
if s1.UI8 != s2.UI8 {
log.Printf("UI8 differ; %d != %d", s1.UI8, s2.UI8)
return false
}
if s1.I16 != s2.I16 {
log.Printf("I16 differ; %d != %d", s1.I16, s2.I16)
return false
}
if s1.UI16 != s2.UI16 {
log.Printf("UI16 differ; %d != %d", s1.UI16, s2.UI16)
return false
}
if s1.I32 != s2.I32 {
log.Printf("I32 differ; %d != %d", s1.I32, s2.I32)
return false
}
if s1.UI32 != s2.UI32 {
log.Printf("UI32 differ; %d != %d", s1.UI32, s2.UI32)
return false
}
if s1.I64 != s2.I64 {
log.Printf("I64 differ; %d != %d", s1.I64, s2.I64)
return false
}
if s1.UI64 != s2.UI64 {
log.Printf("UI64 differ; %d != %d", s1.UI64, s2.UI64)
return false
}
if !bytes.Equal(s1.BS, s2.BS) {
log.Println("BS differ")
return false
}
if s1.S != s2.S {
log.Printf("S differ; %q != %q", s1.S, s2.S)
return false
}
if s1.C != s2.C {
log.Printf("C differ; %q != %q", s1.C, s2.C)
return false
}
if len(s1.SS) != len(s2.SS) {
log.Printf("len(SS) differ; %q != %q", len(s1.SS), len(s2.SS))
return false
}
for i := range s1.SS {
if s1.SS[i] != s2.SS[i] {
log.Printf("SS[%d] differ; %q != %q", i, s1.SS[i], s2.SS[i])
return false
}
}
if s1.OS != s2.OS {
log.Printf("OS differ; %q != %q", s1.OS, s2.OS)
return false
}
if len(s1.OSs) != len(s2.OSs) {
log.Printf("len(OSs) differ; %q != %q", len(s1.OSs), len(s2.OSs))
return false
}
for i := range s1.OSs {
if s1.OSs[i] != s2.OSs[i] {
log.Printf("OSs[%d] differ; %q != %q", i, s1.OSs[i], s2.OSs[i])
return false
}
}
return true
}
type EmptyStruct struct {
}
type OtherStruct struct {
F1 uint32
F2 string
}
type Opaque [32]byte
func (u *Opaque) XDRSize() int {
return 32
}
func (u *Opaque) MarshalXDRInto(m *xdr.Marshaller) error {
m.MarshalRaw(u[:])
return m.Error
}
func (o *Opaque) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
copy((*o)[:], u.UnmarshalRaw(32))
return u.Error
}
func (Opaque) Generate(rand *rand.Rand, size int) reflect.Value {
var u Opaque
for i := range u[:] {
u[i] = byte(rand.Int())
}
return reflect.ValueOf(u)
}
func TestEncDec(t *testing.T) {
fn := func(t0 TestStruct) bool {
bs, err := t0.MarshalXDR()
if err != nil {
t.Fatal(err)
}
var t1 TestStruct
err = t1.UnmarshalXDR(bs)
if err != nil {
t.Fatal(err)
}
return t0.TestEquals(t1)
}
if err := quick.Check(fn, nil); err != nil {
t.Error(err)
}
}
func TestMarshalShortBuffer(t *testing.T) {
var s TestStruct
buf := make([]byte, s.XDRSize())
if err := s.MarshalXDRInto(&xdr.Marshaller{Data: buf}); err != nil {
t.Fatal("Unexpected error", err)
}
if err := s.MarshalXDRInto(&xdr.Marshaller{Data: buf[1:]}); err != io.ErrShortBuffer {
t.Fatal("Expected io.ErrShortBuffer, got", err)
}
}
func TestUnmarshalUnexpectedEOF(t *testing.T) {
var s TestStruct
buf := make([]byte, s.XDRSize())
if err := s.MarshalXDRInto(&xdr.Marshaller{Data: buf}); err != nil {
t.Fatal("Unexpected error", err)
}
if err := s.UnmarshalXDR(buf[:len(buf)-1]); err != io.ErrUnexpectedEOF {
t.Fatal("Expected io.ErrUnexpectedEOF, got", err)
}
u := &xdr.Unmarshaller{Data: buf[:3]}
u.UnmarshalRaw(4)
if err := u.Error; err != io.ErrUnexpectedEOF {
t.Fatal("Expected io.ErrUnexpectedEOF, got", err)
}
u = &xdr.Unmarshaller{Data: buf[:3]}
u.UnmarshalString()
if err := u.Error; err != io.ErrUnexpectedEOF {
t.Fatal("Expected io.ErrUnexpectedEOF, got", err)
}
u = &xdr.Unmarshaller{Data: buf[:3]}
u.UnmarshalBytes()
if err := u.Error; err != io.ErrUnexpectedEOF {
t.Fatal("Expected io.ErrUnexpectedEOF, got", err)
}
u = &xdr.Unmarshaller{Data: buf[:3]}
u.UnmarshalBool()
if err := u.Error; err != io.ErrUnexpectedEOF {
t.Fatal("Expected io.ErrUnexpectedEOF, got", err)
}
u = &xdr.Unmarshaller{Data: buf[:3]}
u.UnmarshalUint8()
if err := u.Error; err != io.ErrUnexpectedEOF {
t.Fatal("Expected io.ErrUnexpectedEOF, got", err)
}
u = &xdr.Unmarshaller{Data: buf[:3]}
u.UnmarshalUint16()
if err := u.Error; err != io.ErrUnexpectedEOF {
t.Fatal("Expected io.ErrUnexpectedEOF, got", err)
}
u = &xdr.Unmarshaller{Data: buf[:3]}
u.UnmarshalUint32()
if err := u.Error; err != io.ErrUnexpectedEOF {
t.Fatal("Expected io.ErrUnexpectedEOF, got", err)
}
u = &xdr.Unmarshaller{Data: buf[:7]}
u.UnmarshalUint64()
if err := u.Error; err != io.ErrUnexpectedEOF {
t.Fatal("Expected io.ErrUnexpectedEOF, got", err)
}
}