File: entry_test.rb

package info (click to toggle)
ruby-zip 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,120 kB
  • sloc: ruby: 9,958; makefile: 23
file content (360 lines) | stat: -rw-r--r-- 10,622 bytes parent folder | download
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# frozen_string_literal: true

require_relative 'test_helper'
require_relative 'helpers/zip_entry_data'

class ZipEntryTest < Minitest::Test
  include ZipEntryData

  def teardown
    ::Zip.reset!
  end

  def test_constructor_and_getters
    entry = ::Zip::Entry.new(
      TEST_ZIPFILE, TEST_NAME,
      comment: TEST_COMMENT, extra: TEST_EXTRA,
      compressed_size: TEST_COMPRESSED_SIZE,
      crc: TEST_CRC, size: TEST_SIZE, time: TEST_TIME,
      compression_method: TEST_COMPRESSIONMETHOD,
      compression_level: TEST_COMPRESSIONLEVEL
    )

    assert_equal(TEST_COMMENT, entry.comment)
    assert_equal(TEST_COMPRESSED_SIZE, entry.compressed_size)
    assert_equal(TEST_CRC, entry.crc)
    assert_instance_of(::Zip::ExtraField, entry.extra)
    assert_equal(TEST_COMPRESSIONMETHOD, entry.compression_method)
    assert_equal(TEST_NAME, entry.name)
    assert_equal(TEST_SIZE, entry.size)

    # Reverse times when testing because we need to use DOSTime#== for the
    # comparison, not Time#==.
    assert_equal(entry.time, TEST_TIME)
  end

  def test_is_directory_and_is_file
    assert(::Zip::Entry.new(TEST_ZIPFILE, 'hello').file?)
    assert(!::Zip::Entry.new(TEST_ZIPFILE, 'hello').directory?)

    assert(::Zip::Entry.new(TEST_ZIPFILE, 'dir/hello').file?)
    assert(!::Zip::Entry.new(TEST_ZIPFILE, 'dir/hello').directory?)

    assert(::Zip::Entry.new(TEST_ZIPFILE, 'hello/').directory?)
    assert(!::Zip::Entry.new(TEST_ZIPFILE, 'hello/').file?)

    assert(::Zip::Entry.new(TEST_ZIPFILE, 'dir/hello/').directory?)
    assert(!::Zip::Entry.new(TEST_ZIPFILE, 'dir/hello/').file?)
  end

  def test_equality
    entry1 = ::Zip::Entry.new(
      'file.zip', 'name',
      comment: 'isNotCompared', extra: 'something extra',
      compressed_size: 123, crc: 1234, size: 10_000
    )

    entry2 = ::Zip::Entry.new(
      'file.zip', 'name',
      comment: 'isNotComparedXXX', extra: 'something extra',
      compressed_size: 123, crc: 1234, size: 10_000
    )

    entry3 = ::Zip::Entry.new(
      'file.zip', 'name2',
      comment: 'isNotComparedXXX', extra: 'something extra',
      compressed_size: 123, crc: 1234, size: 10_000
    )

    entry4 = ::Zip::Entry.new(
      'file.zip', 'name2',
      comment: 'isNotComparedXXX', extra: 'something extraXX',
      compressed_size: 123, crc: 1234, size: 10_000
    )

    entry5 = ::Zip::Entry.new(
      'file.zip', 'name2',
      comment: 'isNotComparedXXX', extra: 'something extraXX',
      compressed_size: 12, crc: 1234, size: 10_000
    )

    entry6 = ::Zip::Entry.new(
      'file.zip', 'name2',
      comment: 'isNotComparedXXX', extra: 'something extraXX',
      compressed_size: 12, crc: 123, size: 10_000
    )

    entry7 = ::Zip::Entry.new(
      'file.zip', 'name2', comment: 'isNotComparedXXX',
      extra: 'something extraXX', compressed_size: 12, crc: 123, size: 10_000,
      compression_method: ::Zip::Entry::STORED
    )

    entry8 = ::Zip::Entry.new(
      'file.zip', 'name2',
      comment: 'isNotComparedXXX', extra: 'something extraXX',
      compressed_size: 12, crc: 123, size: 100_000,
      compression_method: ::Zip::Entry::STORED
    )

    assert_equal(entry1, entry1)
    assert_equal(entry1, entry2)

    assert(entry2 != entry3)
    assert(entry3 != entry4)
    assert(entry4 != entry5)
    assert(entry5 != entry6)
    assert(entry6 != entry7)
    assert(entry7 != entry8)

    assert(entry7 != 'hello')
    assert(entry7 != 12)
  end

  def test_compare
    assert_equal(0, ::Zip::Entry.new('zf.zip', 'a') <=> ::Zip::Entry.new('zf.zip', 'a'))
    assert_equal(1, ::Zip::Entry.new('zf.zip', 'b') <=> ::Zip::Entry.new('zf.zip', 'a'))
    assert_equal(-1, ::Zip::Entry.new('zf.zip', 'a') <=> ::Zip::Entry.new('zf.zip', 'b'))

    entries = [
      ::Zip::Entry.new('zf.zip', '5'),
      ::Zip::Entry.new('zf.zip', '1'),
      ::Zip::Entry.new('zf.zip', '3'),
      ::Zip::Entry.new('zf.zip', '4'),
      ::Zip::Entry.new('zf.zip', '0'),
      ::Zip::Entry.new('zf.zip', '2')
    ]

    entries.sort!
    assert_equal('0', entries[0].to_s)
    assert_equal('1', entries[1].to_s)
    assert_equal('2', entries[2].to_s)
    assert_equal('3', entries[3].to_s)
    assert_equal('4', entries[4].to_s)
    assert_equal('5', entries[5].to_s)
  end

  def test_parent_as_string
    entry1 = ::Zip::Entry.new('zf.zip', 'aa')
    entry2 = ::Zip::Entry.new('zf.zip', 'aa/')
    entry3 = ::Zip::Entry.new('zf.zip', 'aa/bb')
    entry4 = ::Zip::Entry.new('zf.zip', 'aa/bb/')
    entry5 = ::Zip::Entry.new('zf.zip', 'aa/bb/cc')
    entry6 = ::Zip::Entry.new('zf.zip', 'aa/bb/cc/')

    assert_nil(entry1.parent_as_string)
    assert_nil(entry2.parent_as_string)
    assert_equal('aa/', entry3.parent_as_string)
    assert_equal('aa/', entry4.parent_as_string)
    assert_equal('aa/bb/', entry5.parent_as_string)
    assert_equal('aa/bb/', entry6.parent_as_string)
  end

  def test_entry_name_cannot_start_with_slash
    error = assert_raises(::Zip::EntryNameError) do
      ::Zip::Entry.new('zf.zip', '/hej/der')
    end
    assert_match(/'\/hej\/der'/, error.message)
  end

  def test_entry_name_cannot_be_too_long
    name = 'a' * 65_535
    ::Zip::Entry.new('', name) # Should not raise anything.

    error = assert_raises(::Zip::EntryNameError) do
      ::Zip::Entry.new('', "a#{name}")
    end
    assert_match(/65,536/, error.message)
  end

  def test_store_file_without_compression
    Dir.mktmpdir do |tmp|
      tmp_zip = File.join(tmp, 'no_compress.zip')

      Zip.setup do |z|
        z.write_zip64_support = false
      end

      zipfile = Zip::File.open(tmp_zip, create: true)

      mimetype_entry = Zip::Entry.new(
        zipfile,                # @zipfile
        'mimetype',             # @name
        compression_method: Zip::Entry::STORED
      )
      zipfile.add(mimetype_entry, 'test/data/mimetype')

      files = Dir[File.join('test/data/globTest', '**', '**')]
      files.each do |file|
        zipfile.add(file.sub('test/data/globTest/', ''), file)
      end

      zipfile.close

      f = File.open(tmp_zip, 'rb')
      first_100_bytes = f.read(100)
      f.close

      assert_match(/mimetypeapplication\/epub\+zip/, first_100_bytes)
    end
  end

  def test_encrypted?
    entry = Zip::Entry.new
    entry.gp_flags = 1
    assert_equal(true, entry.encrypted?)

    entry.gp_flags = 0
    assert_equal(false, entry.encrypted?)
  end

  def test_incomplete?
    entry = Zip::Entry.new
    entry.gp_flags = 8
    assert_equal(true, entry.incomplete?)

    entry.gp_flags = 0
    assert_equal(false, entry.incomplete?)
  end

  def test_compression_level_flags
    [
      [Zip.default_compression, 0],
      [0, 0],
      [1, 6],
      [2, 4],
      [3, 0],
      [7, 0],
      [8, 2],
      [9, 2]
    ].each do |level, flags|
      # Check flags are set correctly when DEFLATED is (implicitly) specified.
      e_def = Zip::Entry.new(
        '', '',
        compression_level: level
      )
      assert_equal(flags, e_def.gp_flags & 0b110)

      # Check that flags are not set when STORED is specified.
      e_sto = Zip::Entry.new(
        '', '',
        compression_method: Zip::Entry::STORED,
        compression_level:  level
      )
      assert_equal(0, e_sto.gp_flags & 0b110)
    end

    # Check that a directory entry's flags are not set, even if DEFLATED
    # is specified.
    e_dir = Zip::Entry.new(
      '', 'd/', compression_method: Zip::Entry::DEFLATED, compression_level: 1
    )
    assert_equal(0, e_dir.gp_flags & 0b110)
  end

  def test_compression_method_reader
    [
      [Zip.default_compression, Zip::Entry::DEFLATED],
      [0, Zip::Entry::STORED],
      [1, Zip::Entry::DEFLATED],
      [9, Zip::Entry::DEFLATED]
    ].each do |level, method|
      # Check that the correct method is returned when DEFLATED is specified.
      entry = Zip::Entry.new(compression_level: level)
      assert_equal(method, entry.compression_method)
    end

    # Check that the correct method is returned when STORED is specified.
    entry = Zip::Entry.new(
      compression_method: Zip::Entry::STORED, compression_level: 1
    )
    assert_equal(Zip::Entry::STORED, entry.compression_method)

    # Check that directories are always STORED, whatever level is specified.
    entry = Zip::Entry.new(
      '', 'd/', compression_method: Zip::Entry::DEFLATED, compression_level: 1
    )
    assert_equal(Zip::Entry::STORED, entry.compression_method)
  end

  def test_set_time_as_dos_time
    entry = ::Zip::Entry.new
    assert(entry.time.kind_of?(::Zip::DOSTime))
    entry.time = Time.now
    assert(entry.time.kind_of?(::Zip::DOSTime))
    entry.time = ::Zip::DOSTime.now
    assert(entry.time.kind_of?(::Zip::DOSTime))
  end

  def test_atime
    entry = ::Zip::Entry.new
    time = Time.new(1999, 12, 31, 23, 59, 59)

    entry.atime = time
    assert(entry.dirty?)
    assert_equal(::Zip::DOSTime.from_time(time), entry.atime)
    refute_equal(entry.time, entry.atime)
    assert(entry.atime.kind_of?(::Zip::DOSTime))
    assert_nil(entry.ctime)
  end

  def test_ctime
    entry = ::Zip::Entry.new
    time = Time.new(1999, 12, 31, 23, 59, 59)

    entry.ctime = time
    assert(entry.dirty?)
    assert_equal(::Zip::DOSTime.from_time(time), entry.ctime)
    refute_equal(entry.time, entry.ctime)
    assert(entry.ctime.kind_of?(::Zip::DOSTime))
    assert_nil(entry.atime)
  end

  def test_mtime
    entry = ::Zip::Entry.new
    time = Time.new(1999, 12, 31, 23, 59, 59)

    entry.mtime = time
    assert(entry.dirty?)
    assert_equal(::Zip::DOSTime.from_time(time), entry.mtime)
    assert_equal(entry.time, entry.mtime)
    assert(entry.mtime.kind_of?(::Zip::DOSTime))
    assert_nil(entry.atime)
    assert_nil(entry.ctime)
  end

  def test_time
    entry = ::Zip::Entry.new
    time = Time.new(1999, 12, 31, 23, 59, 59)

    entry.time = time
    assert(entry.dirty?)
    assert_equal(::Zip::DOSTime.from_time(time), entry.time)
    assert_equal(entry.mtime, entry.time)
    assert(entry.time.kind_of?(::Zip::DOSTime))
    assert_nil(entry.atime)
    assert_nil(entry.ctime)
  end

  def test_ensure_entry_time_set_to_file_mtime
    entry = ::Zip::Entry.new
    entry.gather_fileinfo_from_srcpath('test/data/mimetype')
    assert_equal(entry.time, File.stat('test/data/mimetype').mtime)
  end

  def test_gather_fileinfo_from_srcpath
    entry = ::Zip::Entry.new(nil, 'directory_name/')

    assert_raises(ArgumentError) do
      entry.gather_fileinfo_from_srcpath('test/data/mimetype')
    end
  end

  def test_absolute_time
    entry = ::Zip::Entry.new
    refute(entry.absolute_time?)

    entry.time = Time.now
    assert(entry.absolute_time?)
  end
end