PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jruby-1.7.3/test/test_zlib.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 955 lines | 779 code | 136 blank | 40 comment | 4 complexity | e7d310bce27adb275a1932111a92782b MD5 | raw file
  1. require 'test/unit'
  2. require 'zlib'
  3. require 'stringio'
  4. require 'tempfile'
  5. class TestZlib < Test::Unit::TestCase
  6. def teardown; File.unlink @filename if @filename; end
  7. def test_inflate_deflate
  8. s = "test comression string"
  9. [Zlib::NO_COMPRESSION, Zlib::BEST_SPEED, Zlib::BEST_COMPRESSION, Zlib::DEFAULT_COMPRESSION].each do |level|
  10. assert_equal(s, Zlib::Inflate.inflate(Zlib::Deflate.deflate(s, level)))
  11. end
  12. end
  13. # Zlib::Inflate uses org.jruby.util.ZlibInflate for low-level decompression logic, which is built around
  14. # java.util.zip.Inflater.
  15. #
  16. # Inflater, and hence ZlibInflate, can run in one of two basic modes:
  17. #
  18. # - "wrapped" IO stream: when working with an input stream that will contain
  19. # Zlib headers and checksum
  20. # - "unwrapped" IO stream: input streams that do not contain Zlib headers
  21. # and checksum
  22. #
  23. # This tests whether an instance of Zlib::Inflate correctly reports its completion status
  24. # after decompressing the full contents of an unwrapped input stream.
  25. #
  26. def test_inflate_should_be_finished_after_decompressing_full_unwrapped_stream
  27. actual = "test compression string\n" * 5
  28. # This is a base64-encoded representation of a zipped text file.
  29. # This is not the ZIP archive itself; rather, it is the compressed representation
  30. # of a deflated file inside the archive.
  31. data = "K0ktLlFIzs8tKEotLs7Mz1MoLinKzEvnKqGxOABQSwECFwMUAAIACAAxKng3dpOMHR0AAAB4AAAACAANAAAAAAABAAAApIEAAAAAdGVzdC50eHRVVAUAAz76R0dVeAAAUEsFBgAAAAABAAEAQwAAAFgAAAAAAA=="
  32. inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
  33. stream = StringIO.new(data.unpack('m*')[0])
  34. assert_equal(actual, inflater.inflate(stream.read(nil, '')), "Unexpected result of decompression.")
  35. assert(inflater.finished?, "Inflater should be finished after inflating all input.")
  36. end
  37. def test_gzip_reader_writer
  38. @filename = "____temp_zlib_file";
  39. Zlib::GzipWriter.open(@filename) { |z| z.puts 'HEH' }
  40. Zlib::GzipReader.open(@filename) do |z|
  41. assert_equal("HEH\n", z.gets)
  42. assert_nil z.getc
  43. assert z.eof?
  44. end
  45. File.unlink(@filename)
  46. Zlib::GzipWriter.open(@filename) { |z| z.write "HEH\n" }
  47. Zlib::GzipReader.open(@filename) do |z|
  48. assert_equal("HEH\n", z.gets)
  49. assert_nil z.getc
  50. assert z.eof?
  51. end
  52. File.unlink(@filename)
  53. z = Zlib::GzipWriter.open(@filename)
  54. z.puts 'HOH'
  55. z.puts 'foo|bar'
  56. z.close
  57. z = Zlib::GzipReader.open(@filename)
  58. assert_equal("HOH\n", z.gets)
  59. assert_equal("foo|", z.gets("|"))
  60. assert_equal("bar\n", z.gets)
  61. z.close
  62. end
  63. def test_native_exception_from_zlib_on_broken_header
  64. corrupt = StringIO.new
  65. corrupt.write('borkborkbork')
  66. begin
  67. Zlib::GzipReader.new(corrupt)
  68. flunk()
  69. rescue Zlib::GzipReader::Error
  70. end
  71. end
  72. def test_deflate_positive_winbits
  73. d = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, Zlib::MAX_WBITS)
  74. d << 'hello'
  75. res = d.finish
  76. assert_equal("x\234\313H\315\311\311\a\000\006,\002\025", res)
  77. end
  78. # negative winbits means no header and no checksum.
  79. def test_deflate_negative_winbits
  80. d = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, -Zlib::MAX_WBITS)
  81. d << 'hello'
  82. res = d.finish
  83. assert_equal("\313H\315\311\311\a\000", res)
  84. end
  85. # JRUBY-2228
  86. def test_gzipreader_descriptor_leak
  87. @filename = "____temp_zlib_file";
  88. Zlib::GzipWriter.open(@filename) { |z| z.puts 'HEH' }
  89. ios = [] # to prevent opened files GC'ed
  90. assert_nothing_raised() {
  91. 2048.times {
  92. z = Zlib::GzipReader.open(@filename)
  93. z.close
  94. ios << z
  95. }
  96. }
  97. end
  98. def test_wrap
  99. content = StringIO.new "", "r+"
  100. Zlib::GzipWriter.wrap(content) do |io|
  101. io.write "hello\nworld\n"
  102. end
  103. content = StringIO.new content.string, "rb"
  104. gin = Zlib::GzipReader.new(content)
  105. assert_equal("hello\n", gin.gets)
  106. assert_equal("world\n", gin.gets)
  107. assert_nil gin.gets
  108. assert gin.eof?
  109. gin.close
  110. end
  111. def test_each_line_no_block
  112. @filename = "____temp_zlib_file";
  113. Zlib::GzipWriter.open(@filename) { |io| io.write "hello\nworld\n" }
  114. lines = []
  115. z = Zlib::GzipReader.open(@filename)
  116. z.each_line do |line|
  117. lines << line
  118. end
  119. z.close
  120. assert_equal(2, lines.size, lines.inspect)
  121. assert_equal("hello\n", lines.first)
  122. assert_equal("world\n", lines.last)
  123. end
  124. def test_each_line_block
  125. @filename = "____temp_zlib_file";
  126. Zlib::GzipWriter.open(@filename) { |io| io.write "hello\nworld\n" }
  127. lines = []
  128. Zlib::GzipReader.open(@filename) do |z|
  129. z.each_line do |line|
  130. lines << line
  131. end
  132. end
  133. assert_equal(2, lines.size, lines.inspect)
  134. end
  135. def test_empty_line
  136. @filename = "____temp_zlib_file";
  137. Zlib::GzipWriter.open(@filename) { |io| io.write "hello\nworld\n\ngoodbye\n" }
  138. lines = nil
  139. Zlib::GzipReader.open(@filename) do |z|
  140. lines = z.readlines
  141. end
  142. assert_equal(4, lines.size, lines.inspect)
  143. end
  144. def test_inflate_empty_string
  145. assert_raise(Zlib::BufError) { Zlib::Inflate.inflate('') }
  146. end
  147. def test_inflate_bad_data
  148. assert_raise(Zlib::DataError) { Zlib::Inflate.inflate(' ')}
  149. end
  150. def test_inflate_finish
  151. z = Zlib::Inflate.new
  152. z << Zlib::Deflate.deflate('foo')
  153. assert_equal('foo', z.finish)
  154. assert(z.finished?)
  155. assert(!z.closed?)
  156. assert(!z.ended?)
  157. end
  158. def test_inflate_close
  159. z = Zlib::Inflate.new
  160. z << Zlib::Deflate.deflate('foo')
  161. z.close
  162. assert (z.closed?)
  163. assert (z.ended?)
  164. assert_raise(Zlib::Error) { z.finish }
  165. assert_raise(Zlib::Error) { z.finished? }
  166. end
  167. def test_inflate_end
  168. z = Zlib::Inflate.new
  169. z << Zlib::Deflate.deflate('foo')
  170. z.end
  171. assert (z.closed?)
  172. assert (z.ended?)
  173. assert_raise(Zlib::Error) { z.finish }
  174. assert_raise(Zlib::Error) { z.finished? }
  175. end
  176. def test_inflate_incomplete
  177. z = Zlib::Inflate.new
  178. z << Zlib::Deflate.deflate('foo')[0, 1]
  179. assert_raise(Zlib::BufError) { z.finish }
  180. z = Zlib::Inflate.new
  181. z << Zlib::Deflate.deflate('foo')[0, 1]
  182. assert_raise(Zlib::BufError) { z.inflate(nil) }
  183. end
  184. def test_inflate_finished
  185. z = Zlib::Inflate.new
  186. assert(!z.finished?)
  187. z << Zlib::Deflate.deflate('foo')
  188. assert(z.finished?)
  189. # on incomplete data
  190. z = Zlib::Inflate.new
  191. z << Zlib::Deflate.deflate('foo')[0, 1]
  192. assert(!z.finished?)
  193. end
  194. def test_inflate_flush_next_in
  195. z = Zlib::Inflate.new
  196. z << Zlib::Deflate.deflate('foo')
  197. assert_equal("", z.flush_next_in)
  198. assert_equal("foo", z.finish)
  199. assert_equal("", z.flush_next_in)
  200. end
  201. def test_inflate_sync
  202. z = Zlib::Inflate.new
  203. z << Zlib::Deflate.deflate('foo')
  204. assert (!z.sync(''))
  205. assert (!z.sync_point?)
  206. assert_equal("foo", z.finish)
  207. assert (!z.sync(''))
  208. assert (!z.sync_point?)
  209. end
  210. def test_sync
  211. z = Zlib::Deflate.new
  212. s = z.deflate("foo" * 1000, Zlib::FULL_FLUSH)
  213. z.avail_out = 0
  214. z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)
  215. s << z.deflate("bar" * 1000, Zlib::FULL_FLUSH)
  216. z.avail_out = 0
  217. z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)
  218. s << z.deflate("baz" * 1000, Zlib::FINISH)
  219. z = Zlib::Inflate.new
  220. assert_raise(Zlib::DataError) { z << "\0" * 100 }
  221. assert_equal(false, z.sync(""))
  222. assert_equal(false, z.sync_point?)
  223. z = Zlib::Inflate.new
  224. assert_raise(Zlib::DataError) { z << "\0" * 100 + s }
  225. assert_equal(true, z.sync(""))
  226. #assert_equal(true, z.sync_point?)
  227. assert(z.avail_in>0)
  228. z = Zlib::Inflate.new
  229. assert_equal(false, z.sync("\0" * 100))
  230. assert_equal(false, z.sync_point?)
  231. z = Zlib::Inflate.new
  232. assert_equal(true, z.sync("\0" * 100 + s))
  233. #assert_equal(true, z.sync_point?)
  234. end
  235. def test_inflate_broken_data_with_sync
  236. d = Zlib::Deflate.new
  237. i = Zlib::Inflate.new
  238. foo = d.deflate("foo", Zlib::SYNC_FLUSH)
  239. noise = "noise"+d.deflate("*", Zlib::SYNC_FLUSH)
  240. bar = d.deflate("bar", Zlib::SYNC_FLUSH)
  241. begin
  242. i << (foo+noise+bar)
  243. rescue Zlib::DataError
  244. end
  245. i.sync(d.finish)
  246. begin
  247. i.finish
  248. rescue Zlib::DataError # failed in checking checksum because of broken data
  249. end
  250. assert_equal("foobar", i.flush_next_out)
  251. end
  252. # JRUBY-4502: 1.4 raises native exception at gz.read
  253. def test_corrupted_data
  254. zip = "\037\213\b\000,\334\321G\000\005\000\235\005\000$\n\000\000"
  255. io = StringIO.new(zip)
  256. # JRuby cannot check corrupted data format at GzipReader.new for now
  257. # because of different input buffer handling.
  258. assert_raise(Zlib::DataError) do
  259. gz = Zlib::GzipReader.new(io) # CRuby raises here
  260. # if size of input is less that 2048
  261. gz.read # JRuby raises here
  262. end
  263. end
  264. def test_inflate_after_finish
  265. z = Zlib::Inflate.new
  266. data = "x\234c`\200\001\000\000\n\000\001"
  267. unzipped = z.inflate data
  268. z.finish # this is a precondition
  269. out = z.inflate('uncompressed_data')
  270. out << z.finish
  271. assert_equal('uncompressed_data', out)
  272. z << ('uncompressed_data') << nil
  273. assert_equal('uncompressed_data', z.finish)
  274. end
  275. def test_inflate_pass_through
  276. main_data = "x\234K\313\317\a\000\002\202\001E"
  277. result = ""
  278. z = Zlib::Inflate.new
  279. # add bytes, one by one
  280. (main_data * 2).each_byte { |d| result << z.inflate(d.chr)}
  281. assert_equal("foo", result)
  282. # the first chunk is inflated to its completion,
  283. # the second chunk is just passed through.
  284. result << z.finish
  285. assert_equal("foo" + main_data, result)
  286. end
  287. # JRUBY-4503: cruby-zlib does not require 'close'
  288. def test_gzip_writer_restricted_io
  289. z = Object.new
  290. def z.write(arg)
  291. (@buf ||= []) << arg
  292. end
  293. def z.buf
  294. @buf
  295. end
  296. assert_nil z.buf
  297. Zlib::GzipWriter.wrap(z) { |io| io.write("hello") }
  298. assert_not_nil z.buf
  299. end
  300. # JRUBY-4503: cruby-zlib does not require 'close'
  301. def test_gzip_reader_restricted_io
  302. z = Object.new
  303. def z.read(size)
  304. @buf ||= TestZlib.create_gzip_stream("hello")
  305. @buf.slice!(0, size)
  306. end
  307. called = false
  308. Zlib::GzipReader.wrap(z) { |io|
  309. assert_equal("hello", io.read)
  310. called = true
  311. }
  312. assert(called)
  313. end
  314. # JRUBY-4503: trailer CRC check failed
  315. def test_gzip_reader_trailer_from_buffer
  316. z = Object.new
  317. def z.read(size)
  318. @buf ||= TestZlib.create_gzip_stream("hello")
  319. # emulate sliced buffer reading
  320. @buf.slice!(0, 1)
  321. end
  322. called = false
  323. Zlib::GzipReader.wrap(z) { |io|
  324. assert_equal("hello", io.read)
  325. called = true
  326. }
  327. assert(called)
  328. end
  329. def test_gzip_reader_check_corrupted_trailer
  330. data = TestZlib.create_gzip_stream("hello")
  331. assert_raise(Zlib::GzipFile::CRCError) do
  332. _data = data.dup
  333. _data[_data.size-5] = 'X' # checksum
  334. gz = Zlib::GzipReader.new(StringIO.new(_data))
  335. gz.read
  336. gz.finish
  337. end
  338. assert_raise(Zlib::GzipFile::LengthError) do
  339. _data = data.dup
  340. _data[_data.size-4] = 'X' # length
  341. gz = Zlib::GzipReader.new(StringIO.new(_data))
  342. gz.read
  343. gz.finish
  344. end
  345. assert_raise(Zlib::GzipFile::NoFooter) do
  346. _data = data.dup
  347. _data = _data.slice!(0, _data.size-1)
  348. gz = Zlib::GzipReader.new(StringIO.new(_data))
  349. gz.read
  350. gz.finish
  351. end
  352. assert_raise(Zlib::GzipFile::NoFooter) do
  353. _data = data.dup
  354. _data[_data.size-5] = 'X' # checksum
  355. _data = _data.slice!(0, _data.size-1)
  356. gz = Zlib::GzipReader.new(StringIO.new(_data))
  357. gz.read
  358. gz.finish
  359. end
  360. end
  361. def self.create_gzip_stream(string)
  362. s = StringIO.new
  363. Zlib::GzipWriter.wrap(s) { |io|
  364. io.write("hello")
  365. }
  366. s.string
  367. end
  368. def test_dup
  369. d1 = Zlib::Deflate.new
  370. data = "foo" * 10
  371. d1 << data
  372. d2 = d1.dup
  373. d1 << "bar"
  374. d2 << "goo"
  375. data1 = d1.finish
  376. data2 = d2.finish
  377. i = Zlib::Inflate.new
  378. assert_equal(data+"bar",
  379. i.inflate(data1) + i.finish);
  380. i.reset
  381. assert_equal(data+"goo",
  382. i.inflate(data2) + i.finish);
  383. end
  384. def test_adler32_combine
  385. one = Zlib.adler32("fo")
  386. two = Zlib.adler32("o")
  387. begin
  388. assert_equal(0x02820145, Zlib.adler32_combine(one, two, 1))
  389. rescue NotImplementedError
  390. skip "adler32_combine is not implemented"
  391. end
  392. end
  393. def test_crc32_combine
  394. one = Zlib.crc32("fo")
  395. two = Zlib.crc32("o")
  396. begin
  397. assert_equal(0x8c736521, Zlib.crc32_combine(one, two, 1))
  398. rescue NotImplementedError
  399. skip "crc32_combine is not implemented"
  400. end
  401. end
  402. def test_writer_sync
  403. marker = "\x00\x00\xff\xff"
  404. sio = StringIO.new("")
  405. if RUBY_VERSION >= '1.9.0'
  406. sio.set_encoding "ASCII-8BIT"
  407. end
  408. Zlib::GzipWriter.wrap(sio) { |z|
  409. z.write 'a'
  410. z.sync = true
  411. z.write 'b' # marker
  412. z.write 'c' # marker
  413. z.sync = false
  414. z.write 'd'
  415. z.write 'e'
  416. assert_equal(false, z.sync);
  417. }
  418. data = sio.string
  419. i = Zlib::Inflate.new(Zlib::MAX_WBITS + 16)
  420. assert_equal("ab", i.inflate(data.slice!(0, data.index(marker)+4)))
  421. assert_equal("c", i.inflate(data.slice!(0, data.index(marker)+4)))
  422. assert_equal("de", i.inflate(data))
  423. end
  424. def test_writer_flush
  425. marker = "\x00\x00\xff\xff"
  426. sio = StringIO.new("")
  427. Zlib::GzipWriter.wrap(sio) { |z|
  428. z.write 'a'
  429. z.write 'b' # marker
  430. z.flush
  431. z.write 'c' # marker
  432. z.flush
  433. z.write 'd'
  434. z.write 'e'
  435. assert_equal(false, z.sync);
  436. }
  437. data = sio.string
  438. if RUBY_VERSION >= '1.9.0'
  439. #data.index(marker) will return nil
  440. else
  441. i = Zlib::Inflate.new(Zlib::MAX_WBITS + 16)
  442. assert_equal("ab", i.inflate(data.slice!(0, data.index(marker)+4)))
  443. assert_equal("c", i.inflate(data.slice!(0, data.index(marker)+4)))
  444. assert_equal("de", i.inflate(data))
  445. end
  446. end
  447. if RUBY_VERSION >= '1.9.0'
  448. def test_error_input
  449. t = Tempfile.new("test_zlib_gzip_reader_open")
  450. t.close
  451. e = assert_raise(Zlib::GzipFile::Error) {
  452. Zlib::GzipReader.open(t.path)
  453. }
  454. assert_equal("not in gzip format", e.message)
  455. assert_nil(e.input)
  456. open(t.path, "wb") {|f| f.write("foo")}
  457. e = assert_raise(Zlib::GzipFile::Error) {
  458. Zlib::GzipReader.open(t.path)
  459. }
  460. assert_equal("not in gzip format", e.message)
  461. assert_equal("foo", e.input)
  462. open(t.path, "wb") {|f| f.write("foobarzothoge")}
  463. e = assert_raise(Zlib::GzipFile::Error) {
  464. Zlib::GzipReader.open(t.path)
  465. }
  466. assert_equal("not in gzip format", e.message)
  467. assert_equal("foobarzothoge", e.input)
  468. end
  469. end
  470. end
  471. # Test for MAX_WBITS + 16
  472. class TestZlibDeflateGzip < Test::Unit::TestCase
  473. def test_deflate_gzip
  474. d = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, Zlib::MAX_WBITS + 16)
  475. d << "foo"
  476. s = d.finish
  477. assert_equal("foo", Zlib::GzipReader.new(StringIO.new(s)).read)
  478. end
  479. def test_deflate_gzip_compat
  480. z = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, Zlib::MAX_WBITS + 16)
  481. s = z.deflate("foo") + z.finish
  482. assert_equal("foo", Zlib::Inflate.new(Zlib::MAX_WBITS + 16).inflate(s))
  483. end
  484. def test_initialize
  485. z = Zlib::Deflate.new(8, 15+16)
  486. s = z.deflate("foo", Zlib::FINISH)
  487. assert_equal("foo", Zlib::Inflate.new(15+16).inflate(s))
  488. z = Zlib::Deflate.new(8, 15+16)
  489. s = z.deflate("foo")
  490. s << z.deflate(nil, Zlib::FINISH)
  491. assert_equal("foo", Zlib::Inflate.new(15+16).inflate(s))
  492. assert_raise(Zlib::StreamError) { Zlib::Deflate.new(10000) }
  493. end
  494. def test_addstr
  495. z = Zlib::Deflate.new(8, 15+16)
  496. z << "foo"
  497. s = z.deflate(nil, Zlib::FINISH)
  498. assert_equal("foo", Zlib::Inflate.new(15+16).inflate(s))
  499. end
  500. def test_flush
  501. z = Zlib::Deflate.new(8, 15+16)
  502. z << "foo"
  503. s = z.flush
  504. z << "bar"
  505. s << z.flush_next_in
  506. z << "baz"
  507. s << z.flush_next_out
  508. s << z.deflate("qux", Zlib::FINISH)
  509. assert_equal("foobarbazqux", Zlib::Inflate.new(15+16).inflate(s))
  510. end
  511. def test_avail
  512. z = Zlib::Deflate.new(8, 15+16)
  513. assert_equal(0, z.avail_in)
  514. assert_equal(0, z.avail_out)
  515. z << "foo"
  516. z.avail_out += 100
  517. z << "bar"
  518. s = z.finish
  519. assert_equal("foobar", Zlib::Inflate.new(15+16).inflate(s))
  520. end
  521. def test_total
  522. z = Zlib::Deflate.new(8, 15+16)
  523. 1000.times { z << "foo" }
  524. s = z.finish
  525. assert_equal(3000, z.total_in)
  526. assert_operator(3000, :>, z.total_out)
  527. assert_equal("foo" * 1000, Zlib::Inflate.new(15+16).inflate(s))
  528. end
  529. def test_data_type
  530. z = Zlib::Deflate.new(8, 15+16)
  531. assert([Zlib::ASCII, Zlib::BINARY, Zlib::UNKNOWN].include?(z.data_type))
  532. end
  533. def test_adler
  534. z = Zlib::Deflate.new(8, 15+16)
  535. z << "foo"
  536. s = z.finish
  537. assert_equal(0x8c736521, z.adler)
  538. end
  539. def test_finished_p
  540. z = Zlib::Deflate.new(8, 15+16)
  541. assert_equal(false, z.finished?)
  542. z << "foo"
  543. assert_equal(false, z.finished?)
  544. s = z.finish
  545. assert_equal(true, z.finished?)
  546. z.close
  547. assert_raise(Zlib::Error) { z.finished? }
  548. end
  549. def test_closed_p
  550. z = Zlib::Deflate.new(8, 15+16)
  551. assert_equal(false, z.closed?)
  552. z << "foo"
  553. assert_equal(false, z.closed?)
  554. s = z.finish
  555. assert_equal(false, z.closed?)
  556. z.close
  557. assert_equal(true, z.closed?)
  558. end
  559. def test_params
  560. z = Zlib::Deflate.new(8, 15+16)
  561. z << "foo"
  562. z.params(Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_STRATEGY)
  563. z << "bar"
  564. s = z.finish
  565. assert_equal("foobar", Zlib::Inflate.new(15+16).inflate(s))
  566. data = ('a'..'z').to_a.join
  567. z = Zlib::Deflate.new(Zlib::NO_COMPRESSION, Zlib::MAX_WBITS+16,
  568. Zlib::DEF_MEM_LEVEL, Zlib::DEFAULT_STRATEGY)
  569. z << data[0, 10]
  570. z.params(Zlib::BEST_COMPRESSION, Zlib::DEFAULT_STRATEGY)
  571. z << data[10 .. -1]
  572. assert_equal(data, Zlib::Inflate.new(15+16).inflate(z.finish))
  573. z = Zlib::Deflate.new(8, 15+16)
  574. s = z.deflate("foo", Zlib::FULL_FLUSH)
  575. z.avail_out = 0
  576. z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)
  577. s << z.deflate("bar", Zlib::FULL_FLUSH)
  578. z.avail_out = 0
  579. z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)
  580. s << z.deflate("baz", Zlib::FINISH)
  581. assert_equal("foobarbaz", Zlib::Inflate.new(15+16).inflate(s))
  582. z = Zlib::Deflate.new(8, 15+16)
  583. assert_raise(Zlib::StreamError) { z.params(10000, 10000) }
  584. z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
  585. end
  586. def test_reset
  587. z = Zlib::Deflate.new(Zlib::NO_COMPRESSION, 15+16)
  588. z << "foo"
  589. z.reset
  590. z << "bar"
  591. s = z.finish
  592. assert_equal("bar", Zlib::Inflate.new(15+16).inflate(s))
  593. end
  594. def test_close
  595. z = Zlib::Deflate.new(8, 15+16)
  596. z.close
  597. assert_raise(Zlib::Error) { z << "foo" }
  598. assert_raise(Zlib::Error) { z.reset }
  599. end
  600. COMPRESS_MSG = '0000000100100011010001010110011110001001101010111100110111101111'
  601. def test_deflate_no_flush
  602. d = Zlib::Deflate.new(8, 15+16)
  603. d.deflate(COMPRESS_MSG, Zlib::SYNC_FLUSH) # for header output
  604. assert(d.deflate(COMPRESS_MSG, Zlib::NO_FLUSH).empty?)
  605. assert(!d.finish.empty?)
  606. d.close
  607. end
  608. def test_deflate_sync_flush
  609. d = Zlib::Deflate.new(8, 15+16)
  610. assert_nothing_raised do
  611. d.deflate(COMPRESS_MSG, Zlib::SYNC_FLUSH)
  612. end
  613. assert(!d.finish.empty?)
  614. d.close
  615. end
  616. def test_deflate_sync_flush_inflate
  617. d = Zlib::Deflate.new(8, 15+16)
  618. i = Zlib::Inflate.new(15+16)
  619. "a".upto("z") do |c|
  620. assert_equal(c, i.inflate(d.deflate(c, Zlib::SYNC_FLUSH)))
  621. end
  622. i.inflate(d.finish)
  623. i.close
  624. d.close
  625. end
  626. def test_deflate_full_flush
  627. d = Zlib::Deflate.new(8, 15+16)
  628. assert_nothing_raised do
  629. d.deflate(COMPRESS_MSG, Zlib::FULL_FLUSH)
  630. end
  631. assert(!d.finish.empty?)
  632. d.close
  633. end
  634. def test_deflate_flush_finish
  635. d = Zlib::Deflate.new(8, 15+16)
  636. d.deflate("init", Zlib::SYNC_FLUSH) # for flushing header
  637. assert(!d.deflate(COMPRESS_MSG, Zlib::FINISH).empty?)
  638. d.close
  639. end
  640. def test_deflate_raise_after_finish
  641. d = Zlib::Deflate.new(8, 15+16)
  642. d.deflate("init")
  643. d.finish
  644. assert_raise(Zlib::StreamError) do
  645. d.deflate('foo')
  646. end
  647. #
  648. d = Zlib::Deflate.new(8, 15+16)
  649. d.deflate("init", Zlib::FINISH)
  650. assert_raise(Zlib::StreamError) do
  651. d.deflate('foo')
  652. end
  653. end
  654. end
  655. # Test for MAX_WBITS + 16
  656. class TestZlibInflateGzip < Test::Unit::TestCase
  657. def test_inflate_gzip
  658. Zlib::GzipWriter.wrap(sio = StringIO.new("")) { |gz| gz << "foo" }
  659. assert_equal("foo", Zlib::Inflate.new(Zlib::MAX_WBITS + 16).inflate(sio.string))
  660. i = Zlib::Inflate.new(Zlib::MAX_WBITS + 16)
  661. i << sio.string
  662. assert_equal("foo", i.finish)
  663. end
  664. def test_initialize
  665. assert_raise(Zlib::StreamError) { Zlib::Inflate.new(-1) }
  666. z = Zlib::Deflate.new(8, 15+16)
  667. s = z.deflate("foo") + z.finish
  668. z = Zlib::Inflate.new(15+16)
  669. z << s << nil
  670. assert_equal("foo", z.finish)
  671. end
  672. def test_inflate
  673. z = Zlib::Deflate.new(8, 15+16)
  674. s = z.deflate("foo") + z.finish
  675. z = Zlib::Inflate.new(15+16)
  676. s = z.inflate(s)
  677. s << z.inflate(nil)
  678. assert_equal("foo", s)
  679. z.inflate("foo") # ???
  680. z << "foo" # ???
  681. end
  682. def test_incomplete_finish
  683. gzip = "\x1f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x05\x00\x00"
  684. z = Zlib::Inflate.new(Zlib::MAX_WBITS + 16)
  685. assert_equal("hello", z.inflate(gzip))
  686. assert_raise(Zlib::BufError) do
  687. z.finish
  688. end
  689. end
  690. def test_wrong_length
  691. gzip = "\x1f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x04\x00\x00\x00"
  692. z = Zlib::Inflate.new(Zlib::MAX_WBITS + 16)
  693. assert_raise(Zlib::DataError) do
  694. z.inflate(gzip)
  695. end
  696. end
  697. def test_wrong_length_split_trailer
  698. gzip = "\x1f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x04\x00\x00"
  699. z = Zlib::Inflate.new(Zlib::MAX_WBITS + 16)
  700. assert_equal("hello", z.inflate(gzip))
  701. assert_raise(Zlib::DataError) do
  702. z.inflate("\x00")
  703. end
  704. end
  705. end
  706. # Test for MAX_WBITS + 32
  707. class TestZlibInflateAuto < Test::Unit::TestCase
  708. def test_inflate_auto_detection_zip
  709. s = Zlib::Deflate.deflate("foo")
  710. assert_equal("foo", Zlib::Inflate.new(Zlib::MAX_WBITS + 32).inflate(s))
  711. i = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
  712. i << s
  713. assert_equal("foo", i.finish)
  714. end
  715. def test_inflate_auto_detection_gzip
  716. Zlib::GzipWriter.wrap(sio = StringIO.new("")) { |gz| gz << "foo" }
  717. assert_equal("foo", Zlib::Inflate.new(Zlib::MAX_WBITS + 32).inflate(sio.string))
  718. i = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
  719. i << sio.string
  720. assert_equal("foo", i.finish)
  721. end
  722. def test_corrupted_header
  723. gz = Zlib::GzipWriter.new(StringIO.new(s = ""))
  724. gz.orig_name = "X"
  725. gz.comment = "Y"
  726. gz.print("foo")
  727. gz.finish
  728. # 14: magic(2) + method(1) + flag(1) + mtime(4) + exflag(1) + os(1) + orig_name(2) + comment(2)
  729. 1.upto(14) do |idx|
  730. assert_raise(Zlib::BufError, idx) do
  731. z = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
  732. p z.inflate(s[0, idx]) + z.finish
  733. end
  734. end
  735. end
  736. def test_split_header
  737. gz = Zlib::GzipWriter.new(StringIO.new(s = ""))
  738. gz.orig_name = "X"
  739. gz.comment = "Y"
  740. gz.print("foo")
  741. gz.finish
  742. # 14: magic(2) + method(1) + flag(1) + mtime(4) + exflag(1) + os(1) + orig_name(2) + comment(2)
  743. z = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
  744. assert_equal(
  745. "foo",
  746. [
  747. z.inflate(s.slice!(0, 5)),
  748. z.inflate(s.slice!(0, 5)),
  749. z.inflate(s.slice!(0, 5)),
  750. z.inflate(s.slice!(0, 5)),
  751. z.inflate(s.slice!(0, 5)),
  752. z.inflate(s.slice!(0, 5))
  753. ].join + z.finish
  754. )
  755. end
  756. def test_initialize
  757. assert_raise(Zlib::StreamError) { Zlib::Inflate.new(-1) }
  758. s = Zlib::Deflate.deflate("foo")
  759. z = Zlib::Inflate.new(15+32)
  760. z << s << nil
  761. assert_equal("foo", z.finish)
  762. end
  763. def test_inflate
  764. s = Zlib::Deflate.deflate("foo")
  765. z = Zlib::Inflate.new(15+32)
  766. s = z.inflate(s)
  767. s << z.inflate(nil)
  768. assert_equal("foo", s)
  769. z.inflate("foo") # ???
  770. z << "foo" # ???
  771. end
  772. def test_incomplete_finish
  773. gzip = "\x1f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x05\x00\x00"
  774. z = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
  775. assert_equal("hello", z.inflate(gzip))
  776. assert_raise(Zlib::BufError) do
  777. z.finish
  778. end
  779. end
  780. def test_wrong_length
  781. gzip = "\x1f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x04\x00\x00\x00"
  782. z = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
  783. assert_raise(Zlib::DataError) do
  784. z.inflate(gzip)
  785. end
  786. end
  787. def test_dictionary
  788. dict = "hello"
  789. str = "hello, hello!"
  790. d = Zlib::Deflate.new
  791. d.set_dictionary(dict)
  792. comp_str = d.deflate(str)
  793. comp_str << d.finish
  794. comp_str.size
  795. i = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
  796. begin
  797. i.inflate(comp_str)
  798. rescue Zlib::NeedDict
  799. end
  800. #i.reset
  801. i.set_dictionary(dict)
  802. i << ""
  803. assert_equal(str, i.finish)
  804. end
  805. def test_wrong_length_split_trailer
  806. gzip = "\x1f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x04\x00\x00"
  807. z = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
  808. assert_equal("hello", z.inflate(gzip))
  809. assert_raise(Zlib::DataError) do
  810. z.inflate("\x00")
  811. end
  812. end
  813. def test_deflate_full_flush
  814. z = Zlib::Deflate.new(8, 15)
  815. s = z.deflate("f", Zlib::FULL_FLUSH)
  816. s << z.deflate("b", Zlib::FINISH)
  817. assert_equal("x\332J\003\000\000\000\377\377K\002\000\0010\000\311", s)
  818. end
  819. end