writer_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // Copyright 2012 The Go Authors. 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 flate
  5. import (
  6. "archive/zip"
  7. "bytes"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "math"
  12. "math/rand"
  13. "runtime"
  14. "strconv"
  15. "strings"
  16. "testing"
  17. )
  18. func TestWriterRegression(t *testing.T) {
  19. data, err := ioutil.ReadFile("testdata/regression.zip")
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. for level := HuffmanOnly; level <= BestCompression; level++ {
  24. t.Run(fmt.Sprint("level_", level), func(t *testing.T) {
  25. zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. for _, tt := range zr.File {
  30. if !strings.HasSuffix(t.Name(), "") {
  31. continue
  32. }
  33. t.Run(tt.Name, func(t *testing.T) {
  34. r, err := tt.Open()
  35. if err != nil {
  36. t.Error(err)
  37. return
  38. }
  39. in, err := ioutil.ReadAll(r)
  40. if err != nil {
  41. t.Error(err)
  42. }
  43. msg := "level " + strconv.Itoa(level) + ":"
  44. buf := new(bytes.Buffer)
  45. fw, err := NewWriter(buf, level)
  46. if err != nil {
  47. t.Fatal(msg + err.Error())
  48. }
  49. n, err := fw.Write(in)
  50. if n != len(in) {
  51. t.Fatal(msg + "short write")
  52. }
  53. if err != nil {
  54. t.Fatal(msg + err.Error())
  55. }
  56. err = fw.Close()
  57. if err != nil {
  58. t.Fatal(msg + err.Error())
  59. }
  60. fr1 := NewReader(buf)
  61. data2, err := ioutil.ReadAll(fr1)
  62. if err != nil {
  63. t.Fatal(msg + err.Error())
  64. }
  65. if bytes.Compare(in, data2) != 0 {
  66. t.Fatal(msg + "not equal")
  67. }
  68. // Do it again...
  69. msg = "level " + strconv.Itoa(level) + " (reset):"
  70. buf.Reset()
  71. fw.Reset(buf)
  72. n, err = fw.Write(in)
  73. if n != len(in) {
  74. t.Fatal(msg + "short write")
  75. }
  76. if err != nil {
  77. t.Fatal(msg + err.Error())
  78. }
  79. err = fw.Close()
  80. if err != nil {
  81. t.Fatal(msg + err.Error())
  82. }
  83. fr1 = NewReader(buf)
  84. data2, err = ioutil.ReadAll(fr1)
  85. if err != nil {
  86. t.Fatal(msg + err.Error())
  87. }
  88. if bytes.Compare(in, data2) != 0 {
  89. t.Fatal(msg + "not equal")
  90. }
  91. })
  92. }
  93. })
  94. }
  95. }
  96. func benchmarkEncoder(b *testing.B, testfile, level, n int) {
  97. b.SetBytes(int64(n))
  98. buf0, err := ioutil.ReadFile(testfiles[testfile])
  99. if err != nil {
  100. b.Fatal(err)
  101. }
  102. if len(buf0) == 0 {
  103. b.Fatalf("test file %q has no data", testfiles[testfile])
  104. }
  105. buf1 := make([]byte, n)
  106. for i := 0; i < n; i += len(buf0) {
  107. if len(buf0) > n-i {
  108. buf0 = buf0[:n-i]
  109. }
  110. copy(buf1[i:], buf0)
  111. }
  112. buf0 = nil
  113. runtime.GC()
  114. w, err := NewWriter(ioutil.Discard, level)
  115. b.ResetTimer()
  116. b.ReportAllocs()
  117. for i := 0; i < b.N; i++ {
  118. w.Reset(ioutil.Discard)
  119. _, err = w.Write(buf1)
  120. if err != nil {
  121. b.Fatal(err)
  122. }
  123. err = w.Close()
  124. if err != nil {
  125. b.Fatal(err)
  126. }
  127. }
  128. }
  129. func BenchmarkEncodeDigitsConstant1e4(b *testing.B) { benchmarkEncoder(b, digits, constant, 1e4) }
  130. func BenchmarkEncodeDigitsConstant1e5(b *testing.B) { benchmarkEncoder(b, digits, constant, 1e5) }
  131. func BenchmarkEncodeDigitsConstant1e6(b *testing.B) { benchmarkEncoder(b, digits, constant, 1e6) }
  132. func BenchmarkEncodeDigitsSpeed1e4(b *testing.B) { benchmarkEncoder(b, digits, speed, 1e4) }
  133. func BenchmarkEncodeDigitsSpeed1e5(b *testing.B) { benchmarkEncoder(b, digits, speed, 1e5) }
  134. func BenchmarkEncodeDigitsSpeed1e6(b *testing.B) { benchmarkEncoder(b, digits, speed, 1e6) }
  135. func BenchmarkEncodeDigitsDefault1e4(b *testing.B) { benchmarkEncoder(b, digits, default_, 1e4) }
  136. func BenchmarkEncodeDigitsDefault1e5(b *testing.B) { benchmarkEncoder(b, digits, default_, 1e5) }
  137. func BenchmarkEncodeDigitsDefault1e6(b *testing.B) { benchmarkEncoder(b, digits, default_, 1e6) }
  138. func BenchmarkEncodeDigitsCompress1e4(b *testing.B) { benchmarkEncoder(b, digits, compress, 1e4) }
  139. func BenchmarkEncodeDigitsCompress1e5(b *testing.B) { benchmarkEncoder(b, digits, compress, 1e5) }
  140. func BenchmarkEncodeDigitsCompress1e6(b *testing.B) { benchmarkEncoder(b, digits, compress, 1e6) }
  141. func BenchmarkEncodeDigitsSL1e4(b *testing.B) { benchmarkStatelessEncoder(b, digits, 1e4) }
  142. func BenchmarkEncodeDigitsSL1e5(b *testing.B) { benchmarkStatelessEncoder(b, digits, 1e5) }
  143. func BenchmarkEncodeDigitsSL1e6(b *testing.B) { benchmarkStatelessEncoder(b, digits, 1e6) }
  144. func BenchmarkEncodeTwainConstant1e4(b *testing.B) { benchmarkEncoder(b, twain, constant, 1e4) }
  145. func BenchmarkEncodeTwainConstant1e5(b *testing.B) { benchmarkEncoder(b, twain, constant, 1e5) }
  146. func BenchmarkEncodeTwainConstant1e6(b *testing.B) { benchmarkEncoder(b, twain, constant, 1e6) }
  147. func BenchmarkEncodeTwainSpeed1e4(b *testing.B) { benchmarkEncoder(b, twain, speed, 1e4) }
  148. func BenchmarkEncodeTwainSpeed1e5(b *testing.B) { benchmarkEncoder(b, twain, speed, 1e5) }
  149. func BenchmarkEncodeTwainSpeed1e6(b *testing.B) { benchmarkEncoder(b, twain, speed, 1e6) }
  150. func BenchmarkEncodeTwainDefault1e4(b *testing.B) { benchmarkEncoder(b, twain, default_, 1e4) }
  151. func BenchmarkEncodeTwainDefault1e5(b *testing.B) { benchmarkEncoder(b, twain, default_, 1e5) }
  152. func BenchmarkEncodeTwainDefault1e6(b *testing.B) { benchmarkEncoder(b, twain, default_, 1e6) }
  153. func BenchmarkEncodeTwainCompress1e4(b *testing.B) { benchmarkEncoder(b, twain, compress, 1e4) }
  154. func BenchmarkEncodeTwainCompress1e5(b *testing.B) { benchmarkEncoder(b, twain, compress, 1e5) }
  155. func BenchmarkEncodeTwainCompress1e6(b *testing.B) { benchmarkEncoder(b, twain, compress, 1e6) }
  156. func BenchmarkEncodeTwainSL1e4(b *testing.B) { benchmarkStatelessEncoder(b, twain, 1e4) }
  157. func BenchmarkEncodeTwainSL1e5(b *testing.B) { benchmarkStatelessEncoder(b, twain, 1e5) }
  158. func BenchmarkEncodeTwainSL1e6(b *testing.B) { benchmarkStatelessEncoder(b, twain, 1e6) }
  159. func benchmarkStatelessEncoder(b *testing.B, testfile, n int) {
  160. b.SetBytes(int64(n))
  161. buf0, err := ioutil.ReadFile(testfiles[testfile])
  162. if err != nil {
  163. b.Fatal(err)
  164. }
  165. if len(buf0) == 0 {
  166. b.Fatalf("test file %q has no data", testfiles[testfile])
  167. }
  168. buf1 := make([]byte, n)
  169. for i := 0; i < n; i += len(buf0) {
  170. if len(buf0) > n-i {
  171. buf0 = buf0[:n-i]
  172. }
  173. copy(buf1[i:], buf0)
  174. }
  175. buf0 = nil
  176. runtime.GC()
  177. b.ResetTimer()
  178. b.ReportAllocs()
  179. for i := 0; i < b.N; i++ {
  180. w := NewStatelessWriter(ioutil.Discard)
  181. _, err = w.Write(buf1)
  182. if err != nil {
  183. b.Fatal(err)
  184. }
  185. err = w.Close()
  186. if err != nil {
  187. b.Fatal(err)
  188. }
  189. }
  190. }
  191. // A writer that fails after N writes.
  192. type errorWriter struct {
  193. N int
  194. }
  195. func (e *errorWriter) Write(b []byte) (int, error) {
  196. if e.N <= 0 {
  197. return 0, io.ErrClosedPipe
  198. }
  199. e.N--
  200. return len(b), nil
  201. }
  202. // Test if errors from the underlying writer is passed upwards.
  203. func TestWriteError(t *testing.T) {
  204. buf := new(bytes.Buffer)
  205. n := 65536
  206. if !testing.Short() {
  207. n *= 4
  208. }
  209. for i := 0; i < n; i++ {
  210. fmt.Fprintf(buf, "asdasfasf%d%dfghfgujyut%dyutyu\n", i, i, i)
  211. }
  212. in := buf.Bytes()
  213. // We create our own buffer to control number of writes.
  214. copyBuf := make([]byte, 128)
  215. for l := 0; l < 10; l++ {
  216. for fail := 1; fail <= 256; fail *= 2 {
  217. // Fail after 'fail' writes
  218. ew := &errorWriter{N: fail}
  219. w, err := NewWriter(ew, l)
  220. if err != nil {
  221. t.Fatalf("NewWriter: level %d: %v", l, err)
  222. }
  223. n, err := copyBuffer(w, bytes.NewBuffer(in), copyBuf)
  224. if err == nil {
  225. t.Fatalf("Level %d: Expected an error, writer was %#v", l, ew)
  226. }
  227. n2, err := w.Write([]byte{1, 2, 2, 3, 4, 5})
  228. if n2 != 0 {
  229. t.Fatal("Level", l, "Expected 0 length write, got", n)
  230. }
  231. if err == nil {
  232. t.Fatal("Level", l, "Expected an error")
  233. }
  234. err = w.Flush()
  235. if err == nil {
  236. t.Fatal("Level", l, "Expected an error on flush")
  237. }
  238. err = w.Close()
  239. if err == nil {
  240. t.Fatal("Level", l, "Expected an error on close")
  241. }
  242. w.Reset(ioutil.Discard)
  243. n2, err = w.Write([]byte{1, 2, 3, 4, 5, 6})
  244. if err != nil {
  245. t.Fatal("Level", l, "Got unexpected error after reset:", err)
  246. }
  247. if n2 == 0 {
  248. t.Fatal("Level", l, "Got 0 length write, expected > 0")
  249. }
  250. if testing.Short() {
  251. return
  252. }
  253. }
  254. }
  255. }
  256. // Test if errors from the underlying writer is passed upwards.
  257. func TestWriter_Reset(t *testing.T) {
  258. buf := new(bytes.Buffer)
  259. n := 65536
  260. if !testing.Short() {
  261. n *= 4
  262. }
  263. for i := 0; i < n; i++ {
  264. fmt.Fprintf(buf, "asdasfasf%d%dfghfgujyut%dyutyu\n", i, i, i)
  265. }
  266. in := buf.Bytes()
  267. for l := 0; l < 10; l++ {
  268. l := l
  269. if testing.Short() && l > 1 {
  270. continue
  271. }
  272. t.Run(fmt.Sprintf("level-%d", l), func(t *testing.T) {
  273. t.Parallel()
  274. offset := 1
  275. if testing.Short() {
  276. offset = 256
  277. }
  278. for ; offset <= 256; offset *= 2 {
  279. // Fail after 'fail' writes
  280. w, err := NewWriter(ioutil.Discard, l)
  281. if err != nil {
  282. t.Fatalf("NewWriter: level %d: %v", l, err)
  283. }
  284. if w.d.fast == nil {
  285. t.Skip("Not Fast...")
  286. return
  287. }
  288. for i := 0; i < (bufferReset-len(in)-offset-maxMatchOffset)/maxMatchOffset; i++ {
  289. // skip ahead to where we are close to wrap around...
  290. w.d.fast.Reset()
  291. }
  292. w.d.fast.Reset()
  293. _, err = w.Write(in)
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. for i := 0; i < 50; i++ {
  298. // skip ahead again... This should wrap around...
  299. w.d.fast.Reset()
  300. }
  301. w.d.fast.Reset()
  302. _, err = w.Write(in)
  303. if err != nil {
  304. t.Fatal(err)
  305. }
  306. for i := 0; i < (math.MaxUint32-bufferReset)/maxMatchOffset; i++ {
  307. // skip ahead to where we are close to wrap around...
  308. w.d.fast.Reset()
  309. }
  310. _, err = w.Write(in)
  311. if err != nil {
  312. t.Fatal(err)
  313. }
  314. err = w.Close()
  315. if err != nil {
  316. t.Fatal(err)
  317. }
  318. }
  319. })
  320. }
  321. }
  322. func TestDeterministicL1(t *testing.T) { testDeterministic(1, t) }
  323. func TestDeterministicL2(t *testing.T) { testDeterministic(2, t) }
  324. func TestDeterministicL3(t *testing.T) { testDeterministic(3, t) }
  325. func TestDeterministicL4(t *testing.T) { testDeterministic(4, t) }
  326. func TestDeterministicL5(t *testing.T) { testDeterministic(5, t) }
  327. func TestDeterministicL6(t *testing.T) { testDeterministic(6, t) }
  328. func TestDeterministicL7(t *testing.T) { testDeterministic(7, t) }
  329. func TestDeterministicL8(t *testing.T) { testDeterministic(8, t) }
  330. func TestDeterministicL9(t *testing.T) { testDeterministic(9, t) }
  331. func TestDeterministicL0(t *testing.T) { testDeterministic(0, t) }
  332. func TestDeterministicLM2(t *testing.T) { testDeterministic(-2, t) }
  333. func testDeterministic(i int, t *testing.T) {
  334. // Test so much we cross a good number of block boundaries.
  335. var length = maxStoreBlockSize*30 + 500
  336. if testing.Short() {
  337. length /= 10
  338. }
  339. // Create a random, but compressible stream.
  340. rng := rand.New(rand.NewSource(1))
  341. t1 := make([]byte, length)
  342. for i := range t1 {
  343. t1[i] = byte(rng.Int63() & 7)
  344. }
  345. // Do our first encode.
  346. var b1 bytes.Buffer
  347. br := bytes.NewBuffer(t1)
  348. w, err := NewWriter(&b1, i)
  349. if err != nil {
  350. t.Fatal(err)
  351. }
  352. // Use a very small prime sized buffer.
  353. cbuf := make([]byte, 787)
  354. _, err = copyBuffer(w, br, cbuf)
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. w.Close()
  359. // We choose a different buffer size,
  360. // bigger than a maximum block, and also a prime.
  361. var b2 bytes.Buffer
  362. cbuf = make([]byte, 81761)
  363. br2 := bytes.NewBuffer(t1)
  364. w2, err := NewWriter(&b2, i)
  365. if err != nil {
  366. t.Fatal(err)
  367. }
  368. _, err = copyBuffer(w2, br2, cbuf)
  369. if err != nil {
  370. t.Fatal(err)
  371. }
  372. w2.Close()
  373. b1b := b1.Bytes()
  374. b2b := b2.Bytes()
  375. if !bytes.Equal(b1b, b2b) {
  376. t.Errorf("level %d did not produce deterministic result, result mismatch, len(a) = %d, len(b) = %d", i, len(b1b), len(b2b))
  377. }
  378. // Test using io.WriterTo interface.
  379. var b3 bytes.Buffer
  380. br = bytes.NewBuffer(t1)
  381. w, err = NewWriter(&b3, i)
  382. if err != nil {
  383. t.Fatal(err)
  384. }
  385. _, err = br.WriteTo(w)
  386. if err != nil {
  387. t.Fatal(err)
  388. }
  389. w.Close()
  390. b3b := b3.Bytes()
  391. if !bytes.Equal(b1b, b3b) {
  392. t.Errorf("level %d (io.WriterTo) did not produce deterministic result, result mismatch, len(a) = %d, len(b) = %d", i, len(b1b), len(b3b))
  393. }
  394. }
  395. // copyBuffer is a copy of io.CopyBuffer, since we want to support older go versions.
  396. // This is modified to never use io.WriterTo or io.ReaderFrom interfaces.
  397. func copyBuffer(dst io.Writer, src io.Reader, buf []byte) (written int64, err error) {
  398. if buf == nil {
  399. buf = make([]byte, 32*1024)
  400. }
  401. for {
  402. nr, er := src.Read(buf)
  403. if nr > 0 {
  404. nw, ew := dst.Write(buf[0:nr])
  405. if nw > 0 {
  406. written += int64(nw)
  407. }
  408. if ew != nil {
  409. err = ew
  410. break
  411. }
  412. if nr != nw {
  413. err = io.ErrShortWrite
  414. break
  415. }
  416. }
  417. if er == io.EOF {
  418. break
  419. }
  420. if er != nil {
  421. err = er
  422. break
  423. }
  424. }
  425. return written, err
  426. }