rubyzip 0.5.7

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.

@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class NotZippedRuby
4
+ def returnTrue
5
+ true
6
+ end
7
+ end
Binary file
Binary file
Binary file
@@ -0,0 +1,155 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ class TestFiles
6
+ RANDOM_ASCII_FILE1 = "data/generated/randomAscii1.txt"
7
+ RANDOM_ASCII_FILE2 = "data/generated/randomAscii2.txt"
8
+ RANDOM_ASCII_FILE3 = "data/generated/randomAscii3.txt"
9
+ RANDOM_BINARY_FILE1 = "data/generated/randomBinary1.bin"
10
+ RANDOM_BINARY_FILE2 = "data/generated/randomBinary2.bin"
11
+
12
+ EMPTY_TEST_DIR = "data/generated/emptytestdir"
13
+
14
+ ASCII_TEST_FILES = [ RANDOM_ASCII_FILE1, RANDOM_ASCII_FILE2, RANDOM_ASCII_FILE3 ]
15
+ BINARY_TEST_FILES = [ RANDOM_BINARY_FILE1, RANDOM_BINARY_FILE2 ]
16
+ TEST_DIRECTORIES = [ EMPTY_TEST_DIR ]
17
+ TEST_FILES = [ ASCII_TEST_FILES, BINARY_TEST_FILES, EMPTY_TEST_DIR ].flatten!
18
+
19
+ def TestFiles.create_test_files(recreate)
20
+ if (recreate ||
21
+ ! (TEST_FILES.inject(true) { |accum, element| accum && File.exists?(element) }))
22
+
23
+ Dir.mkdir "data/generated" rescue Errno::EEXIST
24
+
25
+ ASCII_TEST_FILES.each_with_index {
26
+ |filename, index|
27
+ create_random_ascii(filename, 1E4 * (index+1))
28
+ }
29
+
30
+ BINARY_TEST_FILES.each_with_index {
31
+ |filename, index|
32
+ create_random_binary(filename, 1E4 * (index+1))
33
+ }
34
+
35
+ ensure_dir(EMPTY_TEST_DIR)
36
+ end
37
+ end
38
+
39
+ private
40
+ def TestFiles.create_random_ascii(filename, size)
41
+ File.open(filename, "wb") {
42
+ |file|
43
+ while (file.tell < size)
44
+ file << rand
45
+ end
46
+ }
47
+ end
48
+
49
+ def TestFiles.create_random_binary(filename, size)
50
+ File.open(filename, "wb") {
51
+ |file|
52
+ while (file.tell < size)
53
+ file << [rand].pack("V")
54
+ end
55
+ }
56
+ end
57
+
58
+ def TestFiles.ensure_dir(name)
59
+ if File.exists?(name)
60
+ return if File.stat(name).directory?
61
+ File.delete(name)
62
+ end
63
+ Dir.mkdir(name)
64
+ end
65
+
66
+ end
67
+
68
+
69
+
70
+ # For representation and creation of
71
+ # test data
72
+ class TestZipFile
73
+ attr_accessor :zip_name, :entry_names, :comment
74
+
75
+ def initialize(zip_name, entry_names, comment = "")
76
+ @zip_name=zip_name
77
+ @entry_names=entry_names
78
+ @comment = comment
79
+ end
80
+
81
+ def TestZipFile.create_test_zips(recreate)
82
+ files = Dir.entries("data/generated")
83
+ if (recreate ||
84
+ ! (files.index(File.basename(TEST_ZIP1.zip_name)) &&
85
+ files.index(File.basename(TEST_ZIP2.zip_name)) &&
86
+ files.index(File.basename(TEST_ZIP3.zip_name)) &&
87
+ files.index(File.basename(TEST_ZIP4.zip_name)) &&
88
+ files.index("empty.txt") &&
89
+ files.index("short.txt") &&
90
+ files.index("longAscii.txt") &&
91
+ files.index("longBinary.bin") ))
92
+ raise "failed to create test zip '#{TEST_ZIP1.zip_name}'" unless
93
+ system("zip #{TEST_ZIP1.zip_name} data/file2.txt")
94
+ raise "failed to remove entry from '#{TEST_ZIP1.zip_name}'" unless
95
+ system("zip #{TEST_ZIP1.zip_name} -d data/file2.txt")
96
+
97
+ File.open("data/generated/empty.txt", "w") {}
98
+
99
+ File.open("data/generated/short.txt", "w") { |file| file << "ABCDEF" }
100
+ ziptestTxt=""
101
+ File.open("data/file2.txt") { |file| ziptestTxt=file.read }
102
+ File.open("data/generated/longAscii.txt", "w") {
103
+ |file|
104
+ while (file.tell < 1E5)
105
+ file << ziptestTxt
106
+ end
107
+ }
108
+
109
+ testBinaryPattern=""
110
+ File.open("data/generated/empty.zip") { |file| testBinaryPattern=file.read }
111
+ testBinaryPattern *= 4
112
+
113
+ File.open("data/generated/longBinary.bin", "wb") {
114
+ |file|
115
+ while (file.tell < 3E5)
116
+ file << testBinaryPattern << rand
117
+ end
118
+ }
119
+ raise "failed to create test zip '#{TEST_ZIP2.zip_name}'" unless
120
+ system("zip #{TEST_ZIP2.zip_name} #{TEST_ZIP2.entry_names.join(' ')}")
121
+
122
+ # without bash system interprets everything after echo as parameters to
123
+ # echo including | zip -z ...
124
+ raise "failed to add comment to test zip '#{TEST_ZIP2.zip_name}'" unless
125
+ system("bash -c \"echo #{TEST_ZIP2.comment} | zip -z #{TEST_ZIP2.zip_name}\"")
126
+
127
+ raise "failed to create test zip '#{TEST_ZIP3.zip_name}'" unless
128
+ system("zip #{TEST_ZIP3.zip_name} #{TEST_ZIP3.entry_names.join(' ')}")
129
+
130
+ raise "failed to create test zip '#{TEST_ZIP4.zip_name}'" unless
131
+ system("zip #{TEST_ZIP4.zip_name} #{TEST_ZIP4.entry_names.join(' ')}")
132
+ end
133
+ rescue
134
+ raise $!.to_s +
135
+ "\n\nziptest.rb requires the Info-ZIP program 'zip' in the path\n" +
136
+ "to create test data. If you don't have it you can download\n" +
137
+ "the necessary test files at http://sf.net/projects/rubyzip."
138
+ end
139
+
140
+ TEST_ZIP1 = TestZipFile.new("data/generated/empty.zip", [])
141
+ TEST_ZIP2 = TestZipFile.new("data/generated/4entry.zip", %w{ data/generated/longAscii.txt data/generated/empty.txt data/generated/short.txt data/generated/longBinary.bin},
142
+ "my zip comment")
143
+ TEST_ZIP3 = TestZipFile.new("data/generated/test1.zip", %w{ data/file1.txt })
144
+ TEST_ZIP4 = TestZipFile.new("data/generated/zipWithDir.zip", [ "data/file1.txt",
145
+ TestFiles::EMPTY_TEST_DIR])
146
+ end
147
+
148
+
149
+ END {
150
+ TestFiles::create_test_files(ARGV.index("recreate") != nil ||
151
+ ARGV.index("recreateonly") != nil)
152
+ TestZipFile::create_test_zips(ARGV.index("recreate") != nil ||
153
+ ARGV.index("recreateonly") != nil)
154
+ exit if ARGV.index("recreateonly") != nil
155
+ }
@@ -0,0 +1,208 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ $: << "../lib"
6
+
7
+ require 'test/unit'
8
+ require 'zip/ioextras'
9
+
10
+ include 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#{$/}",
32
+ "this is the second line#{$/}",
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 read(charsToRead)
44
+ retVal=@contents[@readPointer, charsToRead]
45
+ @readPointer+=charsToRead
46
+ return retVal
47
+ end
48
+
49
+ def produce_input
50
+ read(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[1], @io.gets)
66
+ assert_equal(2, @io.lineno)
67
+ assert_equal(TEST_LINES[2], @io.gets)
68
+ assert_equal(3, @io.lineno)
69
+ assert_equal(nil, @io.gets)
70
+ assert_equal(4, @io.lineno)
71
+ end
72
+
73
+ def test_getsMultiCharSeperator
74
+ assert_equal("Hell", @io.gets("ll"))
75
+ assert_equal("o world#{$/}this is the second l", @io.gets("d l"))
76
+ end
77
+
78
+ def test_each_line
79
+ lineNumber=0
80
+ @io.each_line {
81
+ |line|
82
+ assert_equal(TEST_LINES[lineNumber], line)
83
+ lineNumber+=1
84
+ }
85
+ end
86
+
87
+ def test_readlines
88
+ assert_equal(TEST_LINES, @io.readlines)
89
+ end
90
+
91
+ def test_readline
92
+ test_gets
93
+ begin
94
+ @io.readline
95
+ fail "EOFError expected"
96
+ rescue EOFError
97
+ end
98
+ end
99
+ end
100
+
101
+ class AbstractOutputStreamTest < Test::Unit::TestCase
102
+ class TestOutputStream
103
+ include AbstractOutputStream
104
+
105
+ attr_accessor :buffer
106
+
107
+ def initialize
108
+ @buffer = ""
109
+ end
110
+
111
+ def << (data)
112
+ @buffer << data
113
+ self
114
+ end
115
+ end
116
+
117
+ def setup
118
+ @outputStream = TestOutputStream.new
119
+
120
+ @origCommaSep = $,
121
+ @origOutputSep = $\
122
+ end
123
+
124
+ def teardown
125
+ $, = @origCommaSep
126
+ $\ = @origOutputSep
127
+ end
128
+
129
+ def test_write
130
+ count = @outputStream.write("a little string")
131
+ assert_equal("a little string", @outputStream.buffer)
132
+ assert_equal("a little string".length, count)
133
+
134
+ count = @outputStream.write(". a little more")
135
+ assert_equal("a little string. a little more", @outputStream.buffer)
136
+ assert_equal(". a little more".length, count)
137
+ end
138
+
139
+ def test_print
140
+ $\ = nil # record separator set to nil
141
+ @outputStream.print("hello")
142
+ assert_equal("hello", @outputStream.buffer)
143
+
144
+ @outputStream.print(" world.")
145
+ assert_equal("hello world.", @outputStream.buffer)
146
+
147
+ @outputStream.print(" You ok ", "out ", "there?")
148
+ assert_equal("hello world. You ok out there?", @outputStream.buffer)
149
+
150
+ $\ = "\n"
151
+ @outputStream.print
152
+ assert_equal("hello world. You ok out there?\n", @outputStream.buffer)
153
+
154
+ @outputStream.print("I sure hope so!")
155
+ assert_equal("hello world. You ok out there?\nI sure hope so!\n", @outputStream.buffer)
156
+
157
+ $, = "X"
158
+ @outputStream.buffer = ""
159
+ @outputStream.print("monkey", "duck", "zebra")
160
+ assert_equal("monkeyXduckXzebra\n", @outputStream.buffer)
161
+
162
+ $\ = nil
163
+ @outputStream.buffer = ""
164
+ @outputStream.print(20)
165
+ assert_equal("20", @outputStream.buffer)
166
+ end
167
+
168
+ def test_printf
169
+ @outputStream.printf("%d %04x", 123, 123)
170
+ assert_equal("123 007b", @outputStream.buffer)
171
+ end
172
+
173
+ def test_putc
174
+ @outputStream.putc("A")
175
+ assert_equal("A", @outputStream.buffer)
176
+ @outputStream.putc(65)
177
+ assert_equal("AA", @outputStream.buffer)
178
+ end
179
+
180
+ def test_puts
181
+ @outputStream.puts
182
+ assert_equal("\n", @outputStream.buffer)
183
+
184
+ @outputStream.puts("hello", "world")
185
+ assert_equal("\nhello\nworld\n", @outputStream.buffer)
186
+
187
+ @outputStream.buffer = ""
188
+ @outputStream.puts("hello\n", "world\n")
189
+ assert_equal("hello\nworld\n", @outputStream.buffer)
190
+
191
+ @outputStream.buffer = ""
192
+ @outputStream.puts(["hello\n", "world\n"])
193
+ assert_equal("hello\nworld\n", @outputStream.buffer)
194
+
195
+ @outputStream.buffer = ""
196
+ @outputStream.puts(["hello\n", "world\n"], "bingo")
197
+ assert_equal("hello\nworld\nbingo\n", @outputStream.buffer)
198
+
199
+ @outputStream.buffer = ""
200
+ @outputStream.puts(16, 20, 50, "hello")
201
+ assert_equal("16\n20\n50\nhello\n", @outputStream.buffer)
202
+ end
203
+ end
204
+
205
+
206
+ # Copyright (C) 2002-2004 Thomas Sondergaard
207
+ # rubyzip is free software; you can redistribute it and/or
208
+ # modify it under the terms of the ruby license.
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ $: << "../lib"
6
+
7
+ require 'test/unit'
8
+ require 'zip/stdrubyext'
9
+
10
+ class ModuleTest < Test::Unit::TestCase
11
+
12
+ def test_select_map
13
+ assert_equal([2, 4, 8, 10], [1, 2, 3, 4, 5].select_map { |e| e == 3 ? nil : 2*e })
14
+ end
15
+
16
+ end
17
+
18
+ class StringExtensionsTest < Test::Unit::TestCase
19
+
20
+ def test_starts_with
21
+ assert("hello".starts_with(""))
22
+ assert("hello".starts_with("h"))
23
+ assert("hello".starts_with("he"))
24
+ assert(! "hello".starts_with("hello there"))
25
+ assert(! "hello".starts_with(" he"))
26
+
27
+ assert_raise(TypeError, "type mismatch: NilClass given") {
28
+ "hello".starts_with(nil)
29
+ }
30
+ end
31
+
32
+ def test_ends_with
33
+ assert("hello".ends_with("o"))
34
+ assert("hello".ends_with("lo"))
35
+ assert("hello".ends_with("hello"))
36
+ assert(!"howdy".ends_with("o"))
37
+ assert(!"howdy".ends_with("oy"))
38
+ assert(!"howdy".ends_with("howdy doody"))
39
+ assert(!"howdy".ends_with("doody howdy"))
40
+ end
41
+
42
+ def test_ensure_end
43
+ assert_equal("hello!", "hello!".ensure_end("!"))
44
+ assert_equal("hello!", "hello!".ensure_end("o!"))
45
+ assert_equal("hello!", "hello".ensure_end("!"))
46
+ assert_equal("hello!", "hel".ensure_end("lo!"))
47
+ end
48
+ end
49
+
50
+ # Copyright (C) 2002, 2003 Thomas Sondergaard
51
+ # rubyzip is free software; you can redistribute it and/or
52
+ # modify it under the terms of the ruby license.
@@ -0,0 +1,829 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ $: << "../lib"
6
+
7
+ require 'zip/zipfilesystem'
8
+ require 'test/unit'
9
+
10
+ module ExtraAssertions
11
+
12
+ def assert_forwarded(anObject, method, retVal, *expectedArgs)
13
+ callArgs = nil
14
+ setCallArgsProc = proc { |args| callArgs = args }
15
+ anObject.instance_eval <<-"end_eval"
16
+ alias #{method}_org #{method}
17
+ def #{method}(*args)
18
+ ObjectSpace._id2ref(#{setCallArgsProc.object_id}).call(args)
19
+ ObjectSpace._id2ref(#{retVal.object_id})
20
+ end
21
+ end_eval
22
+
23
+ assert_equal(retVal, yield) # Invoke test
24
+ assert_equal(expectedArgs, callArgs)
25
+ ensure
26
+ anObject.instance_eval "alias #{method} #{method}_org"
27
+ end
28
+
29
+ end
30
+
31
+ include Zip
32
+
33
+ class ZipFsFileNonmutatingTest < Test::Unit::TestCase
34
+ def setup
35
+ @zipFile = ZipFile.new("data/zipWithDirs.zip")
36
+ end
37
+
38
+ def teardown
39
+ @zipFile.close if @zipFile
40
+ end
41
+
42
+ def test_umask
43
+ assert_equal(File.umask, @zipFile.file.umask)
44
+ @zipFile.file.umask(0006)
45
+ end
46
+
47
+ def test_exists?
48
+ assert(! @zipFile.file.exists?("notAFile"))
49
+ assert(@zipFile.file.exists?("file1"))
50
+ assert(@zipFile.file.exists?("dir1"))
51
+ assert(@zipFile.file.exists?("dir1/"))
52
+ assert(@zipFile.file.exists?("dir1/file12"))
53
+ assert(@zipFile.file.exist?("dir1/file12")) # notice, tests exist? alias of exists? !
54
+
55
+ @zipFile.dir.chdir "dir1/"
56
+ assert(!@zipFile.file.exists?("file1"))
57
+ assert(@zipFile.file.exists?("file12"))
58
+ end
59
+
60
+ def test_open_read
61
+ blockCalled = false
62
+ @zipFile.file.open("file1", "r") {
63
+ |f|
64
+ blockCalled = true
65
+ assert_equal("this is the entry 'file1' in my test archive!",
66
+ f.readline.chomp)
67
+ }
68
+ assert(blockCalled)
69
+
70
+ blockCalled = false
71
+ @zipFile.dir.chdir "dir2"
72
+ @zipFile.file.open("file21", "r") {
73
+ |f|
74
+ blockCalled = true
75
+ assert_equal("this is the entry 'dir2/file21' in my test archive!",
76
+ f.readline.chomp)
77
+ }
78
+ assert(blockCalled)
79
+ @zipFile.dir.chdir "/"
80
+
81
+ assert_raise(Errno::ENOENT) {
82
+ @zipFile.file.open("noSuchEntry")
83
+ }
84
+
85
+ begin
86
+ is = @zipFile.file.open("file1")
87
+ assert_equal("this is the entry 'file1' in my test archive!",
88
+ is.readline.chomp)
89
+ ensure
90
+ is.close if is
91
+ end
92
+ end
93
+
94
+ def test_new
95
+ begin
96
+ is = @zipFile.file.new("file1")
97
+ assert_equal("this is the entry 'file1' in my test archive!",
98
+ is.readline.chomp)
99
+ ensure
100
+ is.close if is
101
+ end
102
+ begin
103
+ is = @zipFile.file.new("file1") {
104
+ fail "should not call block"
105
+ }
106
+ ensure
107
+ is.close if is
108
+ end
109
+ end
110
+
111
+ def test_symlink
112
+ assert_raise(NotImplementedError) {
113
+ @zipFile.file.symlink("file1", "aSymlink")
114
+ }
115
+ end
116
+
117
+ def test_size
118
+ assert_raise(Errno::ENOENT) { @zipFile.file.size("notAFile") }
119
+ assert_equal(72, @zipFile.file.size("file1"))
120
+ assert_equal(0, @zipFile.file.size("dir2/dir21"))
121
+
122
+ assert_equal(72, @zipFile.file.stat("file1").size)
123
+ assert_equal(0, @zipFile.file.stat("dir2/dir21").size)
124
+ end
125
+
126
+ def test_size?
127
+ assert_equal(nil, @zipFile.file.size?("notAFile"))
128
+ assert_equal(72, @zipFile.file.size?("file1"))
129
+ assert_equal(nil, @zipFile.file.size?("dir2/dir21"))
130
+
131
+ assert_equal(72, @zipFile.file.stat("file1").size?)
132
+ assert_equal(nil, @zipFile.file.stat("dir2/dir21").size?)
133
+ end
134
+
135
+
136
+ def test_file?
137
+ assert(@zipFile.file.file?("file1"))
138
+ assert(@zipFile.file.file?("dir2/file21"))
139
+ assert(! @zipFile.file.file?("dir1"))
140
+ assert(! @zipFile.file.file?("dir1/dir11"))
141
+
142
+ assert(@zipFile.file.stat("file1").file?)
143
+ assert(@zipFile.file.stat("dir2/file21").file?)
144
+ assert(! @zipFile.file.stat("dir1").file?)
145
+ assert(! @zipFile.file.stat("dir1/dir11").file?)
146
+ end
147
+
148
+ include ExtraAssertions
149
+
150
+ def test_dirname
151
+ assert_forwarded(File, :dirname, "retVal", "a/b/c/d") {
152
+ @zipFile.file.dirname("a/b/c/d")
153
+ }
154
+ end
155
+
156
+ def test_basename
157
+ assert_forwarded(File, :basename, "retVal", "a/b/c/d") {
158
+ @zipFile.file.basename("a/b/c/d")
159
+ }
160
+ end
161
+
162
+ def test_split
163
+ assert_forwarded(File, :split, "retVal", "a/b/c/d") {
164
+ @zipFile.file.split("a/b/c/d")
165
+ }
166
+ end
167
+
168
+ def test_join
169
+ assert_equal("a/b/c", @zipFile.file.join("a/b", "c"))
170
+ assert_equal("a/b/c/d", @zipFile.file.join("a/b", "c/d"))
171
+ assert_equal("/c/d", @zipFile.file.join("", "c/d"))
172
+ assert_equal("a/b/c/d", @zipFile.file.join("a", "b", "c", "d"))
173
+ end
174
+
175
+ def test_utime
176
+ t_now = Time.now
177
+ t_bak = @zipFile.file.mtime("file1")
178
+ @zipFile.file.utime(t_now, "file1")
179
+ assert_equal(t_now, @zipFile.file.mtime("file1"))
180
+ @zipFile.file.utime(t_bak, "file1")
181
+ assert_equal(t_bak, @zipFile.file.mtime("file1"))
182
+ end
183
+
184
+
185
+ def assert_always_false(operation)
186
+ assert(! @zipFile.file.send(operation, "noSuchFile"))
187
+ assert(! @zipFile.file.send(operation, "file1"))
188
+ assert(! @zipFile.file.send(operation, "dir1"))
189
+ assert(! @zipFile.file.stat("file1").send(operation))
190
+ assert(! @zipFile.file.stat("dir1").send(operation))
191
+ end
192
+
193
+ def assert_true_if_entry_exists(operation)
194
+ assert(! @zipFile.file.send(operation, "noSuchFile"))
195
+ assert(@zipFile.file.send(operation, "file1"))
196
+ assert(@zipFile.file.send(operation, "dir1"))
197
+ assert(@zipFile.file.stat("file1").send(operation))
198
+ assert(@zipFile.file.stat("dir1").send(operation))
199
+ end
200
+
201
+ def test_pipe?
202
+ assert_always_false(:pipe?)
203
+ end
204
+
205
+ def test_blockdev?
206
+ assert_always_false(:blockdev?)
207
+ end
208
+
209
+ def test_symlink?
210
+ assert_always_false(:symlink?)
211
+ end
212
+
213
+ def test_socket?
214
+ assert_always_false(:socket?)
215
+ end
216
+
217
+ def test_chardev?
218
+ assert_always_false(:chardev?)
219
+ end
220
+
221
+ def test_truncate
222
+ assert_raise(StandardError, "truncate not supported") {
223
+ @zipFile.file.truncate("file1", 100)
224
+ }
225
+ end
226
+
227
+ def assert_e_n_o_e_n_t(operation, args = ["NoSuchFile"])
228
+ assert_raise(Errno::ENOENT) {
229
+ @zipFile.file.send(operation, *args)
230
+ }
231
+ end
232
+
233
+ def test_ftype
234
+ assert_e_n_o_e_n_t(:ftype)
235
+ assert_equal("file", @zipFile.file.ftype("file1"))
236
+ assert_equal("directory", @zipFile.file.ftype("dir1/dir11"))
237
+ assert_equal("directory", @zipFile.file.ftype("dir1/dir11/"))
238
+ end
239
+
240
+ def test_link
241
+ assert_raise(NotImplementedError) {
242
+ @zipFile.file.link("file1", "someOtherString")
243
+ }
244
+ end
245
+
246
+ def test_directory?
247
+ assert(! @zipFile.file.directory?("notAFile"))
248
+ assert(! @zipFile.file.directory?("file1"))
249
+ assert(! @zipFile.file.directory?("dir1/file11"))
250
+ assert(@zipFile.file.directory?("dir1"))
251
+ assert(@zipFile.file.directory?("dir1/"))
252
+ assert(@zipFile.file.directory?("dir2/dir21"))
253
+
254
+ assert(! @zipFile.file.stat("file1").directory?)
255
+ assert(! @zipFile.file.stat("dir1/file11").directory?)
256
+ assert(@zipFile.file.stat("dir1").directory?)
257
+ assert(@zipFile.file.stat("dir1/").directory?)
258
+ assert(@zipFile.file.stat("dir2/dir21").directory?)
259
+ end
260
+
261
+ def test_chown
262
+ assert_equal(2, @zipFile.file.chown(1,2, "dir1", "file1"))
263
+ assert_equal(1, @zipFile.file.stat("dir1").uid)
264
+ assert_equal(2, @zipFile.file.stat("dir1").gid)
265
+ assert_equal(2, @zipFile.file.chown(nil, nil, "dir1", "file1"))
266
+ end
267
+
268
+ def test_zero?
269
+ assert(! @zipFile.file.zero?("notAFile"))
270
+ assert(! @zipFile.file.zero?("file1"))
271
+ assert(@zipFile.file.zero?("dir1"))
272
+ blockCalled = false
273
+ ZipFile.open("data/generated/4entry.zip") {
274
+ |zf|
275
+ blockCalled = true
276
+ assert(zf.file.zero?("data/generated/empty.txt"))
277
+ }
278
+ assert(blockCalled)
279
+
280
+ assert(! @zipFile.file.stat("file1").zero?)
281
+ assert(@zipFile.file.stat("dir1").zero?)
282
+ blockCalled = false
283
+ ZipFile.open("data/generated/4entry.zip") {
284
+ |zf|
285
+ blockCalled = true
286
+ assert(zf.file.stat("data/generated/empty.txt").zero?)
287
+ }
288
+ assert(blockCalled)
289
+ end
290
+
291
+ def test_expand_path
292
+ ZipFile.open("data/zipWithDirs.zip") {
293
+ |zf|
294
+ assert_equal("/", zf.file.expand_path("."))
295
+ zf.dir.chdir "dir1"
296
+ assert_equal("/dir1", zf.file.expand_path("."))
297
+ assert_equal("/dir1/file12", zf.file.expand_path("file12"))
298
+ assert_equal("/", zf.file.expand_path(".."))
299
+ assert_equal("/dir2/dir21", zf.file.expand_path("../dir2/dir21"))
300
+ }
301
+ end
302
+
303
+ def test_mtime
304
+ assert_equal(Time.at(1027694306),
305
+ @zipFile.file.mtime("dir2/file21"))
306
+ assert_equal(Time.at(1027690863),
307
+ @zipFile.file.mtime("dir2/dir21"))
308
+ assert_raise(Errno::ENOENT) {
309
+ @zipFile.file.mtime("noSuchEntry")
310
+ }
311
+
312
+ assert_equal(Time.at(1027694306),
313
+ @zipFile.file.stat("dir2/file21").mtime)
314
+ assert_equal(Time.at(1027690863),
315
+ @zipFile.file.stat("dir2/dir21").mtime)
316
+ end
317
+
318
+ def test_ctime
319
+ assert_nil(@zipFile.file.ctime("file1"))
320
+ assert_nil(@zipFile.file.stat("file1").ctime)
321
+ end
322
+
323
+ def test_atime
324
+ assert_nil(@zipFile.file.atime("file1"))
325
+ assert_nil(@zipFile.file.stat("file1").atime)
326
+ end
327
+
328
+ def test_readable?
329
+ assert(! @zipFile.file.readable?("noSuchFile"))
330
+ assert(@zipFile.file.readable?("file1"))
331
+ assert(@zipFile.file.readable?("dir1"))
332
+ assert(@zipFile.file.stat("file1").readable?)
333
+ assert(@zipFile.file.stat("dir1").readable?)
334
+ end
335
+
336
+ def test_readable_real?
337
+ assert(! @zipFile.file.readable_real?("noSuchFile"))
338
+ assert(@zipFile.file.readable_real?("file1"))
339
+ assert(@zipFile.file.readable_real?("dir1"))
340
+ assert(@zipFile.file.stat("file1").readable_real?)
341
+ assert(@zipFile.file.stat("dir1").readable_real?)
342
+ end
343
+
344
+ def test_writable?
345
+ assert(! @zipFile.file.writable?("noSuchFile"))
346
+ assert(@zipFile.file.writable?("file1"))
347
+ assert(@zipFile.file.writable?("dir1"))
348
+ assert(@zipFile.file.stat("file1").writable?)
349
+ assert(@zipFile.file.stat("dir1").writable?)
350
+ end
351
+
352
+ def test_writable_real?
353
+ assert(! @zipFile.file.writable_real?("noSuchFile"))
354
+ assert(@zipFile.file.writable_real?("file1"))
355
+ assert(@zipFile.file.writable_real?("dir1"))
356
+ assert(@zipFile.file.stat("file1").writable_real?)
357
+ assert(@zipFile.file.stat("dir1").writable_real?)
358
+ end
359
+
360
+ def test_executable?
361
+ assert(! @zipFile.file.executable?("noSuchFile"))
362
+ assert(! @zipFile.file.executable?("file1"))
363
+ assert(@zipFile.file.executable?("dir1"))
364
+ assert(! @zipFile.file.stat("file1").executable?)
365
+ assert(@zipFile.file.stat("dir1").executable?)
366
+ end
367
+
368
+ def test_executable_real?
369
+ assert(! @zipFile.file.executable_real?("noSuchFile"))
370
+ assert(! @zipFile.file.executable_real?("file1"))
371
+ assert(@zipFile.file.executable_real?("dir1"))
372
+ assert(! @zipFile.file.stat("file1").executable_real?)
373
+ assert(@zipFile.file.stat("dir1").executable_real?)
374
+ end
375
+
376
+ def test_owned?
377
+ assert_true_if_entry_exists(:owned?)
378
+ end
379
+
380
+ def test_grpowned?
381
+ assert_true_if_entry_exists(:grpowned?)
382
+ end
383
+
384
+ def test_setgid?
385
+ assert_always_false(:setgid?)
386
+ end
387
+
388
+ def test_setuid?
389
+ assert_always_false(:setgid?)
390
+ end
391
+
392
+ def test_sticky?
393
+ assert_always_false(:sticky?)
394
+ end
395
+
396
+ def test_readlink
397
+ assert_raise(NotImplementedError) {
398
+ @zipFile.file.readlink("someString")
399
+ }
400
+ end
401
+
402
+ def test_stat
403
+ s = @zipFile.file.stat("file1")
404
+ assert(s.kind_of?(File::Stat)) # It pretends
405
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
406
+ @zipFile.file.stat("noSuchFile")
407
+ }
408
+ end
409
+
410
+ def test_lstat
411
+ assert(@zipFile.file.lstat("file1").file?)
412
+ end
413
+
414
+
415
+ def test_chmod
416
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
417
+ @zipFile.file.chmod(0644, "file1", "NoSuchFile")
418
+ }
419
+ assert_equal(2, @zipFile.file.chmod(0644, "file1", "dir1"))
420
+ end
421
+
422
+ def test_pipe
423
+ assert_raise(NotImplementedError) {
424
+ @zipFile.file.pipe
425
+ }
426
+ end
427
+
428
+ def test_foreach
429
+ ZipFile.open("data/generated/zipWithDir.zip") {
430
+ |zf|
431
+ ref = []
432
+ File.foreach("data/file1.txt") { |e| ref << e }
433
+
434
+ index = 0
435
+ zf.file.foreach("data/file1.txt") {
436
+ |l|
437
+ assert_equal(ref[index], l)
438
+ index = index.next
439
+ }
440
+ assert_equal(ref.size, index)
441
+ }
442
+
443
+ ZipFile.open("data/generated/zipWithDir.zip") {
444
+ |zf|
445
+ ref = []
446
+ File.foreach("data/file1.txt", " ") { |e| ref << e }
447
+
448
+ index = 0
449
+ zf.file.foreach("data/file1.txt", " ") {
450
+ |l|
451
+ assert_equal(ref[index], l)
452
+ index = index.next
453
+ }
454
+ assert_equal(ref.size, index)
455
+ }
456
+ end
457
+
458
+ def test_popen
459
+ assert_equal(File.popen("ls") { |f| f.read },
460
+ @zipFile.file.popen("ls") { |f| f.read })
461
+ end
462
+
463
+ # Can be added later
464
+ # def test_select
465
+ # fail "implement test"
466
+ # end
467
+
468
+ def test_readlines
469
+ ZipFile.open("data/generated/zipWithDir.zip") {
470
+ |zf|
471
+ assert_equal(File.readlines("data/file1.txt"),
472
+ zf.file.readlines("data/file1.txt"))
473
+ }
474
+ end
475
+
476
+ def test_read
477
+ ZipFile.open("data/generated/zipWithDir.zip") {
478
+ |zf|
479
+ assert_equal(File.read("data/file1.txt"),
480
+ zf.file.read("data/file1.txt"))
481
+ }
482
+ end
483
+
484
+ end
485
+
486
+ class ZipFsFileStatTest < Test::Unit::TestCase
487
+
488
+ def setup
489
+ @zipFile = ZipFile.new("data/zipWithDirs.zip")
490
+ end
491
+
492
+ def teardown
493
+ @zipFile.close if @zipFile
494
+ end
495
+
496
+ def test_blocks
497
+ assert_equal(nil, @zipFile.file.stat("file1").blocks)
498
+ end
499
+
500
+ def test_ino
501
+ assert_equal(0, @zipFile.file.stat("file1").ino)
502
+ end
503
+
504
+ def test_uid
505
+ assert_equal(0, @zipFile.file.stat("file1").uid)
506
+ end
507
+
508
+ def test_gid
509
+ assert_equal(0, @zipFile.file.stat("file1").gid)
510
+ end
511
+
512
+ def test_ftype
513
+ assert_equal("file", @zipFile.file.stat("file1").ftype)
514
+ assert_equal("directory", @zipFile.file.stat("dir1").ftype)
515
+ end
516
+
517
+ def test_mode
518
+ assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
519
+ assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
520
+ assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
521
+ assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
522
+ end
523
+
524
+ def test_dev
525
+ assert_equal(0, @zipFile.file.stat("file1").dev)
526
+ end
527
+
528
+ def test_rdev
529
+ assert_equal(0, @zipFile.file.stat("file1").rdev)
530
+ end
531
+
532
+ def test_rdev_major
533
+ assert_equal(0, @zipFile.file.stat("file1").rdev_major)
534
+ end
535
+
536
+ def test_rdev_minor
537
+ assert_equal(0, @zipFile.file.stat("file1").rdev_minor)
538
+ end
539
+
540
+ def test_nlink
541
+ assert_equal(1, @zipFile.file.stat("file1").nlink)
542
+ end
543
+
544
+ def test_blksize
545
+ assert_nil(@zipFile.file.stat("file1").blksize)
546
+ end
547
+
548
+ end
549
+
550
+ class ZipFsFileMutatingTest < Test::Unit::TestCase
551
+ TEST_ZIP = "zipWithDirs_copy.zip"
552
+ def setup
553
+ File.copy("data/zipWithDirs.zip", TEST_ZIP)
554
+ end
555
+
556
+ def teardown
557
+ end
558
+
559
+ def test_delete
560
+ do_test_delete_or_unlink(:delete)
561
+ end
562
+
563
+ def test_unlink
564
+ do_test_delete_or_unlink(:unlink)
565
+ end
566
+
567
+ def test_open_write
568
+ ZipFile.open(TEST_ZIP) {
569
+ |zf|
570
+
571
+ zf.file.open("test_open_write_entry", "w") {
572
+ |f|
573
+ blockCalled = true
574
+ f.write "This is what I'm writing"
575
+ }
576
+ assert_equal("This is what I'm writing",
577
+ zf.file.read("test_open_write_entry"))
578
+
579
+ # Test with existing entry
580
+ zf.file.open("file1", "w") {
581
+ |f|
582
+ blockCalled = true
583
+ f.write "This is what I'm writing too"
584
+ }
585
+ assert_equal("This is what I'm writing too",
586
+ zf.file.read("file1"))
587
+ }
588
+ end
589
+
590
+ def test_rename
591
+ ZipFile.open(TEST_ZIP) {
592
+ |zf|
593
+ assert_raise(Errno::ENOENT, "") {
594
+ zf.file.rename("NoSuchFile", "bimse")
595
+ }
596
+ zf.file.rename("file1", "newNameForFile1")
597
+ }
598
+
599
+ ZipFile.open(TEST_ZIP) {
600
+ |zf|
601
+ assert(! zf.file.exists?("file1"))
602
+ assert(zf.file.exists?("newNameForFile1"))
603
+ }
604
+ end
605
+
606
+ def do_test_delete_or_unlink(symbol)
607
+ ZipFile.open(TEST_ZIP) {
608
+ |zf|
609
+ assert(zf.file.exists?("dir2/dir21/dir221/file2221"))
610
+ zf.file.send(symbol, "dir2/dir21/dir221/file2221")
611
+ assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
612
+
613
+ assert(zf.file.exists?("dir1/file11"))
614
+ assert(zf.file.exists?("dir1/file12"))
615
+ zf.file.send(symbol, "dir1/file11", "dir1/file12")
616
+ assert(! zf.file.exists?("dir1/file11"))
617
+ assert(! zf.file.exists?("dir1/file12"))
618
+
619
+ assert_raise(Errno::ENOENT) { zf.file.send(symbol, "noSuchFile") }
620
+ assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11") }
621
+ assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11/") }
622
+ }
623
+
624
+ ZipFile.open(TEST_ZIP) {
625
+ |zf|
626
+ assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
627
+ assert(! zf.file.exists?("dir1/file11"))
628
+ assert(! zf.file.exists?("dir1/file12"))
629
+
630
+ assert(zf.file.exists?("dir1/dir11"))
631
+ assert(zf.file.exists?("dir1/dir11/"))
632
+ }
633
+ end
634
+
635
+ end
636
+
637
+ class ZipFsDirectoryTest < Test::Unit::TestCase
638
+ TEST_ZIP = "zipWithDirs_copy.zip"
639
+
640
+ def setup
641
+ File.copy("data/zipWithDirs.zip", TEST_ZIP)
642
+ end
643
+
644
+ def test_delete
645
+ ZipFile.open(TEST_ZIP) {
646
+ |zf|
647
+ assert_raise(Errno::ENOENT, "No such file or directory - NoSuchFile.txt") {
648
+ zf.dir.delete("NoSuchFile.txt")
649
+ }
650
+ assert_raise(Errno::EINVAL, "Invalid argument - file1") {
651
+ zf.dir.delete("file1")
652
+ }
653
+ assert(zf.file.exists?("dir1"))
654
+ zf.dir.delete("dir1")
655
+ assert(! zf.file.exists?("dir1"))
656
+ }
657
+ end
658
+
659
+ def test_mkdir
660
+ ZipFile.open(TEST_ZIP) {
661
+ |zf|
662
+ assert_raise(Errno::EEXIST, "File exists - dir1") {
663
+ zf.dir.mkdir("file1")
664
+ }
665
+ assert_raise(Errno::EEXIST, "File exists - dir1") {
666
+ zf.dir.mkdir("dir1")
667
+ }
668
+ assert(!zf.file.exists?("newDir"))
669
+ zf.dir.mkdir("newDir")
670
+ assert(zf.file.directory?("newDir"))
671
+ assert(!zf.file.exists?("newDir2"))
672
+ zf.dir.mkdir("newDir2", 3485)
673
+ assert(zf.file.directory?("newDir2"))
674
+ }
675
+ end
676
+
677
+ def test_pwd_chdir_entries
678
+ ZipFile.open(TEST_ZIP) {
679
+ |zf|
680
+ assert_equal("/", zf.dir.pwd)
681
+
682
+ assert_raise(Errno::ENOENT, "No such file or directory - no such dir") {
683
+ zf.dir.chdir "no such dir"
684
+ }
685
+
686
+ assert_raise(Errno::EINVAL, "Invalid argument - file1") {
687
+ zf.dir.chdir "file1"
688
+ }
689
+
690
+ assert_equal(["dir1", "dir2", "file1"].sort, zf.dir.entries(".").sort)
691
+ zf.dir.chdir "dir1"
692
+ assert_equal("/dir1", zf.dir.pwd)
693
+ assert_equal(["dir11", "file11", "file12"], zf.dir.entries(".").sort)
694
+
695
+ zf.dir.chdir "../dir2/dir21"
696
+ assert_equal("/dir2/dir21", zf.dir.pwd)
697
+ assert_equal(["dir221"].sort, zf.dir.entries(".").sort)
698
+ }
699
+ end
700
+
701
+ def test_foreach
702
+ ZipFile.open(TEST_ZIP) {
703
+ |zf|
704
+
705
+ blockCalled = false
706
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchDir") {
707
+ zf.dir.foreach("noSuchDir") { |e| blockCalled = true }
708
+ }
709
+ assert(! blockCalled)
710
+
711
+ assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
712
+ zf.dir.foreach("file1") { |e| blockCalled = true }
713
+ }
714
+ assert(! blockCalled)
715
+
716
+ entries = []
717
+ zf.dir.foreach(".") { |e| entries << e }
718
+ assert_equal(["dir1", "dir2", "file1"].sort, entries.sort)
719
+
720
+ entries = []
721
+ zf.dir.foreach("dir1") { |e| entries << e }
722
+ assert_equal(["dir11", "file11", "file12"], entries.sort)
723
+ }
724
+ end
725
+
726
+ def test_chroot
727
+ ZipFile.open(TEST_ZIP) {
728
+ |zf|
729
+ assert_raise(NotImplementedError) {
730
+ zf.dir.chroot
731
+ }
732
+ }
733
+ end
734
+
735
+ # Globbing not supported yet
736
+ #def test_glob
737
+ # # test alias []-operator too
738
+ # fail "implement test"
739
+ #end
740
+
741
+ def test_open_new
742
+ ZipFile.open(TEST_ZIP) {
743
+ |zf|
744
+
745
+ assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
746
+ zf.dir.new("file1")
747
+ }
748
+
749
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
750
+ zf.dir.new("noSuchFile")
751
+ }
752
+
753
+ d = zf.dir.new(".")
754
+ assert_equal(["file1", "dir1", "dir2"].sort, d.entries.sort)
755
+ d.close
756
+
757
+ zf.dir.open("dir1") {
758
+ |d|
759
+ assert_equal(["dir11", "file11", "file12"].sort, d.entries.sort)
760
+ }
761
+ }
762
+ end
763
+
764
+ end
765
+
766
+ class ZipFsDirIteratorTest < Test::Unit::TestCase
767
+
768
+ FILENAME_ARRAY = [ "f1", "f2", "f3", "f4", "f5", "f6" ]
769
+
770
+ def setup
771
+ @dirIt = ZipFileSystem::ZipFsDirIterator.new(FILENAME_ARRAY)
772
+ end
773
+
774
+ def test_close
775
+ @dirIt.close
776
+ assert_raise(IOError, "closed directory") {
777
+ @dirIt.each { |e| p e }
778
+ }
779
+ assert_raise(IOError, "closed directory") {
780
+ @dirIt.read
781
+ }
782
+ assert_raise(IOError, "closed directory") {
783
+ @dirIt.rewind
784
+ }
785
+ assert_raise(IOError, "closed directory") {
786
+ @dirIt.seek(0)
787
+ }
788
+ assert_raise(IOError, "closed directory") {
789
+ @dirIt.tell
790
+ }
791
+
792
+ end
793
+
794
+ def test_each
795
+ # Tested through Enumerable.entries
796
+ assert_equal(FILENAME_ARRAY, @dirIt.entries)
797
+ end
798
+
799
+ def test_read
800
+ FILENAME_ARRAY.size.times {
801
+ |i|
802
+ assert_equal(FILENAME_ARRAY[i], @dirIt.read)
803
+ }
804
+ end
805
+
806
+ def test_rewind
807
+ @dirIt.read
808
+ @dirIt.read
809
+ assert_equal(FILENAME_ARRAY[2], @dirIt.read)
810
+ @dirIt.rewind
811
+ assert_equal(FILENAME_ARRAY[0], @dirIt.read)
812
+ end
813
+
814
+ def test_tell_seek
815
+ @dirIt.read
816
+ @dirIt.read
817
+ pos = @dirIt.tell
818
+ valAtPos = @dirIt.read
819
+ @dirIt.read
820
+ @dirIt.seek(pos)
821
+ assert_equal(valAtPos, @dirIt.read)
822
+ end
823
+
824
+ end
825
+
826
+
827
+ # Copyright (C) 2002, 2003 Thomas Sondergaard
828
+ # rubyzip is free software; you can redistribute it and/or
829
+ # modify it under the terms of the ruby license.