reader_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2009 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 zlib
  5. import (
  6. "bytes"
  7. "io"
  8. "testing"
  9. )
  10. type zlibTest struct {
  11. desc string
  12. raw string
  13. compressed []byte
  14. dict []byte
  15. err error
  16. }
  17. // Compare-to-golden test data was generated by the ZLIB example program at
  18. // http://www.zlib.net/zpipe.c
  19. var zlibTests = []zlibTest{
  20. {
  21. "truncated empty",
  22. "",
  23. []byte{},
  24. nil,
  25. io.ErrUnexpectedEOF,
  26. },
  27. {
  28. "truncated dict",
  29. "",
  30. []byte{0x78, 0xbb},
  31. []byte{0x00},
  32. io.ErrUnexpectedEOF,
  33. },
  34. {
  35. "truncated checksum",
  36. "",
  37. []byte{0x78, 0xbb, 0x00, 0x01, 0x00, 0x01, 0xca, 0x48,
  38. 0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0x28, 0xcf, 0x2f,
  39. 0xca, 0x49, 0x01, 0x04, 0x00, 0x00, 0xff, 0xff,
  40. },
  41. []byte{0x00},
  42. io.ErrUnexpectedEOF,
  43. },
  44. {
  45. "empty",
  46. "",
  47. []byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01},
  48. nil,
  49. nil,
  50. },
  51. {
  52. "goodbye",
  53. "goodbye, world",
  54. []byte{
  55. 0x78, 0x9c, 0x4b, 0xcf, 0xcf, 0x4f, 0x49, 0xaa,
  56. 0x4c, 0xd5, 0x51, 0x28, 0xcf, 0x2f, 0xca, 0x49,
  57. 0x01, 0x00, 0x28, 0xa5, 0x05, 0x5e,
  58. },
  59. nil,
  60. nil,
  61. },
  62. {
  63. "bad header",
  64. "",
  65. []byte{0x78, 0x9f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01},
  66. nil,
  67. ErrHeader,
  68. },
  69. {
  70. "bad checksum",
  71. "",
  72. []byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0xff},
  73. nil,
  74. ErrChecksum,
  75. },
  76. {
  77. "not enough data",
  78. "",
  79. []byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00},
  80. nil,
  81. io.ErrUnexpectedEOF,
  82. },
  83. {
  84. "excess data is silently ignored",
  85. "",
  86. []byte{
  87. 0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01,
  88. 0x78, 0x9c, 0xff,
  89. },
  90. nil,
  91. nil,
  92. },
  93. {
  94. "dictionary",
  95. "Hello, World!\n",
  96. []byte{
  97. 0x78, 0xbb, 0x1c, 0x32, 0x04, 0x27, 0xf3, 0x00,
  98. 0xb1, 0x75, 0x20, 0x1c, 0x45, 0x2e, 0x00, 0x24,
  99. 0x12, 0x04, 0x74,
  100. },
  101. []byte{
  102. 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0a,
  103. },
  104. nil,
  105. },
  106. {
  107. "wrong dictionary",
  108. "",
  109. []byte{
  110. 0x78, 0xbb, 0x1c, 0x32, 0x04, 0x27, 0xf3, 0x00,
  111. 0xb1, 0x75, 0x20, 0x1c, 0x45, 0x2e, 0x00, 0x24,
  112. 0x12, 0x04, 0x74,
  113. },
  114. []byte{
  115. 0x48, 0x65, 0x6c, 0x6c,
  116. },
  117. ErrDictionary,
  118. },
  119. {
  120. "truncated zlib stream amid raw-block",
  121. "hello",
  122. []byte{
  123. 0x78, 0x9c, 0x00, 0x0c, 0x00, 0xf3, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
  124. },
  125. nil,
  126. io.ErrUnexpectedEOF,
  127. },
  128. {
  129. "truncated zlib stream amid fixed-block",
  130. "He",
  131. []byte{
  132. 0x78, 0x9c, 0xf2, 0x48, 0xcd,
  133. },
  134. nil,
  135. io.ErrUnexpectedEOF,
  136. },
  137. }
  138. func TestDecompressor(t *testing.T) {
  139. b := new(bytes.Buffer)
  140. for _, tt := range zlibTests {
  141. in := bytes.NewReader(tt.compressed)
  142. zr, err := NewReaderDict(in, tt.dict)
  143. if err != nil {
  144. if err != tt.err {
  145. t.Errorf("%s: NewReader: %s", tt.desc, err)
  146. }
  147. continue
  148. }
  149. defer zr.Close()
  150. // Read and verify correctness of data.
  151. b.Reset()
  152. n, err := io.Copy(b, zr)
  153. if err != nil {
  154. if err != tt.err {
  155. t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
  156. }
  157. continue
  158. }
  159. s := b.String()
  160. if s != tt.raw {
  161. t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw)
  162. }
  163. // Check for sticky errors.
  164. if n, err := zr.Read([]byte{0}); n != 0 || err != io.EOF {
  165. t.Errorf("%s: Read() = (%d, %v), want (0, io.EOF)", tt.desc, n, err)
  166. }
  167. if err := zr.Close(); err != nil {
  168. t.Errorf("%s: Close() = %v, want nil", tt.desc, err)
  169. }
  170. }
  171. }