minitar-jmazzi 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,629 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
4
+
5
+ require 'archive/tar/minitar'
6
+ require 'test/unit'
7
+ require 'stringio'
8
+ require 'yaml'
9
+ require 'zlib'
10
+
11
+ module TarTester
12
+ private
13
+ def assert_headers_equal(h1, h2)
14
+ fields = %w(name 100 mode 8 uid 8 gid 8 size 12 mtime 12 checksum 8
15
+ typeflag 1 linkname 100 magic 6 version 2 uname 32 gname 32
16
+ devmajor 8 devminor 8 prefix 155)
17
+ offset = 0
18
+ until fields.empty?
19
+ name = fields.shift
20
+ length = fields.shift.to_i
21
+ if name == "checksum"
22
+ chksum_off = offset
23
+ offset += length
24
+ next
25
+ end
26
+ assert_equal(h1[offset, length], h2[offset, length],
27
+ "Field #{name} of the tar header differs.")
28
+ offset += length
29
+ end
30
+ assert_equal(h1[chksum_off, 8], h2[chksum_off, 8], "Checksumes differ.")
31
+ end
32
+
33
+ def tar_file_header(fname, dname, mode, length)
34
+ h = header("0", fname, dname, length, mode)
35
+ checksum = calc_checksum(h)
36
+ header("0", fname, dname, length, mode, checksum)
37
+ end
38
+
39
+ def tar_dir_header(name, prefix, mode)
40
+ h = header("5", name, prefix, 0, mode)
41
+ checksum = calc_checksum(h)
42
+ header("5", name, prefix, 0, mode, checksum)
43
+ end
44
+
45
+ def header(type, fname, dname, length, mode, checksum = nil)
46
+ checksum ||= " " * 8
47
+ arr = [ASCIIZ(fname, 100), Z(to_oct(mode, 7)), Z(to_oct(nil, 7)),
48
+ Z(to_oct(nil, 7)), Z(to_oct(length, 11)), Z(to_oct(0, 11)),
49
+ checksum, type, "\0" * 100, "ustar\0", "00", ASCIIZ("", 32),
50
+ ASCIIZ("", 32), Z(to_oct(nil, 7)), Z(to_oct(nil, 7)),
51
+ ASCIIZ(dname, 155) ]
52
+ arr = arr.join("").split(//).map{ |x| x[0] }
53
+ h = arr.pack("C100C8C8C8C12C12C8CC100C6C2C32C32C8C8C155")
54
+ ret = h + "\0" * (512 - h.size)
55
+ assert_equal(512, ret.size)
56
+ ret
57
+ end
58
+
59
+ def calc_checksum(header)
60
+ sum = header.unpack("C*").inject { |s, a| s + a }
61
+ SP(Z(to_oct(sum, 6)))
62
+ end
63
+
64
+ def to_oct(n, pad_size)
65
+ if n.nil?
66
+ "\0" * pad_size
67
+ else
68
+ "%0#{pad_size}o" % n
69
+ end
70
+ end
71
+
72
+ def ASCIIZ(str, length)
73
+ str + "\0" * (length - str.length)
74
+ end
75
+
76
+ def SP(s)
77
+ s + " "
78
+ end
79
+
80
+ def Z(s)
81
+ s + "\0"
82
+ end
83
+
84
+ def SP_Z(s)
85
+ s + " \0"
86
+ end
87
+ end
88
+
89
+ class TC_Tar__Header < Test::Unit::TestCase
90
+ include Archive::Tar::Minitar
91
+ include TarTester
92
+
93
+ def test_arguments_are_checked
94
+ e = ArgumentError
95
+ assert_raises(e) { Archive::Tar::PosixHeader.new(:name => "", :size => "", :mode => "") }
96
+ assert_raises(e) { Archive::Tar::PosixHeader.new(:name => "", :size => "", :prefix => "") }
97
+ assert_raises(e) { Archive::Tar::PosixHeader.new(:name => "", :prefix => "", :mode => "") }
98
+ assert_raises(e) { Archive::Tar::PosixHeader.new(:prefix => "", :size => "", :mode => "") }
99
+ end
100
+
101
+ def test_basic_headers
102
+ header = { :name => "bla", :mode => 012345, :size => 10, :prefix => "", :typeflag => "0" }
103
+ assert_headers_equal(tar_file_header("bla", "", 012345, 10),
104
+ Archive::Tar::PosixHeader.new(header).to_s)
105
+ header = { :name => "bla", :mode => 012345, :size => 0, :prefix => "", :typeflag => "5" }
106
+ assert_headers_equal(tar_dir_header("bla", "", 012345),
107
+ Archive::Tar::PosixHeader.new(header).to_s)
108
+ end
109
+
110
+ def test_long_name_works
111
+ header = { :name => "a" * 100, :mode => 012345, :size => 10, :prefix => "" }
112
+ assert_headers_equal(tar_file_header("a" * 100, "", 012345, 10),
113
+ Archive::Tar::PosixHeader.new(header).to_s)
114
+ header = { :name => "a" * 100, :mode => 012345, :size => 10, :prefix => "bb" * 60 }
115
+ assert_headers_equal(tar_file_header("a" * 100, "bb" * 60, 012345, 10),
116
+ Archive::Tar::PosixHeader.new(header).to_s)
117
+ end
118
+
119
+ def test_new_from_stream
120
+ header = tar_file_header("a" * 100, "", 012345, 10)
121
+ h = nil
122
+ header = StringIO.new(header)
123
+ assert_nothing_raised { h = Archive::Tar::PosixHeader.new_from_stream(header) }
124
+ assert_equal("a" * 100, h.name)
125
+ assert_equal(012345, h.mode)
126
+ assert_equal(10, h.size)
127
+ assert_equal("", h.prefix)
128
+ assert_equal("ustar", h.magic)
129
+ end
130
+
131
+ def test_new_from_stream_with_evil_name
132
+ header = tar_file_header("a \0" + "\0" * 97, "", 012345, 10)
133
+ h = nil
134
+ header = StringIO.new(header)
135
+ assert_nothing_raised{ h = Archive::Tar::PosixHeader.new_from_stream header }
136
+ assert_equal("a ", h.name)
137
+ end
138
+ end
139
+
140
+ class TC_Tar__Writer < Test::Unit::TestCase
141
+ include Archive::Tar::Minitar
142
+ include TarTester
143
+
144
+ class DummyIO
145
+ attr_reader :data
146
+
147
+ def initialize
148
+ @data = ""
149
+ end
150
+
151
+ def write(dat)
152
+ data << dat
153
+ dat.size
154
+ end
155
+
156
+ def reset
157
+ @data = ""
158
+ end
159
+ end
160
+
161
+ def setup
162
+ @data = "a" * 10
163
+ @dummyos = DummyIO.new
164
+ @os = Writer.new(@dummyos)
165
+ end
166
+
167
+ def teardown
168
+ @os.close
169
+ end
170
+
171
+ def test_add_file_simple
172
+ @dummyos.reset
173
+
174
+ Writer.open(@dummyos) do |os|
175
+ os.add_file_simple("lib/foo/bar", :mode => 0644, :size => 10) do |f|
176
+ f.write "a" * 10
177
+ end
178
+ os.add_file_simple("lib/bar/baz", :mode => 0644, :size => 100) do |f|
179
+ f.write "fillme"
180
+ end
181
+ end
182
+
183
+ assert_headers_equal(tar_file_header("lib/foo/bar", "", 0644, 10),
184
+ @dummyos.data[0, 512])
185
+ assert_equal("a" * 10 + "\0" * 502, @dummyos.data[512, 512])
186
+ assert_headers_equal(tar_file_header("lib/bar/baz", "", 0644, 100),
187
+ @dummyos.data[512 * 2, 512])
188
+ assert_equal("fillme" + "\0" * 506, @dummyos.data[512 * 3, 512])
189
+ assert_equal("\0" * 512, @dummyos.data[512 * 4, 512])
190
+ assert_equal("\0" * 512, @dummyos.data[512 * 5, 512])
191
+ end
192
+
193
+ def test_write_operations_fail_after_closed
194
+ @dummyos.reset
195
+ @os.add_file_simple("sadd", :mode => 0644, :size => 20) { |f| }
196
+ @os.close
197
+ assert_raises(ClosedStream) { @os.flush }
198
+ assert_raises(ClosedStream) { @os.add_file("dfdsf", :mode => 0644) {} }
199
+ assert_raises(ClosedStream) { @os.mkdir "sdfdsf", :mode => 0644 }
200
+ end
201
+
202
+ def test_file_name_is_split_correctly
203
+ # test insane file lengths, and: a{100}/b{155}, etc
204
+ @dummyos.reset
205
+ names = [ "#{'a' * 155}/#{'b' * 100}", "#{'a' * 151}/#{'qwer/' * 19}bla" ]
206
+ o_names = [ "#{'b' * 100}", "#{'qwer/' * 19}bla" ]
207
+ o_prefixes = [ "a" * 155, "a" * 151 ]
208
+ names.each do |name|
209
+ @os.add_file_simple(name, :mode => 0644, :size => 10) { }
210
+ end
211
+ o_names.each_with_index do |nam, i|
212
+ assert_headers_equal(tar_file_header(nam, o_prefixes[i], 0644, 10),
213
+ @dummyos.data[2 * i * 512, 512])
214
+ end
215
+ assert_raises(FileNameTooLong) do
216
+ @os.add_file_simple(File.join("a" * 152, "b" * 10, "a" * 92),
217
+ :mode => 0644, :size => 10) { }
218
+ end
219
+ assert_raises(FileNameTooLong) do
220
+ @os.add_file_simple(File.join("a" * 162, "b" * 10),
221
+ :mode => 0644, :size => 10) { }
222
+ end
223
+ assert_raises(FileNameTooLong) do
224
+ @os.add_file_simple(File.join("a" * 10, "b" * 110),
225
+ :mode => 0644, :size => 10) { }
226
+ end
227
+ end
228
+
229
+ def test_add_file
230
+ dummyos = StringIO.new
231
+ class << dummyos
232
+ def method_missing(meth, *a)
233
+ self.string.send(meth, *a)
234
+ end
235
+ end
236
+ os = Writer.new dummyos
237
+ content1 = ('a'..'z').to_a.join("") # 26
238
+ content2 = ('aa'..'zz').to_a.join("") # 1352
239
+ Writer.open(dummyos) do |os|
240
+ os.add_file("lib/foo/bar", :mode => 0644) { |f, opts| f.write "a" * 10 }
241
+ os.add_file("lib/bar/baz", :mode => 0644) { |f, opts| f.write content1 }
242
+ os.add_file("lib/bar/baz", :mode => 0644) { |f, opts| f.write content2 }
243
+ os.add_file("lib/bar/baz", :mode => 0644) { |f, opts| }
244
+ end
245
+ assert_headers_equal(tar_file_header("lib/foo/bar", "", 0644, 10),
246
+ dummyos[0, 512])
247
+ assert_equal(%Q(#{"a" * 10}#{"\0" * 502}), dummyos[512, 512])
248
+ offset = 512 * 2
249
+ [content1, content2, ""].each do |data|
250
+ assert_headers_equal(tar_file_header("lib/bar/baz", "", 0644,
251
+ data.size), dummyos[offset, 512])
252
+ offset += 512
253
+ until !data || data == ""
254
+ chunk = data[0, 512]
255
+ data[0, 512] = ""
256
+ assert_equal(chunk + "\0" * (512 - chunk.size),
257
+ dummyos[offset, 512])
258
+ offset += 512
259
+ end
260
+ end
261
+ assert_equal("\0" * 1024, dummyos[offset, 1024])
262
+ end
263
+
264
+ def test_add_file_tests_seekability
265
+ assert_raises(Archive::Tar::Minitar::NonSeekableStream) do
266
+ @os.add_file("libdfdsfd", :mode => 0644) { |f| }
267
+ end
268
+ end
269
+
270
+ def test_write_header
271
+ @dummyos.reset
272
+ @os.add_file_simple("lib/foo/bar", :mode => 0644, :size => 0) { |f| }
273
+ @os.flush
274
+ assert_headers_equal(tar_file_header("lib/foo/bar", "", 0644, 0),
275
+ @dummyos.data[0, 512])
276
+ @dummyos.reset
277
+ @os.mkdir("lib/foo", :mode => 0644)
278
+ assert_headers_equal(tar_dir_header("lib/foo", "", 0644),
279
+ @dummyos.data[0, 512])
280
+ @os.mkdir("lib/bar", :mode => 0644)
281
+ assert_headers_equal(tar_dir_header("lib/bar", "", 0644),
282
+ @dummyos.data[512 * 1, 512])
283
+ end
284
+
285
+ def test_write_data
286
+ @dummyos.reset
287
+ @os.add_file_simple("lib/foo/bar", :mode => 0644, :size => 10) do |f|
288
+ f.write @data
289
+ end
290
+ @os.flush
291
+ assert_equal(@data + ("\0" * (512-@data.size)),
292
+ @dummyos.data[512, 512])
293
+ end
294
+
295
+ def test_file_size_is_checked
296
+ @dummyos.reset
297
+ assert_raises(Archive::Tar::Minitar::Writer::BoundedStream::FileOverflow) do
298
+ @os.add_file_simple("lib/foo/bar", :mode => 0644, :size => 10) do |f|
299
+ f.write "1" * 100
300
+ end
301
+ end
302
+ assert_nothing_raised do
303
+ @os.add_file_simple("lib/foo/bar", :mode => 0644, :size => 10) {|f| }
304
+ end
305
+ end
306
+ end
307
+
308
+ class TC_Tar__Reader < Test::Unit::TestCase
309
+ include Archive::Tar::Minitar
310
+ include TarTester
311
+
312
+ def setup
313
+ end
314
+
315
+ def teardown
316
+ end
317
+
318
+ def test_multiple_entries
319
+ str = tar_file_header("lib/foo", "", 010644, 10) + "\0" * 512
320
+ str += tar_file_header("bar", "baz", 0644, 0)
321
+ str += tar_dir_header("foo", "bar", 012345)
322
+ str += "\0" * 1024
323
+ names = %w[lib/foo bar foo]
324
+ prefixes = ["", "baz", "bar"]
325
+ modes = [010644, 0644, 012345]
326
+ sizes = [10, 0, 0]
327
+ isdir = [false, false, true]
328
+ isfile = [true, true, false]
329
+ Reader.new(StringIO.new(str)) do |is|
330
+ i = 0
331
+ is.each_entry do |entry|
332
+ assert_kind_of(Reader::EntryStream, entry)
333
+ assert_equal(names[i], entry.name)
334
+ assert_equal(prefixes[i], entry.prefix)
335
+ assert_equal(sizes[i], entry.size)
336
+ assert_equal(modes[i], entry.mode)
337
+ assert_equal(isdir[i], entry.directory?)
338
+ assert_equal(isfile[i], entry.file?)
339
+ if prefixes[i] != ""
340
+ assert_equal(File.join(prefixes[i], names[i]), entry.full_name)
341
+ else
342
+ assert_equal(names[i], entry.name)
343
+ end
344
+ i += 1
345
+ end
346
+ assert_equal(names.size, i)
347
+ end
348
+ end
349
+
350
+ def test_rewind_entry_works
351
+ content = ('a'..'z').to_a.join(" ")
352
+ str = tar_file_header("lib/foo", "", 010644, content.size) + content +
353
+ "\0" * (512 - content.size)
354
+ str << "\0" * 1024
355
+ Reader.new(StringIO.new(str)) do |is|
356
+ is.each_entry do |entry|
357
+ 3.times do
358
+ entry.rewind
359
+ assert_equal(content, entry.read)
360
+ assert_equal(content.size, entry.pos)
361
+ end
362
+ end
363
+ end
364
+ end
365
+
366
+ def test_rewind_works
367
+ content = ('a'..'z').to_a.join(" ")
368
+ str = tar_file_header("lib/foo", "", 010644, content.size) + content +
369
+ "\0" * (512 - content.size)
370
+ str << "\0" * 1024
371
+ Reader.new(StringIO.new(str)) do |is|
372
+ 3.times do
373
+ is.rewind
374
+ i = 0
375
+ is.each_entry do |entry|
376
+ assert_equal(content, entry.read)
377
+ i += 1
378
+ end
379
+ assert_equal(1, i)
380
+ end
381
+ end
382
+ end
383
+
384
+ def test_read_works
385
+ contents = ('a'..'z').inject(""){|s, x| s << x * 100}
386
+ str = tar_file_header("lib/foo", "", 010644, contents.size) + contents
387
+ str += "\0" * (512 - (str.size % 512))
388
+ Reader.new(StringIO.new(str)) do |is|
389
+ is.each_entry do |entry|
390
+ assert_kind_of(Reader::EntryStream, entry)
391
+ data = entry.read(3000) # bigger than contents.size
392
+ assert_equal(contents, data)
393
+ assert_equal(true, entry.eof?)
394
+ end
395
+ end
396
+ Reader.new(StringIO.new(str)) do |is|
397
+ is.each_entry do |entry|
398
+ assert_kind_of(Reader::EntryStream, entry)
399
+ data = entry.read(100)
400
+ (entry.size - data.size).times {|i| data << entry.getc.chr }
401
+ assert_equal(contents, data)
402
+ assert_equal(nil, entry.read(10))
403
+ assert_equal(true, entry.eof?)
404
+ end
405
+ end
406
+ Reader.new(StringIO.new(str)) do |is|
407
+ is.each_entry do |entry|
408
+ assert_kind_of(Reader::EntryStream, entry)
409
+ data = entry.read
410
+ assert_equal(contents, data)
411
+ assert_equal(nil, entry.read(10))
412
+ assert_equal(nil, entry.read)
413
+ assert_equal(nil, entry.getc)
414
+ assert_equal(true, entry.eof?)
415
+ end
416
+ end
417
+ end
418
+
419
+ def test_eof_works
420
+ str = tar_file_header("bar", "baz", 0644, 0)
421
+ Reader.new(StringIO.new(str)) do |is|
422
+ is.each_entry do |entry|
423
+ assert_kind_of(Reader::EntryStream, entry)
424
+ data = entry.read
425
+ assert_equal(nil, data)
426
+ assert_equal(nil, entry.read(10))
427
+ assert_equal(nil, entry.read)
428
+ assert_equal(nil, entry.getc)
429
+ assert_equal(true, entry.eof?)
430
+ end
431
+ end
432
+ str = tar_dir_header("foo", "bar", 012345)
433
+ Reader.new(StringIO.new(str)) do |is|
434
+ is.each_entry do |entry|
435
+ assert_kind_of(Reader::EntryStream, entry)
436
+ data = entry.read
437
+ assert_equal(nil, data)
438
+ assert_equal(nil, entry.read(10))
439
+ assert_equal(nil, entry.read)
440
+ assert_equal(nil, entry.getc)
441
+ assert_equal(true, entry.eof?)
442
+ end
443
+ end
444
+ str = tar_dir_header("foo", "bar", 012345)
445
+ str += tar_file_header("bar", "baz", 0644, 0)
446
+ str += tar_file_header("bar", "baz", 0644, 0)
447
+ Reader.new(StringIO.new(str)) do |is|
448
+ is.each_entry do |entry|
449
+ assert_kind_of(Reader::EntryStream, entry)
450
+ data = entry.read
451
+ assert_equal(nil, data)
452
+ assert_equal(nil, entry.read(10))
453
+ assert_equal(nil, entry.read)
454
+ assert_equal(nil, entry.getc)
455
+ assert_equal(true, entry.eof?)
456
+ end
457
+ end
458
+ end
459
+ end
460
+
461
+ class TC_Tar__Input < Test::Unit::TestCase
462
+ include Archive::Tar::Minitar
463
+ include TarTester
464
+
465
+ require 'rbconfig'
466
+
467
+ TEST_TGZ = "\037\213\010\000\001B1A\000\vKI,I\324+I,\322K\257b\240\0250\000\002sSS\254\342 `dj\306``nnnbndbjd\000\0247336`P0\240\231\213\220@i1\320\367@+\351a\327 \004\362\335\034\f\313\034\r\035\031\270\337Ns\344b2\344q\335\375M\304\266QM1W\357\321>\221U\021\005\246\306\367\356\367u3\262;\212\004\265\236\\\334}\351,\377\037;\217\223\301e\247\030\024\\\236\211\277\347\346sii\265\010\330\355\234\240\362\274\371[\202\361\366\302S\316\335o&~m\237r\355\377\303\230\365\352WNW\334\266_\373\273\237\347Q\315t?\263{\377?\006\271\337?\367\207\325\346]\371\376y\307_\234~d\3772\265\346\261}\323\317\373\315\352\377O\376\271/\305\377?X\253\324\303S\373\361\347\277\372^)\267\377\363\03460\331\311\\wW|\031\203\300@\207\325p\004i\2319\251\3064\266\203P\376702B\313\377\246\246\006&\243\371\237\036 $#\263X\001\210@\351@\301XO\201\227k\240]4\nF\301(\030\005\243\200\036\000\000\004\330t\023\000\f\000\000"
468
+ TEST_LONG_ENTRY_TGZ = "\037\213\b\000\022LAL\000\003\355\323Kn\3020\020\006`\2579EN\020l\3428\333\036 w\210\fq!\305\261\301\017H8=\345QJ\027\024\251*\251\250\376o1\036yF\262ek\322q:~)\255\231\227\215Y\222\307\240'\267V\232\361\374\222\037\367\031\313EA\222\362A\367\371\"\372 ]\222\020gm\370\256\357^\375I\371\270R\256\252\217\361:W]p\322\272\2721\322\365\225SR\353\276\332(w\016>Nu\323\352KENg\265z\235/\232\267\245n\215]\255\235\017q\263\355\372]\025\224\017\237a\350\363\322\320\375\307o\3735C\374\307a\250\005\347\207\031gEN\257\3273A\030\343\023:\341y\221\275\367e\2023A\022:\304\003|\314\177\260Q\267J\232[}\367\352Oj\241\264\266\243\277\276\005\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\374\324\036\373\275\021\004\000(\000\000"
469
+ FILETIMES = Time.utc(2004,"jan",1,5,0,0).to_i
470
+
471
+ TEST_CONTENTS = [
472
+ [ "data.tar.gz", 174, 0755 ],
473
+ [ "file3", 18, 0755 ],
474
+ ]
475
+
476
+ TEST_DATA_CONTENTS = [
477
+ [ "data", 0, 040755 ],
478
+ [ "data/file1", 16, 010644 ],
479
+ [ "data/file2", 16, 010644 ],
480
+ [ "data/__dir__", 0, 010644 ],
481
+ ]
482
+
483
+ def setup
484
+ FileUtils.mkdir_p("data__")
485
+ end
486
+
487
+ def teardown
488
+ FileUtils.rm_rf("data__")
489
+ end
490
+
491
+ def test_each_works
492
+ gzr = Zlib::GzipReader.new(StringIO.new(TEST_TGZ))
493
+ Input.open(gzr) do |is|
494
+ ii = 0
495
+ is.each_with_index do |entry, ii|
496
+ assert_kind_of(Reader::EntryStream, entry)
497
+ assert_equal(TEST_CONTENTS[ii][0], entry.name)
498
+ assert_equal(TEST_CONTENTS[ii][1], entry.size)
499
+ assert_equal(TEST_CONTENTS[ii][2], entry.mode)
500
+ assert_equal(FILETIMES, entry.mtime)
501
+
502
+ if 0 == ii
503
+ gzr2 = Zlib::GzipReader.new(StringIO.new(entry.read))
504
+ Input.open(gzr2) do |is2|
505
+ jj = 0
506
+ is2.each_with_index do |entry2, jj|
507
+ assert_kind_of(Reader::EntryStream, entry2)
508
+ assert_equal(TEST_DATA_CONTENTS[jj][0], entry2.name)
509
+ assert_equal(TEST_DATA_CONTENTS[jj][1], entry2.size)
510
+ assert_equal(TEST_DATA_CONTENTS[jj][2], entry2.mode)
511
+ assert_equal(FILETIMES, entry2.mtime)
512
+ end
513
+ assert_equal(3, jj)
514
+ end
515
+ end
516
+ end
517
+ assert_equal(1, ii)
518
+ end
519
+ end
520
+
521
+ def test_extract_entry_works
522
+ gzr = Zlib::GzipReader.new(StringIO.new(TEST_TGZ))
523
+ Input.open(gzr) do |is|
524
+ ii = 0
525
+ is.each_with_index do |entry, ii|
526
+ is.extract_entry("data__", entry)
527
+ name = File.join("data__", entry.name)
528
+
529
+ if entry.directory?
530
+ assert(File.directory?(name))
531
+ else
532
+ assert(File.file?(name))
533
+
534
+ assert_equal(TEST_CONTENTS[ii][1], File.stat(name).size)
535
+ end
536
+ assert_equal(TEST_CONTENTS[ii][2], File.stat(name).mode & 0777) unless RUBY_PLATFORM =~ /win32/
537
+
538
+ if 0 == ii
539
+ begin
540
+ ff = File.open(name, "rb")
541
+ gzr2 = Zlib::GzipReader.new(ff)
542
+ Input.open(gzr2) do |is2|
543
+ jj = 0
544
+ is2.each_with_index do |entry2, jj|
545
+ is2.extract_entry("data__", entry2)
546
+ name2 = File.join("data__", entry2.name)
547
+
548
+ if entry2.directory?
549
+ assert(File.directory?(name2))
550
+ else
551
+ assert(File.file?(name2))
552
+ assert_equal(TEST_DATA_CONTENTS[jj][1], File.stat(name2).size, name2)
553
+ end
554
+ assert_equal(TEST_DATA_CONTENTS[jj][2], File.stat(name2).mode, name2) unless RUBY_PLATFORM =~ /win32/
555
+ end
556
+ end
557
+ ensure
558
+ ff.close unless ff.closed?
559
+ end
560
+ end
561
+ end
562
+ assert_equal(1, ii)
563
+ end
564
+ end
565
+
566
+ def test_extract_long_file_name_entry_works
567
+ gzr = Zlib::GzipReader.new(StringIO.new(TEST_LONG_ENTRY_TGZ))
568
+ Input.open(gzr) do |is|
569
+ ii = 0
570
+ is.each_with_index do |entry, ii|
571
+ assert_equal("super_duper_super_duper_extraordinary_really_very_very_sublimly_really_abcdefghijklmnopqrstuvwxyz_test_test_testsuper_duper_super_duper_extraordinary_really_very_very_sublimly_really_abcdefghijklmnopqrstuvwxyz_test_test_test.txt", entry.name)
572
+ is.extract_entry("data__", entry)
573
+ name = File.join("data__", entry.name)
574
+ assert(File.file?(name))
575
+ end
576
+ assert_equal(0, ii)
577
+ end
578
+ end
579
+ end
580
+
581
+ class TC_Tar__Output < Test::Unit::TestCase
582
+ include Archive::Tar::Minitar
583
+ include TarTester
584
+
585
+ def setup
586
+ FileUtils.mkdir_p("data__")
587
+ %w(a b c).each do |filename|
588
+ name = File.join("data__", filename)
589
+ File.open(name, "wb") { |f| f.puts "#{name}: 123456789012345678901234567890" }
590
+ end
591
+ @tarfile = "data__/bla2.tar"
592
+ end
593
+
594
+ def teardown
595
+ FileUtils.rm_rf("data__")
596
+ end
597
+
598
+ def test_file_looks_good
599
+ Output.open(@tarfile) do |os|
600
+ Dir.chdir("data__") do
601
+ %w(a b c).each do |name|
602
+ stat = File.stat(name)
603
+ opts = { :size => stat.size, :mode => 0644 }
604
+ os.tar.add_file_simple(name, opts) do |ss|
605
+ File.open(name, "rb") { |ff| ss.write(ff.read(4096)) until ff.eof? }
606
+ end
607
+ end
608
+ end
609
+ end
610
+ ff = File.open(@tarfile, "rb")
611
+ Reader.open(ff) do |is|
612
+ ii = 0
613
+ is.each do |entry|
614
+ case ii
615
+ when 0
616
+ assert_equal("a", entry.name)
617
+ when 1
618
+ assert_equal("b", entry.name)
619
+ when 2
620
+ assert_equal("c", entry.name)
621
+ end
622
+ ii += 1
623
+ end
624
+ assert_equal(3, ii)
625
+ end
626
+ ensure
627
+ ff.close if ff
628
+ end
629
+ end