qrcode_decode_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. package qrcode
  4. import (
  5. "bytes"
  6. "flag"
  7. "fmt"
  8. "math/rand"
  9. "os/exec"
  10. "strings"
  11. "testing"
  12. )
  13. // These tests use zbarimg to decode generated QR Codes to ensure they are
  14. // readable. sudo apt-get install zbar-tools, or download from
  15. // http://zbar.sourceforge.net.
  16. //
  17. // By default these tests are disabled to avoid a dependency on zbarimg if
  18. // you're not running the tests. Use the -test-decode flag (go test
  19. // -test-decode) to enable.
  20. var testDecode *bool = flag.Bool("test-decode",
  21. false,
  22. "Enable decode tests. Requires zbarimg installed.")
  23. var testDecodeFuzz *bool = flag.Bool("test-decode-fuzz",
  24. false,
  25. "Enable decode fuzz tests. Requires zbarimg installed.")
  26. func TestDecodeBasic(t *testing.T) {
  27. if !*testDecode {
  28. t.Skip("Decode tests not enabled")
  29. }
  30. tests := []struct {
  31. content string
  32. numRepetitions int
  33. level RecoveryLevel
  34. }{
  35. {
  36. "A",
  37. 1,
  38. Low,
  39. },
  40. {
  41. "A",
  42. 1,
  43. Medium,
  44. },
  45. {
  46. "A",
  47. 1,
  48. High,
  49. },
  50. {
  51. "A",
  52. 1,
  53. Highest,
  54. },
  55. {
  56. "01234567",
  57. 1,
  58. Medium,
  59. },
  60. }
  61. for _, test := range tests {
  62. content := strings.Repeat(test.content, test.numRepetitions)
  63. q, err := New(content, test.level)
  64. if err != nil {
  65. t.Error(err.Error())
  66. }
  67. err = zbarimgCheck(q)
  68. if err != nil {
  69. t.Error(err.Error())
  70. }
  71. }
  72. }
  73. func TestDecodeAllVersionLevels(t *testing.T) {
  74. if !*testDecode {
  75. t.Skip("Decode tests not enabled")
  76. }
  77. for version := 1; version <= 40; version++ {
  78. for _, level := range []RecoveryLevel{Low, Medium, High, Highest} {
  79. t.Logf("Version=%d Level=%d",
  80. version,
  81. level)
  82. q, err := NewWithForcedVersion(
  83. fmt.Sprintf("v-%d l-%d", version, level), version, level)
  84. if err != nil {
  85. t.Fatal(err.Error())
  86. return
  87. }
  88. err = zbarimgCheck(q)
  89. if err != nil {
  90. t.Errorf("Version=%d Level=%d, err=%s, expected success",
  91. version,
  92. level,
  93. err.Error())
  94. continue
  95. }
  96. }
  97. }
  98. }
  99. func TestDecodeAllCharacters(t *testing.T) {
  100. if !*testDecode {
  101. t.Skip("Decode tests not enabled")
  102. }
  103. var content string
  104. // zbarimg has trouble with null bytes, hence start from ASCII 1.
  105. for i := 1; i < 256; i++ {
  106. content += string(i)
  107. }
  108. q, err := New(content, Low)
  109. if err != nil {
  110. t.Error(err.Error())
  111. }
  112. err = zbarimgCheck(q)
  113. if err != nil {
  114. t.Error(err.Error())
  115. }
  116. }
  117. func TestDecodeFuzz(t *testing.T) {
  118. if !*testDecodeFuzz {
  119. t.Skip("Decode fuzz tests not enabled")
  120. }
  121. r := rand.New(rand.NewSource(0))
  122. const iterations int = 32
  123. const maxLength int = 128
  124. for i := 0; i < iterations; i++ {
  125. len := r.Intn(maxLength-1) + 1
  126. var content string
  127. for j := 0; j < len; j++ {
  128. // zbarimg seems to have trouble with special characters, test printable
  129. // characters only for now.
  130. content += string(32 + r.Intn(94))
  131. }
  132. for _, level := range []RecoveryLevel{Low, Medium, High, Highest} {
  133. q, err := New(content, level)
  134. if err != nil {
  135. t.Error(err.Error())
  136. }
  137. err = zbarimgCheck(q)
  138. if err != nil {
  139. t.Error(err.Error())
  140. }
  141. }
  142. }
  143. }
  144. func zbarimgCheck(q *QRCode) error {
  145. s, err := zbarimgDecode(q)
  146. if err != nil {
  147. return err
  148. }
  149. if s != q.Content {
  150. q.WriteFile(256, fmt.Sprintf("%x.png", q.Content))
  151. return fmt.Errorf("got '%s' (%x) expected '%s' (%x)", s, s, q.Content, q.Content)
  152. }
  153. return nil
  154. }
  155. func zbarimgDecode(q *QRCode) (string, error) {
  156. var png []byte
  157. // 512x512px
  158. png, err := q.PNG(512)
  159. if err != nil {
  160. return "", err
  161. }
  162. cmd := exec.Command("zbarimg", "--quiet", "-Sdisable",
  163. "-Sqrcode.enable", "-")
  164. var out bytes.Buffer
  165. cmd.Stdin = bytes.NewBuffer(png)
  166. cmd.Stdout = &out
  167. err = cmd.Run()
  168. if err != nil {
  169. return "", err
  170. }
  171. return strings.TrimSuffix(strings.TrimPrefix(out.String(), "QR-Code:"), "\n"), nil
  172. }
  173. func BenchmarkDecodeTest(b *testing.B) {
  174. if !*testDecode {
  175. b.Skip("Decode benchmarks not enabled")
  176. }
  177. for n := 0; n < b.N; n++ {
  178. q, err := New("content", Medium)
  179. if err != nil {
  180. b.Error(err.Error())
  181. }
  182. err = zbarimgCheck(q)
  183. if err != nil {
  184. b.Error(err.Error())
  185. }
  186. }
  187. }