-
Notifications
You must be signed in to change notification settings - Fork 198
/
image_test.go
150 lines (138 loc) · 3.3 KB
/
image_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
package imageserver_test
import (
"encoding"
"encoding/binary"
"reflect"
"strings"
"testing"
"unsafe"
"github.com/pierrre/compare"
. "github.com/pierrre/imageserver"
"github.com/pierrre/imageserver/testdata"
)
var _ encoding.BinaryMarshaler = new(Image)
var _ encoding.BinaryUnmarshaler = new(Image)
func TestImageMarshal(t *testing.T) {
data, err := testdata.Medium.MarshalBinary()
if err != nil {
t.Fatal(err)
}
im := new(Image)
err = im.UnmarshalBinary(data)
if err != nil {
t.Fatal(err)
}
diff := compare.Compare(im, testdata.Medium)
if len(diff) != 0 {
t.Fatalf("images not equal, diff:\n%+v", diff)
}
}
func TestImageMarshallErrorFormatMaxLen(t *testing.T) {
im := &Image{
Format: strings.Repeat("a", ImageFormatMaxLen+1),
}
_, err := im.MarshalBinary()
if err == nil {
t.Fatal("no error")
}
if _, ok := err.(*ImageError); !ok {
t.Fatalf("unexpected error type: %T", err)
}
}
func TestImageMarshallErrorDataMaxLen(t *testing.T) {
var data []byte
dataHeader := (*reflect.SliceHeader)(unsafe.Pointer(&data))
dataHeader.Len = ImageDataMaxLen + 1
im := &Image{
Data: data,
}
_, err := im.MarshalBinary()
if err == nil {
t.Fatal("no error")
}
if _, ok := err.(*ImageError); !ok {
t.Fatalf("unexpected error type: %T", err)
}
}
func TestImageUnmarshalBinaryErrorEndOfData(t *testing.T) {
data, err := testdata.Medium.MarshalBinary()
if err != nil {
t.Fatal(err)
}
index := -1 // Always truncate 1 byte
for _, offset := range []int{
4,
len(testdata.Medium.Format),
4,
len(testdata.Medium.Data),
} {
index += offset
errorData := data[0:index]
im := new(Image)
err := im.UnmarshalBinary(errorData)
if err == nil {
t.Fatal("no error")
}
if _, ok := err.(*ImageError); !ok {
t.Fatalf("unexpected error type: %T", err)
}
}
}
func TestImageUnmarshalBinaryErrorFormatMaxLen(t *testing.T) {
data, err := testdata.Medium.MarshalBinary()
if err != nil {
t.Fatal(err)
}
formatLenPosition := 0
binary.LittleEndian.PutUint32(data[formatLenPosition:formatLenPosition+4], uint32(ImageFormatMaxLen+1))
im := new(Image)
err = im.UnmarshalBinary(data)
if err == nil {
t.Fatal("no error")
}
if _, ok := err.(*ImageError); !ok {
t.Fatalf("unexpected error type: %T", err)
}
}
func TestImageUnmarshalBinaryErrorDataMaxLen(t *testing.T) {
data, err := testdata.Medium.MarshalBinary()
if err != nil {
t.Fatal(err)
}
dataLenPosition := 4 + len(testdata.Medium.Format)
binary.LittleEndian.PutUint32(data[dataLenPosition:dataLenPosition+4], uint32(ImageDataMaxLen+1))
im := new(Image)
err = im.UnmarshalBinary(data)
if err == nil {
t.Fatal("no error")
}
if _, ok := err.(*ImageError); !ok {
t.Fatalf("unexpected error type: %T", err)
}
}
// TestImageMarshalBugBuffer is a test for a bug with a misused byte buffer pool.
// Successive calls to Image.MarshalBinary() write to the same byte slice.
func TestImageMarshalBugByteBufferPool(t *testing.T) {
d1, err := testdata.Small.MarshalBinary()
if err != nil {
t.Fatal(err)
}
d2, err := testdata.Medium.MarshalBinary()
if err != nil {
t.Fatal(err)
}
im1 := new(Image)
err = im1.UnmarshalBinary(d1)
if err != nil {
t.Fatal(err)
}
im2 := new(Image)
err = im2.UnmarshalBinary(d2)
if err != nil {
t.Fatal(err)
}
}
func TestImageError(t *testing.T) {
err := &ImageError{Message: "test"}
_ = err.Error()
}