Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 303 lines (231 sloc) 11.679 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
5 use Test::More tests => 84;
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
117 $response = $ua->get("$http_root/zip-404.txt");
118 is($response->code, 500, "Server error with bad file");
119
120 $response = $ua->get("$http_root/zip-many-files.txt");
121 is($response->code, 200, "Returns OK with many files");
122
123 $zip = test_zip_archive($response->content, "with many files");
124 is($zip->numberOfMembers(), 136, "Correct number in many-file ZIP");
125
126 $response = $ua->get("$http_root/zip-local-files.txt");
127 is($response->code, 200, "Returns OK with local files");
128
129 $zip = test_zip_archive($response->content, "with local files");
130 is($zip->numberOfMembers(), 2, "Correct number in local-file ZIP");
131 is($zip->memberNamed("file1.txt")->crc32String(), "1a6349c5", "Generated file1.txt CRC is correct (local)");
132 is($zip->memberNamed("file2.txt")->crc32String(), "5d70c4d3", "Generated file2.txt CRC is correct (local)");
133
911b447 Update from Google Code SVN
Evan Miller authored
134 $response = $ua->get("$http_root/zip-spaces.txt");
135 is($response->code, 200, "Returns OK with spaces in URLs");
136
137 $zip = test_zip_archive($response->content, "with spaces in URLs");
138 is($zip->numberOfMembers(), 2, "Correct number in spaced-out ZIP");
139
72f60ce First commit.
Evan Miller authored
140 open LARGEFILE, ">", "nginx/html/largefile.txt";
141 for (0..99999) {
142 print LARGEFILE "X" x 99;
143 print LARGEFILE "\n";
144 }
145 close LARGEFILE;
146
911b447 Update from Google Code SVN
Evan Miller authored
147 set_debug_log("zip-large");
148
72f60ce First commit.
Evan Miller authored
149 $response = $ua->get("$http_root/zip-large-file.txt");
150 is($response->code, 200, "Returns OK with large file");
151
152 $zip = write_temp_zip($response->content);
153 $large_zip_file_content = $zip->contents( 'largefile.txt' );
154 $large_file_content = read_file("nginx/html/largefile.txt");
155 is(length($large_zip_file_content), length($large_file_content), "Found large file in ZIP");
156
157 unlink "nginx/html/largefile.txt";
158
911b447 Update from Google Code SVN
Evan Miller authored
159 set_debug_log("zip-headers");
160
72f60ce First commit.
Evan Miller authored
161 $response = $ua->get("$http_root/zip.txt");
162 is( $response->header( "X-Archive-Files" ), "zip",
163 "X-Archive-Files header" );
164 is( $response->header( "Content-Type" ), "application/zip",
165 "Content-Type header" );
166 is( $response->header( "Cache-Control" ), "max-age=0",
167 "Cache-Control header" );
168 is( $response->header( "Accept-Ranges" ), "bytes",
169 "Accept-Ranges header" );
170 is( $response->header( "Content-Length" ), $zip_length,
171 "Content-Length header" );
911b447 Update from Google Code SVN
Evan Miller authored
172 is( $response->header( "Last-Modified" ), "Wed, 15 Nov 1995 04:58:08 GMT",
173 "Last-Modified header" );
72f60ce First commit.
Evan Miller authored
174
911b447 Update from Google Code SVN
Evan Miller authored
175 $zip = test_zip_archive($response->content, "with CRC");
72f60ce First commit.
Evan Miller authored
176 is($zip->memberNamed("file1.txt")->hasDataDescriptor(), 0, "No data descriptor when CRC supplied");
177
178 ########### Byte-range
179
911b447 Update from Google Code SVN
Evan Miller authored
180 set_debug_log("range");
181
72f60ce First commit.
Evan Miller authored
182 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=10000-10001");
183 is($response->code, 416, "Request range not satisfiable");
184
185 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=0-1");
186 is($response->code, 206, "206 Partial Content");
187 is(length($response->content), 2, "Length of partial content");
188 is($response->content, "PK", "Subrange at beginning");
189
190 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=30-38");
191 is($response->code, 206, "206 Partial Content");
192 is(length($response->content), 9, "Length of partial content");
193 is($response->content, "file1.txt", "Subrange in a file header");
194
195 $response = $ua->get("$http_root/zip.txt",
196 "Range" => "bytes=".($file2_offset+1)."-152");
197 is($response->code, 206, "206 Partial Content");
198 is(length($response->content), 25, "Length of partial content");
199 is($response->content, "This is the second file.\n", "Subrange of included file");
200
201 $response = $ua->get("$http_root/zip.txt",
202 "Range" => "bytes=".($file2_offset+1)."-".($file2_offset+4));
203 is($response->code, 206, "206 Partial Content");
204 is(length($response->content), 4, "Length of partial content");
205 is($response->content, "This", "Subrange is a subrange of included file");
206
207 $response = $ua->get("$http_root/zip.txt",
208 "Range" => "bytes=".($file2_offset+3)."-".($file2_offset+4));
209 is($response->code, 206, "206 Partial Content");
210 is(length($response->content), 2, "Length of partial content");
211 is($response->content, "is", "Subrange is a subrange of included file");
212
213 # Subrange including all of first file and part of second.
214 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=0-".($file2_offset+5));
215 open TMPFILE, ">", "/tmp/partial.zip";
216 print TMPFILE $response->content;
217 close TMPFILE;
218 is($response->code, 206, "206 Partial Content");
219 is(length($response->content), $file2_offset+1+5, "Length of partial content");
220 is(substr($response->content, $file2_offset+1, 4), "This", "Subrange spanning multiple files");
221
222 # Subrange including part of first and second files.
223 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=".($file1_offset+9)."-".($file2_offset+4));
224 open TMPFILE, ">", "/tmp/partial.zip";
225 print TMPFILE $response->content;
226 close TMPFILE;
227 is($response->code, 206, "206 Partial Content");
228 is(length($response->content), ($file2_offset+4)-($file1_offset+9)+1, "Length of partial content");
229 is(substr($response->content, 0, 14), "the first file", "Subrange spanning part of first file");
230 is(substr($response->content, 68, 4), "This", "Subrange spanning part of second file");
231
232 $response = $ua->get("$http_root/zip-local-files.txt", "Range" => "bytes=".($file1_offset+9)."-".($file2_offset+4));
233 open TMPFILE, ">", "/tmp/partial.zip";
234 print TMPFILE $response->content;
235 close TMPFILE;
236 is($response->code, 206, "206 Partial Content");
237 is(length($response->content), ($file2_offset+4)-($file1_offset+9)+1, "Length of partial content");
238 is(substr($response->content, 0, 14), "the first file", "Subrange spanning part of first file");
239 is(substr($response->content, 68, 4), "This", "Subrange spanning part of second file");
240
241 ########### Prefix & suffix
242
243 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=".($file2_offset+1)."-");
244 is($response->code, 206, "206 Partial Content");
245 is(length($response->content), $zip_length - ($file2_offset+1), "Length of partial content (prefix)");
246 is(substr($response->content, 0, 25), "This is the second file.\n", "Subrange with prefix notation");
247
248 $response = $ua->get("$http_root/zip.txt", "Range" => "bytes=-".($zip_length-($file2_offset+1)));
249 is($response->code, 206, "206 Partial Content");
250 is(length($response->content), $zip_length - ($file2_offset+1), "Length of partial content (suffix)");
251 is(substr($response->content, 0, 25), "This is the second file.\n", "Subrange with suffix notation");
252
253 ########### Multiple byte-ranges
254
255 $response = $ua->get("$http_root/zip.txt",
256 "Range" => "bytes=0-1,".($file2_offset+1)."-".($file2_offset+4));
257 is($response->code, 206, "206 Partial Content");
258 like($response->header("Content-Type"), qr(multipart/byteranges), "Content-Type: multipart/byteranges");
259 ($boundary) = $response->header("Content-Type") =~ /multipart\/byteranges; boundary=(\w+)/;
260 like($response->content,
261 qr(--$boundary\r\nContent-Type: application/zip\r\nContent-Range: bytes 0-1/$zip_length\r\n\r\nPK\r\n),
262 "First chunk");
263 like($response->content,
264 qr(--$boundary\r\nContent-Type: application/zip\r\nContent-Range: bytes 128-131/$zip_length\r\n\r\nThis\r\n),
265 "Second chunk");
266
267 $response = $ua->get("$http_root/zip.txt",
268 "Range" => "bytes=".($file2_offset+1)."-".($file2_offset+4).",0-1");
269 is($response->code, 206, "206 Partial Content");
270 like($response->header("Content-Type"), qr(multipart/byteranges), "Content-Type: multipart/byteranges");
271 ($boundary) = $response->header("Content-Type") =~ /multipart\/byteranges; boundary=(\w+)/;
272 like($response->content,
273 qr(--$boundary\r\nContent-Type: application/zip\r\nContent-Range: bytes 128-131/$zip_length\r\n\r\nThis\r\n),
274 "First chunk");
275 like($response->content,
276 qr(--$boundary\r\nContent-Type: application/zip\r\nContent-Range: bytes 0-1/$zip_length\r\n\r\nPK\r\n),
277 "Second chunk");
278
911b447 Update from Google Code SVN
Evan Miller authored
279 ### If-Range ###
280 set_debug_log("if-range");
281
282 # last-modified
283 $response = $ua->get("$http_root/zip.txt",
284 "If-Range" => "Wed, 15 Nov 1995 04:58:07 GMT",
285 "Range" => "bytes=0-1");
286 is($response->code, 200, "200 OK -- when If-Range is not Last-Modified time");
287
288 $response = $ua->get("$http_root/zip.txt",
289 "If-Range" => "Wed, 15 Nov 1995 04:58:08 GMT",
290 "Range" => "bytes=0-1");
291 is($response->code, 206, "206 Partial Content -- when If-Range is Last-Modified time");
292
293 # etag
294 $response = $ua->get("$http_root/zip.txt",
295 "If-Range" => "2.71828",
296 "Range" => "bytes=0-1");
297 is($response->code, 200, "200 OK -- when If-Range is not ETag");
298
299 $response = $ua->get("$http_root/zip.txt",
300 "If-Range" => "3.14159",
301 "Range" => "bytes=0-1");
302 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.