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,106 @@
1
+ require 'test_helper'
2
+ require 'zip/ioextras'
3
+
4
+ class AbstractOutputStreamTest < MiniTest::Unit::TestCase
5
+ class TestOutputStream
6
+ include ::Zip::IOExtras::AbstractOutputStream
7
+
8
+ attr_accessor :buffer
9
+
10
+ def initialize
11
+ @buffer = ""
12
+ end
13
+
14
+ def << (data)
15
+ @buffer << data
16
+ self
17
+ end
18
+ end
19
+
20
+ def setup
21
+ @output_stream = TestOutputStream.new
22
+
23
+ @origCommaSep = $,
24
+ @origOutputSep = $\
25
+ end
26
+
27
+ def teardown
28
+ $, = @origCommaSep
29
+ $\ = @origOutputSep
30
+ end
31
+
32
+ def test_write
33
+ count = @output_stream.write("a little string")
34
+ assert_equal("a little string", @output_stream.buffer)
35
+ assert_equal("a little string".length, count)
36
+
37
+ count = @output_stream.write(". a little more")
38
+ assert_equal("a little string. a little more", @output_stream.buffer)
39
+ assert_equal(". a little more".length, count)
40
+ end
41
+
42
+ def test_print
43
+ $\ = nil # record separator set to nil
44
+ @output_stream.print("hello")
45
+ assert_equal("hello", @output_stream.buffer)
46
+
47
+ @output_stream.print(" world.")
48
+ assert_equal("hello world.", @output_stream.buffer)
49
+
50
+ @output_stream.print(" You ok ", "out ", "there?")
51
+ assert_equal("hello world. You ok out there?", @output_stream.buffer)
52
+
53
+ $\ = "\n"
54
+ @output_stream.print
55
+ assert_equal("hello world. You ok out there?\n", @output_stream.buffer)
56
+
57
+ @output_stream.print("I sure hope so!")
58
+ assert_equal("hello world. You ok out there?\nI sure hope so!\n", @output_stream.buffer)
59
+
60
+ $, = "X"
61
+ @output_stream.buffer = ""
62
+ @output_stream.print("monkey", "duck", "zebra")
63
+ assert_equal("monkeyXduckXzebra\n", @output_stream.buffer)
64
+
65
+ $\ = nil
66
+ @output_stream.buffer = ""
67
+ @output_stream.print(20)
68
+ assert_equal("20", @output_stream.buffer)
69
+ end
70
+
71
+ def test_printf
72
+ @output_stream.printf("%d %04x", 123, 123)
73
+ assert_equal("123 007b", @output_stream.buffer)
74
+ end
75
+
76
+ def test_putc
77
+ @output_stream.putc("A")
78
+ assert_equal("A", @output_stream.buffer)
79
+ @output_stream.putc(65)
80
+ assert_equal("AA", @output_stream.buffer)
81
+ end
82
+
83
+ def test_puts
84
+ @output_stream.puts
85
+ assert_equal("\n", @output_stream.buffer)
86
+
87
+ @output_stream.puts("hello", "world")
88
+ assert_equal("\nhello\nworld\n", @output_stream.buffer)
89
+
90
+ @output_stream.buffer = ""
91
+ @output_stream.puts("hello\n", "world\n")
92
+ assert_equal("hello\nworld\n", @output_stream.buffer)
93
+
94
+ @output_stream.buffer = ""
95
+ @output_stream.puts(["hello\n", "world\n"])
96
+ assert_equal("hello\nworld\n", @output_stream.buffer)
97
+
98
+ @output_stream.buffer = ""
99
+ @output_stream.puts(["hello\n", "world\n"], "bingo")
100
+ assert_equal("hello\nworld\nbingo\n", @output_stream.buffer)
101
+
102
+ @output_stream.buffer = ""
103
+ @output_stream.puts(16, 20, 50, "hello")
104
+ assert_equal("16\n20\n50\nhello\n", @output_stream.buffer)
105
+ end
106
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+ require 'zip/ioextras'
3
+
4
+ class FakeIOTest < MiniTest::Unit::TestCase
5
+ class FakeIOUsingClass
6
+ include ::Zip::IOExtras::FakeIO
7
+ end
8
+
9
+ def test_kind_of?
10
+ obj = FakeIOUsingClass.new
11
+
12
+ assert(obj.kind_of?(Object))
13
+ assert(obj.kind_of?(FakeIOUsingClass))
14
+ assert(obj.kind_of?(IO))
15
+ assert(!obj.kind_of?(Fixnum))
16
+ assert(!obj.kind_of?(String))
17
+ end
18
+ end
@@ -0,0 +1,233 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ $LOAD_PATH << '../lib'
6
+
7
+ require 'test/unit'
8
+ require 'zip/ioextras'
9
+
10
+ include ::Zip::IOExtras
11
+
12
+ class FakeIOTest < Test::Unit::TestCase
13
+ class FakeIOUsingClass
14
+ include FakeIO
15
+ end
16
+
17
+ def test_kind_of?
18
+ obj = FakeIOUsingClass.new
19
+
20
+ assert(obj.kind_of?(Object))
21
+ assert(obj.kind_of?(FakeIOUsingClass))
22
+ assert(obj.kind_of?(IO))
23
+ assert(!obj.kind_of?(Fixnum))
24
+ assert(!obj.kind_of?(String))
25
+ end
26
+ end
27
+
28
+ class AbstractInputStreamTest < Test::Unit::TestCase
29
+ # AbstractInputStream subclass that provides a read method
30
+
31
+ TEST_LINES = ["Hello world#{$INPUT_RECORD_SEPARATOR}",
32
+ "this is the second line#{$INPUT_RECORD_SEPARATOR}",
33
+ 'this is the last line']
34
+ TEST_STRING = TEST_LINES.join
35
+ class TestAbstractInputStream
36
+ include AbstractInputStream
37
+ def initialize(aString)
38
+ super()
39
+ @contents = aString
40
+ @readPointer = 0
41
+ end
42
+
43
+ def sysread(charsToRead, _buf = nil)
44
+ retVal = @contents[@readPointer, charsToRead]
45
+ @readPointer += charsToRead
46
+ retVal
47
+ end
48
+
49
+ def produce_input
50
+ sysread(100)
51
+ end
52
+
53
+ def input_finished?
54
+ @contents[@readPointer].nil?
55
+ end
56
+ end
57
+
58
+ def setup
59
+ @io = TestAbstractInputStream.new(TEST_STRING)
60
+ end
61
+
62
+ def test_gets
63
+ assert_equal(TEST_LINES[0], @io.gets)
64
+ assert_equal(1, @io.lineno)
65
+ assert_equal(TEST_LINES[0].length, @io.pos)
66
+ assert_equal(TEST_LINES[1], @io.gets)
67
+ assert_equal(2, @io.lineno)
68
+ assert_equal(TEST_LINES[2], @io.gets)
69
+ assert_equal(3, @io.lineno)
70
+ assert_equal(nil, @io.gets)
71
+ assert_equal(4, @io.lineno)
72
+ end
73
+
74
+ def test_getsMultiCharSeperator
75
+ assert_equal('Hell', @io.gets('ll'))
76
+ assert_equal("o world#{$INPUT_RECORD_SEPARATOR}this is the second l", @io.gets('d l'))
77
+ end
78
+
79
+ LONG_LINES = [
80
+ 'x' * 48 + "\r\n",
81
+ 'y' * 49 + "\r\n",
82
+ 'rest',
83
+ ]
84
+ def test_getsMulitCharSeperator_split
85
+ io = TestAbstractInputStream.new(LONG_LINES.join)
86
+ assert_equal(LONG_LINES[0], io.gets("\r\n"))
87
+ assert_equal(LONG_LINES[1], io.gets("\r\n"))
88
+ assert_equal(LONG_LINES[2], io.gets("\r\n"))
89
+ end
90
+
91
+ def test_getsWithSepAndIndex
92
+ io = TestAbstractInputStream.new(LONG_LINES.join)
93
+ assert_equal('x', io.gets("\r\n", 1))
94
+ assert_equal('x' * 47 + "\r", io.gets("\r\n", 48))
95
+ assert_equal("\n", io.gets(nil, 1))
96
+ assert_equal('yy', io.gets(nil, 2))
97
+ end
98
+
99
+ def test_getsWithIndex
100
+ assert_equal(TEST_LINES[0], @io.gets(100))
101
+ assert_equal('this', @io.gets(4))
102
+ end
103
+
104
+ def test_each_line
105
+ lineNumber = 0
106
+ @io.each_line {
107
+ |line|
108
+ assert_equal(TEST_LINES[lineNumber], line)
109
+ lineNumber += 1
110
+ }
111
+ end
112
+
113
+ def test_readlines
114
+ assert_equal(TEST_LINES, @io.readlines)
115
+ end
116
+
117
+ def test_readline
118
+ test_gets
119
+ begin
120
+ @io.readline
121
+ fail 'EOFError expected'
122
+ rescue EOFError
123
+ end
124
+ end
125
+ end
126
+
127
+ class AbstractOutputStreamTest < Test::Unit::TestCase
128
+ class TestOutputStream
129
+ include AbstractOutputStream
130
+
131
+ attr_accessor :buffer
132
+
133
+ def initialize
134
+ @buffer = ''
135
+ end
136
+
137
+ def <<(data)
138
+ @buffer << data
139
+ self
140
+ end
141
+ end
142
+
143
+ def setup
144
+ @output_stream = TestOutputStream.new
145
+
146
+ @origCommaSep = $OUTPUT_FIELD_SEPARATOR
147
+ @origOutputSep = $OUTPUT_RECORD_SEPARATOR
148
+ end
149
+
150
+ def teardown
151
+ $, = @origCommaSep
152
+ $\ = @origOutputSep
153
+ end
154
+
155
+ def test_write
156
+ count = @output_stream.write('a little string')
157
+ assert_equal('a little string', @output_stream.buffer)
158
+ assert_equal('a little string'.length, count)
159
+
160
+ count = @output_stream.write('. a little more')
161
+ assert_equal('a little string. a little more', @output_stream.buffer)
162
+ assert_equal('. a little more'.length, count)
163
+ end
164
+
165
+ def test_print
166
+ $\ = nil # record separator set to nil
167
+ @output_stream.print('hello')
168
+ assert_equal('hello', @output_stream.buffer)
169
+
170
+ @output_stream.print(' world.')
171
+ assert_equal('hello world.', @output_stream.buffer)
172
+
173
+ @output_stream.print(' You ok ', 'out ', 'there?')
174
+ assert_equal('hello world. You ok out there?', @output_stream.buffer)
175
+
176
+ $\ = "\n"
177
+ @output_stream.print
178
+ assert_equal("hello world. You ok out there?\n", @output_stream.buffer)
179
+
180
+ @output_stream.print('I sure hope so!')
181
+ assert_equal("hello world. You ok out there?\nI sure hope so!\n", @output_stream.buffer)
182
+
183
+ $, = 'X'
184
+ @output_stream.buffer = ''
185
+ @output_stream.print('monkey', 'duck', 'zebra')
186
+ assert_equal("monkeyXduckXzebra\n", @output_stream.buffer)
187
+
188
+ $\ = nil
189
+ @output_stream.buffer = ''
190
+ @output_stream.print(20)
191
+ assert_equal('20', @output_stream.buffer)
192
+ end
193
+
194
+ def test_printf
195
+ @output_stream.printf('%d %04x', 123, 123)
196
+ assert_equal('123 007b', @output_stream.buffer)
197
+ end
198
+
199
+ def test_putc
200
+ @output_stream.putc('A')
201
+ assert_equal('A', @output_stream.buffer)
202
+ @output_stream.putc(65)
203
+ assert_equal('AA', @output_stream.buffer)
204
+ end
205
+
206
+ def test_puts
207
+ @output_stream.puts
208
+ assert_equal("\n", @output_stream.buffer)
209
+
210
+ @output_stream.puts('hello', 'world')
211
+ assert_equal("\nhello\nworld\n", @output_stream.buffer)
212
+
213
+ @output_stream.buffer = ''
214
+ @output_stream.puts("hello\n", "world\n")
215
+ assert_equal("hello\nworld\n", @output_stream.buffer)
216
+
217
+ @output_stream.buffer = ''
218
+ @output_stream.puts(["hello\n", "world\n"])
219
+ assert_equal("hello\nworld\n", @output_stream.buffer)
220
+
221
+ @output_stream.buffer = ''
222
+ @output_stream.puts(["hello\n", "world\n"], 'bingo')
223
+ assert_equal("hello\nworld\nbingo\n", @output_stream.buffer)
224
+
225
+ @output_stream.buffer = ''
226
+ @output_stream.puts(16, 20, 50, 'hello')
227
+ assert_equal("16\n20\n50\nhello\n", @output_stream.buffer)
228
+ end
229
+ end
230
+
231
+ # Copyright (C) 2002-2004 Thomas Sondergaard
232
+ # rubyzip is free software; you can redistribute it and/or
233
+ # modify it under the terms of the ruby license.
@@ -0,0 +1,153 @@
1
+ require 'test_helper'
2
+
3
+ class ZipLocalEntryTest < MiniTest::Unit::TestCase
4
+
5
+ def teardown
6
+ ::Zip.write_zip64_support = false
7
+ end
8
+
9
+ def test_read_local_entryHeaderOfFirstTestZipEntry
10
+ ::File.open(TestZipFile::TEST_ZIP3.zip_name, "rb") do |file|
11
+ entry = ::Zip::Entry.read_local_entry(file)
12
+
13
+ assert_equal('', entry.comment)
14
+ # Differs from windows and unix because of CR LF
15
+ # assert_equal(480, entry.compressed_size)
16
+ # assert_equal(0x2a27930f, entry.crc)
17
+ # extra field is 21 bytes long
18
+ # probably contains some unix attrutes or something
19
+ # disabled: assert_equal(nil, entry.extra)
20
+ assert_equal(::Zip::Entry::DEFLATED, entry.compression_method)
21
+ assert_equal(TestZipFile::TEST_ZIP3.entry_names[0], entry.name)
22
+ assert_equal(::File.size(TestZipFile::TEST_ZIP3.entry_names[0]), entry.size)
23
+ assert(!entry.directory?)
24
+ end
25
+ end
26
+
27
+ def test_readDateTime
28
+ ::File.open("test/data/rubycode.zip", "rb") {
29
+ |file|
30
+ entry = ::Zip::Entry.read_local_entry(file)
31
+ assert_equal("zippedruby1.rb", entry.name)
32
+ assert_equal(::Zip::DOSTime.at(1019261638), entry.time)
33
+ }
34
+ end
35
+
36
+ def test_read_local_entryFromNonZipFile
37
+ ::File.open("test/data/file2.txt") {
38
+ |file|
39
+ assert_equal(nil, ::Zip::Entry.read_local_entry(file))
40
+ }
41
+ end
42
+
43
+ def test_read_local_entryFromTruncatedZipFile
44
+ zipFragment=""
45
+ ::File.open(TestZipFile::TEST_ZIP2.zip_name) { |f| zipFragment = f.read(12) } # local header is at least 30 bytes
46
+ zipFragment.extend(IOizeString).reset
47
+ entry = ::Zip::Entry.new
48
+ entry.read_local_entry(zipFragment)
49
+ fail "ZipError expected"
50
+ rescue ::Zip::Error
51
+ end
52
+
53
+ def test_writeEntry
54
+ entry = ::Zip::Entry.new("file.zip", "entryName", "my little comment",
55
+ "thisIsSomeExtraInformation", 100, 987654,
56
+ ::Zip::Entry::DEFLATED, 400)
57
+ write_to_file("localEntryHeader.bin", "centralEntryHeader.bin", entry)
58
+ entryReadLocal, entryReadCentral = read_from_file("localEntryHeader.bin", "centralEntryHeader.bin")
59
+ assert(entryReadCentral.extra['Zip64Placeholder'].nil?, 'zip64 placeholder should not be used in central directory')
60
+ compare_local_entry_headers(entry, entryReadLocal)
61
+ compare_c_dir_entry_headers(entry, entryReadCentral)
62
+ end
63
+
64
+ def test_writeEntryWithZip64
65
+ ::Zip.write_zip64_support = true
66
+ entry = ::Zip::Entry.new("file.zip", "entryName", "my little comment",
67
+ "thisIsSomeExtraInformation", 100, 987654,
68
+ ::Zip::Entry::DEFLATED, 400)
69
+ write_to_file("localEntryHeader.bin", "centralEntryHeader.bin", entry)
70
+ entryReadLocal, entryReadCentral = read_from_file("localEntryHeader.bin", "centralEntryHeader.bin")
71
+ assert(entryReadLocal.extra['Zip64Placeholder'], 'zip64 placeholder should be used in local file header')
72
+ entryReadLocal.extra.delete('Zip64Placeholder') # it was removed when writing the c_dir_entry, so remove from compare
73
+ assert(entryReadCentral.extra['Zip64Placeholder'].nil?, 'zip64 placeholder should not be used in central directory')
74
+ compare_local_entry_headers(entry, entryReadLocal)
75
+ compare_c_dir_entry_headers(entry, entryReadCentral)
76
+ end
77
+
78
+ def test_write64Entry
79
+ ::Zip.write_zip64_support = true
80
+ entry = ::Zip::Entry.new("bigfile.zip", "entryName", "my little equine",
81
+ "malformed extra field because why not",
82
+ 0x7766554433221100, 0xDEADBEEF, ::Zip::Entry::DEFLATED,
83
+ 0x9988776655443322)
84
+ write_to_file("localEntryHeader.bin", "centralEntryHeader.bin", entry)
85
+ entryReadLocal, entryReadCentral = read_from_file("localEntryHeader.bin", "centralEntryHeader.bin")
86
+ compare_local_entry_headers(entry, entryReadLocal)
87
+ compare_c_dir_entry_headers(entry, entryReadCentral)
88
+ end
89
+
90
+ def test_rewriteLocalHeader64
91
+ ::Zip.write_zip64_support = true
92
+ buf1 = StringIO.new
93
+ entry = ::Zip::Entry.new("file.zip", "entryName")
94
+ entry.write_local_entry(buf1)
95
+ assert(entry.extra['Zip64'].nil?, "zip64 extra is unnecessarily present")
96
+
97
+ buf2 = StringIO.new
98
+ entry.size = 0x123456789ABCDEF0
99
+ entry.compressed_size = 0x0123456789ABCDEF
100
+ entry.write_local_entry(buf2, true)
101
+ refute_nil(entry.extra['Zip64'])
102
+ refute_equal(buf1.size, 0)
103
+ assert_equal(buf1.size, buf2.size) # it can't grow, or we'd clobber file data
104
+ end
105
+
106
+ def test_readLocalOffset
107
+ entry = ::Zip::Entry.new("file.zip", "entryName")
108
+ entry.local_header_offset = 12345
109
+ ::File.open('centralEntryHeader.bin', 'wb') { |f| entry.write_c_dir_entry(f) }
110
+ read_entry = nil
111
+ ::File.open('centralEntryHeader.bin', 'rb') { |f| read_entry = ::Zip::Entry.read_c_dir_entry(f) }
112
+ compare_c_dir_entry_headers(entry, read_entry)
113
+ end
114
+
115
+ def test_read64LocalOffset
116
+ ::Zip.write_zip64_support = true
117
+ entry = ::Zip::Entry.new("file.zip", "entryName")
118
+ entry.local_header_offset = 0x0123456789ABCDEF
119
+ ::File.open('centralEntryHeader.bin', 'wb') { |f| entry.write_c_dir_entry(f) }
120
+ read_entry = nil
121
+ ::File.open('centralEntryHeader.bin', 'rb') { |f| read_entry = ::Zip::Entry.read_c_dir_entry(f) }
122
+ compare_c_dir_entry_headers(entry, read_entry)
123
+ end
124
+
125
+ private
126
+ def compare_local_entry_headers(entry1, entry2)
127
+ assert_equal(entry1.compressed_size, entry2.compressed_size)
128
+ assert_equal(entry1.crc, entry2.crc)
129
+ assert_equal(entry1.extra, entry2.extra)
130
+ assert_equal(entry1.compression_method, entry2.compression_method)
131
+ assert_equal(entry1.name, entry2.name)
132
+ assert_equal(entry1.size, entry2.size)
133
+ assert_equal(entry1.local_header_offset, entry2.local_header_offset)
134
+ end
135
+
136
+ def compare_c_dir_entry_headers(entry1, entry2)
137
+ compare_local_entry_headers(entry1, entry2)
138
+ assert_equal(entry1.comment, entry2.comment)
139
+ end
140
+
141
+ def write_to_file(localFileName, centralFileName, entry)
142
+ ::File.open(localFileName, "wb") { |f| entry.write_local_entry(f) }
143
+ ::File.open(centralFileName, "wb") { |f| entry.write_c_dir_entry(f) }
144
+ end
145
+
146
+ def read_from_file(localFileName, centralFileName)
147
+ localEntry = nil
148
+ cdirEntry = nil
149
+ ::File.open(localFileName, "rb") { |f| localEntry = ::Zip::Entry.read_local_entry(f) }
150
+ ::File.open(centralFileName, "rb") { |f| cdirEntry = ::Zip::Entry.read_c_dir_entry(f) }
151
+ [localEntry, cdirEntry]
152
+ end
153
+ end