PageRenderTime 114ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/test/jruby/test_zlib.rb

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