zip 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,157 @@
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
+ FileUtils.rm(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("empty_chmod640.txt") &&
90
+ files.index("short.txt") &&
91
+ files.index("longAscii.txt") &&
92
+ files.index("longBinary.bin") ))
93
+ raise "failed to create test zip '#{TEST_ZIP1.zip_name}'" unless
94
+ system("zip #{TEST_ZIP1.zip_name} data/file2.txt")
95
+ raise "failed to remove entry from '#{TEST_ZIP1.zip_name}'" unless
96
+ system("zip #{TEST_ZIP1.zip_name} -d data/file2.txt")
97
+
98
+ File.open("data/generated/empty.txt", "w") {}
99
+ File.open("data/generated/empty_chmod640.txt", "w") { |f| f.chmod(0640) }
100
+
101
+ File.open("data/generated/short.txt", "w") { |file| file << "ABCDEF" }
102
+ ziptestTxt=""
103
+ File.open("data/file2.txt") { |file| ziptestTxt=file.read }
104
+ File.open("data/generated/longAscii.txt", "w") {
105
+ |file|
106
+ while (file.tell < 1E5)
107
+ file << ziptestTxt
108
+ end
109
+ }
110
+
111
+ testBinaryPattern=""
112
+ File.open("data/generated/empty.zip") { |file| testBinaryPattern=file.read }
113
+ testBinaryPattern *= 4
114
+
115
+ File.open("data/generated/longBinary.bin", "wb") {
116
+ |file|
117
+ while (file.tell < 3E5)
118
+ file << testBinaryPattern << rand << "\0"
119
+ end
120
+ }
121
+ raise "failed to create test zip '#{TEST_ZIP2.zip_name}'" unless
122
+ system("zip #{TEST_ZIP2.zip_name} #{TEST_ZIP2.entry_names.join(' ')}")
123
+
124
+ # without bash system interprets everything after echo as parameters to
125
+ # echo including | zip -z ...
126
+ raise "failed to add comment to test zip '#{TEST_ZIP2.zip_name}'" unless
127
+ system("bash -c \"echo #{TEST_ZIP2.comment} | zip -z #{TEST_ZIP2.zip_name}\"")
128
+
129
+ raise "failed to create test zip '#{TEST_ZIP3.zip_name}'" unless
130
+ system("zip #{TEST_ZIP3.zip_name} #{TEST_ZIP3.entry_names.join(' ')}")
131
+
132
+ raise "failed to create test zip '#{TEST_ZIP4.zip_name}'" unless
133
+ system("zip #{TEST_ZIP4.zip_name} #{TEST_ZIP4.entry_names.join(' ')}")
134
+ end
135
+ rescue
136
+ raise $!.to_s +
137
+ "\n\nziptest.rb requires the Info-ZIP program 'zip' in the path\n" +
138
+ "to create test data. If you don't have it you can download\n" +
139
+ "the necessary test files at http://sf.net/projects/rubyzip."
140
+ end
141
+
142
+ TEST_ZIP1 = TestZipFile.new("data/generated/empty.zip", [])
143
+ TEST_ZIP2 = TestZipFile.new("data/generated/5entry.zip", %w{ data/generated/longAscii.txt data/generated/empty.txt data/generated/empty_chmod640.txt data/generated/short.txt data/generated/longBinary.bin},
144
+ "my zip comment")
145
+ TEST_ZIP3 = TestZipFile.new("data/generated/test1.zip", %w{ data/file1.txt })
146
+ TEST_ZIP4 = TestZipFile.new("data/generated/zipWithDir.zip", [ "data/file1.txt",
147
+ TestFiles::EMPTY_TEST_DIR])
148
+ end
149
+
150
+
151
+ END {
152
+ TestFiles::create_test_files(ARGV.index("recreate") != nil ||
153
+ ARGV.index("recreateonly") != nil)
154
+ TestZipFile::create_test_zips(ARGV.index("recreate") != nil ||
155
+ ARGV.index("recreateonly") != nil)
156
+ exit if ARGV.index("recreateonly") != nil
157
+ }
@@ -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,831 @@
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/5entry.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/5entry.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
+ cmd = /mswin/i =~ RUBY_PLATFORM ? 'dir' : 'ls'
460
+
461
+ assert_equal(File.popen(cmd) { |f| f.read },
462
+ @zipFile.file.popen(cmd) { |f| f.read })
463
+ end
464
+
465
+ # Can be added later
466
+ # def test_select
467
+ # fail "implement test"
468
+ # end
469
+
470
+ def test_readlines
471
+ ZipFile.open("data/generated/zipWithDir.zip") {
472
+ |zf|
473
+ assert_equal(File.readlines("data/file1.txt"),
474
+ zf.file.readlines("data/file1.txt"))
475
+ }
476
+ end
477
+
478
+ def test_read
479
+ ZipFile.open("data/generated/zipWithDir.zip") {
480
+ |zf|
481
+ assert_equal(File.read("data/file1.txt"),
482
+ zf.file.read("data/file1.txt"))
483
+ }
484
+ end
485
+
486
+ end
487
+
488
+ class ZipFsFileStatTest < Test::Unit::TestCase
489
+
490
+ def setup
491
+ @zipFile = ZipFile.new("data/zipWithDirs.zip")
492
+ end
493
+
494
+ def teardown
495
+ @zipFile.close if @zipFile
496
+ end
497
+
498
+ def test_blocks
499
+ assert_equal(nil, @zipFile.file.stat("file1").blocks)
500
+ end
501
+
502
+ def test_ino
503
+ assert_equal(0, @zipFile.file.stat("file1").ino)
504
+ end
505
+
506
+ def test_uid
507
+ assert_equal(0, @zipFile.file.stat("file1").uid)
508
+ end
509
+
510
+ def test_gid
511
+ assert_equal(0, @zipFile.file.stat("file1").gid)
512
+ end
513
+
514
+ def test_ftype
515
+ assert_equal("file", @zipFile.file.stat("file1").ftype)
516
+ assert_equal("directory", @zipFile.file.stat("dir1").ftype)
517
+ end
518
+
519
+ def test_mode
520
+ assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
521
+ assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
522
+ assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
523
+ assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
524
+ end
525
+
526
+ def test_dev
527
+ assert_equal(0, @zipFile.file.stat("file1").dev)
528
+ end
529
+
530
+ def test_rdev
531
+ assert_equal(0, @zipFile.file.stat("file1").rdev)
532
+ end
533
+
534
+ def test_rdev_major
535
+ assert_equal(0, @zipFile.file.stat("file1").rdev_major)
536
+ end
537
+
538
+ def test_rdev_minor
539
+ assert_equal(0, @zipFile.file.stat("file1").rdev_minor)
540
+ end
541
+
542
+ def test_nlink
543
+ assert_equal(1, @zipFile.file.stat("file1").nlink)
544
+ end
545
+
546
+ def test_blksize
547
+ assert_nil(@zipFile.file.stat("file1").blksize)
548
+ end
549
+
550
+ end
551
+
552
+ class ZipFsFileMutatingTest < Test::Unit::TestCase
553
+ TEST_ZIP = "zipWithDirs_copy.zip"
554
+ def setup
555
+ FileUtils.cp("data/zipWithDirs.zip", TEST_ZIP)
556
+ end
557
+
558
+ def teardown
559
+ end
560
+
561
+ def test_delete
562
+ do_test_delete_or_unlink(:delete)
563
+ end
564
+
565
+ def test_unlink
566
+ do_test_delete_or_unlink(:unlink)
567
+ end
568
+
569
+ def test_open_write
570
+ ZipFile.open(TEST_ZIP) {
571
+ |zf|
572
+
573
+ zf.file.open("test_open_write_entry", "w") {
574
+ |f|
575
+ blockCalled = true
576
+ f.write "This is what I'm writing"
577
+ }
578
+ assert_equal("This is what I'm writing",
579
+ zf.file.read("test_open_write_entry"))
580
+
581
+ # Test with existing entry
582
+ zf.file.open("file1", "w") {
583
+ |f|
584
+ blockCalled = true
585
+ f.write "This is what I'm writing too"
586
+ }
587
+ assert_equal("This is what I'm writing too",
588
+ zf.file.read("file1"))
589
+ }
590
+ end
591
+
592
+ def test_rename
593
+ ZipFile.open(TEST_ZIP) {
594
+ |zf|
595
+ assert_raise(Errno::ENOENT, "") {
596
+ zf.file.rename("NoSuchFile", "bimse")
597
+ }
598
+ zf.file.rename("file1", "newNameForFile1")
599
+ }
600
+
601
+ ZipFile.open(TEST_ZIP) {
602
+ |zf|
603
+ assert(! zf.file.exists?("file1"))
604
+ assert(zf.file.exists?("newNameForFile1"))
605
+ }
606
+ end
607
+
608
+ def do_test_delete_or_unlink(symbol)
609
+ ZipFile.open(TEST_ZIP) {
610
+ |zf|
611
+ assert(zf.file.exists?("dir2/dir21/dir221/file2221"))
612
+ zf.file.send(symbol, "dir2/dir21/dir221/file2221")
613
+ assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
614
+
615
+ assert(zf.file.exists?("dir1/file11"))
616
+ assert(zf.file.exists?("dir1/file12"))
617
+ zf.file.send(symbol, "dir1/file11", "dir1/file12")
618
+ assert(! zf.file.exists?("dir1/file11"))
619
+ assert(! zf.file.exists?("dir1/file12"))
620
+
621
+ assert_raise(Errno::ENOENT) { zf.file.send(symbol, "noSuchFile") }
622
+ assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11") }
623
+ assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11/") }
624
+ }
625
+
626
+ ZipFile.open(TEST_ZIP) {
627
+ |zf|
628
+ assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
629
+ assert(! zf.file.exists?("dir1/file11"))
630
+ assert(! zf.file.exists?("dir1/file12"))
631
+
632
+ assert(zf.file.exists?("dir1/dir11"))
633
+ assert(zf.file.exists?("dir1/dir11/"))
634
+ }
635
+ end
636
+
637
+ end
638
+
639
+ class ZipFsDirectoryTest < Test::Unit::TestCase
640
+ TEST_ZIP = "zipWithDirs_copy.zip"
641
+
642
+ def setup
643
+ FileUtils.cp("data/zipWithDirs.zip", TEST_ZIP)
644
+ end
645
+
646
+ def test_delete
647
+ ZipFile.open(TEST_ZIP) {
648
+ |zf|
649
+ assert_raise(Errno::ENOENT, "No such file or directory - NoSuchFile.txt") {
650
+ zf.dir.delete("NoSuchFile.txt")
651
+ }
652
+ assert_raise(Errno::EINVAL, "Invalid argument - file1") {
653
+ zf.dir.delete("file1")
654
+ }
655
+ assert(zf.file.exists?("dir1"))
656
+ zf.dir.delete("dir1")
657
+ assert(! zf.file.exists?("dir1"))
658
+ }
659
+ end
660
+
661
+ def test_mkdir
662
+ ZipFile.open(TEST_ZIP) {
663
+ |zf|
664
+ assert_raise(Errno::EEXIST, "File exists - dir1") {
665
+ zf.dir.mkdir("file1")
666
+ }
667
+ assert_raise(Errno::EEXIST, "File exists - dir1") {
668
+ zf.dir.mkdir("dir1")
669
+ }
670
+ assert(!zf.file.exists?("newDir"))
671
+ zf.dir.mkdir("newDir")
672
+ assert(zf.file.directory?("newDir"))
673
+ assert(!zf.file.exists?("newDir2"))
674
+ zf.dir.mkdir("newDir2", 3485)
675
+ assert(zf.file.directory?("newDir2"))
676
+ }
677
+ end
678
+
679
+ def test_pwd_chdir_entries
680
+ ZipFile.open(TEST_ZIP) {
681
+ |zf|
682
+ assert_equal("/", zf.dir.pwd)
683
+
684
+ assert_raise(Errno::ENOENT, "No such file or directory - no such dir") {
685
+ zf.dir.chdir "no such dir"
686
+ }
687
+
688
+ assert_raise(Errno::EINVAL, "Invalid argument - file1") {
689
+ zf.dir.chdir "file1"
690
+ }
691
+
692
+ assert_equal(["dir1", "dir2", "file1"].sort, zf.dir.entries(".").sort)
693
+ zf.dir.chdir "dir1"
694
+ assert_equal("/dir1", zf.dir.pwd)
695
+ assert_equal(["dir11", "file11", "file12"], zf.dir.entries(".").sort)
696
+
697
+ zf.dir.chdir "../dir2/dir21"
698
+ assert_equal("/dir2/dir21", zf.dir.pwd)
699
+ assert_equal(["dir221"].sort, zf.dir.entries(".").sort)
700
+ }
701
+ end
702
+
703
+ def test_foreach
704
+ ZipFile.open(TEST_ZIP) {
705
+ |zf|
706
+
707
+ blockCalled = false
708
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchDir") {
709
+ zf.dir.foreach("noSuchDir") { |e| blockCalled = true }
710
+ }
711
+ assert(! blockCalled)
712
+
713
+ assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
714
+ zf.dir.foreach("file1") { |e| blockCalled = true }
715
+ }
716
+ assert(! blockCalled)
717
+
718
+ entries = []
719
+ zf.dir.foreach(".") { |e| entries << e }
720
+ assert_equal(["dir1", "dir2", "file1"].sort, entries.sort)
721
+
722
+ entries = []
723
+ zf.dir.foreach("dir1") { |e| entries << e }
724
+ assert_equal(["dir11", "file11", "file12"], entries.sort)
725
+ }
726
+ end
727
+
728
+ def test_chroot
729
+ ZipFile.open(TEST_ZIP) {
730
+ |zf|
731
+ assert_raise(NotImplementedError) {
732
+ zf.dir.chroot
733
+ }
734
+ }
735
+ end
736
+
737
+ # Globbing not supported yet
738
+ #def test_glob
739
+ # # test alias []-operator too
740
+ # fail "implement test"
741
+ #end
742
+
743
+ def test_open_new
744
+ ZipFile.open(TEST_ZIP) {
745
+ |zf|
746
+
747
+ assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
748
+ zf.dir.new("file1")
749
+ }
750
+
751
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
752
+ zf.dir.new("noSuchFile")
753
+ }
754
+
755
+ d = zf.dir.new(".")
756
+ assert_equal(["file1", "dir1", "dir2"].sort, d.entries.sort)
757
+ d.close
758
+
759
+ zf.dir.open("dir1") {
760
+ |d|
761
+ assert_equal(["dir11", "file11", "file12"].sort, d.entries.sort)
762
+ }
763
+ }
764
+ end
765
+
766
+ end
767
+
768
+ class ZipFsDirIteratorTest < Test::Unit::TestCase
769
+
770
+ FILENAME_ARRAY = [ "f1", "f2", "f3", "f4", "f5", "f6" ]
771
+
772
+ def setup
773
+ @dirIt = ZipFileSystem::ZipFsDirIterator.new(FILENAME_ARRAY)
774
+ end
775
+
776
+ def test_close
777
+ @dirIt.close
778
+ assert_raise(IOError, "closed directory") {
779
+ @dirIt.each { |e| p e }
780
+ }
781
+ assert_raise(IOError, "closed directory") {
782
+ @dirIt.read
783
+ }
784
+ assert_raise(IOError, "closed directory") {
785
+ @dirIt.rewind
786
+ }
787
+ assert_raise(IOError, "closed directory") {
788
+ @dirIt.seek(0)
789
+ }
790
+ assert_raise(IOError, "closed directory") {
791
+ @dirIt.tell
792
+ }
793
+
794
+ end
795
+
796
+ def test_each
797
+ # Tested through Enumerable.entries
798
+ assert_equal(FILENAME_ARRAY, @dirIt.entries)
799
+ end
800
+
801
+ def test_read
802
+ FILENAME_ARRAY.size.times {
803
+ |i|
804
+ assert_equal(FILENAME_ARRAY[i], @dirIt.read)
805
+ }
806
+ end
807
+
808
+ def test_rewind
809
+ @dirIt.read
810
+ @dirIt.read
811
+ assert_equal(FILENAME_ARRAY[2], @dirIt.read)
812
+ @dirIt.rewind
813
+ assert_equal(FILENAME_ARRAY[0], @dirIt.read)
814
+ end
815
+
816
+ def test_tell_seek
817
+ @dirIt.read
818
+ @dirIt.read
819
+ pos = @dirIt.tell
820
+ valAtPos = @dirIt.read
821
+ @dirIt.read
822
+ @dirIt.seek(pos)
823
+ assert_equal(valAtPos, @dirIt.read)
824
+ end
825
+
826
+ end
827
+
828
+
829
+ # Copyright (C) 2002, 2003 Thomas Sondergaard
830
+ # rubyzip is free software; you can redistribute it and/or
831
+ # modify it under the terms of the ruby license.