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"
12 EMPTY_TEST_DIR = "data/generated/emptytestdir"
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!
19 def TestFiles.create_test_files(recreate)
21 ! (TEST_FILES.inject(true) { |accum, element| accum && File.exists?(element) }))
23 Dir.mkdir "data/generated" rescue Errno::EEXIST
25 ASCII_TEST_FILES.each_with_index {
27 create_random_ascii(filename, 1E4 * (index+1))
30 BINARY_TEST_FILES.each_with_index {
32 create_random_binary(filename, 1E4 * (index+1))
35 ensure_dir(EMPTY_TEST_DIR)
40 def TestFiles.create_random_ascii(filename, size)
41 File.open(filename, "wb") {
43 while (file.tell < size)
49 def TestFiles.create_random_binary(filename, size)
50 File.open(filename, "wb") {
52 while (file.tell < size)
53 file << [rand].pack("V")
58 def TestFiles.ensure_dir(name)
60 return if File.stat(name).directory?
70 # For representation and creation of
73 attr_accessor :zip_name, :entry_names, :comment
75 def initialize(zip_name, entry_names, comment = "")
77 @entry_names=entry_names
81 def TestZipFile.create_test_zips(recreate)
82 files = Dir.entries("data/generated")
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")
98 File.open("data/generated/empty.txt", "w") {}
99 File.open("data/generated/empty_chmod640.txt", "w") { |f| f.chmod(0640) }
101 File.open("data/generated/short.txt", "w") { |file| file << "ABCDEF" }
103 File.open("data/file2.txt") { |file| ziptestTxt=file.read }
104 File.open("data/generated/longAscii.txt", "w") {
106 while (file.tell < 1E5)
112 File.open("data/generated/empty.zip") { |file| testBinaryPattern=file.read }
113 testBinaryPattern *= 4
115 File.open("data/generated/longBinary.bin", "wb") {
117 while (file.tell < 3E5)
118 file << testBinaryPattern << rand << "\0"
121 raise "failed to create test zip '#{TEST_ZIP2.zip_name}'" unless
122 system("zip #{TEST_ZIP2.zip_name} #{TEST_ZIP2.entry_names.join(' ')}")
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}\"")
129 raise "failed to create test zip '#{TEST_ZIP3.zip_name}'" unless
130 system("zip #{TEST_ZIP3.zip_name} #{TEST_ZIP3.entry_names.join(' ')}")
132 raise "failed to create test zip '#{TEST_ZIP4.zip_name}'" unless
133 system("zip #{TEST_ZIP4.zip_name} #{TEST_ZIP4.entry_names.join(' ')}")
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."
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},
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])
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