zip-container 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,109 @@
1
+ # Copyright (c) 2013 The University of Manchester, UK.
2
+ #
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright notice,
9
+ # this list of conditions and the following disclaimer.
10
+ #
11
+ # * Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # * Neither the names of The University of Manchester nor the names of its
16
+ # contributors may be used to endorse or promote products derived from this
17
+ # software without specific prior written permission.
18
+ #
19
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
+ # POSSIBILITY OF SUCH DAMAGE.
30
+ #
31
+ # Author: Robert Haines
32
+
33
+ require 'test/unit'
34
+ require 'zip-container'
35
+
36
+ class TestRead < Test::Unit::TestCase
37
+
38
+ # Check that the null file does not verify.
39
+ def test_verify_null_file
40
+ assert_raise(Zip::ZipError) do
41
+ ZipContainer::Container.verify!($file_null)
42
+ end
43
+
44
+ refute(ZipContainer::Container.verify($file_null))
45
+ end
46
+
47
+ # Check that the empty container file does verify.
48
+ def test_verify_empty_container
49
+ assert_nothing_raised(ZipContainer::MalformedZipContainerError, Zip::ZipError) do
50
+ ZipContainer::Container.verify!($empty)
51
+ end
52
+
53
+ assert(ZipContainer::Container.verify($empty))
54
+ end
55
+
56
+ # Check that the empty zip file does not verify.
57
+ def test_verify_empty_zip
58
+ assert_raise(ZipContainer::MalformedZipContainerError) do
59
+ ZipContainer::Container.verify!($empty_zip)
60
+ end
61
+
62
+ refute(ZipContainer::Container.verify($empty_zip))
63
+ end
64
+
65
+ # Check that a compressed mimetype file is detected.
66
+ def test_verify_compressed_mimetype
67
+ assert_raise(ZipContainer::MalformedZipContainerError) do
68
+ ZipContainer::Container.verify!($compressed_mimetype)
69
+ end
70
+
71
+ refute(ZipContainer::Container.verify($compressed_mimetype))
72
+ end
73
+
74
+ # Check the raw mimetype bytes
75
+ def test_raw_mimetypes
76
+ empty_container = File.read($empty)
77
+ assert_equal("application/epub+zip", empty_container[38..57])
78
+
79
+ compressed_mimetype = File.read($compressed_mimetype)
80
+ assert_not_equal("application/epub+zip", compressed_mimetype[38..57])
81
+ end
82
+
83
+ # Check reading files out of a container file and make sure we don't change
84
+ # it.
85
+ def test_read_files_from_container
86
+ assert_nothing_raised(ZipContainer::MalformedZipContainerError, Zip::ZipError) do
87
+ ZipContainer::Container.open($example) do |c|
88
+ assert(c.on_disk?)
89
+ refute(c.in_memory?)
90
+
91
+ assert(c.file.exists?("greeting.txt"))
92
+
93
+ greeting = c.file.read("greeting.txt")
94
+ assert_equal("Hello, World!\n", greeting)
95
+
96
+ assert(c.file.exists?("dir"))
97
+ assert(c.file.directory?("dir"))
98
+
99
+ assert(c.file.exists?("dir/code.rb"))
100
+
101
+ assert_equal("This is an example Container file!", c.comment)
102
+
103
+ refute(c.commit_required?)
104
+ refute(c.commit)
105
+ end
106
+ end
107
+ end
108
+
109
+ end
@@ -0,0 +1,334 @@
1
+ # Copyright (c) 2013 The University of Manchester, UK.
2
+ #
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright notice,
9
+ # this list of conditions and the following disclaimer.
10
+ #
11
+ # * Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # * Neither the names of The University of Manchester nor the names of its
16
+ # contributors may be used to endorse or promote products derived from this
17
+ # software without specific prior written permission.
18
+ #
19
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
+ # POSSIBILITY OF SUCH DAMAGE.
30
+ #
31
+ # Author: Robert Haines
32
+
33
+ require 'test/unit'
34
+ require 'zip-container'
35
+
36
+ # A class to test the overriding of reserved and managed names.
37
+ class NewZipContainer < ZipContainer::Container
38
+
39
+ private_class_method :new
40
+
41
+ def initialize(filename)
42
+ super(filename)
43
+ register_managed_entry(ZipContainer::ManagedDirectory.new("src"))
44
+ register_managed_entry(ZipContainer::ManagedDirectory.new("test"))
45
+ register_managed_entry(ZipContainer::ManagedDirectory.new("lib"))
46
+ register_managed_entry(ZipContainer::ManagedFile.new("index.html"))
47
+ register_managed_entry(ZipContainer::ManagedFile.new("greeting.txt"))
48
+
49
+ register_reserved_name("META-INF")
50
+ register_reserved_name("reserved_dir")
51
+ end
52
+
53
+ end
54
+
55
+ class TestReservedNames < Test::Unit::TestCase
56
+
57
+ # Check that the reserved names verify correctly.
58
+ def test_verify_reserved_name
59
+ assert(NewZipContainer.verify($example))
60
+
61
+ assert_nothing_raised(ZipContainer::MalformedZipContainerError) do
62
+ NewZipContainer.verify!($example)
63
+ end
64
+ end
65
+
66
+ # Check the reserved names stuff all works correctly, baring in mind that
67
+ # such comparisons for ZipContainer file should be case sensitive.
68
+ def test_reserved_names
69
+ ZipContainer::Container.open($example) do |c|
70
+ assert_equal(1, c.reserved_names.length)
71
+ assert_equal(["mimetype"], c.reserved_names)
72
+
73
+ assert_equal(0, c.managed_files.length)
74
+ assert_equal([], c.managed_file_names)
75
+ assert(c.reserved_entry?("mimetype"))
76
+ assert(c.reserved_entry?("mimetype/"))
77
+ assert(c.reserved_entry?("MimeType"))
78
+
79
+ assert_equal(0, c.managed_directories.length)
80
+ assert_equal([], c.managed_directory_names)
81
+ refute(c.reserved_entry?("META-INF"))
82
+
83
+ assert_equal(0, c.managed_entries.length)
84
+ assert_equal([], c.managed_entry_names)
85
+ refute(c.managed_entry?("This_should_fail"))
86
+ end
87
+ end
88
+
89
+ # Check that overriding the reserved names in a sub-class works correctly
90
+ def test_subclass_reserved_names
91
+ NewZipContainer.open($example) do |c|
92
+ assert_equal(3, c.reserved_names.length)
93
+ assert_equal(["mimetype", "META-INF", "reserved_dir"],
94
+ c.reserved_names)
95
+
96
+ assert_equal(2, c.managed_files.length)
97
+ assert_equal(["index.html", "greeting.txt"], c.managed_file_names)
98
+ assert(c.reserved_entry?("mimetype"))
99
+ assert(c.reserved_entry?("mimetype/"))
100
+ assert(c.reserved_entry?("MimeType"))
101
+ assert(c.managed_entry?("index.html"))
102
+ assert(c.managed_entry?("Index.HTML"))
103
+ refute(c.reserved_entry?("index.html"))
104
+
105
+ assert_equal(3, c.managed_directories.length)
106
+ assert_equal(["src", "test", "lib"], c.managed_directory_names)
107
+ assert(c.managed_entry?("src"))
108
+ assert(c.managed_entry?("SRC"))
109
+ assert(c.managed_entry?("test"))
110
+ assert(c.managed_entry?("lib"))
111
+ assert(c.managed_entry?("lIb/"))
112
+ refute(c.managed_entry?("META-INF"))
113
+ assert(c.reserved_entry?("META-INF"))
114
+ refute(c.reserved_entry?("src"))
115
+ refute(c.reserved_entry?("test"))
116
+ refute(c.reserved_entry?("lib"))
117
+
118
+ assert_equal(5, c.managed_entries.length)
119
+ assert_equal(["index.html", "greeting.txt", "src", "test", "lib"],
120
+ c.managed_entry_names)
121
+
122
+ refute(c.managed_entry?("This_should_fail"))
123
+ refute(c.reserved_entry?("META_INF"))
124
+ refute(c.reserved_entry?("META_INF/"))
125
+ refute(c.managed_entry?("index.htm"))
126
+ refute(c.reserved_entry?("index.html"))
127
+ end
128
+ end
129
+
130
+ # Check that nothing happens when trying to delete the mimetype file.
131
+ def test_delete_mimetype
132
+ ZipContainer::Container.open($example) do |c|
133
+ assert(c.file.exists?("mimetype"))
134
+ assert_nil(c.remove("mimetype"))
135
+ assert(c.file.exists?("mimetype"))
136
+ end
137
+ end
138
+
139
+ # Check that nothing happens when trying to rename the mimetype file.
140
+ def test_rename_mimetype
141
+ ZipContainer::Container.open($example) do |c|
142
+ assert(c.file.exists?("mimetype"))
143
+ assert_nil(c.rename("mimetype", "something-else"))
144
+ assert(c.file.exists?("mimetype"))
145
+ refute(c.file.exists?("something-else"))
146
+ end
147
+ end
148
+
149
+ # Check that nothing happens when trying to replace the contents of the
150
+ # mimetype file.
151
+ def test_replace_mimetype
152
+ ZipContainer::Container.open($example) do |c|
153
+ assert(c.file.exists?("mimetype"))
154
+ assert_nil(c.replace("mimetype", $zip_empty))
155
+ assert_equal("application/epub+zip", c.file.read("mimetype"))
156
+ end
157
+ end
158
+
159
+ # Check that an exception is raised when trying to add file with a reserved
160
+ # name.
161
+ def test_add_reserved
162
+ ZipContainer::Container.open($empty) do |c|
163
+ assert_raises(ZipContainer::ReservedNameClashError) do
164
+ c.add("mimetype", $zip_empty)
165
+ end
166
+ end
167
+ end
168
+
169
+ # Check that an exception is raised when trying to add file with a reserved
170
+ # name to a subclassed container.
171
+ def test_subclass_add_reserved
172
+ NewZipContainer.open($empty) do |c|
173
+ assert_raises(ZipContainer::ReservedNameClashError) do
174
+ c.add("mimetype", $zip_empty)
175
+ end
176
+
177
+ assert_raises(ZipContainer::ReservedNameClashError) do
178
+ c.add("reserved_dir", $zip_empty)
179
+ end
180
+
181
+ assert_raises(ZipContainer::ReservedNameClashError) do
182
+ c.add("MimeType", $zip_empty)
183
+ end
184
+
185
+ assert_raises(ZipContainer::ReservedNameClashError) do
186
+ c.add("Reserved_Dir", $zip_empty)
187
+ end
188
+ end
189
+ end
190
+
191
+ # Check that the META-INF directory is detected as non-existent when trying
192
+ # to delete it.
193
+ def test_delete_metainf
194
+ ZipContainer::Container.open($example) do |c|
195
+ assert_raises(Errno::ENOENT) do
196
+ c.remove("META-INF")
197
+ end
198
+ end
199
+ end
200
+
201
+ # Check that the META-INF directory is detected as non-existent when trying
202
+ # to rename it.
203
+ def test_rename_metainf
204
+ ZipContainer::Container.open($example) do |c|
205
+ assert_raises(Errno::ENOENT) do
206
+ c.rename("META-INF", "something-else")
207
+ end
208
+ end
209
+ end
210
+
211
+ # Check that an exception is raised when trying to create a directory with a
212
+ # reserved name.
213
+ def test_mkdir_reserved
214
+ ZipContainer::Container.open($empty) do |c|
215
+ assert_raises(ZipContainer::ReservedNameClashError) do
216
+ c.mkdir("mimetype")
217
+ end
218
+ end
219
+ end
220
+
221
+ # Check that an exception is raised when trying to create a directory with a
222
+ # reserved name in a subclassed container.
223
+ def test_subclass_mkdir_reserved
224
+ NewZipContainer.open($empty) do |c|
225
+ assert_raises(ZipContainer::ReservedNameClashError) do
226
+ c.mkdir("mimetype")
227
+ end
228
+
229
+ assert_raises(ZipContainer::ReservedNameClashError) do
230
+ c.mkdir("index.html")
231
+ end
232
+
233
+ assert_raises(ZipContainer::ReservedNameClashError) do
234
+ c.mkdir("reserved_dir")
235
+ end
236
+
237
+ assert_raises(ZipContainer::ReservedNameClashError) do
238
+ c.mkdir("Reserved_Dir")
239
+ end
240
+
241
+ assert_raises(ZipContainer::ReservedNameClashError) do
242
+ c.mkdir("META-INF")
243
+ end
244
+ end
245
+ end
246
+
247
+ # Check that a file cannot be renamed to one of the reserved names.
248
+ def test_rename_to_reserved
249
+ ZipContainer::Container.open($example) do |c|
250
+ assert_raises(ZipContainer::ReservedNameClashError) do
251
+ c.rename("dir/code.rb", "mimetype")
252
+ end
253
+ end
254
+ end
255
+
256
+ # Check that a file cannot be renamed to one of the reserved names in a
257
+ # subclassed container.
258
+ def test_subclass_rename_to_reserved
259
+ NewZipContainer.open($example) do |c|
260
+ assert_raises(ZipContainer::ReservedNameClashError) do
261
+ c.rename("dir/code.rb", "mimetype")
262
+ end
263
+
264
+ assert_raises(ZipContainer::ReservedNameClashError) do
265
+ c.rename("dir", "reserved_dir")
266
+ end
267
+ end
268
+ end
269
+
270
+ # Check that the ruby-like File and Dir classes respect reserved and managed
271
+ # names.
272
+ def test_file_dir_ops_reserved
273
+ ZipContainer::Container.open($empty) do |c|
274
+ assert_raises(ZipContainer::ReservedNameClashError) do
275
+ c.file.open("mimetype", "w") do |f|
276
+ f.puts "TESTING"
277
+ end
278
+ end
279
+
280
+ assert_nothing_raised(ZipContainer::ReservedNameClashError) do
281
+ c.file.open("mimetype") do |f|
282
+ assert_equal("application/epub+zip", f.read)
283
+ end
284
+ end
285
+
286
+ assert_nothing_raised(ZipContainer::ReservedNameClashError) do
287
+ c.file.delete("mimetype")
288
+ assert(c.file.exists?("mimetype"))
289
+ end
290
+ end
291
+ end
292
+
293
+ # Check that the ruby-like File and Dir classes respect reserved names in a
294
+ # subclassed container.
295
+ def test_subclass_file_dir_ops_reserved
296
+ NewZipContainer.open($empty) do |c|
297
+ assert_raises(ZipContainer::ReservedNameClashError) do
298
+ c.file.open("META-INF", "w") do |f|
299
+ f.puts "TESTING"
300
+ end
301
+ end
302
+
303
+ assert_raises(ZipContainer::ReservedNameClashError) do
304
+ c.file.open("TEST", "w") do |f|
305
+ f.puts "TESTING"
306
+ end
307
+ end
308
+
309
+ assert_nothing_raised(ZipContainer::ReservedNameClashError) do
310
+ c.file.open("mimetype") do |f|
311
+ assert_equal("application/epub+zip", f.read)
312
+ end
313
+ end
314
+
315
+ assert_nothing_raised(ZipContainer::ReservedNameClashError) do
316
+ c.file.delete("mimetype")
317
+ assert(c.file.exists?("mimetype"))
318
+ end
319
+
320
+ assert_raises(ZipContainer::ReservedNameClashError) do
321
+ c.dir.mkdir("index.html")
322
+ end
323
+
324
+ assert_raises(ZipContainer::ReservedNameClashError) do
325
+ c.dir.mkdir("reserved_dir")
326
+ end
327
+
328
+ assert_raises(ZipContainer::ReservedNameClashError) do
329
+ c.dir.mkdir("Reserved_Dir")
330
+ end
331
+ end
332
+ end
333
+
334
+ end
@@ -0,0 +1,47 @@
1
+ # Copyright (c) 2013 The University of Manchester, UK.
2
+ #
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright notice,
9
+ # this list of conditions and the following disclaimer.
10
+ #
11
+ # * Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # * Neither the names of The University of Manchester nor the names of its
16
+ # contributors may be used to endorse or promote products derived from this
17
+ # software without specific prior written permission.
18
+ #
19
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
+ # POSSIBILITY OF SUCH DAMAGE.
30
+ #
31
+ # Author: Robert Haines
32
+
33
+ # Example default mimetype
34
+ $mimetype = "application/epub+zip"
35
+
36
+ # Example data files
37
+ $file_null = "test/data/null.file"
38
+ $empty = "test/data/empty.container"
39
+ $empty_zip = "test/data/empty.zip"
40
+ $compressed_mimetype = "test/data/compressed_mimetype.container"
41
+ $example = "test/data/example.container"
42
+
43
+ # Run test cases.
44
+ require 'tc_create'
45
+ require 'tc_read'
46
+ require 'tc_reserved_names'
47
+ require 'tc_managed_entries'