-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathinput_stream_test.rb
259 lines (224 loc) · 7.86 KB
/
input_stream_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# frozen_string_literal: true
require 'test_helper'
class ZipInputStreamTest < MiniTest::Test
include AssertEntry
class IOLike
extend Forwardable
def initialize(path, mode)
@file = File.new(path, mode)
end
delegate ::Zip::File::IO_METHODS => :@file
end
def test_new
zis = ::Zip::InputStream.new(TestZipFile::TEST_ZIP2.zip_name)
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
assert_equal(true, zis.eof?)
zis.close
end
def test_open_with_block
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
assert_equal(true, zis.eof?)
end
end
def test_open_without_block
zis = ::Zip::InputStream.open(File.new(TestZipFile::TEST_ZIP2.zip_name, 'rb'))
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
end
def test_open_buffer_with_block
::Zip::InputStream.open(File.new(TestZipFile::TEST_ZIP2.zip_name, 'rb')) do |zis|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
assert_equal(true, zis.eof?)
end
end
def test_open_string_io_without_block
string_io = ::StringIO.new(::File.read(TestZipFile::TEST_ZIP2.zip_name, mode: 'rb'))
zis = ::Zip::InputStream.open(string_io)
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
end
def test_open_string_io_with_block
string_io = ::StringIO.new(::File.read(TestZipFile::TEST_ZIP2.zip_name, mode: 'rb'))
::Zip::InputStream.open(string_io) do |zis|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
assert_equal(true, zis.eof?)
end
end
def test_open_buffer_without_block
zis = ::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name)
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
end
def test_open_io_like_with_block
::Zip::InputStream.open(IOLike.new(TestZipFile::TEST_ZIP2.zip_name, 'rb')) do |zis|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
assert_equal(true, zis.eof?)
end
end
def test_open_file_with_gp3bit_set
::Zip::InputStream.open('test/data/gpbit3stored.zip') do |zis|
error = assert_raises(::Zip::StreamingError) do
zis.get_next_entry
end
assert_match(/file1\.txt/, error.message)
assert_equal('file1.txt', error.entry.name)
end
end
def test_open_file_with_gp3bit_set_created_by_osx_archive
::Zip::InputStream.open('test/data/osx-archive.zip') do |zis|
error = assert_raises(::Zip::StreamingError) do
zis.get_next_entry
end
assert_match(/1\.txt/, error.message)
assert_equal('1.txt', error.entry.name)
end
end
def test_open_split_archive_raises_error
::Zip::InputStream.open('test/data/invalid-split.zip') do |zis|
error = assert_raises(::Zip::SplitArchiveError) do
zis.get_next_entry
end
refute(error.message.empty?)
end
end
def test_open_encrypted_archive_raises_error
::Zip::InputStream.open('test/data/zipWithEncryption.zip') do |zis|
assert_raises(::Zip::Error) do
zis.get_next_entry
end
end
end
def test_size_no_entry
zis = ::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name)
assert_nil(zis.size)
end
def test_size_with_entry
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
zis.get_next_entry
assert_equal(123_702, zis.size)
end
end
def test_get_entry_ftypes
::Zip::InputStream.open(TestZipFile::TEST_ZIP4.zip_name) do |zis|
entry = zis.get_next_entry
assert_equal(:file, entry.ftype)
entry = zis.get_next_entry
assert_equal(:directory, entry.ftype)
end
end
def test_incomplete_reads
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
entry = zis.get_next_entry # longAscii.txt
assert_equal(false, zis.eof?)
assert_equal(TestZipFile::TEST_ZIP2.entry_names[0], entry.name)
assert !zis.gets.empty?
assert_equal(false, zis.eof?)
entry = zis.get_next_entry # empty.txt
assert_equal(TestZipFile::TEST_ZIP2.entry_names[1], entry.name)
assert_equal(0, entry.size)
assert_nil(zis.gets)
assert_equal(true, zis.eof?)
entry = zis.get_next_entry # empty_chmod640.txt
assert_equal(TestZipFile::TEST_ZIP2.entry_names[2], entry.name)
assert_equal(0, entry.size)
assert_nil(zis.gets)
assert_equal(true, zis.eof?)
entry = zis.get_next_entry # short.txt
assert_equal(TestZipFile::TEST_ZIP2.entry_names[3], entry.name)
assert !zis.gets.empty?
entry = zis.get_next_entry # longBinary.bin
assert_equal(TestZipFile::TEST_ZIP2.entry_names[4], entry.name)
assert !zis.gets.empty?
end
end
def test_incomplete_reads_from_string_io
string_io = ::StringIO.new(::File.read(TestZipFile::TEST_ZIP2.zip_name, mode: 'rb'))
::Zip::InputStream.open(string_io) do |zis|
entry = zis.get_next_entry # longAscii.txt
assert_equal(false, zis.eof?)
assert_equal(TestZipFile::TEST_ZIP2.entry_names[0], entry.name)
assert !zis.gets.empty?
assert_equal(false, zis.eof?)
entry = zis.get_next_entry # empty.txt
assert_equal(TestZipFile::TEST_ZIP2.entry_names[1], entry.name)
assert_equal(0, entry.size)
assert_nil(zis.gets)
assert_equal(true, zis.eof?)
entry = zis.get_next_entry # empty_chmod640.txt
assert_equal(TestZipFile::TEST_ZIP2.entry_names[2], entry.name)
assert_equal(0, entry.size)
assert_nil(zis.gets)
assert_equal(true, zis.eof?)
entry = zis.get_next_entry # short.txt
assert_equal(TestZipFile::TEST_ZIP2.entry_names[3], entry.name)
assert !zis.gets.empty?
entry = zis.get_next_entry # longBinary.bin
assert_equal(TestZipFile::TEST_ZIP2.entry_names[4], entry.name)
assert !zis.gets.empty?
end
end
def test_read_with_number_of_bytes_returns_nil_at_eof
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
entry = zis.get_next_entry # longAscii.txt
zis.read(entry.size)
assert_equal(true, zis.eof?)
assert_nil(zis.read(1))
assert_nil(zis.read(1))
end
end
def test_read_with_zero_returns_empty_string
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
assert_equal('', zis.read(0))
end
end
def test_rewind
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
e = zis.get_next_entry
assert_equal(TestZipFile::TEST_ZIP2.entry_names[0], e.name)
# Do a little reading
buf = +''
buf << zis.read(100)
assert_equal(100, zis.pos)
buf << (zis.gets || '')
buf << (zis.gets || '')
assert_equal(false, zis.eof?)
zis.rewind
buf2 = +''
buf2 << zis.read(100)
buf2 << (zis.gets || '')
buf2 << (zis.gets || '')
assert_equal(buf, buf2)
zis.rewind
assert_equal(false, zis.eof?)
assert_equal(0, zis.pos)
assert_entry(e.name, zis, e.name)
end
end
def test_mix_read_and_gets
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
zis.get_next_entry
assert_equal('#!/usr/bin/env ruby', zis.gets.chomp)
assert_equal(false, zis.eof?)
assert_equal('', zis.gets.chomp)
assert_equal(false, zis.eof?)
assert_equal('$VERBOSE =', zis.read(10))
assert_equal(false, zis.eof?)
end
end
def test_ungetc
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
zis.get_next_entry
first_line = zis.gets.chomp
first_line.reverse.bytes.each { |b| zis.ungetc(b) }
assert_equal('#!/usr/bin/env ruby', zis.gets.chomp)
assert_equal('$VERBOSE =', zis.read(10))
end
end
def test_readline_then_read
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
zis.get_next_entry
assert_equal("#!/usr/bin/env ruby\n", zis.readline)
refute(zis.eof?)
refute_empty(zis.read) # Also should not raise an error.
assert(zis.eof?)
end
end
end