gunzip_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 gzip
  5. import (
  6. "bytes"
  7. oldgz "compress/gzip"
  8. "crypto/rand"
  9. "io"
  10. "io/ioutil"
  11. "os"
  12. "strings"
  13. "testing"
  14. "time"
  15. "github.com/klauspost/compress/flate"
  16. )
  17. type gunzipTest struct {
  18. name string
  19. desc string
  20. raw string
  21. gzip []byte
  22. err error
  23. }
  24. var gunzipTests = []gunzipTest{
  25. { // has 1 empty fixed-huffman block
  26. "empty.txt",
  27. "empty.txt",
  28. "",
  29. []byte{
  30. 0x1f, 0x8b, 0x08, 0x08, 0xf7, 0x5e, 0x14, 0x4a,
  31. 0x00, 0x03, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e,
  32. 0x74, 0x78, 0x74, 0x00, 0x03, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34. },
  35. nil,
  36. },
  37. {
  38. "",
  39. "empty - with no file name",
  40. "",
  41. []byte{
  42. 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88,
  43. 0x00, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. },
  46. nil,
  47. },
  48. { // has 1 non-empty fixed huffman block
  49. "hello.txt",
  50. "hello.txt",
  51. "hello world\n",
  52. []byte{
  53. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  54. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  55. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  56. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  57. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  58. 0x00, 0x00,
  59. },
  60. nil,
  61. },
  62. { // concatenation
  63. "hello.txt",
  64. "hello.txt x2",
  65. "hello world\n" +
  66. "hello world\n",
  67. []byte{
  68. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  69. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  70. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  71. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  72. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  73. 0x00, 0x00,
  74. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  75. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  76. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  77. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  78. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  79. 0x00, 0x00,
  80. },
  81. nil,
  82. },
  83. { // has a fixed huffman block with some length-distance pairs
  84. "shesells.txt",
  85. "shesells.txt",
  86. "she sells seashells by the seashore\n",
  87. []byte{
  88. 0x1f, 0x8b, 0x08, 0x08, 0x72, 0x66, 0x8b, 0x4a,
  89. 0x00, 0x03, 0x73, 0x68, 0x65, 0x73, 0x65, 0x6c,
  90. 0x6c, 0x73, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x2b,
  91. 0xce, 0x48, 0x55, 0x28, 0x4e, 0xcd, 0xc9, 0x29,
  92. 0x06, 0x92, 0x89, 0xc5, 0x19, 0x60, 0x56, 0x52,
  93. 0xa5, 0x42, 0x09, 0x58, 0x18, 0x28, 0x90, 0x5f,
  94. 0x94, 0xca, 0x05, 0x00, 0x76, 0xb0, 0x3b, 0xeb,
  95. 0x24, 0x00, 0x00, 0x00,
  96. },
  97. nil,
  98. },
  99. { // has dynamic huffman blocks
  100. "gettysburg",
  101. "gettysburg",
  102. " Four score and seven years ago our fathers brought forth on\n" +
  103. "this continent, a new nation, conceived in Liberty, and dedicated\n" +
  104. "to the proposition that all men are created equal.\n" +
  105. " Now we are engaged in a great Civil War, testing whether that\n" +
  106. "nation, or any nation so conceived and so dedicated, can long\n" +
  107. "endure.\n" +
  108. " We are met on a great battle-field of that war.\n" +
  109. " We have come to dedicate a portion of that field, as a final\n" +
  110. "resting place for those who here gave their lives that that\n" +
  111. "nation might live. It is altogether fitting and proper that\n" +
  112. "we should do this.\n" +
  113. " But, in a larger sense, we can not dedicate — we can not\n" +
  114. "consecrate — we can not hallow — this ground.\n" +
  115. " The brave men, living and dead, who struggled here, have\n" +
  116. "consecrated it, far above our poor power to add or detract.\n" +
  117. "The world will little note, nor long remember what we say here,\n" +
  118. "but it can never forget what they did here.\n" +
  119. " It is for us the living, rather, to be dedicated here to the\n" +
  120. "unfinished work which they who fought here have thus far so\n" +
  121. "nobly advanced. It is rather for us to be here dedicated to\n" +
  122. "the great task remaining before us — that from these honored\n" +
  123. "dead we take increased devotion to that cause for which they\n" +
  124. "gave the last full measure of devotion —\n" +
  125. " that we here highly resolve that these dead shall not have\n" +
  126. "died in vain — that this nation, under God, shall have a new\n" +
  127. "birth of freedom — and that government of the people, by the\n" +
  128. "people, for the people, shall not perish from this earth.\n" +
  129. "\n" +
  130. "Abraham Lincoln, November 19, 1863, Gettysburg, Pennsylvania\n",
  131. []byte{
  132. 0x1f, 0x8b, 0x08, 0x08, 0xd1, 0x12, 0x2b, 0x4a,
  133. 0x00, 0x03, 0x67, 0x65, 0x74, 0x74, 0x79, 0x73,
  134. 0x62, 0x75, 0x72, 0x67, 0x00, 0x65, 0x54, 0xcd,
  135. 0x6e, 0xd4, 0x30, 0x10, 0xbe, 0xfb, 0x29, 0xe6,
  136. 0x01, 0x42, 0xa5, 0x0a, 0x09, 0xc1, 0x11, 0x90,
  137. 0x40, 0x48, 0xa8, 0xe2, 0x80, 0xd4, 0xf3, 0x24,
  138. 0x9e, 0x24, 0x56, 0xbd, 0x9e, 0xc5, 0x76, 0x76,
  139. 0x95, 0x1b, 0x0f, 0xc1, 0x13, 0xf2, 0x24, 0x7c,
  140. 0x63, 0x77, 0x9b, 0x4a, 0x5c, 0xaa, 0x6e, 0x6c,
  141. 0xcf, 0x7c, 0x7f, 0x33, 0x44, 0x5f, 0x74, 0xcb,
  142. 0x54, 0x26, 0xcd, 0x42, 0x9c, 0x3c, 0x15, 0xb9,
  143. 0x48, 0xa2, 0x5d, 0x38, 0x17, 0xe2, 0x45, 0xc9,
  144. 0x4e, 0x67, 0xae, 0xab, 0xe0, 0xf7, 0x98, 0x75,
  145. 0x5b, 0xd6, 0x4a, 0xb3, 0xe6, 0xba, 0x92, 0x26,
  146. 0x57, 0xd7, 0x50, 0x68, 0xd2, 0x54, 0x43, 0x92,
  147. 0x54, 0x07, 0x62, 0x4a, 0x72, 0xa5, 0xc4, 0x35,
  148. 0x68, 0x1a, 0xec, 0x60, 0x92, 0x70, 0x11, 0x4f,
  149. 0x21, 0xd1, 0xf7, 0x30, 0x4a, 0xae, 0xfb, 0xd0,
  150. 0x9a, 0x78, 0xf1, 0x61, 0xe2, 0x2a, 0xde, 0x55,
  151. 0x25, 0xd4, 0xa6, 0x73, 0xd6, 0xb3, 0x96, 0x60,
  152. 0xef, 0xf0, 0x9b, 0x2b, 0x71, 0x8c, 0x74, 0x02,
  153. 0x10, 0x06, 0xac, 0x29, 0x8b, 0xdd, 0x25, 0xf9,
  154. 0xb5, 0x71, 0xbc, 0x73, 0x44, 0x0f, 0x7a, 0xa5,
  155. 0xab, 0xb4, 0x33, 0x49, 0x0b, 0x2f, 0xbd, 0x03,
  156. 0xd3, 0x62, 0x17, 0xe9, 0x73, 0xb8, 0x84, 0x48,
  157. 0x8f, 0x9c, 0x07, 0xaa, 0x52, 0x00, 0x6d, 0xa1,
  158. 0xeb, 0x2a, 0xc6, 0xa0, 0x95, 0x76, 0x37, 0x78,
  159. 0x9a, 0x81, 0x65, 0x7f, 0x46, 0x4b, 0x45, 0x5f,
  160. 0xe1, 0x6d, 0x42, 0xe8, 0x01, 0x13, 0x5c, 0x38,
  161. 0x51, 0xd4, 0xb4, 0x38, 0x49, 0x7e, 0xcb, 0x62,
  162. 0x28, 0x1e, 0x3b, 0x82, 0x93, 0x54, 0x48, 0xf1,
  163. 0xd2, 0x7d, 0xe4, 0x5a, 0xa3, 0xbc, 0x99, 0x83,
  164. 0x44, 0x4f, 0x3a, 0x77, 0x36, 0x57, 0xce, 0xcf,
  165. 0x2f, 0x56, 0xbe, 0x80, 0x90, 0x9e, 0x84, 0xea,
  166. 0x51, 0x1f, 0x8f, 0xcf, 0x90, 0xd4, 0x60, 0xdc,
  167. 0x5e, 0xb4, 0xf7, 0x10, 0x0b, 0x26, 0xe0, 0xff,
  168. 0xc4, 0xd1, 0xe5, 0x67, 0x2e, 0xe7, 0xc8, 0x93,
  169. 0x98, 0x05, 0xb8, 0xa8, 0x45, 0xc0, 0x4d, 0x09,
  170. 0xdc, 0x84, 0x16, 0x2b, 0x0d, 0x9a, 0x21, 0x53,
  171. 0x04, 0x8b, 0xd2, 0x0b, 0xbd, 0xa2, 0x4c, 0xa7,
  172. 0x60, 0xee, 0xd9, 0xe1, 0x1d, 0xd1, 0xb7, 0x4a,
  173. 0x30, 0x8f, 0x63, 0xd5, 0xa5, 0x8b, 0x33, 0x87,
  174. 0xda, 0x1a, 0x18, 0x79, 0xf3, 0xe3, 0xa6, 0x17,
  175. 0x94, 0x2e, 0xab, 0x6e, 0xa0, 0xe3, 0xcd, 0xac,
  176. 0x50, 0x8c, 0xca, 0xa7, 0x0d, 0x76, 0x37, 0xd1,
  177. 0x23, 0xe7, 0x05, 0x57, 0x8b, 0xa4, 0x22, 0x83,
  178. 0xd9, 0x62, 0x52, 0x25, 0xad, 0x07, 0xbb, 0xbf,
  179. 0xbf, 0xff, 0xbc, 0xfa, 0xee, 0x20, 0x73, 0x91,
  180. 0x29, 0xff, 0x7f, 0x02, 0x71, 0x62, 0x84, 0xb5,
  181. 0xf6, 0xb5, 0x25, 0x6b, 0x41, 0xde, 0x92, 0xb7,
  182. 0x76, 0x3f, 0x91, 0x91, 0x31, 0x1b, 0x41, 0x84,
  183. 0x62, 0x30, 0x0a, 0x37, 0xa4, 0x5e, 0x18, 0x3a,
  184. 0x99, 0x08, 0xa5, 0xe6, 0x6d, 0x59, 0x22, 0xec,
  185. 0x33, 0x39, 0x86, 0x26, 0xf5, 0xab, 0x66, 0xc8,
  186. 0x08, 0x20, 0xcf, 0x0c, 0xd7, 0x47, 0x45, 0x21,
  187. 0x0b, 0xf6, 0x59, 0xd5, 0xfe, 0x5c, 0x8d, 0xaa,
  188. 0x12, 0x7b, 0x6f, 0xa1, 0xf0, 0x52, 0x33, 0x4f,
  189. 0xf5, 0xce, 0x59, 0xd3, 0xab, 0x66, 0x10, 0xbf,
  190. 0x06, 0xc4, 0x31, 0x06, 0x73, 0xd6, 0x80, 0xa2,
  191. 0x78, 0xc2, 0x45, 0xcb, 0x03, 0x65, 0x39, 0xc9,
  192. 0x09, 0xd1, 0x06, 0x04, 0x33, 0x1a, 0x5a, 0xf1,
  193. 0xde, 0x01, 0xb8, 0x71, 0x83, 0xc4, 0xb5, 0xb3,
  194. 0xc3, 0x54, 0x65, 0x33, 0x0d, 0x5a, 0xf7, 0x9b,
  195. 0x90, 0x7c, 0x27, 0x1f, 0x3a, 0x58, 0xa3, 0xd8,
  196. 0xfd, 0x30, 0x5f, 0xb7, 0xd2, 0x66, 0xa2, 0x93,
  197. 0x1c, 0x28, 0xb7, 0xe9, 0x1b, 0x0c, 0xe1, 0x28,
  198. 0x47, 0x26, 0xbb, 0xe9, 0x7d, 0x7e, 0xdc, 0x96,
  199. 0x10, 0x92, 0x50, 0x56, 0x7c, 0x06, 0xe2, 0x27,
  200. 0xb4, 0x08, 0xd3, 0xda, 0x7b, 0x98, 0x34, 0x73,
  201. 0x9f, 0xdb, 0xf6, 0x62, 0xed, 0x31, 0x41, 0x13,
  202. 0xd3, 0xa2, 0xa8, 0x4b, 0x3a, 0xc6, 0x1d, 0xe4,
  203. 0x2f, 0x8c, 0xf8, 0xfb, 0x97, 0x64, 0xf4, 0xb6,
  204. 0x2f, 0x80, 0x5a, 0xf3, 0x56, 0xe0, 0x40, 0x50,
  205. 0xd5, 0x19, 0xd0, 0x1e, 0xfc, 0xca, 0xe5, 0xc9,
  206. 0xd4, 0x60, 0x00, 0x81, 0x2e, 0xa3, 0xcc, 0xb6,
  207. 0x52, 0xf0, 0xb4, 0xdb, 0x69, 0x99, 0xce, 0x7a,
  208. 0x32, 0x4c, 0x08, 0xed, 0xaa, 0x10, 0x10, 0xe3,
  209. 0x6f, 0xee, 0x99, 0x68, 0x95, 0x9f, 0x04, 0x71,
  210. 0xb2, 0x49, 0x2f, 0x62, 0xa6, 0x5e, 0xb4, 0xef,
  211. 0x02, 0xed, 0x4f, 0x27, 0xde, 0x4a, 0x0f, 0xfd,
  212. 0xc1, 0xcc, 0xdd, 0x02, 0x8f, 0x08, 0x16, 0x54,
  213. 0xdf, 0xda, 0xca, 0xe0, 0x82, 0xf1, 0xb4, 0x31,
  214. 0x7a, 0xa9, 0x81, 0xfe, 0x90, 0xb7, 0x3e, 0xdb,
  215. 0xd3, 0x35, 0xc0, 0x20, 0x80, 0x33, 0x46, 0x4a,
  216. 0x63, 0xab, 0xd1, 0x0d, 0x29, 0xd2, 0xe2, 0x84,
  217. 0xb8, 0xdb, 0xfa, 0xe9, 0x89, 0x44, 0x86, 0x7c,
  218. 0xe8, 0x0b, 0xe6, 0x02, 0x6a, 0x07, 0x9b, 0x96,
  219. 0xd0, 0xdb, 0x2e, 0x41, 0x4c, 0xa1, 0xd5, 0x57,
  220. 0x45, 0x14, 0xfb, 0xe3, 0xa6, 0x72, 0x5b, 0x87,
  221. 0x6e, 0x0c, 0x6d, 0x5b, 0xce, 0xe0, 0x2f, 0xe2,
  222. 0x21, 0x81, 0x95, 0xb0, 0xe8, 0xb6, 0x32, 0x0b,
  223. 0xb2, 0x98, 0x13, 0x52, 0x5d, 0xfb, 0xec, 0x63,
  224. 0x17, 0x8a, 0x9e, 0x23, 0x22, 0x36, 0xee, 0xcd,
  225. 0xda, 0xdb, 0xcf, 0x3e, 0xf1, 0xc7, 0xf1, 0x01,
  226. 0x12, 0x93, 0x0a, 0xeb, 0x6f, 0xf2, 0x02, 0x15,
  227. 0x96, 0x77, 0x5d, 0xef, 0x9c, 0xfb, 0x88, 0x91,
  228. 0x59, 0xf9, 0x84, 0xdd, 0x9b, 0x26, 0x8d, 0x80,
  229. 0xf9, 0x80, 0x66, 0x2d, 0xac, 0xf7, 0x1f, 0x06,
  230. 0xba, 0x7f, 0xff, 0xee, 0xed, 0x40, 0x5f, 0xa5,
  231. 0xd6, 0xbd, 0x8c, 0x5b, 0x46, 0xd2, 0x7e, 0x48,
  232. 0x4a, 0x65, 0x8f, 0x08, 0x42, 0x60, 0xf7, 0x0f,
  233. 0xb9, 0x16, 0x0b, 0x0c, 0x1a, 0x06, 0x00, 0x00,
  234. },
  235. nil,
  236. },
  237. { // has 1 non-empty fixed huffman block then garbage
  238. "hello.txt",
  239. "hello.txt + garbage",
  240. "hello world\n",
  241. []byte{
  242. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  243. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  244. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  245. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  246. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  247. 0x00, 0x00, 'g', 'a', 'r', 'b', 'a', 'g', 'e', '!', '!', '!',
  248. },
  249. ErrHeader,
  250. },
  251. { // has 1 non-empty fixed huffman block not enough header
  252. "hello.txt",
  253. "hello.txt + garbage",
  254. "hello world\n",
  255. []byte{
  256. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  257. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  258. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  259. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  260. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  261. 0x00, 0x00, gzipID1,
  262. },
  263. io.ErrUnexpectedEOF,
  264. },
  265. { // has 1 non-empty fixed huffman block but corrupt checksum
  266. "hello.txt",
  267. "hello.txt + corrupt checksum",
  268. "hello world\n",
  269. []byte{
  270. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  271. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  272. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  273. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  274. 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0c, 0x00,
  275. 0x00, 0x00,
  276. },
  277. ErrChecksum,
  278. },
  279. { // has 1 non-empty fixed huffman block but corrupt size
  280. "hello.txt",
  281. "hello.txt + corrupt size",
  282. "hello world\n",
  283. []byte{
  284. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  285. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  286. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  287. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  288. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0xff, 0x00,
  289. 0x00, 0x00,
  290. },
  291. ErrChecksum,
  292. },
  293. {
  294. "f1l3n4m3.tXt",
  295. "header with all fields used",
  296. "",
  297. []byte{
  298. 0x1f, 0x8b, 0x08, 0x1e, 0x70, 0xf0, 0xf9, 0x4a,
  299. 0x00, 0xaa, 0x09, 0x00, 0x7a, 0x7a, 0x05, 0x00,
  300. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x31, 0x6c,
  301. 0x33, 0x6e, 0x34, 0x6d, 0x33, 0x2e, 0x74, 0x58,
  302. 0x74, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  303. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  304. 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
  305. 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
  306. 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
  307. 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
  308. 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
  309. 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
  310. 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
  311. 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
  312. 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
  313. 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e,
  314. 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  315. 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
  316. 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
  317. 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e,
  318. 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
  319. 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
  320. 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
  321. 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
  322. 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
  323. 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae,
  324. 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  325. 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe,
  326. 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
  327. 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
  328. 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
  329. 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
  330. 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
  331. 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
  332. 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
  333. 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe,
  334. 0xff, 0x00, 0x92, 0xfd, 0x01, 0x00, 0x00, 0xff,
  335. 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  336. 0x00,
  337. },
  338. nil,
  339. },
  340. {
  341. "",
  342. "truncated gzip file amid raw-block",
  343. "hello",
  344. []byte{
  345. 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  346. 0x00, 0x0c, 0x00, 0xf3, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
  347. },
  348. io.ErrUnexpectedEOF,
  349. },
  350. {
  351. "",
  352. "truncated gzip file amid fixed-block",
  353. "He",
  354. []byte{
  355. 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  356. 0xf2, 0x48, 0xcd,
  357. },
  358. io.ErrUnexpectedEOF,
  359. },
  360. }
  361. func TestDecompressor(t *testing.T) {
  362. b := new(bytes.Buffer)
  363. for _, tt := range gunzipTests {
  364. in := bytes.NewReader(tt.gzip)
  365. gzip, err := NewReader(in)
  366. if err != nil {
  367. t.Errorf("%s: NewReader: %s", tt.name, err)
  368. continue
  369. }
  370. defer gzip.Close()
  371. if tt.name != gzip.Name {
  372. t.Errorf("%s: got name %s", tt.name, gzip.Name)
  373. }
  374. b.Reset()
  375. n, err := io.Copy(b, gzip)
  376. if err != tt.err {
  377. t.Errorf("%s: io.Copy: %v want %v", tt.name, err, tt.err)
  378. }
  379. s := b.String()
  380. if s != tt.raw {
  381. t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.name, n, s, len(tt.raw), tt.raw)
  382. }
  383. // Test Reader Reset.
  384. in = bytes.NewReader(tt.gzip)
  385. err = gzip.Reset(in)
  386. if err != nil {
  387. t.Errorf("%s: Reset: %s", tt.name, err)
  388. continue
  389. }
  390. if tt.name != gzip.Name {
  391. t.Errorf("%s: got name %s", tt.name, gzip.Name)
  392. }
  393. b.Reset()
  394. n, err = io.Copy(b, gzip)
  395. if err != tt.err {
  396. t.Errorf("%s: io.Copy: %v want %v", tt.name, err, tt.err)
  397. }
  398. s = b.String()
  399. if s != tt.raw {
  400. t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.name, n, s, len(tt.raw), tt.raw)
  401. }
  402. }
  403. }
  404. func TestIssue6550(t *testing.T) {
  405. f, err := os.Open("testdata/issue6550.gz")
  406. if err != nil {
  407. t.Fatal(err)
  408. }
  409. gzip, err := NewReader(f)
  410. if err != nil {
  411. t.Fatalf("NewReader(testdata/issue6550.gz): %v", err)
  412. }
  413. defer gzip.Close()
  414. done := make(chan bool, 1)
  415. go func() {
  416. _, err := io.Copy(ioutil.Discard, gzip)
  417. if err == nil {
  418. t.Errorf("Copy succeeded")
  419. } else {
  420. t.Logf("Copy failed (correctly): %v", err)
  421. }
  422. done <- true
  423. }()
  424. select {
  425. case <-time.After(1 * time.Second):
  426. t.Errorf("Copy hung")
  427. case <-done:
  428. // ok
  429. }
  430. }
  431. func TestInitialReset(t *testing.T) {
  432. var r Reader
  433. if err := r.Reset(bytes.NewReader(gunzipTests[1].gzip)); err != nil {
  434. t.Error(err)
  435. }
  436. var buf bytes.Buffer
  437. if _, err := io.Copy(&buf, &r); err != nil {
  438. t.Error(err)
  439. }
  440. if s := buf.String(); s != gunzipTests[1].raw {
  441. t.Errorf("got %q want %q", s, gunzipTests[1].raw)
  442. }
  443. }
  444. func TestMultistreamFalse(t *testing.T) {
  445. // Find concatenation test.
  446. var tt gunzipTest
  447. for _, tt = range gunzipTests {
  448. if strings.HasSuffix(tt.desc, " x2") {
  449. goto Found
  450. }
  451. }
  452. t.Fatal("cannot find hello.txt x2 in gunzip tests")
  453. Found:
  454. br := bytes.NewReader(tt.gzip)
  455. var r Reader
  456. if err := r.Reset(br); err != nil {
  457. t.Fatalf("first reset: %v", err)
  458. }
  459. // Expect two streams with "hello world\n", then real EOF.
  460. const hello = "hello world\n"
  461. r.Multistream(false)
  462. data, err := ioutil.ReadAll(&r)
  463. if string(data) != hello || err != nil {
  464. t.Fatalf("first stream = %q, %v, want %q, %v", string(data), err, hello, nil)
  465. }
  466. if err := r.Reset(br); err != nil {
  467. t.Fatalf("second reset: %v", err)
  468. }
  469. r.Multistream(false)
  470. data, err = ioutil.ReadAll(&r)
  471. if string(data) != hello || err != nil {
  472. t.Fatalf("second stream = %q, %v, want %q, %v", string(data), err, hello, nil)
  473. }
  474. if err := r.Reset(br); err != io.EOF {
  475. t.Fatalf("third reset: err=%v, want io.EOF", err)
  476. }
  477. }
  478. func TestWriteTo(t *testing.T) {
  479. input := make([]byte, 100000)
  480. n, err := rand.Read(input)
  481. if err != nil {
  482. t.Fatal(err)
  483. }
  484. if n != len(input) {
  485. t.Fatal("did not fill buffer")
  486. }
  487. compressed := &bytes.Buffer{}
  488. // Do it twice to test MultiStream functionality
  489. for i := 0; i < 2; i++ {
  490. w, err := NewWriterLevel(compressed, -2)
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. n, err = w.Write(input)
  495. if err != nil {
  496. t.Fatal(err)
  497. }
  498. if n != len(input) {
  499. t.Fatal("did not fill buffer")
  500. }
  501. w.Close()
  502. }
  503. input = append(input, input...)
  504. buf := compressed.Bytes()
  505. dec, err := NewReader(bytes.NewBuffer(buf))
  506. if err != nil {
  507. t.Fatal(err)
  508. }
  509. // ReadAll does not use WriteTo, but we wrap it in a NopCloser to be sure.
  510. readall, err := ioutil.ReadAll(ioutil.NopCloser(dec))
  511. if err != nil {
  512. t.Fatal(err)
  513. }
  514. if len(readall) != len(input) {
  515. t.Errorf("did not decompress everything, want %d, got %d", len(input), len(readall))
  516. }
  517. if bytes.Compare(readall, input) != 0 {
  518. t.Error("output did not match input")
  519. }
  520. dec, err = NewReader(bytes.NewBuffer(buf))
  521. if err != nil {
  522. t.Fatal(err)
  523. }
  524. wtbuf := &bytes.Buffer{}
  525. written, err := dec.WriteTo(wtbuf)
  526. if err != nil {
  527. t.Fatal(err)
  528. }
  529. if written != int64(len(input)) {
  530. t.Error("Returned length did not match, expected", len(input), "got", written)
  531. }
  532. if wtbuf.Len() != len(input) {
  533. t.Error("Actual Length did not match, expected", len(input), "got", wtbuf.Len())
  534. }
  535. if bytes.Compare(wtbuf.Bytes(), input) != 0 {
  536. t.Fatal("output did not match input")
  537. }
  538. }
  539. func TestNilStream(t *testing.T) {
  540. // Go liberally interprets RFC 1952 section 2.2 to mean that a gzip file
  541. // consist of zero or more members. Thus, we test that a nil stream is okay.
  542. _, err := NewReader(bytes.NewReader(nil))
  543. if err != io.EOF {
  544. t.Fatalf("NewReader(nil) on empty stream: got %v, want io.EOF", err)
  545. }
  546. }
  547. func TestTruncatedStreams(t *testing.T) {
  548. const data = "\x1f\x8b\b\x04\x00\tn\x88\x00\xff\a\x00foo bar\xcbH\xcd\xc9\xc9\xd7Q(\xcf/\xcaI\x01\x04:r\xab\xff\f\x00\x00\x00"
  549. // Intentionally iterate starting with at least one byte in the stream.
  550. for i := 1; i < len(data)-1; i++ {
  551. r, err := NewReader(strings.NewReader(data[:i]))
  552. if err != nil {
  553. if err != io.ErrUnexpectedEOF {
  554. t.Errorf("NewReader(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF)
  555. }
  556. continue
  557. }
  558. _, err = io.Copy(ioutil.Discard, r)
  559. if ferr, ok := err.(*flate.ReadError); ok {
  560. err = ferr.Err
  561. }
  562. if err != io.ErrUnexpectedEOF {
  563. t.Errorf("io.Copy(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF)
  564. }
  565. }
  566. }
  567. func BenchmarkGunzipCopy(b *testing.B) {
  568. dat, _ := ioutil.ReadFile("testdata/test.json")
  569. dat = append(dat, dat...)
  570. dat = append(dat, dat...)
  571. dat = append(dat, dat...)
  572. dat = append(dat, dat...)
  573. dat = append(dat, dat...)
  574. dst := &bytes.Buffer{}
  575. w, _ := NewWriterLevel(dst, 1)
  576. _, err := w.Write(dat)
  577. if err != nil {
  578. b.Fatal(err)
  579. }
  580. w.Close()
  581. input := dst.Bytes()
  582. b.SetBytes(int64(len(dat)))
  583. b.ResetTimer()
  584. for n := 0; n < b.N; n++ {
  585. r, err := NewReader(bytes.NewBuffer(input))
  586. if err != nil {
  587. b.Fatal(err)
  588. }
  589. _, err = io.Copy(ioutil.Discard, r)
  590. if err != nil {
  591. b.Fatal(err)
  592. }
  593. }
  594. }
  595. func BenchmarkGunzipNoWriteTo(b *testing.B) {
  596. dat, _ := ioutil.ReadFile("testdata/test.json")
  597. dat = append(dat, dat...)
  598. dat = append(dat, dat...)
  599. dat = append(dat, dat...)
  600. dat = append(dat, dat...)
  601. dat = append(dat, dat...)
  602. dst := &bytes.Buffer{}
  603. w, _ := NewWriterLevel(dst, 1)
  604. _, err := w.Write(dat)
  605. if err != nil {
  606. b.Fatal(err)
  607. }
  608. w.Close()
  609. input := dst.Bytes()
  610. r, err := NewReader(bytes.NewBuffer(input))
  611. if err != nil {
  612. b.Fatal(err)
  613. }
  614. b.SetBytes(int64(len(dat)))
  615. b.ResetTimer()
  616. for n := 0; n < b.N; n++ {
  617. err := r.Reset(bytes.NewBuffer(input))
  618. if err != nil {
  619. b.Fatal(err)
  620. }
  621. _, err = io.Copy(ioutil.Discard, ioutil.NopCloser(r))
  622. if err != nil {
  623. b.Fatal(err)
  624. }
  625. }
  626. }
  627. func BenchmarkGunzipStdlib(b *testing.B) {
  628. dat, _ := ioutil.ReadFile("testdata/test.json")
  629. dat = append(dat, dat...)
  630. dat = append(dat, dat...)
  631. dat = append(dat, dat...)
  632. dat = append(dat, dat...)
  633. dat = append(dat, dat...)
  634. dst := &bytes.Buffer{}
  635. w, _ := NewWriterLevel(dst, 1)
  636. _, err := w.Write(dat)
  637. if err != nil {
  638. b.Fatal(err)
  639. }
  640. w.Close()
  641. input := dst.Bytes()
  642. r, err := oldgz.NewReader(bytes.NewBuffer(input))
  643. if err != nil {
  644. b.Fatal(err)
  645. }
  646. b.SetBytes(int64(len(dat)))
  647. b.ResetTimer()
  648. for n := 0; n < b.N; n++ {
  649. err := r.Reset(bytes.NewBuffer(input))
  650. if err != nil {
  651. b.Fatal(err)
  652. }
  653. _, err = io.Copy(ioutil.Discard, r)
  654. if err != nil {
  655. b.Fatal(err)
  656. }
  657. }
  658. }
  659. func TestTruncatedGunzip(t *testing.T) {
  660. in := []byte(strings.Repeat("ASDFASDFASDFASDFASDF", 1000))
  661. var buf bytes.Buffer
  662. enc := NewWriter(&buf)
  663. _, err := enc.Write(in)
  664. if err != nil {
  665. t.Fatal(err)
  666. }
  667. enc.Close()
  668. testdata := buf.Bytes()
  669. for i := 5; i < len(testdata); i += 10 {
  670. timer := time.NewTimer(time.Second)
  671. done := make(chan struct{})
  672. fail := make(chan struct{})
  673. go func() {
  674. r, err := NewReader(bytes.NewBuffer(testdata[:i]))
  675. if err == nil {
  676. b, err := ioutil.ReadAll(r)
  677. if err == nil && !bytes.Equal(testdata[:i], b) {
  678. close(fail)
  679. }
  680. }
  681. close(done)
  682. }()
  683. select {
  684. case <-timer.C:
  685. t.Fatal("Timeout decoding")
  686. case <-fail:
  687. t.Fatal("No error, but mismatch")
  688. case <-done:
  689. timer.Stop()
  690. }
  691. }
  692. }