Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 316 lines (240 sloc) 12.308 kB
72f60ce First commit.
Evan Miller authored
1 #!/usr/bin/perl
2
911b447 Update from Google Code SVN
Evan Miller authored
3 # TODO tests for Zip64
4
4bffd6b @anthonyryan1 Use carton to install modules form cpan
anthonyryan1 authored
5 use Test::More tests => 93;
72f60ce First commit.
Evan Miller authored
6 use LWP::UserAgent;
7 use Archive::Zip;
8
9 $temp_zip_path = "/tmp/mod_zip.zip";
10 $http_root = "http://localhost:8081";
11
911b447 Update from Google Code SVN
Evan Miller authored
12 sub set_debug_log($) {
13 my $label = shift;
14 $/ = "\n";
15 open( NEWCONF, ">", "nginx/conf/nginx.conf" );
16
17 open( CONF, "<", "nginx.conf" );
18 while(my $line = <CONF>) {
19 if ($line eq "error_log logs/error.log debug;\n") {
20 print NEWCONF "error_log logs/error-$label.log debug;\n";
21 } else {
22 print NEWCONF $line;
23 }
24 }
25 close( CONF );
26
27 close( NEWCONF );
28
29 my $pid = `cat nginx/logs/nginx.pid`;
30 if ($?) {
31 print "Starting nginx...\n";
32 `nginx/sbin/nginx`;
33 }
34 `/bin/kill -HUP $pid`;
35 sleep 1;
36 }
37
38 sub read_file($) {
72f60ce First commit.
Evan Miller authored
39 $file = shift;
40 undef $/;
41
42 open( FILE, "<", $file );
43 $contents = <FILE>;
44 close( FILE );
45
46 return $contents;
47 }
48
911b447 Update from Google Code SVN
Evan Miller authored
49 sub write_temp_zip($) {
72f60ce First commit.
Evan Miller authored
50 my $content = shift;
51
52 open TEMPFILE, ">", $temp_zip_path;
53 print TEMPFILE $content;
54 close TEMPFILE;
55
56 return Archive::Zip->new($temp_zip_path);
57 }
58
911b447 Update from Google Code SVN
Evan Miller authored
59 sub test_zip_archive($$) {
72f60ce First commit.
Evan Miller authored
60 my ($content, $condition) = @_;
61
62 my $zip = write_temp_zip($content);
63
64 my $zip_file1_content = $zip->contents( 'file1.txt' );
65 my $zip_file2_content = $zip->contents( 'file2.txt' );
66
67 my $file1_content = read_file("nginx/html/file1.txt");
68 my $file2_content = read_file("nginx/html/file2.txt");
69
70 is($zip_file1_content, $file1_content, "file1.txt in the ZIP $condition");
71 is($zip_file2_content, $file2_content, "file2.txt in the ZIP $condition");
72
73 return $zip;
74 }
75
76 $ua = LWP::UserAgent->new;
77
78 $zip_length = 303;
79
80 $file1_content = read_file("nginx/html/file1.txt");
81 $file2_content = read_file("nginx/html/file2.txt");
82
83 $file1_offset = 51;
84 $file2_offset = 127;
85
86
87 ########## Dowload component files
88
911b447 Update from Google Code SVN
Evan Miller authored
89 set_debug_log("component");
90
72f60ce First commit.
Evan Miller authored
91 $response = $ua->get("$http_root/file1.txt");
92
93 is( $response->content, $file1_content, "download file1.txt" );
94 is( length($file1_content), 24, "file1.txt file size" );
95
96 $response = $ua->get("$http_root/file1.txt", "Range" => "bytes=0-3");
97 is( $response->content, "This", "download file1.txt partial" );
98
99 $response = $ua->get("$http_root/file2.txt");
100 is( $response->content, $file2_content, "download file2.txt" );
101 is( length($file2_content), 25, "file2.txt file size" );
102
103 ########## Download ZIP files
104
911b447 Update from Google Code SVN
Evan Miller authored
105 set_debug_log("basic-zip");
106
72f60ce First commit.
Evan Miller authored
107 $response = $ua->get("$http_root/zip-missing-crc.txt");
108 is($response->code, 200, "Returns OK with missing CRC");
109 like($response->header("Content-Length"), qr/^\d+$/, "Content-Length header when missing CRC");
911b447 Update from Google Code SVN
Evan Miller authored
110 is($response->header("Accept-Ranges"), undef, "No Accept-Ranges header when missing CRC (fails with nginx 0.7.44 - 0.8.6)");
72f60ce First commit.
Evan Miller authored
111
112 $zip = test_zip_archive($response->content, "when missing CRC");
113 is($zip->memberNamed("file1.txt")->hasDataDescriptor(), 8, "Has data descriptor when missing CRC");
114 is($zip->memberNamed("file1.txt")->crc32String(), "1a6349c5", "Generated file1.txt CRC is correct");
115 is($zip->memberNamed("file2.txt")->crc32String(), "5d70c4d3", "Generated file2.txt CRC is correct");
116
4bffd6b @anthonyryan1 Use carton to install modules form cpan
anthonyryan1 authored
117 $response = $ua->get("$http_root/zip-uppercase-crc.txt");
118 is($response->code, 200, "Returns OK with uppercase CRC");
119 $zip = test_zip_archive($response->content, "with uppercase CRC");
120 is($zip->memberNamed("file1.txt")->crc32String(), "1a6349c5", "file1.txt CRC is correct");
121 is($zip->memberNamed("file2.txt")->crc32String(), "5d70c4d3", "file2.txt CRC is correct");
122
72f60ce First commit.
Evan Miller authored
123 $response = $ua->get("$http_root/zip-404.txt");
124 is($response->code, 500, "Server error with bad file");
125
126 $response = $ua->get("$http_root/zip-many-files.txt");
127 is($response->code, 200, "Returns OK with many files");
128
129 $zip = test_zip_archive($response->content, "with many files");
130 is($zip->numberOfMembers(), 136, "Correct number in many-file ZIP");
131
132 $response = $ua->get("$http_root/zip-local-files.txt");
133 is($response->code, 200, "Returns OK with local files");
134
135 $zip = test_zip_archive($response->content, "with local files");
136 is($zip->numberOfMembers(), 2, "Correct number in local-file ZIP");
4bffd6b @anthonyryan1 Use carton to install modules form cpan
anthonyryan1 authored
137 is($zip->memberNamed("file1.txt")->crc32String(), "1a6349c5", "file1.txt CRC is correct (local)");
138 is($zip->memberNamed("file2.txt")->crc32String(), "5d70c4d3", "file2.txt CRC is correct (local)");
72f60ce First commit.
Evan Miller authored
139
911b447 Update from Google Code SVN
Evan Miller authored
140 $response = $ua->get("$http_root/zip-spaces.txt");
141 is($response->code, 200, "Returns OK with spaces in URLs");
142
143 $zip = test_zip_archive($response->content, "with spaces in URLs");
144 is($zip->numberOfMembers(), 2, "Correct number in spaced-out ZIP");
145
3c69ff5 @dup2 Fixed an issue with "+" in the filename
dup2 authored
146 $response = $ua->get("$http_root/zip-spaces-plus.txt");
147 is($response->code, 200, "Returns OK with spaces and plus in URLs");
148
149 $zip = test_zip_archive($response->content, "with spaces and plus in the URLs");
150 is($zip->numberOfMembers(), 2, "Correct number in spaces and plus ZIP");
151
152
72f60ce First commit.
Evan Miller authored
153 open LARGEFILE, ">", "nginx/html/largefile.txt";
154 for (0..99999) {
155 print LARGEFILE "X" x 99;
156 print LARGEFILE "\n";
157 }
158 close LARGEFILE;
159
911b447 Update from Google Code SVN
Evan Miller authored
160 set_debug_log("zip-large");
161
72f60ce First commit.
Evan Miller authored
162 $response = $ua->get("$http_root/zip-large-file.txt");
163 is($response->code, 200, "Returns OK with large file");
164
165 $zip = write_temp_zip($response->content);
166 $large_zip_file_content = $zip->contents( 'largefile.txt' );
167 $large_file_content = read_file("nginx/html/largefile.txt");
168 is(length($large_zip_file_content), length($large_file_content), "Found large file in ZIP");
169
170 unlink "nginx/html/largefile.txt";
171
911b447 Update from Google Code SVN
Evan Miller authored
172 set_debug_log("zip-headers");
173
72f60ce First commit.
Evan Miller authored
174 $response = $ua->get("$http_root/zip.txt");
175 is( $response->header( "X-Archive-Files" ), "zip",
176 "X-Archive-Files header" );
177 is( $response->header( "Content-Type" ), "application/zip",
178 "Content-Type header" );
179 is( $response->header( "Cache-Control" ), "max-age=0",
180 "Cache-Control header" );
181 is( $response->header( "Accept-Ranges" ), "bytes",
182 "Accept-Ranges header" );
183 is( $response->header( "Content-Length" ), $zip_length,
184 "Content-Length header" );
911b447 Update from Google Code SVN
Evan Miller authored
185 is( $response->header( "Last-Modified" ), "Wed, 15 Nov 1995 04:58:08 GMT",
186 "Last-Modified header" );
72f60ce First commit.
Evan Miller authored
187
911b447 Update from Google Code SVN
Evan Miller authored
188 $zip = test_zip_archive($response->content, "with CRC");
72f60ce First commit.
Evan Miller authored
189 is($zip->memberNamed("file1.txt")->hasDataDescriptor(), 0, "No data descriptor when CRC supplied");
190
191 ########### Byte-range
192
911b447 Update from Google Code SVN
Evan Miller authored
193 set_debug_log("range");
194
72f60ce First commit.
Evan Miller authored
195 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=10000-10001");
196 is($response->code, 416, "Request range not satisfiable");
197
198 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=0-1");
199 is($response->code, 206, "206 Partial Content");
200 is(length($response->content), 2, "Length of partial content");
201 is($response->content, "PK", "Subrange at beginning");
202
203 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=30-38");
204 is($response->code, 206, "206 Partial Content");
205 is(length($response->content), 9, "Length of partial content");
206 is($response->content, "file1.txt", "Subrange in a file header");
207
208 $response = $ua->get("$http_root/zip.txt",
209 "Range" => "bytes=".($file2_offset+1)."-152");
210 is($response->code, 206, "206 Partial Content");
211 is(length($response->content), 25, "Length of partial content");
212 is($response->content, "This is the second file.\n", "Subrange of included file");
213
214 $response = $ua->get("$http_root/zip.txt",
215 "Range" => "bytes=".($file2_offset+1)."-".($file2_offset+4));
216 is($response->code, 206, "206 Partial Content");
217 is(length($response->content), 4, "Length of partial content");
218 is($response->content, "This", "Subrange is a subrange of included file");
219
220 $response = $ua->get("$http_root/zip.txt",
221 "Range" => "bytes=".($file2_offset+3)."-".($file2_offset+4));
222 is($response->code, 206, "206 Partial Content");
223 is(length($response->content), 2, "Length of partial content");
224 is($response->content, "is", "Subrange is a subrange of included file");
225
226 # Subrange including all of first file and part of second.
227 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=0-".($file2_offset+5));
228 open TMPFILE, ">", "/tmp/partial.zip";
229 print TMPFILE $response->content;
230 close TMPFILE;
231 is($response->code, 206, "206 Partial Content");
232 is(length($response->content), $file2_offset+1+5, "Length of partial content");
233 is(substr($response->content, $file2_offset+1, 4), "This", "Subrange spanning multiple files");
234
235 # Subrange including part of first and second files.
236 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=".($file1_offset+9)."-".($file2_offset+4));
237 open TMPFILE, ">", "/tmp/partial.zip";
238 print TMPFILE $response->content;
239 close TMPFILE;
240 is($response->code, 206, "206 Partial Content");
241 is(length($response->content), ($file2_offset+4)-($file1_offset+9)+1, "Length of partial content");
242 is(substr($response->content, 0, 14), "the first file", "Subrange spanning part of first file");
243 is(substr($response->content, 68, 4), "This", "Subrange spanning part of second file");
244
245 $response = $ua->get("$http_root/zip-local-files.txt", "Range" => "bytes=".($file1_offset+9)."-".($file2_offset+4));
246 open TMPFILE, ">", "/tmp/partial.zip";
247 print TMPFILE $response->content;
248 close TMPFILE;
249 is($response->code, 206, "206 Partial Content");
250 is(length($response->content), ($file2_offset+4)-($file1_offset+9)+1, "Length of partial content");
251 is(substr($response->content, 0, 14), "the first file", "Subrange spanning part of first file");
252 is(substr($response->content, 68, 4), "This", "Subrange spanning part of second file");
253
254 ########### Prefix & suffix
255
256 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=".($file2_offset+1)."-");
257 is($response->code, 206, "206 Partial Content");
258 is(length($response->content), $zip_length - ($file2_offset+1), "Length of partial content (prefix)");
259 is(substr($response->content, 0, 25), "This is the second file.\n", "Subrange with prefix notation");
260
261 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=-".($zip_length-($file2_offset+1)));
262 is($response->code, 206, "206 Partial Content");
263 is(length($response->content), $zip_length - ($file2_offset+1), "Length of partial content (suffix)");
264 is(substr($response->content, 0, 25), "This is the second file.\n", "Subrange with suffix notation");
265
266 ########### Multiple byte-ranges
267
268 $response = $ua->get("$http_root/zip.txt",
269 "Range" => "bytes=0-1,".($file2_offset+1)."-".($file2_offset+4));
270 is($response->code, 206, "206 Partial Content");
271 like($response->header("Content-Type"), qr(multipart/byteranges), "Content-Type: multipart/byteranges");
272 ($boundary) = $response->header("Content-Type") =~ /multipart\/byteranges; boundary=(\w+)/;
273 like($response->content,
274 qr(--$boundary\r\nContent-Type: application/zip\r\nContent-Range: bytes 0-1/$zip_length\r\n\r\nPK\r\n),
275 "First chunk");
276 like($response->content,
277 qr(--$boundary\r\nContent-Type: application/zip\r\nContent-Range: bytes 128-131/$zip_length\r\n\r\nThis\r\n),
278 "Second chunk");
279
280 $response = $ua->get("$http_root/zip.txt",
281 "Range" => "bytes=".($file2_offset+1)."-".($file2_offset+4).",0-1");
282 is($response->code, 206, "206 Partial Content");
283 like($response->header("Content-Type"), qr(multipart/byteranges), "Content-Type: multipart/byteranges");
284 ($boundary) = $response->header("Content-Type") =~ /multipart\/byteranges; boundary=(\w+)/;
285 like($response->content,
286 qr(--$boundary\r\nContent-Type: application/zip\r\nContent-Range: bytes 128-131/$zip_length\r\n\r\nThis\r\n),
287 "First chunk");
288 like($response->content,
289 qr(--$boundary\r\nContent-Type: application/zip\r\nContent-Range: bytes 0-1/$zip_length\r\n\r\nPK\r\n),
290 "Second chunk");
291
911b447 Update from Google Code SVN
Evan Miller authored
292 ### If-Range ###
293 set_debug_log("if-range");
294
295 # last-modified
296 $response = $ua->get("$http_root/zip.txt",
297 "If-Range" => "Wed, 15 Nov 1995 04:58:07 GMT",
298 "Range" => "bytes=0-1");
299 is($response->code, 200, "200 OK -- when If-Range is not Last-Modified time");
300
301 $response = $ua->get("$http_root/zip.txt",
302 "If-Range" => "Wed, 15 Nov 1995 04:58:08 GMT",
303 "Range" => "bytes=0-1");
304 is($response->code, 206, "206 Partial Content -- when If-Range is Last-Modified time");
305
306 # etag
307 $response = $ua->get("$http_root/zip.txt",
308 "If-Range" => "2.71828",
309 "Range" => "bytes=0-1");
310 is($response->code, 200, "200 OK -- when If-Range is not ETag");
311
312 $response = $ua->get("$http_root/zip.txt",
313 "If-Range" => "3.14159",
314 "Range" => "bytes=0-1");
315 is($response->code, 206, "206 Partial Content -- when If-Range is ETag (requires nginx 0.8.10+ or nginx-0.8.9-etag.patch)");
Something went wrong with that request. Please try again.