encode_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "io/ioutil"
  10. "math/rand"
  11. "strings"
  12. "testing"
  13. "github.com/klauspost/compress/zip"
  14. )
  15. func TestEncoderRegression(t *testing.T) {
  16. data, err := ioutil.ReadFile("testdata/enc_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. // Same as fuzz test...
  25. test := func(t *testing.T, data []byte) {
  26. dec := NewReader(nil)
  27. enc := NewWriter(nil, WriterConcurrency(2), WriterPadding(255), WriterBlockSize(128<<10))
  28. encBetter := NewWriter(nil, WriterConcurrency(2), WriterPadding(255), WriterBetterCompression(), WriterBlockSize(512<<10))
  29. comp := Encode(make([]byte, MaxEncodedLen(len(data))), data)
  30. decoded, err := Decode(nil, comp)
  31. if err != nil {
  32. t.Error(err)
  33. return
  34. }
  35. if !bytes.Equal(data, decoded) {
  36. t.Error("block decoder mismatch")
  37. return
  38. }
  39. if mel := MaxEncodedLen(len(data)); len(comp) > mel {
  40. t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
  41. return
  42. }
  43. // Test writer and use "better":
  44. var buf bytes.Buffer
  45. encBetter.Reset(&buf)
  46. n, err := encBetter.Write(data)
  47. if err != nil {
  48. t.Error(err)
  49. return
  50. }
  51. if n != len(data) {
  52. t.Error(fmt.Errorf("Write: Short write, want %d, got %d", len(data), n))
  53. return
  54. }
  55. err = encBetter.Close()
  56. if err != nil {
  57. t.Error(err)
  58. return
  59. }
  60. // Calling close twice should not affect anything.
  61. err = encBetter.Close()
  62. if err != nil {
  63. t.Error(err)
  64. return
  65. }
  66. comp = buf.Bytes()
  67. if len(comp)%255 != 0 {
  68. t.Error(fmt.Errorf("wanted size to be mutiple of %d, got size %d with remainder %d", 255, len(comp), len(comp)%255))
  69. return
  70. }
  71. dec.Reset(&buf)
  72. got, err := ioutil.ReadAll(dec)
  73. if err != nil {
  74. t.Error(err)
  75. return
  76. }
  77. if !bytes.Equal(data, got) {
  78. t.Error("block (reset) decoder mismatch")
  79. return
  80. }
  81. // Test Reset on both and use ReadFrom instead.
  82. input := bytes.NewBuffer(data)
  83. buf = bytes.Buffer{}
  84. enc.Reset(&buf)
  85. n2, err := enc.ReadFrom(input)
  86. if err != nil {
  87. t.Error(err)
  88. return
  89. }
  90. if n2 != int64(len(data)) {
  91. t.Error(fmt.Errorf("ReadFrom: Short read, want %d, got %d", len(data), n2))
  92. return
  93. }
  94. err = enc.Close()
  95. if err != nil {
  96. t.Error(err)
  97. return
  98. }
  99. if buf.Len()%255 != 0 {
  100. t.Error(fmt.Errorf("wanted size to be mutiple of %d, got size %d with remainder %d", 255, buf.Len(), buf.Len()%255))
  101. return
  102. }
  103. dec.Reset(&buf)
  104. got, err = ioutil.ReadAll(dec)
  105. if err != nil {
  106. t.Error(err)
  107. return
  108. }
  109. if !bytes.Equal(data, got) {
  110. t.Error("frame (reset) decoder mismatch")
  111. return
  112. }
  113. }
  114. for _, tt := range zr.File {
  115. if !strings.HasSuffix(t.Name(), "") {
  116. continue
  117. }
  118. t.Run(tt.Name, func(t *testing.T) {
  119. r, err := tt.Open()
  120. if err != nil {
  121. t.Error(err)
  122. return
  123. }
  124. b, err := ioutil.ReadAll(r)
  125. if err != nil {
  126. t.Error(err)
  127. return
  128. }
  129. test(t, b)
  130. })
  131. }
  132. }
  133. func TestWriterPadding(t *testing.T) {
  134. n := 100
  135. if testing.Short() {
  136. n = 5
  137. }
  138. rng := rand.New(rand.NewSource(0x1337))
  139. d := NewReader(nil)
  140. for i := 0; i < n; i++ {
  141. padding := (rng.Int() & 0xffff) + 1
  142. src := make([]byte, (rng.Int()&0xfffff)+1)
  143. for i := range src {
  144. src[i] = uint8(rng.Uint32()) & 3
  145. }
  146. var dst bytes.Buffer
  147. e := NewWriter(&dst, WriterPadding(padding))
  148. // Test the added padding is invisible.
  149. _, err := io.Copy(e, bytes.NewBuffer(src))
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. err = e.Close()
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. err = e.Close()
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. if dst.Len()%padding != 0 {
  162. t.Fatalf("wanted size to be mutiple of %d, got size %d with remainder %d", padding, dst.Len(), dst.Len()%padding)
  163. }
  164. var got bytes.Buffer
  165. d.Reset(&dst)
  166. _, err = io.Copy(&got, d)
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. if !bytes.Equal(src, got.Bytes()) {
  171. t.Fatal("output mismatch")
  172. }
  173. // Try after reset
  174. dst.Reset()
  175. e.Reset(&dst)
  176. _, err = io.Copy(e, bytes.NewBuffer(src))
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. err = e.Close()
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. if dst.Len()%padding != 0 {
  185. t.Fatalf("wanted size to be mutiple of %d, got size %d with remainder %d", padding, dst.Len(), dst.Len()%padding)
  186. }
  187. got.Reset()
  188. d.Reset(&dst)
  189. _, err = io.Copy(&got, d)
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. if !bytes.Equal(src, got.Bytes()) {
  194. t.Fatal("output mismatch after reset")
  195. }
  196. }
  197. }