forked from skip2/go-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrcode_decode_test.go
232 lines (187 loc) · 3.98 KB
/
qrcode_decode_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
// go-qrcode
// Copyright 2014 Tom Harwood
package qrcode
import (
"bytes"
"flag"
"fmt"
"math/rand"
"os/exec"
"strings"
"testing"
)
// These tests use zbarimg to decode generated QR Codes to ensure they are
// readable. sudo apt-get install zbar-tools, or download from
// http://zbar.sourceforge.net.
//
// By default these tests are disabled to avoid a dependency on zbarimg if
// you're not running the tests. Use the -test-decode flag (go test
// -test-decode) to enable.
var testDecode *bool = flag.Bool("test-decode",
false,
"Enable decode tests. Requires zbarimg installed.")
var testDecodeFuzz *bool = flag.Bool("test-decode-fuzz",
false,
"Enable decode fuzz tests. Requires zbarimg installed.")
func TestDecodeBasic(t *testing.T) {
if !*testDecode {
t.Skip("Decode tests not enabled")
}
tests := []struct {
content string
numRepetitions int
level RecoveryLevel
}{
{
"A",
1,
Low,
},
{
"A",
1,
Medium,
},
{
"A",
1,
High,
},
{
"A",
1,
Highest,
},
{
"01234567",
1,
Medium,
},
}
for _, test := range tests {
content := strings.Repeat(test.content, test.numRepetitions)
q, err := New(content, test.level)
if err != nil {
t.Error(err.Error())
}
err = zbarimgCheck(q)
if err != nil {
t.Error(err.Error())
}
}
}
func TestDecodeAllVersionLevels(t *testing.T) {
if !*testDecode {
t.Skip("Decode tests not enabled")
}
for version := 1; version <= 40; version++ {
for _, level := range []RecoveryLevel{Low, Medium, High, Highest} {
t.Logf("Version=%d Level=%d",
version,
level)
q, err := NewWithForcedVersion(
fmt.Sprintf("v-%d l-%d", version, level), version, level)
if err != nil {
t.Fatal(err.Error())
return
}
err = zbarimgCheck(q)
if err != nil {
t.Errorf("Version=%d Level=%d, err=%s, expected success",
version,
level,
err.Error())
continue
}
}
}
}
func TestDecodeAllCharacters(t *testing.T) {
if !*testDecode {
t.Skip("Decode tests not enabled")
}
var content string
// zbarimg has trouble with null bytes, hence start from ASCII 1.
for i := 1; i < 256; i++ {
content += string(i)
}
q, err := New(content, Low)
if err != nil {
t.Error(err.Error())
}
err = zbarimgCheck(q)
if err != nil {
t.Error(err.Error())
}
}
func TestDecodeFuzz(t *testing.T) {
if !*testDecodeFuzz {
t.Skip("Decode fuzz tests not enabled")
}
r := rand.New(rand.NewSource(0))
const iterations int = 32
const maxLength int = 128
for i := 0; i < iterations; i++ {
len := r.Intn(maxLength-1) + 1
var content string
for j := 0; j < len; j++ {
// zbarimg seems to have trouble with special characters, test printable
// characters only for now.
content += string(32 + r.Intn(94))
}
for _, level := range []RecoveryLevel{Low, Medium, High, Highest} {
q, err := New(content, level)
if err != nil {
t.Error(err.Error())
}
err = zbarimgCheck(q)
if err != nil {
t.Error(err.Error())
}
}
}
}
func zbarimgCheck(q *QRCode) error {
s, err := zbarimgDecode(q)
if err != nil {
return err
}
if s != q.Content {
q.WriteFile(256, fmt.Sprintf("%x.png", q.Content))
return fmt.Errorf("got '%s' (%x) expected '%s' (%x)", s, s, q.Content, q.Content)
}
return nil
}
func zbarimgDecode(q *QRCode) (string, error) {
var png []byte
// 512x512px
png, err := q.PNG(512)
if err != nil {
return "", err
}
cmd := exec.Command("zbarimg", "--quiet", "-Sdisable",
"-Sqrcode.enable", "-")
var out bytes.Buffer
cmd.Stdin = bytes.NewBuffer(png)
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return "", err
}
return strings.TrimSuffix(strings.TrimPrefix(out.String(), "QR-Code:"), "\n"), nil
}
func BenchmarkDecodeTest(b *testing.B) {
if !*testDecode {
b.Skip("Decode benchmarks not enabled")
}
for n := 0; n < b.N; n++ {
q, err := New("content", Medium)
if err != nil {
b.Error(err.Error())
}
err = zbarimgCheck(q)
if err != nil {
b.Error(err.Error())
}
}
}