rubyzip 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubyzip might be problematic. Click here for more details.

Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/lib/zip/file.rb +2 -1
  3. data/lib/zip/inflater.rb +15 -44
  4. data/lib/zip/ioextras/abstract_input_stream.rb +5 -1
  5. data/lib/zip/version.rb +1 -1
  6. data/test/alltests.rb +18 -0
  7. data/test/basic_zip_file_test.rb +64 -0
  8. data/test/central_directory_entry_test.rb +73 -0
  9. data/test/central_directory_test.rb +100 -0
  10. data/test/data/file1.txt +46 -0
  11. data/test/data/file1.txt.deflatedData +0 -0
  12. data/test/data/file2.txt +1504 -0
  13. data/test/data/file2.txt.other +0 -0
  14. data/test/data/globTest.zip +0 -0
  15. data/test/data/globTest/foo.txt +0 -0
  16. data/test/data/globTest/foo/bar/baz/foo.txt +0 -0
  17. data/test/data/globTest/food.txt +0 -0
  18. data/test/data/mimetype +1 -0
  19. data/test/data/notzippedruby.rb +7 -0
  20. data/test/data/rubycode.zip +0 -0
  21. data/test/data/rubycode2.zip +0 -0
  22. data/test/data/testDirectory.bin +0 -0
  23. data/test/data/zip64-sample.zip +0 -0
  24. data/test/data/zipWithDirs.zip +0 -0
  25. data/test/deflater_test.rb +62 -0
  26. data/test/dummy.txt +1 -0
  27. data/test/entry_set_test.rb +125 -0
  28. data/test/entry_test.rb +165 -0
  29. data/test/errors_test.rb +36 -0
  30. data/test/extra_field_test.rb +69 -0
  31. data/test/file_extract_directory_test.rb +55 -0
  32. data/test/file_extract_test.rb +90 -0
  33. data/test/file_split_test.rb +60 -0
  34. data/test/file_test.rb +568 -0
  35. data/test/filesystem/dir_iterator_test.rb +62 -0
  36. data/test/filesystem/directory_test.rb +131 -0
  37. data/test/filesystem/file_mutating_test.rb +100 -0
  38. data/test/filesystem/file_nonmutating_test.rb +505 -0
  39. data/test/filesystem/file_stat_test.rb +66 -0
  40. data/test/gentestfiles.rb +134 -0
  41. data/test/inflater_test.rb +14 -0
  42. data/test/input_stream_test.rb +170 -0
  43. data/test/ioextras/abstract_input_stream_test.rb +103 -0
  44. data/test/ioextras/abstract_output_stream_test.rb +106 -0
  45. data/test/ioextras/fake_io_test.rb +18 -0
  46. data/test/ioextrastest.rb +233 -0
  47. data/test/local_entry_test.rb +153 -0
  48. data/test/odt_test_fix.rb +30 -0
  49. data/test/output_stream_test.rb +114 -0
  50. data/test/pass_thru_compressor_test.rb +31 -0
  51. data/test/pass_thru_decompressor_test.rb +15 -0
  52. data/test/sample.odt +0 -0
  53. data/test/settings_test.rb +71 -0
  54. data/test/test_helper.rb +228 -0
  55. data/test/unicode_file_names_and_comments_test.rb +40 -0
  56. data/test/zip64_full_test.rb +49 -0
  57. data/test/zip64_support_test.rb +15 -0
  58. metadata +107 -3
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'zip'
3
+
4
+ file_name = 'sample.odt'
5
+ content_file = 'content.xml'
6
+
7
+ def extract_and_compress(file, dir, content_file)
8
+ cont = "#{dir}/#{content_file}"
9
+ Zip::File.open(file) do |z|
10
+ z.extract(content_file, cont)
11
+ z.replace(content_file, cont)
12
+ end
13
+ FileUtils.remove cont
14
+ end
15
+
16
+ dir = File.expand_path File.dirname(__FILE__)
17
+ tmpdir = "#{dir}/#{Time.now.to_i}"
18
+ FileUtils.mkdir tmpdir
19
+
20
+ file = "#{dir}/#{file_name}"
21
+ file_cp = "#{tmpdir}/sample_1.odt"
22
+ FileUtils.cp file, file_cp
23
+
24
+ extract_and_compress file_cp, tmpdir, content_file
25
+
26
+ file = "#{tmpdir}/sample_1.odt"
27
+ file_cp = "#{tmpdir}/sample_2.odt"
28
+ FileUtils.cp file, file_cp
29
+
30
+ extract_and_compress file_cp, tmpdir, content_file
@@ -0,0 +1,114 @@
1
+ require 'test_helper'
2
+
3
+ class ZipOutputStreamTest < MiniTest::Unit::TestCase
4
+ include AssertEntry
5
+
6
+ TEST_ZIP = TestZipFile::TEST_ZIP2.clone
7
+ TEST_ZIP.zip_name = "output.zip"
8
+
9
+ def test_new
10
+ zos = ::Zip::OutputStream.new(TEST_ZIP.zip_name)
11
+ zos.comment = TEST_ZIP.comment
12
+ write_test_zip(zos)
13
+ zos.close
14
+ assert_test_zip_contents(TEST_ZIP)
15
+ end
16
+
17
+ def test_open
18
+ ::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
19
+ zos.comment = TEST_ZIP.comment
20
+ write_test_zip(zos)
21
+ end
22
+ assert_test_zip_contents(TEST_ZIP)
23
+ end
24
+
25
+ def test_write_buffer
26
+ io = ::StringIO.new('')
27
+ buffer = ::Zip::OutputStream.write_buffer(io) do |zos|
28
+ zos.comment = TEST_ZIP.comment
29
+ write_test_zip(zos)
30
+ end
31
+ File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string }
32
+ assert_test_zip_contents(TEST_ZIP)
33
+ end
34
+
35
+ def test_writingToClosedStream
36
+ assert_i_o_error_in_closed_stream { |zos| zos << "hello world" }
37
+ assert_i_o_error_in_closed_stream { |zos| zos.puts "hello world" }
38
+ assert_i_o_error_in_closed_stream { |zos| zos.write "hello world" }
39
+ end
40
+
41
+ def test_cannotOpenFile
42
+ name = TestFiles::EMPTY_TEST_DIR
43
+ begin
44
+ ::Zip::OutputStream.open(name)
45
+ rescue Exception
46
+ assert($!.kind_of?(Errno::EISDIR) || # Linux
47
+ $!.kind_of?(Errno::EEXIST) || # Windows/cygwin
48
+ $!.kind_of?(Errno::EACCES), # Windows
49
+ "Expected Errno::EISDIR (or on win/cygwin: Errno::EEXIST), but was: #{$!.class}")
50
+ end
51
+ end
52
+
53
+ def test_put_next_entry
54
+ stored_text = "hello world in stored text"
55
+ entry_name = "file1"
56
+ comment = "my comment"
57
+ ::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
58
+ zos.put_next_entry(entry_name, comment, nil, ::Zip::Entry::STORED)
59
+ zos << stored_text
60
+ end
61
+
62
+ assert(File.read(TEST_ZIP.zip_name)[stored_text])
63
+ ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
64
+ assert_equal(stored_text, zf.read(entry_name))
65
+ end
66
+ end
67
+
68
+ def test_put_next_entry_using_zip_entry_creates_entries_with_correct_timestamps
69
+ file = ::File.open("test/data/file2.txt", "rb")
70
+ ::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
71
+ zip_entry = ::Zip::Entry.new(zos, file.path, "", "", 0, 0, ::Zip::Entry::DEFLATED, 0, ::Zip::DOSTime.at(file.mtime))
72
+ zos.put_next_entry(zip_entry)
73
+ zos << file.read
74
+ end
75
+
76
+ ::Zip::InputStream::open(TEST_ZIP.zip_name) do |io|
77
+ while (entry = io.get_next_entry)
78
+ assert(::Zip::DOSTime.at(file.mtime).dos_equals(::Zip::DOSTime.at(entry.mtime))) # Compare DOS Times, since they are stored with two seconds accuracy
79
+ end
80
+ end
81
+ end
82
+
83
+ def test_chained_put_into_next_entry
84
+ stored_text = "hello world in stored text"
85
+ stored_text2 = "with chain"
86
+ entry_name = "file1"
87
+ comment = "my comment"
88
+ ::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
89
+ zos.put_next_entry(entry_name, comment, nil, ::Zip::Entry::STORED)
90
+ zos << stored_text << stored_text2
91
+ end
92
+
93
+ assert(File.read(TEST_ZIP.zip_name)[stored_text])
94
+ ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
95
+ assert_equal(stored_text + stored_text2, zf.read(entry_name))
96
+ end
97
+
98
+ end
99
+
100
+ def assert_i_o_error_in_closed_stream
101
+ assert_raises(IOError) {
102
+ zos = ::Zip::OutputStream.new("test_putOnClosedStream.zip")
103
+ zos.close
104
+ yield zos
105
+ }
106
+ end
107
+
108
+ def write_test_zip(zos)
109
+ TEST_ZIP.entry_names.each do |entryName|
110
+ zos.put_next_entry(entryName)
111
+ File.open(entryName, "rb") { |f| zos.write(f.read) }
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class PassThruCompressorTest < MiniTest::Unit::TestCase
4
+ include CrcTest
5
+
6
+ def test_size
7
+ File.open("test/dummy.txt", "wb") {
8
+ |file|
9
+ compressor = ::Zip::PassThruCompressor.new(file)
10
+
11
+ assert_equal(0, compressor.size)
12
+
13
+ t1 = "hello world"
14
+ t2 = ""
15
+ t3 = "bingo"
16
+
17
+ compressor << t1
18
+ assert_equal(compressor.size, t1.size)
19
+
20
+ compressor << t2
21
+ assert_equal(compressor.size, t1.size + t2.size)
22
+
23
+ compressor << t3
24
+ assert_equal(compressor.size, t1.size + t2.size + t3.size)
25
+ }
26
+ end
27
+
28
+ def test_crc
29
+ run_crc_test(::Zip::PassThruCompressor)
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+ class PassThruDecompressorTest < MiniTest::Unit::TestCase
3
+ include DecompressorTests
4
+
5
+ def setup
6
+ super
7
+ @file = File.new(TEST_FILE)
8
+ @decompressor = ::Zip::PassThruDecompressor.new(@file, File.size(TEST_FILE))
9
+ end
10
+
11
+ def teardown
12
+ @file.close
13
+ end
14
+ end
15
+
Binary file
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ class ZipSettingsTest < MiniTest::Unit::TestCase
4
+ # TODO Refactor out into common test module
5
+ include CommonZipFileFixture
6
+ TEST_OUT_NAME = "emptyOutDir"
7
+
8
+ def setup
9
+ super
10
+
11
+ Dir.rmdir(TEST_OUT_NAME) if File.directory? TEST_OUT_NAME
12
+ File.delete(TEST_OUT_NAME) if File.exist? TEST_OUT_NAME
13
+ end
14
+
15
+ def open_zip(&aProc)
16
+ assert(aProc != nil)
17
+ ::Zip::File.open(TestZipFile::TEST_ZIP4.zip_name, &aProc)
18
+ end
19
+
20
+ def extract_test_dir(&aProc)
21
+ open_zip {
22
+ |zf|
23
+ zf.extract(TestFiles::EMPTY_TEST_DIR, TEST_OUT_NAME, &aProc)
24
+ }
25
+ end
26
+
27
+ def test_true_on_exists_proc
28
+ Zip.on_exists_proc = true
29
+ File.open(TEST_OUT_NAME, "w") { |f| f.puts "something" }
30
+ extract_test_dir
31
+ assert(File.directory?(TEST_OUT_NAME))
32
+ end
33
+
34
+ def test_false_on_exists_proc
35
+ Zip.on_exists_proc = false
36
+ File.open(TEST_OUT_NAME, "w") { |f| f.puts "something" }
37
+ assert_raises(Zip::DestinationFileExistsError) { extract_test_dir }
38
+ end
39
+
40
+ def test_false_continue_on_exists_proc
41
+ Zip.continue_on_exists_proc = false
42
+
43
+ assert_raises(::Zip::EntryExistsError) do
44
+ ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
45
+ zf.add(zf.entries.first.name, "test/data/file2.txt")
46
+ end
47
+ end
48
+ end
49
+
50
+ def test_true_continue_on_exists_proc
51
+ Zip.continue_on_exists_proc = true
52
+
53
+ replacedEntry = nil
54
+
55
+ ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
56
+ replacedEntry = zf.entries.first.name
57
+ zf.add(replacedEntry, "test/data/file2.txt")
58
+ end
59
+
60
+ ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
61
+ assert_contains(zf, replacedEntry, "test/data/file2.txt")
62
+ end
63
+ end
64
+
65
+
66
+ private
67
+ def assert_contains(zf, entryName, filename = entryName)
68
+ assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
69
+ assert_entryContents(zf, entryName, filename) if File.exist?(filename)
70
+ end
71
+ end
@@ -0,0 +1,228 @@
1
+ require 'simplecov'
2
+ require 'minitest/autorun'
3
+ require 'minitest/unit'
4
+ require 'fileutils'
5
+ require 'digest/sha1'
6
+ require 'zip'
7
+ require 'gentestfiles'
8
+
9
+ TestFiles.create_test_files
10
+ TestZipFile.create_test_zips
11
+
12
+ ::MiniTest::Unit.after_tests do
13
+ FileUtils.rm_rf('test/data/generated')
14
+ end
15
+
16
+ module IOizeString
17
+ attr_reader :tell
18
+
19
+ def read(count = nil)
20
+ @tell ||= 0
21
+ count = size unless count
22
+ retVal = slice(@tell, count)
23
+ @tell += count
24
+ return retVal
25
+ end
26
+
27
+ def seek(index, offset)
28
+ @tell ||= 0
29
+ case offset
30
+ when IO::SEEK_END
31
+ newPos = size + index
32
+ when IO::SEEK_SET
33
+ newPos = index
34
+ when IO::SEEK_CUR
35
+ newPos = @tell + index
36
+ else
37
+ raise "Error in test method IOizeString::seek"
38
+ end
39
+ if (newPos < 0 || newPos >= size)
40
+ raise Errno::EINVAL
41
+ else
42
+ @tell=newPos
43
+ end
44
+ end
45
+
46
+ def reset
47
+ @tell = 0
48
+ end
49
+ end
50
+
51
+ module DecompressorTests
52
+ # expects @refText, @refLines and @decompressor
53
+
54
+ TEST_FILE = "test/data/file1.txt"
55
+
56
+ def setup
57
+ @refText = ''
58
+ File.open(TEST_FILE) { |f| @refText = f.read }
59
+ @refLines = @refText.split($/)
60
+ end
61
+
62
+ def test_readEverything
63
+ assert_equal(@refText, @decompressor.sysread)
64
+ end
65
+
66
+ def test_readInChunks
67
+ chunkSize = 5
68
+ while (decompressedChunk = @decompressor.sysread(chunkSize))
69
+ assert_equal(@refText.slice!(0, chunkSize), decompressedChunk)
70
+ end
71
+ assert_equal(0, @refText.size)
72
+ end
73
+
74
+ def test_mixingReadsAndProduceInput
75
+ # Just some preconditions to make sure we have enough data for this test
76
+ assert(@refText.length > 1000)
77
+ assert(@refLines.length > 40)
78
+
79
+
80
+ assert_equal(@refText[0...100], @decompressor.sysread(100))
81
+
82
+ assert(!@decompressor.input_finished?)
83
+ buf = @decompressor.produce_input
84
+ assert_equal(@refText[100...(100+buf.length)], buf)
85
+ end
86
+ end
87
+
88
+
89
+
90
+ module AssertEntry
91
+ def assert_next_entry(filename, zis)
92
+ assert_entry(filename, zis, zis.get_next_entry.name)
93
+ end
94
+
95
+ def assert_entry(filename, zis, entryName)
96
+ assert_equal(filename, entryName)
97
+ assert_entryContentsForStream(filename, zis, entryName)
98
+ end
99
+
100
+ def assert_entryContentsForStream(filename, zis, entryName)
101
+ File.open(filename, "rb") {
102
+ |file|
103
+ expected = file.read
104
+ actual = zis.read
105
+ if (expected != actual)
106
+ if ((expected && actual) && (expected.length > 400 || actual.length > 400))
107
+ zipEntryFilename=entryName+".zipEntry"
108
+ File.open(zipEntryFilename, "wb") { |entryfile| entryfile << actual }
109
+ fail("File '#{filename}' is different from '#{zipEntryFilename}'")
110
+ else
111
+ assert_equal(expected, actual)
112
+ end
113
+ end
114
+ }
115
+ end
116
+
117
+ def AssertEntry.assert_contents(filename, aString)
118
+ fileContents = ""
119
+ File.open(filename, "rb") { |f| fileContents = f.read }
120
+ if (fileContents != aString)
121
+ if (fileContents.length > 400 || aString.length > 400)
122
+ stringFile = filename + ".other"
123
+ File.open(stringFile, "wb") { |f| f << aString }
124
+ fail("File '#{filename}' is different from contents of string stored in '#{stringFile}'")
125
+ else
126
+ assert_equal(fileContents, aString)
127
+ end
128
+ end
129
+ end
130
+
131
+ def assert_stream_contents(zis, testZipFile)
132
+ assert(zis != nil)
133
+ testZipFile.entry_names.each do |entryName|
134
+ assert_next_entry(entryName, zis)
135
+ end
136
+ assert_equal(nil, zis.get_next_entry)
137
+ end
138
+
139
+ def assert_test_zip_contents(testZipFile)
140
+ ::Zip::InputStream.open(testZipFile.zip_name) do |zis|
141
+ assert_stream_contents(zis, testZipFile)
142
+ end
143
+ end
144
+
145
+ def assert_entryContents(zipFile, entryName, filename = entryName.to_s)
146
+ zis = zipFile.get_input_stream(entryName)
147
+ assert_entryContentsForStream(filename, zis, entryName)
148
+ ensure
149
+ zis.close if zis
150
+ end
151
+ end
152
+
153
+
154
+ module CrcTest
155
+
156
+ class TestOutputStream
157
+ include ::Zip::IOExtras::AbstractOutputStream
158
+
159
+ attr_accessor :buffer
160
+
161
+ def initialize
162
+ @buffer = ""
163
+ end
164
+
165
+ def << (data)
166
+ @buffer << data
167
+ self
168
+ end
169
+ end
170
+
171
+ def run_crc_test(compressorClass)
172
+ str = "Here's a nice little text to compute the crc for! Ho hum, it is nice nice nice nice indeed."
173
+ fakeOut = TestOutputStream.new
174
+
175
+ deflater = compressorClass.new(fakeOut)
176
+ deflater << str
177
+ assert_equal(0x919920fc, deflater.crc)
178
+ end
179
+ end
180
+
181
+
182
+ module Enumerable
183
+ def compare_enumerables(otherEnumerable)
184
+ otherAsArray = otherEnumerable.to_a
185
+ each_with_index {
186
+ |element, index|
187
+ return false unless yield(element, otherAsArray[index])
188
+ }
189
+ return self.size == otherAsArray.size
190
+ end
191
+ end
192
+
193
+
194
+ module CommonZipFileFixture
195
+ include AssertEntry
196
+
197
+ EMPTY_FILENAME = "emptyZipFile.zip"
198
+
199
+ TEST_ZIP = TestZipFile::TEST_ZIP2.clone
200
+ TEST_ZIP.zip_name = "test/data/generated/5entry_copy.zip"
201
+
202
+ def setup
203
+ File.delete(EMPTY_FILENAME) if File.exist?(EMPTY_FILENAME)
204
+ FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
205
+ end
206
+ end
207
+
208
+
209
+ module ExtraAssertions
210
+
211
+ def assert_forwarded(anObject, method, retVal, *expectedArgs)
212
+ callArgs = nil
213
+ setCallArgsProc = proc { |args| callArgs = args }
214
+ anObject.instance_eval <<-"end_eval"
215
+ alias #{method}_org #{method}
216
+ def #{method}(*args)
217
+ ObjectSpace._id2ref(#{setCallArgsProc.object_id}).call(args)
218
+ ObjectSpace._id2ref(#{retVal.object_id})
219
+ end
220
+ end_eval
221
+
222
+ assert_equal(retVal, yield) # Invoke test
223
+ assert_equal(expectedArgs, callArgs)
224
+ ensure
225
+ anObject.instance_eval "undef #{method}; alias #{method} #{method}_org"
226
+ end
227
+
228
+ end