forked from pierrre/imageserver
/
image_test.go
175 lines (161 loc) · 3.94 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
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
package imageserver_test
import (
"encoding"
"encoding/binary"
"reflect"
"strings"
"testing"
"unsafe"
. "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)
}
if !ImageEqual(im, testdata.Medium) {
t.Fatal("image not equals")
}
}
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 TestImageEqual(t *testing.T) {
type TC struct {
im1 *Image
im2 *Image
equal bool
description string
}
for _, tc := range []TC{
{nil, nil, true, "both nil"},
{nil, testdata.Medium, false, "nil / not nil"},
{testdata.Medium, nil, false, "not nil / nil"},
{testdata.Medium, testdata.Medium, true, "same"},
{testdata.Medium, imageCopy(testdata.Medium), true, "copy"},
{testdata.Medium, testdata.Animated, false, "different format"},
{testdata.Medium, testdata.Small, false, "different data"},
} {
if ImageEqual(tc.im1, tc.im2) != tc.equal {
t.Fatalf("invalid result for test: %s", tc.description)
}
}
}
func TestImageError(t *testing.T) {
err := &ImageError{Message: "test"}
_ = err.Error()
}
func imageCopy(im *Image) *Image {
value := *im
return &value
}