gunzip_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. "compress/flate"
  8. "encoding/base64"
  9. "io"
  10. "os"
  11. "strings"
  12. "testing"
  13. "time"
  14. )
  15. type gunzipTest struct {
  16. name string
  17. desc string
  18. raw string
  19. gzip []byte
  20. err error
  21. }
  22. var gunzipTests = []gunzipTest{
  23. { // has 1 empty fixed-huffman block
  24. "empty.txt",
  25. "empty.txt",
  26. "",
  27. []byte{
  28. 0x1f, 0x8b, 0x08, 0x08, 0xf7, 0x5e, 0x14, 0x4a,
  29. 0x00, 0x03, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e,
  30. 0x74, 0x78, 0x74, 0x00, 0x03, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. },
  33. nil,
  34. },
  35. {
  36. "",
  37. "empty - with no file name",
  38. "",
  39. []byte{
  40. 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88,
  41. 0x00, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. },
  44. nil,
  45. },
  46. { // has 1 non-empty fixed huffman block
  47. "hello.txt",
  48. "hello.txt",
  49. "hello world\n",
  50. []byte{
  51. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  52. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  53. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  54. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  55. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  56. 0x00, 0x00,
  57. },
  58. nil,
  59. },
  60. { // concatenation
  61. "hello.txt",
  62. "hello.txt x2",
  63. "hello world\n" +
  64. "hello world\n",
  65. []byte{
  66. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  67. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  68. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  69. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  70. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  71. 0x00, 0x00,
  72. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  73. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  74. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  75. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  76. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  77. 0x00, 0x00,
  78. },
  79. nil,
  80. },
  81. { // has a fixed huffman block with some length-distance pairs
  82. "shesells.txt",
  83. "shesells.txt",
  84. "she sells seashells by the seashore\n",
  85. []byte{
  86. 0x1f, 0x8b, 0x08, 0x08, 0x72, 0x66, 0x8b, 0x4a,
  87. 0x00, 0x03, 0x73, 0x68, 0x65, 0x73, 0x65, 0x6c,
  88. 0x6c, 0x73, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x2b,
  89. 0xce, 0x48, 0x55, 0x28, 0x4e, 0xcd, 0xc9, 0x29,
  90. 0x06, 0x92, 0x89, 0xc5, 0x19, 0x60, 0x56, 0x52,
  91. 0xa5, 0x42, 0x09, 0x58, 0x18, 0x28, 0x90, 0x5f,
  92. 0x94, 0xca, 0x05, 0x00, 0x76, 0xb0, 0x3b, 0xeb,
  93. 0x24, 0x00, 0x00, 0x00,
  94. },
  95. nil,
  96. },
  97. { // has dynamic huffman blocks
  98. "gettysburg",
  99. "gettysburg",
  100. " Four score and seven years ago our fathers brought forth on\n" +
  101. "this continent, a new nation, conceived in Liberty, and dedicated\n" +
  102. "to the proposition that all men are created equal.\n" +
  103. " Now we are engaged in a great Civil War, testing whether that\n" +
  104. "nation, or any nation so conceived and so dedicated, can long\n" +
  105. "endure.\n" +
  106. " We are met on a great battle-field of that war.\n" +
  107. " We have come to dedicate a portion of that field, as a final\n" +
  108. "resting place for those who here gave their lives that that\n" +
  109. "nation might live. It is altogether fitting and proper that\n" +
  110. "we should do this.\n" +
  111. " But, in a larger sense, we can not dedicate — we can not\n" +
  112. "consecrate — we can not hallow — this ground.\n" +
  113. " The brave men, living and dead, who struggled here, have\n" +
  114. "consecrated it, far above our poor power to add or detract.\n" +
  115. "The world will little note, nor long remember what we say here,\n" +
  116. "but it can never forget what they did here.\n" +
  117. " It is for us the living, rather, to be dedicated here to the\n" +
  118. "unfinished work which they who fought here have thus far so\n" +
  119. "nobly advanced. It is rather for us to be here dedicated to\n" +
  120. "the great task remaining before us — that from these honored\n" +
  121. "dead we take increased devotion to that cause for which they\n" +
  122. "gave the last full measure of devotion —\n" +
  123. " that we here highly resolve that these dead shall not have\n" +
  124. "died in vain — that this nation, under God, shall have a new\n" +
  125. "birth of freedom — and that government of the people, by the\n" +
  126. "people, for the people, shall not perish from this earth.\n" +
  127. "\n" +
  128. "Abraham Lincoln, November 19, 1863, Gettysburg, Pennsylvania\n",
  129. []byte{
  130. 0x1f, 0x8b, 0x08, 0x08, 0xd1, 0x12, 0x2b, 0x4a,
  131. 0x00, 0x03, 0x67, 0x65, 0x74, 0x74, 0x79, 0x73,
  132. 0x62, 0x75, 0x72, 0x67, 0x00, 0x65, 0x54, 0xcd,
  133. 0x6e, 0xd4, 0x30, 0x10, 0xbe, 0xfb, 0x29, 0xe6,
  134. 0x01, 0x42, 0xa5, 0x0a, 0x09, 0xc1, 0x11, 0x90,
  135. 0x40, 0x48, 0xa8, 0xe2, 0x80, 0xd4, 0xf3, 0x24,
  136. 0x9e, 0x24, 0x56, 0xbd, 0x9e, 0xc5, 0x76, 0x76,
  137. 0x95, 0x1b, 0x0f, 0xc1, 0x13, 0xf2, 0x24, 0x7c,
  138. 0x63, 0x77, 0x9b, 0x4a, 0x5c, 0xaa, 0x6e, 0x6c,
  139. 0xcf, 0x7c, 0x7f, 0x33, 0x44, 0x5f, 0x74, 0xcb,
  140. 0x54, 0x26, 0xcd, 0x42, 0x9c, 0x3c, 0x15, 0xb9,
  141. 0x48, 0xa2, 0x5d, 0x38, 0x17, 0xe2, 0x45, 0xc9,
  142. 0x4e, 0x67, 0xae, 0xab, 0xe0, 0xf7, 0x98, 0x75,
  143. 0x5b, 0xd6, 0x4a, 0xb3, 0xe6, 0xba, 0x92, 0x26,
  144. 0x57, 0xd7, 0x50, 0x68, 0xd2, 0x54, 0x43, 0x92,
  145. 0x54, 0x07, 0x62, 0x4a, 0x72, 0xa5, 0xc4, 0x35,
  146. 0x68, 0x1a, 0xec, 0x60, 0x92, 0x70, 0x11, 0x4f,
  147. 0x21, 0xd1, 0xf7, 0x30, 0x4a, 0xae, 0xfb, 0xd0,
  148. 0x9a, 0x78, 0xf1, 0x61, 0xe2, 0x2a, 0xde, 0x55,
  149. 0x25, 0xd4, 0xa6, 0x73, 0xd6, 0xb3, 0x96, 0x60,
  150. 0xef, 0xf0, 0x9b, 0x2b, 0x71, 0x8c, 0x74, 0x02,
  151. 0x10, 0x06, 0xac, 0x29, 0x8b, 0xdd, 0x25, 0xf9,
  152. 0xb5, 0x71, 0xbc, 0x73, 0x44, 0x0f, 0x7a, 0xa5,
  153. 0xab, 0xb4, 0x33, 0x49, 0x0b, 0x2f, 0xbd, 0x03,
  154. 0xd3, 0x62, 0x17, 0xe9, 0x73, 0xb8, 0x84, 0x48,
  155. 0x8f, 0x9c, 0x07, 0xaa, 0x52, 0x00, 0x6d, 0xa1,
  156. 0xeb, 0x2a, 0xc6, 0xa0, 0x95, 0x76, 0x37, 0x78,
  157. 0x9a, 0x81, 0x65, 0x7f, 0x46, 0x4b, 0x45, 0x5f,
  158. 0xe1, 0x6d, 0x42, 0xe8, 0x01, 0x13, 0x5c, 0x38,
  159. 0x51, 0xd4, 0xb4, 0x38, 0x49, 0x7e, 0xcb, 0x62,
  160. 0x28, 0x1e, 0x3b, 0x82, 0x93, 0x54, 0x48, 0xf1,
  161. 0xd2, 0x7d, 0xe4, 0x5a, 0xa3, 0xbc, 0x99, 0x83,
  162. 0x44, 0x4f, 0x3a, 0x77, 0x36, 0x57, 0xce, 0xcf,
  163. 0x2f, 0x56, 0xbe, 0x80, 0x90, 0x9e, 0x84, 0xea,
  164. 0x51, 0x1f, 0x8f, 0xcf, 0x90, 0xd4, 0x60, 0xdc,
  165. 0x5e, 0xb4, 0xf7, 0x10, 0x0b, 0x26, 0xe0, 0xff,
  166. 0xc4, 0xd1, 0xe5, 0x67, 0x2e, 0xe7, 0xc8, 0x93,
  167. 0x98, 0x05, 0xb8, 0xa8, 0x45, 0xc0, 0x4d, 0x09,
  168. 0xdc, 0x84, 0x16, 0x2b, 0x0d, 0x9a, 0x21, 0x53,
  169. 0x04, 0x8b, 0xd2, 0x0b, 0xbd, 0xa2, 0x4c, 0xa7,
  170. 0x60, 0xee, 0xd9, 0xe1, 0x1d, 0xd1, 0xb7, 0x4a,
  171. 0x30, 0x8f, 0x63, 0xd5, 0xa5, 0x8b, 0x33, 0x87,
  172. 0xda, 0x1a, 0x18, 0x79, 0xf3, 0xe3, 0xa6, 0x17,
  173. 0x94, 0x2e, 0xab, 0x6e, 0xa0, 0xe3, 0xcd, 0xac,
  174. 0x50, 0x8c, 0xca, 0xa7, 0x0d, 0x76, 0x37, 0xd1,
  175. 0x23, 0xe7, 0x05, 0x57, 0x8b, 0xa4, 0x22, 0x83,
  176. 0xd9, 0x62, 0x52, 0x25, 0xad, 0x07, 0xbb, 0xbf,
  177. 0xbf, 0xff, 0xbc, 0xfa, 0xee, 0x20, 0x73, 0x91,
  178. 0x29, 0xff, 0x7f, 0x02, 0x71, 0x62, 0x84, 0xb5,
  179. 0xf6, 0xb5, 0x25, 0x6b, 0x41, 0xde, 0x92, 0xb7,
  180. 0x76, 0x3f, 0x91, 0x91, 0x31, 0x1b, 0x41, 0x84,
  181. 0x62, 0x30, 0x0a, 0x37, 0xa4, 0x5e, 0x18, 0x3a,
  182. 0x99, 0x08, 0xa5, 0xe6, 0x6d, 0x59, 0x22, 0xec,
  183. 0x33, 0x39, 0x86, 0x26, 0xf5, 0xab, 0x66, 0xc8,
  184. 0x08, 0x20, 0xcf, 0x0c, 0xd7, 0x47, 0x45, 0x21,
  185. 0x0b, 0xf6, 0x59, 0xd5, 0xfe, 0x5c, 0x8d, 0xaa,
  186. 0x12, 0x7b, 0x6f, 0xa1, 0xf0, 0x52, 0x33, 0x4f,
  187. 0xf5, 0xce, 0x59, 0xd3, 0xab, 0x66, 0x10, 0xbf,
  188. 0x06, 0xc4, 0x31, 0x06, 0x73, 0xd6, 0x80, 0xa2,
  189. 0x78, 0xc2, 0x45, 0xcb, 0x03, 0x65, 0x39, 0xc9,
  190. 0x09, 0xd1, 0x06, 0x04, 0x33, 0x1a, 0x5a, 0xf1,
  191. 0xde, 0x01, 0xb8, 0x71, 0x83, 0xc4, 0xb5, 0xb3,
  192. 0xc3, 0x54, 0x65, 0x33, 0x0d, 0x5a, 0xf7, 0x9b,
  193. 0x90, 0x7c, 0x27, 0x1f, 0x3a, 0x58, 0xa3, 0xd8,
  194. 0xfd, 0x30, 0x5f, 0xb7, 0xd2, 0x66, 0xa2, 0x93,
  195. 0x1c, 0x28, 0xb7, 0xe9, 0x1b, 0x0c, 0xe1, 0x28,
  196. 0x47, 0x26, 0xbb, 0xe9, 0x7d, 0x7e, 0xdc, 0x96,
  197. 0x10, 0x92, 0x50, 0x56, 0x7c, 0x06, 0xe2, 0x27,
  198. 0xb4, 0x08, 0xd3, 0xda, 0x7b, 0x98, 0x34, 0x73,
  199. 0x9f, 0xdb, 0xf6, 0x62, 0xed, 0x31, 0x41, 0x13,
  200. 0xd3, 0xa2, 0xa8, 0x4b, 0x3a, 0xc6, 0x1d, 0xe4,
  201. 0x2f, 0x8c, 0xf8, 0xfb, 0x97, 0x64, 0xf4, 0xb6,
  202. 0x2f, 0x80, 0x5a, 0xf3, 0x56, 0xe0, 0x40, 0x50,
  203. 0xd5, 0x19, 0xd0, 0x1e, 0xfc, 0xca, 0xe5, 0xc9,
  204. 0xd4, 0x60, 0x00, 0x81, 0x2e, 0xa3, 0xcc, 0xb6,
  205. 0x52, 0xf0, 0xb4, 0xdb, 0x69, 0x99, 0xce, 0x7a,
  206. 0x32, 0x4c, 0x08, 0xed, 0xaa, 0x10, 0x10, 0xe3,
  207. 0x6f, 0xee, 0x99, 0x68, 0x95, 0x9f, 0x04, 0x71,
  208. 0xb2, 0x49, 0x2f, 0x62, 0xa6, 0x5e, 0xb4, 0xef,
  209. 0x02, 0xed, 0x4f, 0x27, 0xde, 0x4a, 0x0f, 0xfd,
  210. 0xc1, 0xcc, 0xdd, 0x02, 0x8f, 0x08, 0x16, 0x54,
  211. 0xdf, 0xda, 0xca, 0xe0, 0x82, 0xf1, 0xb4, 0x31,
  212. 0x7a, 0xa9, 0x81, 0xfe, 0x90, 0xb7, 0x3e, 0xdb,
  213. 0xd3, 0x35, 0xc0, 0x20, 0x80, 0x33, 0x46, 0x4a,
  214. 0x63, 0xab, 0xd1, 0x0d, 0x29, 0xd2, 0xe2, 0x84,
  215. 0xb8, 0xdb, 0xfa, 0xe9, 0x89, 0x44, 0x86, 0x7c,
  216. 0xe8, 0x0b, 0xe6, 0x02, 0x6a, 0x07, 0x9b, 0x96,
  217. 0xd0, 0xdb, 0x2e, 0x41, 0x4c, 0xa1, 0xd5, 0x57,
  218. 0x45, 0x14, 0xfb, 0xe3, 0xa6, 0x72, 0x5b, 0x87,
  219. 0x6e, 0x0c, 0x6d, 0x5b, 0xce, 0xe0, 0x2f, 0xe2,
  220. 0x21, 0x81, 0x95, 0xb0, 0xe8, 0xb6, 0x32, 0x0b,
  221. 0xb2, 0x98, 0x13, 0x52, 0x5d, 0xfb, 0xec, 0x63,
  222. 0x17, 0x8a, 0x9e, 0x23, 0x22, 0x36, 0xee, 0xcd,
  223. 0xda, 0xdb, 0xcf, 0x3e, 0xf1, 0xc7, 0xf1, 0x01,
  224. 0x12, 0x93, 0x0a, 0xeb, 0x6f, 0xf2, 0x02, 0x15,
  225. 0x96, 0x77, 0x5d, 0xef, 0x9c, 0xfb, 0x88, 0x91,
  226. 0x59, 0xf9, 0x84, 0xdd, 0x9b, 0x26, 0x8d, 0x80,
  227. 0xf9, 0x80, 0x66, 0x2d, 0xac, 0xf7, 0x1f, 0x06,
  228. 0xba, 0x7f, 0xff, 0xee, 0xed, 0x40, 0x5f, 0xa5,
  229. 0xd6, 0xbd, 0x8c, 0x5b, 0x46, 0xd2, 0x7e, 0x48,
  230. 0x4a, 0x65, 0x8f, 0x08, 0x42, 0x60, 0xf7, 0x0f,
  231. 0xb9, 0x16, 0x0b, 0x0c, 0x1a, 0x06, 0x00, 0x00,
  232. },
  233. nil,
  234. },
  235. { // has 1 non-empty fixed huffman block then garbage
  236. "hello.txt",
  237. "hello.txt + garbage",
  238. "hello world\n",
  239. []byte{
  240. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  241. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  242. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  243. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  244. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  245. 0x00, 0x00, 'g', 'a', 'r', 'b', 'a', 'g', 'e', '!', '!', '!',
  246. },
  247. ErrHeader,
  248. },
  249. { // has 1 non-empty fixed huffman block not enough header
  250. "hello.txt",
  251. "hello.txt + garbage",
  252. "hello world\n",
  253. []byte{
  254. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  255. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  256. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  257. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  258. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00,
  259. 0x00, 0x00, gzipID1,
  260. },
  261. io.ErrUnexpectedEOF,
  262. },
  263. { // has 1 non-empty fixed huffman block but corrupt checksum
  264. "hello.txt",
  265. "hello.txt + corrupt checksum",
  266. "hello world\n",
  267. []byte{
  268. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  269. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  270. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  271. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  272. 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0c, 0x00,
  273. 0x00, 0x00,
  274. },
  275. ErrChecksum,
  276. },
  277. { // has 1 non-empty fixed huffman block but corrupt size
  278. "hello.txt",
  279. "hello.txt + corrupt size",
  280. "hello world\n",
  281. []byte{
  282. 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a,
  283. 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
  284. 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9,
  285. 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
  286. 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0xff, 0x00,
  287. 0x00, 0x00,
  288. },
  289. ErrChecksum,
  290. },
  291. {
  292. "f1l3n4m3.tXt",
  293. "header with all fields used",
  294. "",
  295. []byte{
  296. 0x1f, 0x8b, 0x08, 0x1e, 0x70, 0xf0, 0xf9, 0x4a,
  297. 0x00, 0xaa, 0x09, 0x00, 0x7a, 0x7a, 0x05, 0x00,
  298. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x31, 0x6c,
  299. 0x33, 0x6e, 0x34, 0x6d, 0x33, 0x2e, 0x74, 0x58,
  300. 0x74, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  301. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  302. 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
  303. 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
  304. 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
  305. 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
  306. 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
  307. 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
  308. 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
  309. 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
  310. 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
  311. 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e,
  312. 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  313. 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
  314. 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
  315. 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e,
  316. 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
  317. 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
  318. 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
  319. 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
  320. 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
  321. 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae,
  322. 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  323. 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe,
  324. 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
  325. 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
  326. 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
  327. 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
  328. 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
  329. 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
  330. 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
  331. 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe,
  332. 0xff, 0x00, 0x92, 0xfd, 0x01, 0x00, 0x00, 0xff,
  333. 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  334. 0x00,
  335. },
  336. nil,
  337. },
  338. {
  339. "",
  340. "truncated gzip file amid raw-block",
  341. "hello",
  342. []byte{
  343. 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  344. 0x00, 0x0c, 0x00, 0xf3, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
  345. },
  346. io.ErrUnexpectedEOF,
  347. },
  348. {
  349. "",
  350. "truncated gzip file amid fixed-block",
  351. "He",
  352. []byte{
  353. 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  354. 0xf2, 0x48, 0xcd,
  355. },
  356. io.ErrUnexpectedEOF,
  357. },
  358. }
  359. func TestDecompressor(t *testing.T) {
  360. // Keep resetting this reader.
  361. // It is intended behavior that Reader.Reset can be called on a zero-value
  362. // Reader and be the equivalent as if NewReader was used instead.
  363. r1 := new(Reader)
  364. b := new(bytes.Buffer)
  365. for _, tt := range gunzipTests {
  366. // Test NewReader.
  367. in := bytes.NewReader(tt.gzip)
  368. r2, err := NewReader(in)
  369. if err != nil {
  370. t.Errorf("%s: NewReader: %s", tt.desc, err)
  371. continue
  372. }
  373. defer r2.Close()
  374. if tt.name != r2.Name {
  375. t.Errorf("%s: got name %s", tt.desc, r2.Name)
  376. }
  377. b.Reset()
  378. n, err := io.Copy(b, r2)
  379. if err != tt.err {
  380. t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
  381. }
  382. s := b.String()
  383. if s != tt.raw {
  384. t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw)
  385. }
  386. // Test Reader.Reset.
  387. in = bytes.NewReader(tt.gzip)
  388. err = r1.Reset(in)
  389. if err != nil {
  390. t.Errorf("%s: Reset: %s", tt.desc, err)
  391. continue
  392. }
  393. if tt.name != r1.Name {
  394. t.Errorf("%s: got name %s", tt.desc, r1.Name)
  395. }
  396. b.Reset()
  397. n, err = io.Copy(b, r1)
  398. if err != tt.err {
  399. t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
  400. }
  401. s = b.String()
  402. if s != tt.raw {
  403. t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw)
  404. }
  405. }
  406. }
  407. func TestIssue6550(t *testing.T) {
  408. // Apple’s notarization service will recursively attempt to decompress
  409. // files in order to find binaries to notarize. Since the service is
  410. // unable to decompress this file, it may reject the entire toolchain. Use a
  411. // base64-encoded version to avoid this.
  412. // See golang.org/issue/34986
  413. f, err := os.Open("testdata/issue6550.gz.base64")
  414. if err != nil {
  415. t.Fatal(err)
  416. }
  417. gzip, err := NewReader(base64.NewDecoder(base64.StdEncoding, f))
  418. if err != nil {
  419. t.Fatalf("NewReader(testdata/issue6550.gz): %v", err)
  420. }
  421. defer gzip.Close()
  422. done := make(chan bool, 1)
  423. go func() {
  424. _, err := io.Copy(io.Discard, gzip)
  425. if err == nil {
  426. t.Errorf("Copy succeeded")
  427. } else {
  428. t.Logf("Copy failed (correctly): %v", err)
  429. }
  430. done <- true
  431. }()
  432. select {
  433. case <-time.After(1 * time.Second):
  434. t.Errorf("Copy hung")
  435. case <-done:
  436. // ok
  437. }
  438. }
  439. func TestMultistreamFalse(t *testing.T) {
  440. // Find concatenation test.
  441. var tt gunzipTest
  442. for _, tt = range gunzipTests {
  443. if strings.HasSuffix(tt.desc, " x2") {
  444. goto Found
  445. }
  446. }
  447. t.Fatal("cannot find hello.txt x2 in gunzip tests")
  448. Found:
  449. br := bytes.NewReader(tt.gzip)
  450. var r Reader
  451. if err := r.Reset(br); err != nil {
  452. t.Fatalf("first reset: %v", err)
  453. }
  454. // Expect two streams with "hello world\n", then real EOF.
  455. const hello = "hello world\n"
  456. r.Multistream(false)
  457. data, err := io.ReadAll(&r)
  458. if string(data) != hello || err != nil {
  459. t.Fatalf("first stream = %q, %v, want %q, %v", string(data), err, hello, nil)
  460. }
  461. if err := r.Reset(br); err != nil {
  462. t.Fatalf("second reset: %v", err)
  463. }
  464. r.Multistream(false)
  465. data, err = io.ReadAll(&r)
  466. if string(data) != hello || err != nil {
  467. t.Fatalf("second stream = %q, %v, want %q, %v", string(data), err, hello, nil)
  468. }
  469. if err := r.Reset(br); err != io.EOF {
  470. t.Fatalf("third reset: err=%v, want io.EOF", err)
  471. }
  472. }
  473. func TestNilStream(t *testing.T) {
  474. // Go liberally interprets RFC 1952 section 2.2 to mean that a gzip file
  475. // consist of zero or more members. Thus, we test that a nil stream is okay.
  476. _, err := NewReader(bytes.NewReader(nil))
  477. if err != io.EOF {
  478. t.Fatalf("NewReader(nil) on empty stream: got %v, want io.EOF", err)
  479. }
  480. }
  481. func TestTruncatedStreams(t *testing.T) {
  482. 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"
  483. // Intentionally iterate starting with at least one byte in the stream.
  484. for i := 1; i < len(data)-1; i++ {
  485. r, err := NewReader(strings.NewReader(data[:i]))
  486. if err != nil {
  487. if err != io.ErrUnexpectedEOF {
  488. t.Errorf("NewReader(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF)
  489. }
  490. continue
  491. }
  492. _, err = io.Copy(io.Discard, r)
  493. if ferr, ok := err.(*flate.ReadError); ok {
  494. err = ferr.Err
  495. }
  496. if err != io.ErrUnexpectedEOF {
  497. t.Errorf("io.Copy(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF)
  498. }
  499. }
  500. }