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

/test/zlib/test_zlib.rb

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