This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
thumbnailer_test.go
146 lines (129 loc) · 2.14 KB
/
thumbnailer_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
package thumbnailer
import (
"fmt"
"testing"
)
func TestDimensionValidation(t *testing.T) {
t.Parallel()
cases := [...]struct {
name, file string
maxW, maxH uint
err error
}{
{
name: "width check disabled",
file: "too wide.jpg",
},
{
name: "too wide",
file: "too wide.jpg",
maxW: 2000,
err: ErrTooWide,
},
{
name: "height check disabled",
file: "too tall.jpg",
},
{
name: "too tall",
file: "too tall.jpg",
maxH: 2000,
err: ErrTooTall,
},
}
for i := range cases {
c := cases[i]
t.Run(c.name, func(t *testing.T) {
t.Parallel()
opts := Options{
ThumbDims: Dims{
Width: 150,
Height: 150,
},
MaxSourceDims: Dims{
Width: c.maxW,
Height: c.maxH,
},
}
f := openSample(t, c.file)
defer f.Close()
_, _, err := Process(f, opts)
if err != c.err {
t.Fatalf("unexpected error: `%s` : `%s`", c.err, err)
}
})
}
}
func TestDimensionConstraints(t *testing.T) {
t.Parallel()
cases := [...]struct {
name string
constr Dims
}{
{
name: "square",
constr: Dims{
Width: 200,
Height: 200,
},
},
{
name: "rect tall",
constr: Dims{
Width: 100,
Height: 200,
},
},
{
name: "rect wide",
constr: Dims{
Width: 200,
Height: 100,
},
},
}
for i := range cases {
c := cases[i]
t.Run(c.name, func(t *testing.T) {
t.Parallel()
f := openSample(t, "non_square.png")
defer f.Close()
_, thumb, err := Process(f, Options{
ThumbDims: c.constr,
})
if err != nil {
t.Fatal(err)
}
m := thumb.Bounds().Max
if uint(m.X) > c.constr.Width || uint(m.Y) > c.constr.Height {
t.Fatalf(
"thumbnail exceeds bounds: %+v not inside %+v",
m,
c.constr,
)
}
writeSample(
t,
fmt.Sprintf(
"non_square.png_%dx%d_thumb.png",
c.constr.Width, c.constr.Height,
),
thumb,
)
})
}
}
func TestStackOverflow(t *testing.T) {
t.Parallel()
f := openSample(t, "segfault.png")
defer f.Close()
_, _, err := Process(f, Options{
ThumbDims: Dims{
Width: 800,
Height: 1700,
},
})
if err != nil {
t.Fatal(err)
}
}