decode_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (c) 2019 Klaus Post. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package s2
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "os"
  10. "runtime"
  11. "strings"
  12. "testing"
  13. "github.com/klauspost/compress/zip"
  14. )
  15. func TestDecodeRegression(t *testing.T) {
  16. data, err := os.ReadFile("testdata/dec-block-regressions.zip")
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. for _, tt := range zr.File {
  25. if !strings.HasSuffix(t.Name(), "") {
  26. continue
  27. }
  28. t.Run(tt.Name, func(t *testing.T) {
  29. r, err := tt.Open()
  30. if err != nil {
  31. t.Error(err)
  32. return
  33. }
  34. in, err := io.ReadAll(r)
  35. if err != nil {
  36. t.Error(err)
  37. }
  38. got, err := Decode(nil, in)
  39. t.Log("Received:", len(got), err)
  40. })
  41. }
  42. }
  43. func TestDecoderMaxBlockSize(t *testing.T) {
  44. data, err := os.ReadFile("testdata/enc_regressions.zip")
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. sizes := []int{4 << 10, 10 << 10, 1 << 20, 4 << 20}
  53. test := func(t *testing.T, data []byte) {
  54. for _, size := range sizes {
  55. t.Run(fmt.Sprintf("%d", size), func(t *testing.T) {
  56. var buf bytes.Buffer
  57. dec := NewReader(nil, ReaderMaxBlockSize(size), ReaderAllocBlock(size/2))
  58. enc := NewWriter(&buf, WriterBlockSize(size), WriterPadding(16<<10), WriterPaddingSrc(zeroReader{}))
  59. // Test writer.
  60. n, err := enc.Write(data)
  61. if err != nil {
  62. t.Error(err)
  63. return
  64. }
  65. if n != len(data) {
  66. t.Error(fmt.Errorf("Write: Short write, want %d, got %d", len(data), n))
  67. return
  68. }
  69. err = enc.Close()
  70. if err != nil {
  71. t.Error(err)
  72. return
  73. }
  74. // Calling close twice should not affect anything.
  75. err = enc.Close()
  76. if err != nil {
  77. t.Error(err)
  78. return
  79. }
  80. dec.Reset(&buf)
  81. got, err := io.ReadAll(dec)
  82. if err != nil {
  83. t.Error(err)
  84. return
  85. }
  86. if !bytes.Equal(data, got) {
  87. t.Error("block (reset) decoder mismatch")
  88. return
  89. }
  90. // Test Reset on both and use ReadFrom instead.
  91. buf.Reset()
  92. enc.Reset(&buf)
  93. n2, err := enc.ReadFrom(bytes.NewBuffer(data))
  94. if err != nil {
  95. t.Error(err)
  96. return
  97. }
  98. if n2 != int64(len(data)) {
  99. t.Error(fmt.Errorf("ReadFrom: Short read, want %d, got %d", len(data), n2))
  100. return
  101. }
  102. // Encode twice...
  103. n2, err = enc.ReadFrom(bytes.NewBuffer(data))
  104. if err != nil {
  105. t.Error(err)
  106. return
  107. }
  108. if n2 != int64(len(data)) {
  109. t.Error(fmt.Errorf("ReadFrom: Short read, want %d, got %d", len(data), n2))
  110. return
  111. }
  112. err = enc.Close()
  113. if err != nil {
  114. t.Error(err)
  115. return
  116. }
  117. if enc.pad > 0 && buf.Len()%enc.pad != 0 {
  118. t.Error(fmt.Errorf("wanted size to be mutiple of %d, got size %d with remainder %d", enc.pad, buf.Len(), buf.Len()%enc.pad))
  119. return
  120. }
  121. encoded := buf.Bytes()
  122. dec.Reset(&buf)
  123. // Skip first...
  124. dec.Skip(int64(len(data)))
  125. got, err = io.ReadAll(dec)
  126. if err != nil {
  127. t.Error(err)
  128. return
  129. }
  130. if !bytes.Equal(data, got) {
  131. t.Error("frame (reset) decoder mismatch")
  132. return
  133. }
  134. // Re-add data, Read concurrent.
  135. buf.Write(encoded)
  136. dec.Reset(&buf)
  137. var doubleB bytes.Buffer
  138. nb, err := dec.DecodeConcurrent(&doubleB, runtime.GOMAXPROCS(0))
  139. if err != nil {
  140. t.Error(err)
  141. return
  142. }
  143. if nb != int64(len(data)*2) {
  144. t.Errorf("want %d, got %d, err: %v", len(data)*2, nb, err)
  145. return
  146. }
  147. got = doubleB.Bytes()[:len(data)]
  148. if !bytes.Equal(data, got) {
  149. t.Error("frame (DecodeConcurrent) decoder mismatch")
  150. return
  151. }
  152. got = doubleB.Bytes()[len(data):]
  153. if !bytes.Equal(data, got) {
  154. t.Error("frame (DecodeConcurrent) decoder mismatch")
  155. return
  156. }
  157. })
  158. }
  159. }
  160. for _, tt := range zr.File {
  161. if !strings.HasSuffix(t.Name(), "") {
  162. continue
  163. }
  164. t.Run(tt.Name, func(t *testing.T) {
  165. r, err := tt.Open()
  166. if err != nil {
  167. t.Error(err)
  168. return
  169. }
  170. b, err := io.ReadAll(r)
  171. if err != nil {
  172. t.Error(err)
  173. return
  174. }
  175. test(t, b[:len(b):len(b)])
  176. })
  177. }
  178. }