lz4_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. package lz4_test
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "encoding/binary"
  6. "fmt"
  7. "io"
  8. "math/big"
  9. "reflect"
  10. "testing"
  11. "github.com/pierrec/lz4"
  12. )
  13. // testBuffer wraps bytes.Buffer to remove the WriteTo() and ReadFrom() methods.
  14. type testBuffer struct {
  15. buf *bytes.Buffer
  16. }
  17. func (b *testBuffer) Read(buf []byte) (int, error) {
  18. return b.buf.Read(buf)
  19. }
  20. func (b *testBuffer) Write(buf []byte) (int, error) {
  21. return b.buf.Write(buf)
  22. }
  23. func (b *testBuffer) Len() int {
  24. return b.buf.Len()
  25. }
  26. func (b *testBuffer) Bytes() []byte {
  27. return b.buf.Bytes()
  28. }
  29. // testData represents a test data item. It is really used to provide a human readable label to a slice of bytes.
  30. type testData struct {
  31. label string
  32. data []byte
  33. }
  34. // testHeader represents a test data item. It is really used to provide a human readable label to an LZ4 header.
  35. type testHeader struct {
  36. label string
  37. header lz4.Header
  38. }
  39. // compareHeaders... compares 2 lz4 headers.
  40. func compareHeaders(h, hh lz4.Header, t *testing.T) {
  41. ok := true
  42. if h.BlockDependency != hh.BlockDependency {
  43. t.Errorf("BlockDependency: expected %v, got %v", h.BlockDependency, hh.BlockDependency)
  44. ok = false
  45. }
  46. if h.BlockChecksum != hh.BlockChecksum {
  47. t.Errorf("BlockChecksum: expected %v, got %v", h.BlockChecksum, hh.BlockChecksum)
  48. ok = false
  49. }
  50. if h.NoChecksum != hh.NoChecksum {
  51. t.Errorf("NoChecksum: expected %v, got %v", h.NoChecksum, hh.NoChecksum)
  52. ok = false
  53. }
  54. if h.BlockMaxSize != hh.BlockMaxSize {
  55. t.Errorf("BlockMaxSize: expected %d, got %d", h.BlockMaxSize, hh.BlockMaxSize)
  56. ok = false
  57. }
  58. if h.Size != hh.Size {
  59. t.Errorf("Size: expected %d, got %d", h.Size, hh.Size)
  60. ok = false
  61. }
  62. // if h.Dict != hh.Dict {
  63. // t.Errorf("Dict: expected %d, got %d", h.Dict, hh.Dict)
  64. // ok = false
  65. // }
  66. // if h.DictID != hh.DictID {
  67. // t.Errorf("DictID: expected %d, got %d", h.DictID, hh.DictID)
  68. // ok = false
  69. // }
  70. if !ok {
  71. t.FailNow()
  72. }
  73. }
  74. var (
  75. lorem = []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
  76. // Initial data items used for testing. More are added with random and other kind of data.
  77. testDataItems = []testData{
  78. {"empty", nil},
  79. {
  80. "small pattern",
  81. []byte("aaaaaaaaaaaaaaaaaaa"),
  82. },
  83. {
  84. "small pattern long",
  85. []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
  86. },
  87. {
  88. "medium pattern",
  89. []byte("abcdefghijklmnopqabcdefghijklmnopq"),
  90. },
  91. {
  92. "lorem",
  93. lorem,
  94. },
  95. }
  96. testHeaderItems = []testHeader{}
  97. )
  98. // Build the list of all possible headers with the default values + the ones defined in the map.
  99. func buildHeaders(options map[string][]interface{}) []testHeader {
  100. testHeaderItems := make([]testHeader, 1)
  101. for fieldName, fieldData := range options {
  102. for _, o := range fieldData {
  103. for _, d := range testHeaderItems {
  104. s := reflect.ValueOf(&d.header).Elem()
  105. t := s.Type()
  106. for i := 0; i < s.NumField(); i++ {
  107. if t.Field(i).Name == fieldName {
  108. switch f := s.Field(i); f.Kind() {
  109. case reflect.Bool:
  110. f.SetBool(o.(bool))
  111. case reflect.Int:
  112. f.SetInt(int64(o.(int)))
  113. case reflect.Int64:
  114. switch o.(type) {
  115. case int:
  116. f.SetInt(int64(o.(int)))
  117. default:
  118. f.SetInt(o.(int64))
  119. }
  120. case reflect.Uint32:
  121. switch o.(type) {
  122. case int:
  123. f.SetUint(uint64(o.(int)))
  124. default:
  125. f.SetUint(uint64(o.(uint32)))
  126. }
  127. case reflect.Uint64:
  128. switch o.(type) {
  129. case int:
  130. f.SetUint(uint64(o.(int)))
  131. default:
  132. f.SetUint(o.(uint64))
  133. }
  134. default:
  135. panic(fmt.Sprintf("unsupported type: %v", f.Kind()))
  136. }
  137. d.label = fmt.Sprintf("%+v", d.header)
  138. testHeaderItems = append(testHeaderItems, d)
  139. break
  140. }
  141. }
  142. }
  143. }
  144. }
  145. for i, n := 0, len(testHeaderItems); i < n; {
  146. testHeaderItem := testHeaderItems[i]
  147. // remove the 0 BlockMaxSize value as it is invalid and we have provisioned all possible values already.
  148. if testHeaderItem.header.BlockMaxSize == 0 {
  149. n--
  150. testHeaderItems[i], testHeaderItems = testHeaderItems[n], testHeaderItems[:n]
  151. } else {
  152. testHeaderItem.label = fmt.Sprintf("%+v", testHeaderItem)
  153. i++
  154. }
  155. }
  156. return testHeaderItems
  157. }
  158. // Generate all possible LZ4 headers.
  159. func init() {
  160. // Only set the relevant headers having an impact on the comrpession.
  161. seed := map[string][]interface{}{
  162. "BlockDependency": {true},
  163. "BlockChecksum": {true},
  164. "NoChecksum": {true},
  165. // "Dict": {true},
  166. // Enabling this substantially increase the testing time.
  167. // As this test is not really required it is disabled.
  168. // "HighCompression": {true},
  169. }
  170. for _, bms := range lz4.BlockMaxSizeItems {
  171. seed["BlockMaxSize"] = append(seed["BlockMaxSize"], bms)
  172. }
  173. testHeaderItems = buildHeaders(seed)
  174. }
  175. // Initialize the test data with various sizes of uncompressible and compressible data.
  176. func init() {
  177. maxSize := 10 << 20 // > max block max size of 4Mb
  178. // repeated data with very high compression ratio
  179. repeat := make([]byte, maxSize)
  180. for i := copy(repeat, lorem); i < len(repeat); {
  181. i += copy(repeat[i:], repeat[:i])
  182. }
  183. // repeated data with small compression ratio
  184. repeatlow := make([]byte, maxSize)
  185. for i := 0; i < len(repeatlow); {
  186. i += copy(repeatlow[i:], lorem)
  187. // randomly skip some bytes to make sure the pattern does not repeat too much
  188. n, _ := rand.Int(rand.Reader, big.NewInt(int64(10)))
  189. i += int(n.Int64())
  190. }
  191. // random data: low to no compression
  192. random := make([]byte, maxSize)
  193. if _, err := rand.Read(random); err != nil {
  194. panic(fmt.Sprintf("cannot initialize random data for size %d", maxSize))
  195. }
  196. // generate some test data with various sizes and kind of data: all valid block max sizes + others
  197. for _, size := range lz4.BlockMaxSizeItems {
  198. testDataItems = append(
  199. testDataItems,
  200. testData{fmt.Sprintf("random %d", size), random[:size]},
  201. testData{fmt.Sprintf("random < %d", size), random[:size/3]},
  202. testData{fmt.Sprintf("repeated %d", size), repeat[:size]},
  203. testData{fmt.Sprintf("repeated < %d", size), repeat[:size/3]},
  204. )
  205. }
  206. }
  207. // Test low levels core functions:
  208. // a. compress and compare with supplied data if any
  209. // b. decompress the previous data and compare it with the original one
  210. func TestBlock(t *testing.T) {
  211. for _, compress := range []func([]byte, []byte, int) (int, error){
  212. lz4.CompressBlock,
  213. lz4.CompressBlockHC,
  214. } {
  215. for _, item := range testDataItems {
  216. data := item.data
  217. z := make([]byte, lz4.CompressBlockBound(len(data)))
  218. n, err := compress(data, z, 0)
  219. if n == 0 { // not compressible
  220. continue
  221. }
  222. if err != nil {
  223. t.Errorf("CompressBlock: %s", err)
  224. t.FailNow()
  225. }
  226. z = z[:n]
  227. d := make([]byte, len(data))
  228. n, err = lz4.UncompressBlock(z, d, 0)
  229. if err != nil {
  230. t.Errorf("UncompressBlock: %s", err)
  231. t.FailNow()
  232. }
  233. d = d[:n]
  234. if !bytes.Equal(d, data) {
  235. t.Errorf("invalid decompressed data: %s: %s", item.label, string(d))
  236. t.FailNow()
  237. }
  238. }
  239. }
  240. }
  241. func BenchmarkUncompressBlock(b *testing.B) {
  242. d := make([]byte, len(lorem))
  243. z := make([]byte, len(lorem))
  244. n, err := lz4.CompressBlock(lorem, z, 0)
  245. if err != nil {
  246. b.Errorf("CompressBlock: %s", err)
  247. b.FailNow()
  248. }
  249. z = z[:n]
  250. for i := 0; i < b.N; i++ {
  251. lz4.UncompressBlock(z, d, 0)
  252. }
  253. }
  254. func BenchmarkCompressBlock(b *testing.B) {
  255. d := append([]byte{}, lorem...)
  256. z := make([]byte, len(lorem))
  257. n, err := lz4.CompressBlock(d, z, 0)
  258. if err != nil {
  259. b.Errorf("CompressBlock: %s", err)
  260. b.FailNow()
  261. }
  262. z = z[:n]
  263. for i := 0; i < b.N; i++ {
  264. d = append([]byte{}, lorem...)
  265. lz4.CompressBlock(d, z, 0)
  266. }
  267. }
  268. func BenchmarkCompressBlockHC(b *testing.B) {
  269. d := append([]byte{}, lorem...)
  270. z := make([]byte, len(lorem))
  271. n, err := lz4.CompressBlockHC(d, z, 0)
  272. if err != nil {
  273. b.Errorf("CompressBlock: %s", err)
  274. b.FailNow()
  275. }
  276. z = z[:n]
  277. for i := 0; i < b.N; i++ {
  278. d = append([]byte{}, lorem...)
  279. lz4.CompressBlockHC(d, z, 0)
  280. }
  281. }
  282. // TestNoWrite compresses without any call to Write() (empty frame).
  283. // It does so checking all possible headers.
  284. func TestNoWrite(t *testing.T) {
  285. // that is 2*2*2*2*2*2^4 = 512 headers!
  286. seed := map[string][]interface{}{
  287. "BlockDependency": {true},
  288. "BlockChecksum": {true},
  289. "NoChecksum": {true},
  290. "Size": {999},
  291. // "Dict": {true},
  292. // Enabling this substantially increase the testing time.
  293. // As this test is not really required it is disabled.
  294. // "HighCompression": {true},
  295. }
  296. for _, bms := range lz4.BlockMaxSizeItems {
  297. seed["BlockMaxSize"] = append(seed["BlockMaxSize"], bms)
  298. }
  299. testHeaderItems := buildHeaders(seed)
  300. for _, h := range testHeaderItems {
  301. rw := bytes.NewBuffer(nil)
  302. w := lz4.NewWriter(rw)
  303. w.Header = h.header
  304. if err := w.Close(); err != nil {
  305. t.Errorf("Close(): unexpected error: %v", err)
  306. t.FailNow()
  307. }
  308. r := lz4.NewReader(rw)
  309. n, err := r.Read(nil)
  310. if err != nil {
  311. t.Errorf("Read(): unexpected error: %v", err)
  312. t.FailNow()
  313. }
  314. if n != 0 {
  315. t.Errorf("expected 0 bytes read, got %d", n)
  316. t.FailNow()
  317. }
  318. buf := make([]byte, 16)
  319. n, err = r.Read(buf)
  320. if err != nil && err != io.EOF {
  321. t.Errorf("Read(): unexpected error: %v", err)
  322. t.FailNow()
  323. }
  324. if n != 0 {
  325. t.Errorf("expected 0 bytes read, got %d", n)
  326. t.FailNow()
  327. }
  328. }
  329. }
  330. // TestReset tests that the Reset() method resets the header on the Reader and Writer.
  331. func TestReset(t *testing.T) {
  332. h := lz4.Header{
  333. BlockDependency: true,
  334. BlockChecksum: true,
  335. NoChecksum: true,
  336. BlockMaxSize: 123,
  337. Size: 999,
  338. // Dict: true,
  339. // DictID: 555,
  340. }
  341. dh := lz4.Header{}
  342. w := lz4.NewWriter(nil)
  343. w.Header = h
  344. w.Reset(nil)
  345. compareHeaders(w.Header, dh, t)
  346. r := lz4.NewReader(nil)
  347. r.Header = h
  348. r.Reset(nil)
  349. compareHeaders(r.Header, dh, t)
  350. }
  351. // TestFrame compresses and decompresses LZ4 streams with various input data and options.
  352. func TestFrame(t *testing.T) {
  353. for _, tdata := range testDataItems {
  354. data := tdata.data
  355. // test various options
  356. for _, headerItem := range testHeaderItems {
  357. tag := tdata.label + ": " + headerItem.label
  358. rw := bytes.NewBuffer(nil)
  359. // Set all options to non default values and compress
  360. w := lz4.NewWriter(rw)
  361. w.Header = headerItem.header
  362. n, err := w.Write(data)
  363. if err != nil {
  364. t.Errorf("%s: Write(): unexpected error: %v", tag, err)
  365. t.FailNow()
  366. }
  367. if n != len(data) {
  368. t.Errorf("%s: Write(): expected %d bytes written, got %d", tag, len(data), n)
  369. t.FailNow()
  370. }
  371. if err = w.Close(); err != nil {
  372. t.Errorf("%s: Close(): unexpected error: %v", tag, err)
  373. t.FailNow()
  374. }
  375. // Decompress
  376. r := lz4.NewReader(rw)
  377. n, err = r.Read(nil)
  378. if err != nil {
  379. t.Errorf("%s: Read(): unexpected error: %v", tag, err)
  380. t.FailNow()
  381. }
  382. if n != 0 {
  383. t.Errorf("%s: Read(): expected 0 bytes read, got %d", tag, n)
  384. }
  385. buf := make([]byte, len(data))
  386. n, err = r.Read(buf)
  387. if err != nil && err != io.EOF {
  388. t.Errorf("%s: Read(): unexpected error: %v", tag, err)
  389. t.FailNow()
  390. }
  391. if n != len(data) {
  392. t.Errorf("%s: Read(): expected %d bytes read, got %d", tag, len(data), n)
  393. }
  394. buf = buf[:n]
  395. if !bytes.Equal(buf, data) {
  396. t.Errorf("%s: decompress(compress(data)) != data (%d/%d)", tag, len(buf), len(data))
  397. t.FailNow()
  398. }
  399. compareHeaders(w.Header, r.Header, t)
  400. }
  401. }
  402. }
  403. // TestReadFromWriteTo tests the Reader.WriteTo() and Writer.ReadFrom() methods.
  404. func TestReadFromWriteTo(t *testing.T) {
  405. for _, tdata := range testDataItems {
  406. data := tdata.data
  407. // test various options
  408. for _, headerItem := range testHeaderItems {
  409. tag := "ReadFromWriteTo: " + tdata.label + ": " + headerItem.label
  410. dbuf := bytes.NewBuffer(data)
  411. zbuf := bytes.NewBuffer(nil)
  412. w := lz4.NewWriter(zbuf)
  413. w.Header = headerItem.header
  414. if _, err := w.ReadFrom(dbuf); err != nil {
  415. t.Errorf("%s: unexpected error: %s", tag, err)
  416. t.FailNow()
  417. }
  418. if err := w.Close(); err != nil {
  419. t.Errorf("%s: unexpected error: %s", tag, err)
  420. t.FailNow()
  421. }
  422. buf := bytes.NewBuffer(nil)
  423. r := lz4.NewReader(zbuf)
  424. if _, err := r.WriteTo(buf); err != nil {
  425. t.Errorf("%s: unexpected error: %s", tag, err)
  426. t.FailNow()
  427. }
  428. if !bytes.Equal(buf.Bytes(), data) {
  429. t.Errorf("%s: decompress(compress(data)) != data (%d/%d)", tag, buf.Len(), len(data))
  430. t.FailNow()
  431. }
  432. }
  433. }
  434. }
  435. // TestCopy will use io.Copy and avoid using Reader.WriteTo() and Writer.ReadFrom().
  436. func TestCopy(t *testing.T) {
  437. w := lz4.NewWriter(nil)
  438. r := lz4.NewReader(nil)
  439. for _, tdata := range testDataItems {
  440. data := tdata.data
  441. // test various options
  442. for _, headerItem := range testHeaderItems {
  443. tag := "io.Copy: " + tdata.label + ": " + headerItem.label
  444. dbuf := &testBuffer{bytes.NewBuffer(data)}
  445. zbuf := bytes.NewBuffer(nil)
  446. w.Reset(zbuf)
  447. w.Header = headerItem.header
  448. if _, err := io.Copy(w, dbuf); err != nil {
  449. t.Errorf("%s: unexpected error: %s", tag, err)
  450. t.FailNow()
  451. }
  452. if err := w.Close(); err != nil {
  453. t.Errorf("%s: unexpected error: %s", tag, err)
  454. t.FailNow()
  455. }
  456. buf := &testBuffer{bytes.NewBuffer(nil)}
  457. r.Reset(zbuf)
  458. if _, err := io.Copy(buf, r); err != nil {
  459. t.Errorf("%s: unexpected error: %s", tag, err)
  460. t.FailNow()
  461. }
  462. if !bytes.Equal(buf.Bytes(), data) {
  463. t.Errorf("%s: decompress(compress(data)) != data (%d/%d)", tag, buf.Len(), len(data))
  464. t.FailNow()
  465. }
  466. }
  467. }
  468. }
  469. func TestSkippable(t *testing.T) {
  470. w := lz4.NewWriter(nil)
  471. r := lz4.NewReader(nil)
  472. skippable := make([]byte, 1<<20)
  473. binary.LittleEndian.PutUint32(skippable, lz4.FrameSkipMagic)
  474. binary.LittleEndian.PutUint32(skippable[4:], uint32(len(skippable)-8))
  475. buf := make([]byte, len(lorem))
  476. tag := "skippable first"
  477. zbuf := bytes.NewBuffer(skippable)
  478. w.Reset(zbuf)
  479. w.Write(lorem)
  480. w.Close()
  481. r.Reset(zbuf)
  482. if _, err := r.Read(buf); err != nil {
  483. t.Errorf("%s: unexpected error: %s", tag, err)
  484. t.FailNow()
  485. }
  486. tag = "skippable last"
  487. zbuf = bytes.NewBuffer(nil)
  488. w.Reset(zbuf)
  489. w.Write(lorem)
  490. w.Close()
  491. zbuf.Write(skippable)
  492. r.Reset(zbuf)
  493. if _, err := r.Read(buf); err != nil {
  494. t.Errorf("%s: unexpected error: %s", tag, err)
  495. t.FailNow()
  496. }
  497. tag = "skippable middle"
  498. zbuf = bytes.NewBuffer(nil)
  499. w.Reset(zbuf)
  500. w.Write(lorem)
  501. zbuf.Write(skippable)
  502. w.Write(lorem)
  503. w.Close()
  504. r.Reset(zbuf)
  505. if _, err := r.Read(buf); err != nil {
  506. t.Errorf("%s: unexpected error: %s", tag, err)
  507. t.FailNow()
  508. }
  509. }
  510. func TestWrittenCountAfterBufferedWrite(t *testing.T) {
  511. w := lz4.NewWriter(bytes.NewBuffer(nil))
  512. w.Header.BlockDependency = true
  513. if n, _ := w.Write([]byte{1}); n != 1 {
  514. t.Errorf("expected to write 1 byte, wrote %d", n)
  515. t.FailNow()
  516. }
  517. forcesWrite := make([]byte, 1<<16)
  518. if n, _ := w.Write(forcesWrite); n != len(forcesWrite) {
  519. t.Errorf("expected to write %d bytes, wrote %d", len(forcesWrite), n)
  520. t.FailNow()
  521. }
  522. }
  523. func TestWrittenBlocksExactlyWindowSize(t *testing.T) {
  524. input := make([]byte, 128*1024)
  525. copy(input[64*1024-1:], []byte{1, 2, 3, 4, 1, 2, 3, 4})
  526. output := writeReadChunked(t, input, 64*1024)
  527. if !bytes.Equal(input, output) {
  528. t.Errorf("output is not equal to source input")
  529. t.FailNow()
  530. }
  531. }
  532. func TestWrittenBlocksLessThanWindowSize(t *testing.T) {
  533. input := make([]byte, 80*1024)
  534. copy(input[64*1024-1:], []byte{1, 2, 3, 4, 1, 2, 3, 4})
  535. copy(input[72*1024-1:], []byte{5, 6, 7, 8, 5, 6, 7, 8})
  536. output := writeReadChunked(t, input, 8*1024)
  537. if !bytes.Equal(input, output) {
  538. t.Errorf("output is not equal to source input")
  539. t.FailNow()
  540. }
  541. }
  542. func writeReadChunked(t *testing.T, in []byte, chunkSize int) []byte {
  543. compressed := bytes.NewBuffer(nil)
  544. w := lz4.NewWriter(compressed)
  545. w.Header.BlockDependency = true
  546. buf := bytes.NewBuffer(in)
  547. for buf.Len() > 0 {
  548. _, err := w.Write(buf.Next(chunkSize))
  549. if err != nil {
  550. t.Errorf("unexpected error: %v", err)
  551. t.FailNow()
  552. }
  553. }
  554. r := lz4.NewReader(compressed)
  555. out := make([]byte, len(in))
  556. _, err := io.ReadFull(r, out)
  557. if err != nil {
  558. t.Errorf("unexpected error: %v", err)
  559. t.FailNow()
  560. }
  561. return out
  562. }