PageRenderTime 62ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/test/zlib/test_zlib.rb

http://github.com/ruby/ruby
Ruby | 1243 lines | 1025 code | 210 blank | 8 comment | 3 complexity | d5f8d4e10aaa8c594ab9e062c8c3144f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # coding: us-ascii
  2. # frozen_string_literal: true
  3. require 'test/unit'
  4. require 'stringio'
  5. require 'tempfile'
  6. begin
  7. require 'zlib'
  8. rescue LoadError
  9. end
  10. if defined? Zlib
  11. class TestZlibDeflate < Test::Unit::TestCase
  12. def test_initialize
  13. z = Zlib::Deflate.new
  14. s = z.deflate("foo", Zlib::FINISH)
  15. assert_equal("foo", Zlib::Inflate.inflate(s))
  16. z = Zlib::Deflate.new
  17. s = z.deflate("foo")
  18. s << z.deflate(nil, Zlib::FINISH)
  19. assert_equal("foo", Zlib::Inflate.inflate(s))
  20. assert_raise(Zlib::StreamError) { Zlib::Deflate.new(10000) }
  21. end
  22. def test_dup
  23. z1 = Zlib::Deflate.new
  24. s = z1.deflate("foo")
  25. z2 = z1.dup
  26. s1 = s + z1.deflate("bar", Zlib::FINISH)
  27. s2 = s + z2.deflate("baz", Zlib::FINISH)
  28. assert_equal("foobar", Zlib::Inflate.inflate(s1))
  29. assert_equal("foobaz", Zlib::Inflate.inflate(s2))
  30. end
  31. def test_deflate
  32. s = Zlib::Deflate.deflate("foo")
  33. assert_equal("foo", Zlib::Inflate.inflate(s))
  34. assert_raise(Zlib::StreamError) { Zlib::Deflate.deflate("foo", 10000) }
  35. end
  36. def test_deflate_chunked
  37. original = ''.dup
  38. chunks = []
  39. r = Random.new 0
  40. z = Zlib::Deflate.new
  41. 2.times do
  42. input = r.bytes(20000)
  43. original << input
  44. z.deflate(input) do |chunk|
  45. chunks << chunk
  46. end
  47. end
  48. assert_equal [16384, 16384],
  49. chunks.map { |chunk| chunk.length }
  50. final = z.finish
  51. assert_equal 7253, final.length
  52. chunks << final
  53. all = chunks.join
  54. inflated = Zlib.inflate all
  55. assert_equal original, inflated
  56. end
  57. def test_deflate_chunked_break
  58. chunks = []
  59. r = Random.new 0
  60. z = Zlib::Deflate.new
  61. input = r.bytes(20000)
  62. z.deflate(input) do |chunk|
  63. chunks << chunk
  64. break
  65. end
  66. assert_equal [16384], chunks.map { |chunk| chunk.length }
  67. final = z.finish
  68. assert_equal 3632, final.length
  69. all = chunks.join
  70. all << final
  71. original = Zlib.inflate all
  72. assert_equal input, original
  73. end
  74. def test_addstr
  75. z = Zlib::Deflate.new
  76. z << "foo"
  77. s = z.deflate(nil, Zlib::FINISH)
  78. assert_equal("foo", Zlib::Inflate.inflate(s))
  79. end
  80. def test_flush
  81. z = Zlib::Deflate.new
  82. z << "foo"
  83. s = z.flush
  84. z << "bar"
  85. s << z.flush_next_in
  86. z << "baz"
  87. s << z.flush_next_out
  88. s << z.deflate("qux", Zlib::FINISH)
  89. assert_equal("foobarbazqux", Zlib::Inflate.inflate(s))
  90. end
  91. def test_avail
  92. z = Zlib::Deflate.new
  93. assert_equal(0, z.avail_in)
  94. assert_equal(0, z.avail_out)
  95. z << "foo"
  96. z.avail_out += 100
  97. z << "bar"
  98. s = z.finish
  99. assert_equal("foobar", Zlib::Inflate.inflate(s))
  100. end
  101. def test_expand_buffer;
  102. z = Zlib::Deflate.new
  103. src = "baz" * 1000
  104. z.avail_out = 1
  105. GC.stress = true
  106. s = z.deflate(src, Zlib::FINISH)
  107. GC.stress = false
  108. assert_equal(src, Zlib::Inflate.inflate(s))
  109. end
  110. def test_total
  111. z = Zlib::Deflate.new
  112. 1000.times { z << "foo" }
  113. s = z.finish
  114. assert_equal(3000, z.total_in)
  115. assert_operator(3000, :>, z.total_out)
  116. assert_equal("foo" * 1000, Zlib::Inflate.inflate(s))
  117. end
  118. def test_data_type
  119. z = Zlib::Deflate.new
  120. assert([Zlib::ASCII, Zlib::BINARY, Zlib::UNKNOWN].include?(z.data_type))
  121. end
  122. def test_adler
  123. z = Zlib::Deflate.new
  124. z << "foo"
  125. z.finish
  126. assert_equal(0x02820145, z.adler)
  127. end
  128. def test_finished_p
  129. z = Zlib::Deflate.new
  130. assert_equal(false, z.finished?)
  131. z << "foo"
  132. assert_equal(false, z.finished?)
  133. z.finish
  134. assert_equal(true, z.finished?)
  135. z.close
  136. assert_raise(Zlib::Error) { z.finished? }
  137. end
  138. def test_closed_p
  139. z = Zlib::Deflate.new
  140. assert_equal(false, z.closed?)
  141. z << "foo"
  142. assert_equal(false, z.closed?)
  143. z.finish
  144. assert_equal(false, z.closed?)
  145. z.close
  146. assert_equal(true, z.closed?)
  147. end
  148. def test_params
  149. z = Zlib::Deflate.new
  150. z << "foo"
  151. z.params(Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_STRATEGY)
  152. z << "bar"
  153. s = z.finish
  154. assert_equal("foobar", Zlib::Inflate.inflate(s))
  155. data = ('a'..'z').to_a.join
  156. z = Zlib::Deflate.new(Zlib::NO_COMPRESSION, Zlib::MAX_WBITS,
  157. Zlib::DEF_MEM_LEVEL, Zlib::DEFAULT_STRATEGY)
  158. z << data[0, 10]
  159. z.params(Zlib::BEST_COMPRESSION, Zlib::DEFAULT_STRATEGY)
  160. z << data[10 .. -1]
  161. assert_equal(data, Zlib::Inflate.inflate(z.finish))
  162. z = Zlib::Deflate.new
  163. s = z.deflate("foo", Zlib::FULL_FLUSH)
  164. z.avail_out = 0
  165. EnvUtil.suppress_warning {z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)}
  166. s << z.deflate("bar", Zlib::FULL_FLUSH)
  167. z.avail_out = 0
  168. EnvUtil.suppress_warning {z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)}
  169. s << z.deflate("baz", Zlib::FINISH)
  170. assert_equal("foobarbaz", Zlib::Inflate.inflate(s))
  171. z = Zlib::Deflate.new
  172. assert_raise(Zlib::StreamError) { z.params(10000, 10000) }
  173. z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
  174. end
  175. def test_set_dictionary
  176. z = Zlib::Deflate.new
  177. z.set_dictionary("foo")
  178. s = z.deflate("foo" * 100, Zlib::FINISH)
  179. z = Zlib::Inflate.new
  180. assert_raise(Zlib::NeedDict) { z.inflate(s) }
  181. z.set_dictionary("foo")
  182. assert_equal("foo" * 100, z.inflate(s)) # ???
  183. z = Zlib::Deflate.new
  184. z << "foo"
  185. assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
  186. EnvUtil.suppress_warning do
  187. z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
  188. end
  189. end
  190. def test_reset
  191. z = Zlib::Deflate.new
  192. z << "foo"
  193. z.reset
  194. z << "bar"
  195. s = z.finish
  196. assert_equal("bar", Zlib::Inflate.inflate(s))
  197. end
  198. def test_close
  199. z = Zlib::Deflate.new
  200. z.close
  201. assert_raise(Zlib::Error) { z << "foo" }
  202. assert_raise(Zlib::Error) { z.reset }
  203. end
  204. end
  205. class TestZlibInflate < Test::Unit::TestCase
  206. def test_class_inflate_dictionary
  207. assert_raise(Zlib::NeedDict) do
  208. Zlib::Inflate.inflate([0x08,0x3C,0x0,0x0,0x0,0x0].pack("c*"))
  209. end
  210. end
  211. def test_initialize
  212. assert_raise(Zlib::StreamError) { Zlib::Inflate.new(-1) }
  213. s = Zlib::Deflate.deflate("foo")
  214. z = Zlib::Inflate.new
  215. z << s << nil
  216. assert_equal("foo", z.finish)
  217. end
  218. def test_add_dictionary
  219. dictionary = "foo"
  220. deflate = Zlib::Deflate.new
  221. deflate.set_dictionary dictionary
  222. compressed = deflate.deflate "foofoofoo", Zlib::FINISH
  223. deflate.close
  224. out = nil
  225. inflate = Zlib::Inflate.new
  226. inflate.add_dictionary "foo"
  227. out = inflate.inflate compressed
  228. assert_equal "foofoofoo", out
  229. end
  230. def test_finish_chunked
  231. # zeros = Zlib::Deflate.deflate("0" * 100_000)
  232. zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \
  233. "\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  234. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  235. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  236. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  237. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  238. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  239. "\000\000\000\000\000\000\000\257\006\351\247BH"
  240. chunks = []
  241. z = Zlib::Inflate.new
  242. z.inflate(zeros) do |chunk|
  243. chunks << chunk
  244. break
  245. end
  246. z.finish do |chunk|
  247. chunks << chunk
  248. end
  249. assert_equal [16384, 16384, 16384, 16384, 16384, 16384, 1696],
  250. chunks.map { |chunk| chunk.size }
  251. assert chunks.all? { |chunk|
  252. chunk =~ /\A0+\z/
  253. }
  254. end
  255. def test_inflate
  256. s = Zlib::Deflate.deflate("foo")
  257. z = Zlib::Inflate.new
  258. s = z.inflate(s)
  259. s << z.inflate(nil)
  260. assert_equal("foo", s)
  261. z.inflate("foo") # ???
  262. z << "foo" # ???
  263. end
  264. def test_inflate_partial_input
  265. deflated = Zlib::Deflate.deflate "\0"
  266. z = Zlib::Inflate.new
  267. inflated = "".dup
  268. deflated.each_char do |byte|
  269. inflated << z.inflate(byte)
  270. end
  271. inflated << z.finish
  272. assert_equal "\0", inflated
  273. end
  274. def test_inflate_chunked
  275. # s = Zlib::Deflate.deflate("0" * 100_000)
  276. zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \
  277. "\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  278. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  279. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  280. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  281. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  282. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  283. "\000\000\000\000\000\000\000\257\006\351\247BH"
  284. chunks = []
  285. z = Zlib::Inflate.new
  286. z.inflate(zeros) do |chunk|
  287. chunks << chunk
  288. end
  289. assert_equal [16384, 16384, 16384, 16384, 16384, 16384, 1696],
  290. chunks.map { |chunk| chunk.size }
  291. assert chunks.all? { |chunk|
  292. chunk =~ /\A0+\z/
  293. }
  294. end
  295. def test_inflate_chunked_break
  296. # zeros = Zlib::Deflate.deflate("0" * 100_000)
  297. zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \
  298. "\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  299. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  300. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  301. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  302. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  303. "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
  304. "\000\000\000\000\000\000\000\257\006\351\247BH"
  305. chunks = []
  306. z = Zlib::Inflate.new
  307. z.inflate(zeros) do |chunk|
  308. chunks << chunk
  309. break
  310. end
  311. out = z.inflate nil
  312. assert_equal 100_000 - chunks.first.length, out.length
  313. end
  314. def test_inflate_dictionary
  315. dictionary = "foo"
  316. deflate = Zlib::Deflate.new
  317. deflate.set_dictionary dictionary
  318. compressed = deflate.deflate "foofoofoo", Zlib::FINISH
  319. deflate.close
  320. out = nil
  321. inflate = Zlib::Inflate.new
  322. begin
  323. out = inflate.inflate compressed
  324. flunk "Zlib::NeedDict was not raised"
  325. rescue Zlib::NeedDict
  326. inflate.set_dictionary dictionary
  327. out = inflate.inflate ""
  328. end
  329. assert_equal "foofoofoo", out
  330. end
  331. def test_sync
  332. z = Zlib::Deflate.new
  333. s = z.deflate("foo" * 1000, Zlib::FULL_FLUSH)
  334. z.avail_out = 0
  335. EnvUtil.suppress_warning {z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)}
  336. s << z.deflate("bar" * 1000, Zlib::FULL_FLUSH)
  337. z.avail_out = 0
  338. EnvUtil.suppress_warning {z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)}
  339. s << z.deflate("baz" * 1000, Zlib::FINISH)
  340. z = Zlib::Inflate.new
  341. assert_raise(Zlib::DataError) { z << "\0" * 100 }
  342. assert_equal(false, z.sync(""))
  343. assert_equal(false, z.sync_point?)
  344. z = Zlib::Inflate.new
  345. assert_raise(Zlib::DataError) { z << "\0" * 100 + s }
  346. assert_equal(true, z.sync(""))
  347. z = Zlib::Inflate.new
  348. assert_equal(false, z.sync("\0" * 100))
  349. assert_equal(false, z.sync_point?)
  350. z = Zlib::Inflate.new
  351. assert_equal(true, z.sync("\0" * 100 + s))
  352. end
  353. def test_set_dictionary
  354. z = Zlib::Inflate.new
  355. assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
  356. z.close
  357. end
  358. end
  359. class TestZlibGzipFile < Test::Unit::TestCase
  360. def test_to_io
  361. Tempfile.create("test_zlib_gzip_file_to_io") {|t|
  362. t.close
  363. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  364. Zlib::GzipReader.open(t.path) do |f|
  365. assert_kind_of(IO, f.to_io)
  366. end
  367. }
  368. end
  369. def test_crc
  370. Tempfile.create("test_zlib_gzip_file_crc") {|t|
  371. t.close
  372. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  373. Zlib::GzipReader.open(t.path) do |f|
  374. f.read
  375. assert_equal(0x8c736521, f.crc)
  376. end
  377. }
  378. end
  379. def test_mtime
  380. tim = Time.now
  381. Tempfile.create("test_zlib_gzip_file_mtime") {|t|
  382. t.close
  383. Zlib::GzipWriter.open(t.path) do |gz|
  384. gz.mtime = -1
  385. gz.mtime = tim
  386. gz.print("foo")
  387. gz.flush
  388. assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
  389. end
  390. Zlib::GzipReader.open(t.path) do |f|
  391. assert_equal(tim.to_i, f.mtime.to_i)
  392. end
  393. }
  394. end
  395. def test_zero_mtime
  396. sio = StringIO.new
  397. gz = Zlib::GzipWriter.new(sio)
  398. gz.mtime = 0
  399. gz.write("Hi")
  400. gz.close
  401. reading_io = StringIO.new(sio.string)
  402. reader = Zlib::GzipReader.new(reading_io)
  403. assert_equal(0, reader.mtime.to_i)
  404. end
  405. def test_level
  406. Tempfile.create("test_zlib_gzip_file_level") {|t|
  407. t.close
  408. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  409. Zlib::GzipReader.open(t.path) do |f|
  410. assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
  411. end
  412. }
  413. end
  414. def test_os_code
  415. Tempfile.create("test_zlib_gzip_file_os_code") {|t|
  416. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  417. Zlib::GzipReader.open(t.path) do |f|
  418. assert_equal(Zlib::OS_CODE, f.os_code)
  419. end
  420. }
  421. end
  422. def test_orig_name
  423. Tempfile.create("test_zlib_gzip_file_orig_name") {|t|
  424. t.close
  425. Zlib::GzipWriter.open(t.path) do |gz|
  426. gz.orig_name = "foobarbazqux\0quux"
  427. gz.print("foo")
  428. gz.flush
  429. assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
  430. end
  431. Zlib::GzipReader.open(t.path) do |f|
  432. assert_equal("foobarbazqux", f.orig_name)
  433. end
  434. }
  435. end
  436. def test_comment
  437. Tempfile.create("test_zlib_gzip_file_comment") {|t|
  438. t.close
  439. Zlib::GzipWriter.open(t.path) do |gz|
  440. gz.comment = "foobarbazqux\0quux"
  441. gz.print("foo")
  442. gz.flush
  443. assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
  444. end
  445. Zlib::GzipReader.open(t.path) do |f|
  446. assert_equal("foobarbazqux", f.comment)
  447. end
  448. }
  449. end
  450. def test_lineno
  451. Tempfile.create("test_zlib_gzip_file_lineno") {|t|
  452. t.close
  453. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
  454. Zlib::GzipReader.open(t.path) do |f|
  455. assert_equal([0, "foo\n"], [f.lineno, f.gets])
  456. assert_equal([1, "bar\n"], [f.lineno, f.gets])
  457. f.lineno = 1000
  458. assert_equal([1000, "baz\n"], [f.lineno, f.gets])
  459. assert_equal([1001, "qux\n"], [f.lineno, f.gets])
  460. end
  461. }
  462. end
  463. def test_closed_p
  464. Tempfile.create("test_zlib_gzip_file_closed_p") {|t|
  465. t.close
  466. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  467. Zlib::GzipReader.open(t.path) do |f|
  468. assert_equal(false, f.closed?)
  469. f.read
  470. assert_equal(false, f.closed?)
  471. f.close
  472. assert_equal(true, f.closed?)
  473. end
  474. }
  475. end
  476. def test_sync
  477. Tempfile.create("test_zlib_gzip_file_sync") {|t|
  478. t.close
  479. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  480. Zlib::GzipReader.open(t.path) do |f|
  481. f.sync = true
  482. assert_equal(true, f.sync)
  483. f.read
  484. f.sync = false
  485. assert_equal(false, f.sync)
  486. end
  487. }
  488. end
  489. def test_pos
  490. Tempfile.create("test_zlib_gzip_file_pos") {|t|
  491. t.close
  492. Zlib::GzipWriter.open(t.path) do |gz|
  493. gz.print("foo")
  494. gz.flush
  495. assert_equal(3, gz.tell)
  496. end
  497. }
  498. end
  499. def test_path
  500. Tempfile.create("test_zlib_gzip_file_path") {|t|
  501. t.close
  502. gz = Zlib::GzipWriter.open(t.path)
  503. gz.print("foo")
  504. assert_equal(t.path, gz.path)
  505. gz.close
  506. assert_equal(t.path, gz.path)
  507. Zlib::GzipReader.open(t.path) do |f|
  508. assert_equal(t.path, f.path)
  509. f.close
  510. assert_equal(t.path, f.path)
  511. end
  512. s = "".dup
  513. sio = StringIO.new(s)
  514. gz = Zlib::GzipWriter.new(sio)
  515. gz.print("foo")
  516. assert_raise(NoMethodError) { gz.path }
  517. gz.close
  518. sio = StringIO.new(s)
  519. gz = Zlib::GzipReader.new(sio)
  520. assert_raise(NoMethodError) { gz.path }
  521. gz.close
  522. }
  523. end
  524. end
  525. class TestZlibGzipReader < Test::Unit::TestCase
  526. D0 = "\037\213\010\000S`\017A\000\003\003\000\000\000\000\000\000\000\000\000"
  527. def test_read0
  528. assert_equal("", Zlib::GzipReader.new(StringIO.new(D0)).read(0))
  529. end
  530. def test_ungetc
  531. s = "".dup
  532. w = Zlib::GzipWriter.new(StringIO.new(s))
  533. w << (1...1000).to_a.inspect
  534. w.close
  535. r = Zlib::GzipReader.new(StringIO.new(s))
  536. r.read(100)
  537. r.ungetc ?a
  538. assert_nothing_raised("[ruby-dev:24060]") {
  539. r.read(100)
  540. r.read
  541. r.close
  542. }
  543. end
  544. def test_ungetc_paragraph
  545. s = "".dup
  546. w = Zlib::GzipWriter.new(StringIO.new(s))
  547. w << "abc"
  548. w.close
  549. r = Zlib::GzipReader.new(StringIO.new(s))
  550. r.ungetc ?\n
  551. assert_equal("abc", r.gets(""))
  552. assert_nothing_raised("[ruby-dev:24065]") {
  553. r.read
  554. r.close
  555. }
  556. end
  557. def test_ungetc_at_start_of_file
  558. s = "".dup
  559. w = Zlib::GzipWriter.new(StringIO.new(s))
  560. w << "abc"
  561. w.close
  562. r = Zlib::GzipReader.new(StringIO.new(s))
  563. r.ungetc ?!
  564. assert_equal(-1, r.pos, "[ruby-core:81488][Bug #13616]")
  565. end
  566. def test_open
  567. Tempfile.create("test_zlib_gzip_reader_open") {|t|
  568. t.close
  569. e = assert_raise(Zlib::GzipFile::Error) {
  570. Zlib::GzipReader.open(t.path)
  571. }
  572. assert_equal("not in gzip format", e.message)
  573. assert_nil(e.input)
  574. open(t.path, "wb") {|f| f.write("foo")}
  575. e = assert_raise(Zlib::GzipFile::Error) {
  576. Zlib::GzipReader.open(t.path)
  577. }
  578. assert_equal("not in gzip format", e.message)
  579. assert_equal("foo", e.input)
  580. open(t.path, "wb") {|f| f.write("foobarzothoge")}
  581. e = assert_raise(Zlib::GzipFile::Error) {
  582. Zlib::GzipReader.open(t.path)
  583. }
  584. assert_equal("not in gzip format", e.message)
  585. assert_equal("foobarzothoge", e.input)
  586. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  587. assert_raise(ArgumentError) { Zlib::GzipReader.open }
  588. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  589. f = Zlib::GzipReader.open(t.path)
  590. begin
  591. assert_equal("foo", f.read)
  592. ensure
  593. f.close
  594. end
  595. }
  596. end
  597. def test_rewind
  598. bug8467 = '[ruby-core:55220] [Bug #8467]'
  599. Tempfile.create("test_zlib_gzip_reader_rewind") {|t|
  600. t.close
  601. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  602. Zlib::GzipReader.open(t.path) do |f|
  603. assert_equal("foo", f.read)
  604. f.rewind
  605. assert_equal("foo", f.read)
  606. f.rewind
  607. bytes = []
  608. f.each_byte { |b| bytes << b }
  609. assert_equal "foo".bytes.to_a, bytes, '[Bug #10101]'
  610. end
  611. open(t.path, "rb") do |f|
  612. gz = Zlib::GzipReader.new(f)
  613. gz.rewind
  614. assert_equal(["foo"], gz.to_a, bug8467)
  615. end
  616. }
  617. end
  618. def test_unused
  619. Tempfile.create("test_zlib_gzip_reader_unused") {|t|
  620. t.close
  621. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  622. Zlib::GzipReader.open(t.path) do |f|
  623. assert_equal(nil, f.unused)
  624. assert_equal("foo", f.read(3))
  625. assert_equal(nil, f.unused)
  626. assert_equal("bar", f.read)
  627. assert_equal(nil, f.unused)
  628. end
  629. }
  630. end
  631. def test_unused2
  632. zio = StringIO.new
  633. io = Zlib::GzipWriter.new zio
  634. io.write 'aaaa'
  635. io.finish
  636. io = Zlib::GzipWriter.new zio
  637. io.write 'bbbb'
  638. io.finish
  639. zio.rewind
  640. io = Zlib::GzipReader.new zio
  641. assert_equal('aaaa', io.read)
  642. unused = io.unused
  643. assert_equal(24, unused.bytesize)
  644. io.finish
  645. zio.pos -= unused.length
  646. io = Zlib::GzipReader.new zio
  647. assert_equal('bbbb', io.read)
  648. assert_equal(nil, io.unused)
  649. io.finish
  650. end
  651. def test_read
  652. Tempfile.create("test_zlib_gzip_reader_read") {|t|
  653. t.close
  654. str = "\u3042\u3044\u3046"
  655. Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
  656. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  657. assert_raise(ArgumentError) { f.read(-1) }
  658. assert_equal(str, f.read)
  659. end
  660. }
  661. end
  662. def test_readpartial
  663. Tempfile.create("test_zlib_gzip_reader_readpartial") {|t|
  664. t.close
  665. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  666. Zlib::GzipReader.open(t.path) do |f|
  667. assert("foo".start_with?(f.readpartial(3)))
  668. end
  669. Zlib::GzipReader.open(t.path) do |f|
  670. s = "".dup
  671. f.readpartial(3, s)
  672. assert("foo".start_with?(s))
  673. assert_raise(ArgumentError) { f.readpartial(-1) }
  674. end
  675. }
  676. end
  677. def test_getc
  678. Tempfile.create("test_zlib_gzip_reader_getc") {|t|
  679. t.close
  680. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  681. Zlib::GzipReader.open(t.path) do |f|
  682. "foobar".each_char {|c| assert_equal(c, f.getc) }
  683. assert_nil(f.getc)
  684. end
  685. }
  686. end
  687. def test_getbyte
  688. Tempfile.create("test_zlib_gzip_reader_getbyte") {|t|
  689. t.close
  690. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  691. Zlib::GzipReader.open(t.path) do |f|
  692. "foobar".each_byte {|c| assert_equal(c, f.getbyte) }
  693. assert_nil(f.getbyte)
  694. end
  695. }
  696. end
  697. def test_readchar
  698. Tempfile.create("test_zlib_gzip_reader_readchar") {|t|
  699. t.close
  700. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  701. Zlib::GzipReader.open(t.path) do |f|
  702. "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
  703. assert_raise(EOFError) { f.readchar }
  704. end
  705. }
  706. end
  707. def test_each_byte
  708. Tempfile.create("test_zlib_gzip_reader_each_byte") {|t|
  709. t.close
  710. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  711. Zlib::GzipReader.open(t.path) do |f|
  712. a = []
  713. f.each_byte {|c| a << c }
  714. assert_equal("foobar".each_byte.to_a, a)
  715. end
  716. }
  717. end
  718. def test_gets
  719. Tempfile.create("test_zlib_gzip_reader_gets") {|t|
  720. t.close
  721. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  722. Zlib::GzipReader.open(t.path) do |f|
  723. assert_equal("foo\n", f.gets)
  724. assert_equal("bar\n", f.gets)
  725. assert_equal("baz\n", f.gets)
  726. assert_nil(f.gets)
  727. end
  728. Zlib::GzipReader.open(t.path) do |f|
  729. assert_equal("foo\nbar\nbaz\n", f.gets(nil))
  730. end
  731. Zlib::GzipReader.open(t.path) do |f|
  732. assert_equal("foo\n", f.gets(10))
  733. assert_equal("ba", f.gets(2))
  734. assert_equal("r\nb", f.gets(nil, 3))
  735. assert_equal("az\n", f.gets(nil, 10))
  736. assert_nil(f.gets)
  737. end
  738. }
  739. end
  740. def test_gets2
  741. Tempfile.create("test_zlib_gzip_reader_gets2") {|t|
  742. t.close
  743. ustrs = %W"\u{3042 3044 3046}\n \u{304b 304d 304f}\n \u{3055 3057 3059}\n"
  744. Zlib::GzipWriter.open(t.path) {|gz| gz.print(*ustrs) }
  745. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  746. assert_equal(ustrs[0], f.gets)
  747. assert_equal(ustrs[1], f.gets)
  748. assert_equal(ustrs[2], f.gets)
  749. assert_nil(f.gets)
  750. end
  751. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  752. assert_equal(ustrs.join(''), f.gets(nil))
  753. end
  754. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  755. assert_equal(ustrs[0], f.gets(20))
  756. assert_equal(ustrs[1][0,2], f.gets(5))
  757. assert_equal(ustrs[1][2..-1]+ustrs[2][0,1], f.gets(nil, 5))
  758. assert_equal(ustrs[2][1..-1], f.gets(nil, 20))
  759. assert_nil(f.gets)
  760. end
  761. }
  762. end
  763. def test_readline
  764. Tempfile.create("test_zlib_gzip_reader_readline") {|t|
  765. t.close
  766. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  767. Zlib::GzipReader.open(t.path) do |f|
  768. assert_equal("foo\n", f.readline)
  769. assert_equal("bar\n", f.readline)
  770. assert_equal("baz\n", f.readline)
  771. assert_raise(EOFError) { f.readline }
  772. end
  773. }
  774. end
  775. def test_each
  776. Tempfile.create("test_zlib_gzip_reader_each") {|t|
  777. t.close
  778. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  779. Zlib::GzipReader.open(t.path) do |f|
  780. a = ["foo\n", "bar\n", "baz\n"]
  781. f.each {|l| assert_equal(a.shift, l) }
  782. end
  783. }
  784. end
  785. def test_readlines
  786. Tempfile.create("test_zlib_gzip_reader_readlines") {|t|
  787. t.close
  788. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  789. Zlib::GzipReader.open(t.path) do |f|
  790. assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
  791. end
  792. }
  793. end
  794. def test_reader_wrap
  795. Tempfile.create("test_zlib_gzip_reader_wrap") {|t|
  796. t.close
  797. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  798. f = open(t.path)
  799. f.binmode
  800. assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read })
  801. assert(f.closed?)
  802. }
  803. end
  804. def test_corrupted_header
  805. gz = Zlib::GzipWriter.new(StringIO.new(s = "".dup))
  806. gz.orig_name = "X"
  807. gz.comment = "Y"
  808. gz.print("foo")
  809. gz.finish
  810. # 14: magic(2) + method(1) + flag(1) + mtime(4) + exflag(1) + os(1) + orig_name(2) + comment(2)
  811. 1.upto(14) do |idx|
  812. assert_raise(Zlib::GzipFile::Error, idx) do
  813. Zlib::GzipReader.new(StringIO.new(s[0, idx])).read
  814. end
  815. end
  816. end
  817. def test_encoding
  818. Tempfile.create("test_zlib_gzip_reader_encoding") {|t|
  819. t.binmode
  820. content = (0..255).to_a.pack('c*')
  821. Zlib::GzipWriter.wrap(t) {|gz| gz.print(content) }
  822. read_all = Zlib::GzipReader.open(t.path) do |gz|
  823. assert_equal(Encoding.default_external, gz.external_encoding)
  824. gz.read
  825. end
  826. assert_equal(Encoding.default_external, read_all.encoding)
  827. # chunks are in BINARY regardless of encoding settings
  828. read_size = Zlib::GzipReader.open(t.path) {|gz| gz.read(1024) }
  829. assert_equal(Encoding::ASCII_8BIT, read_size.encoding)
  830. assert_equal(content, read_size)
  831. }
  832. end
  833. def test_double_close
  834. Tempfile.create("test_zlib_gzip_reader_close") {|t|
  835. t.binmode
  836. content = "foo"
  837. Zlib::GzipWriter.wrap(t) {|gz| gz.print(content) }
  838. r = Zlib::GzipReader.open(t.path)
  839. assert_equal(content, r.read)
  840. assert_nothing_raised { r.close }
  841. assert_nothing_raised { r.close }
  842. }
  843. end
  844. end
  845. class TestZlibGzipWriter < Test::Unit::TestCase
  846. def test_invalid_new
  847. assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close }
  848. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }
  849. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(0).close }
  850. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(:hoge).close }
  851. end
  852. def test_open
  853. assert_raise(ArgumentError) { Zlib::GzipWriter.open }
  854. Tempfile.create("test_zlib_gzip_writer_open") {|t|
  855. t.close
  856. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  857. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  858. f = Zlib::GzipWriter.open(t.path)
  859. begin
  860. f.print("bar")
  861. ensure
  862. f.close
  863. end
  864. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  865. assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
  866. }
  867. end
  868. def test_write
  869. Tempfile.create("test_zlib_gzip_writer_write") {|t|
  870. t.close
  871. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  872. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  873. o = Object.new
  874. def o.to_s; "bar"; end
  875. Zlib::GzipWriter.open(t.path) {|gz| gz.print(o) }
  876. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  877. }
  878. end
  879. def test_putc
  880. Tempfile.create("test_zlib_gzip_writer_putc") {|t|
  881. t.close
  882. Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
  883. assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  884. # todo: multibyte char
  885. }
  886. end
  887. def test_puts
  888. Tempfile.create("test_zlib_gzip_writer_puts") {|t|
  889. t.close
  890. Zlib::GzipWriter.open(t.path) {|gz| gz.puts("foo") }
  891. assert_equal("foo\n", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  892. }
  893. end
  894. def test_writer_wrap
  895. Tempfile.create("test_zlib_gzip_writer_wrap") {|t|
  896. t.binmode
  897. Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
  898. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  899. }
  900. end
  901. def test_double_close
  902. Tempfile.create("test_zlib_gzip_reader_close") {|t|
  903. t.binmode
  904. w = Zlib::GzipWriter.wrap(t)
  905. assert_nothing_raised { w.close }
  906. assert_nothing_raised { w.close }
  907. }
  908. end
  909. def test_zlib_writer_buffered_write
  910. bug15356 = '[ruby-core:90346] [Bug #15356]'.freeze
  911. fixes = 'r61631 (commit a55abcc0ca6f628fc05304f81e5a044d65ab4a68)'.freeze
  912. ary = []
  913. def ary.write(*args)
  914. self.concat(args)
  915. end
  916. gz = Zlib::GzipWriter.new(ary)
  917. gz.write(bug15356)
  918. gz.write("\n")
  919. gz.write(fixes)
  920. gz.close
  921. assert_not_predicate ary, :empty?
  922. exp = [ bug15356, fixes ]
  923. assert_equal exp, Zlib.gunzip(ary.join('')).split("\n")
  924. end
  925. end
  926. class TestZlib < Test::Unit::TestCase
  927. def test_version
  928. assert_instance_of(String, Zlib.zlib_version)
  929. end
  930. def test_adler32
  931. assert_equal(0x00000001, Zlib.adler32)
  932. assert_equal(0x02820145, Zlib.adler32("foo"))
  933. assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo")))
  934. assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000))
  935. end
  936. def test_adler32_combine
  937. one = Zlib.adler32("fo")
  938. two = Zlib.adler32("o")
  939. begin
  940. assert_equal(0x02820145, Zlib.adler32_combine(one, two, 1))
  941. rescue NotImplementedError
  942. skip "adler32_combine is not implemented"
  943. rescue Minitest::Assertion
  944. if /aix/ =~ RUBY_PLATFORM
  945. skip "zconf.h in zlib does not handle _LARGE_FILES in AIX. Skip until it is fixed"
  946. end
  947. raise $!
  948. end
  949. end
  950. def test_crc32
  951. assert_equal(0x00000000, Zlib.crc32)
  952. assert_equal(0x8c736521, Zlib.crc32("foo"))
  953. assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo")))
  954. assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000))
  955. end
  956. def test_crc32_combine
  957. one = Zlib.crc32("fo")
  958. two = Zlib.crc32("o")
  959. begin
  960. assert_equal(0x8c736521, Zlib.crc32_combine(one, two, 1))
  961. rescue NotImplementedError
  962. skip "crc32_combine is not implemented"
  963. rescue Minitest::Assertion
  964. if /aix/ =~ RUBY_PLATFORM
  965. skip "zconf.h in zlib does not handle _LARGE_FILES in AIX. Skip until it is fixed"
  966. end
  967. raise $!
  968. end
  969. end
  970. def test_crc_table
  971. t = Zlib.crc_table
  972. assert_instance_of(Array, t)
  973. t.each {|x| assert_kind_of(Integer, x) }
  974. end
  975. def test_inflate
  976. TestZlibInflate.new(__name__).test_inflate
  977. end
  978. def test_deflate
  979. TestZlibDeflate.new(__name__).test_deflate
  980. end
  981. def test_deflate_stream
  982. r = Random.new 0
  983. deflated = ''.dup
  984. Zlib.deflate(r.bytes(20000)) do |chunk|
  985. deflated << chunk
  986. end
  987. assert_equal 20016, deflated.length
  988. end
  989. def test_gzip
  990. actual = Zlib.gzip("foo".freeze)
  991. actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
  992. actual[9] = "\xff" # replace OS
  993. expected = %w[1f8b08000000000000ff4bcbcf07002165738c03000000].pack("H*")
  994. assert_equal expected, actual
  995. actual = Zlib.gzip("foo".freeze, level: 0)
  996. actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
  997. actual[9] = "\xff" # replace OS
  998. expected = %w[1f8b08000000000000ff010300fcff666f6f2165738c03000000].pack("H*")
  999. assert_equal expected, actual
  1000. actual = Zlib.gzip("foo".freeze, level: 9)
  1001. actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
  1002. actual[9] = "\xff" # replace OS
  1003. expected = %w[1f8b08000000000002ff4bcbcf07002165738c03000000].pack("H*")
  1004. assert_equal expected, actual
  1005. actual = Zlib.gzip("foo".freeze, level: 9, strategy: Zlib::FILTERED)
  1006. actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
  1007. actual[9] = "\xff" # replace OS
  1008. expected = %w[1f8b08000000000002ff4bcbcf07002165738c03000000].pack("H*")
  1009. assert_equal expected, actual
  1010. end
  1011. def test_gunzip
  1012. src = %w[1f8b08000000000000034bcbcf07002165738c03000000].pack("H*")
  1013. assert_equal 'foo', Zlib.gunzip(src.freeze)
  1014. src = %w[1f8b08000000000000034bcbcf07002165738c03000001].pack("H*")
  1015. assert_raise(Zlib::GzipFile::LengthError){ Zlib.gunzip(src) }
  1016. src = %w[1f8b08000000000000034bcbcf07002165738d03000000].pack("H*")
  1017. assert_raise(Zlib::GzipFile::CRCError){ Zlib.gunzip(src) }
  1018. src = %w[1f8b08000000000000034bcbcf07002165738d030000].pack("H*")
  1019. assert_raise(Zlib::GzipFile::Error){ Zlib.gunzip(src) }
  1020. src = %w[1f8b08000000000000034bcbcf0700].pack("H*")
  1021. assert_raise(Zlib::GzipFile::NoFooter){ Zlib.gunzip(src) }
  1022. src = %w[1f8b080000000000000].pack("H*")
  1023. assert_raise(Zlib::GzipFile::Error){ Zlib.gunzip(src) }
  1024. end
  1025. def test_gunzip_no_memory_leak
  1026. assert_no_memory_leak(%[-rzlib], "#{<<~"{#"}", "#{<<~'};'}")
  1027. d = Zlib.gzip("data")
  1028. {#
  1029. 10_000.times {Zlib.gunzip(d)}
  1030. };
  1031. end
  1032. end
  1033. end