-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathdav.t
More file actions
595 lines (412 loc) · 14.4 KB
/
Copy pathdav.t
File metadata and controls
595 lines (412 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/perl
# (C) Maxim Dounin
# (C) Sai Krishna Kumar Reddy YADAMAKANTI
# (C) Nginx, Inc.
# Tests for nginx dav module.
###############################################################################
use warnings;
use strict;
use Test::More;
use Socket qw/ CRLF /;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
my $t = Test::Nginx->new()->has(qw/http dav/)->plan(94);
$t->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080;
server_name localhost;
absolute_redirect off;
location / {
dav_methods PUT DELETE MKCOL COPY MOVE;
}
location /i/ {
alias %%TESTDIR%%/;
dav_methods PUT DELETE MKCOL COPY MOVE;
}
location /full/ {
dav_methods PUT DELETE MKCOL COPY MOVE;
create_full_put_path on;
}
location /min3/ {
dav_methods PUT DELETE MKCOL COPY MOVE;
min_delete_depth 3;
}
location /min0/ {
dav_methods PUT DELETE MKCOL COPY MOVE;
min_delete_depth 0;
}
location /access/ {
dav_methods PUT DELETE MKCOL COPY MOVE;
dav_access user:rw group:r all:r;
}
}
}
EOF
$t->run();
###############################################################################
my $r;
# basic tests
$r = http_put('/file', '0123456789');
like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'put file');
is(-s $t->testdir() . '/file', 10, 'put file size');
$r = http_put('/file', '');
like($r, qr/204 No Content/, 'put file again');
unlike($r, qr/Content-Length|Transfer-Encoding/, 'no length in 204');
is(-s $t->testdir() . '/file', 0, 'put file again size');
$r = http_delete('/file', 'Content-Length: 0');
like($r, qr/204 No Content/, 'delete file');
unlike($r, qr/Content-Length|Transfer-Encoding/, 'no length in 204');
ok(!-f $t->testdir() . '/file', 'file deleted');
$r = http(<<EOF . '0123456789' . 'extra');
PUT /file HTTP/1.1
Host: localhost
Connection: close
Content-Length: 10
EOF
like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms,
'put file extra data');
is(-s $t->testdir() . '/file', 10,
'put file extra data size');
$r = http_put('/file%20sp', '0123456789');
like($r, qr!Location: /file%20sp\x0d?$!ms, 'put file escaped');
# 201 replies contain body, response should indicate it's empty
$r = http_mkcol('/test/');
like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'mkcol');
SKIP: {
skip 'perl too old', 1 if !$^V or $^V lt v5.12.0;
like($r, qr!(?(?{ $r =~ /Location/ })Location: /test/)!, 'mkcol location');
}
$r = http_copy('/test/', '/test-moved/');
like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'copy dir');
$r = http_move('/test/', '/test-moved/');
like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'move dir');
$r = http_copy('/file', '/file-moved%20escape');
like($r, qr/204 No Content/, 'copy file escaped');
is(-s $t->testdir() . '/file-moved escape', 10, 'file copied unescaped');
$t->write_file('file.exist', join '', (1 .. 42));
$r = http_copy('/file', '/file.exist');
like($r, qr/204 No Content/, 'copy file overwrite');
is(-s $t->testdir() . '/file.exist', 10, 'target file truncated');
$r = http_put('/i/alias', '0123456789');
like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'put alias');
like($r, qr!Location: /i/alias\x0d?$!ms, 'location alias');
is(-s $t->testdir() . '/alias', 10, 'put alias size');
# request methods with unsupported request body
$r = http(<<EOF . '0123456789');
MKCOL /test/ HTTP/1.1
Host: localhost
Connection: close
Content-Length: 10
EOF
like($r, qr/415 Unsupported/, 'mkcol body');
$r = http(<<EOF . '0123456789');
COPY /file HTTP/1.1
Host: localhost
Destination: /file.exist
Connection: close
Content-Length: 10
EOF
like($r, qr/415 Unsupported/, 'copy body');
$r = http(<<EOF . '0123456789');
MOVE /file HTTP/1.1
Host: localhost
Destination: /file_dst
Connection: close
Content-Length: 10
EOF
like($r, qr/415/, 'move body');
$r = http(<<EOF . '0123456789');
DELETE /file HTTP/1.1
Host: localhost
Connection: close
Content-Length: 10
EOF
like($r, qr/415 Unsupported/, 'delete body');
my $chunked = 'a' . CRLF . '0123456789' . CRLF . '0' . CRLF . CRLF;
$r = http(<<EOF . $chunked);
MKCOL /test/ HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked
EOF
like($r, qr/415 Unsupported/, 'mkcol body chunked');
$r = http(<<EOF . $chunked);
COPY /file HTTP/1.1
Host: localhost
Destination: /file.exist
Connection: close
Transfer-Encoding: chunked
EOF
like($r, qr/415 Unsupported/, 'copy body chunked');
$r = http(<<EOF . $chunked);
MOVE /file HTTP/1.1
Host: localhost
Destination: /file_dst
Connection: close
Transfer-Encoding: chunked
EOF
like($r, qr/415/, 'move body chunked');
$r = http(<<EOF . $chunked);
DELETE /file HTTP/1.1
Host: localhost
Connection: close
Transfer-Encoding: chunked
EOF
like($r, qr/415 Unsupported/, 'delete body chunked');
# PUT edge cases
$r = http_put('/trailing/', '0123456789');
like($r, qr/409/, 'put to collection uri');
$r = http_put('/range_file', '0123456789', 'Content-Range: bytes 0-9/20');
like($r, qr/501/, 'put with content-range');
$r = http_put('/dated_file', 'dated', 'Date: Thu, 01 Jan 2026 00:00:00 GMT');
like($r, qr/201 Created/, 'put with valid date header');
$r = http_put('/baddate_file', 'baddt', 'Date: invalid-date-value');
like($r, qr/201 Created/, 'put with invalid date header');
mkdir($t->testdir() . '/existdir');
$r = http_put('/existdir', 'data');
like($r, qr/409/, 'put over existing directory');
$r = http_put('/full/a/b/c/deep_file', 'deep');
like($r, qr/201 Created/, 'put create full path');
ok(-f $t->testdir() . '/full/a/b/c/deep_file', 'put create full path exists');
$r = http_put('/noparent/sub/file', 'fail');
like($r, qr/(?:409|500)/, 'put without full path fails');
$r = http_put('/zerofile', '');
like($r, qr/201 Created/, 'put zero-length new file');
# DELETE edge cases
$r = http_delete('/nonexistent', 'Content-Length: 0');
like($r, qr/404/, 'delete non-existent');
mkdir($t->testdir() . '/deldir_noslash');
$t->write_file('deldir_noslash/f', 'x');
$r = http_delete('/deldir_noslash', 'Content-Length: 0');
like($r, qr/409/, 'delete dir without trailing slash');
mkdir($t->testdir() . '/deldir');
$t->write_file('deldir/f1', 'x');
$r = http_delete('/deldir/');
like($r, qr/204 No Content/, 'delete dir with trailing slash');
ok(!-d $t->testdir() . '/deldir', 'delete dir removed');
mkdir($t->testdir() . '/nested');
mkdir($t->testdir() . '/nested/sub');
$t->write_file('nested/a', 'x');
$t->write_file('nested/sub/b', 'x');
$r = http_delete('/nested/');
like($r, qr/204 No Content/, 'delete nested directory');
ok(!-d $t->testdir() . '/nested', 'delete nested removed');
# Depth header
mkdir($t->testdir() . '/depthdir');
$t->write_file('depthdir/f', 'x');
$r = http_delete('/depthdir/', 'Depth: 0');
like($r, qr/400/, 'delete dir depth 0');
$r = http_delete('/depthdir/', 'Depth: 1');
like($r, qr/400/, 'delete dir depth 1');
$t->write_file('depthfile', 'x');
$r = http_delete('/depthfile', 'Depth: 1');
like($r, qr/400/, 'delete file depth 1');
$t->write_file('depthfile0', 'x');
$r = http_delete('/depthfile0', 'Depth: 0');
like($r, qr/204 No Content/, 'delete file depth 0');
$t->write_file('depthfile_inf', 'x');
$r = http_delete('/depthfile_inf', 'Depth: infinity');
like($r, qr/204 No Content/, 'delete file depth infinity');
$t->write_file('baddepth', 'x');
$r = http_delete('/baddepth', 'Depth: bogus');
like($r, qr/400/, 'delete invalid depth header');
$t->write_file('depth2', 'x');
$r = http_delete('/depth2', 'Depth: 2');
like($r, qr/400/, 'delete depth 2 invalid');
# min_delete_depth
mkdir($t->testdir() . '/min3');
$t->write_file('min3/shallow', 'x');
$r = http_delete('/min3/shallow');
like($r, qr/409/, 'min_delete_depth too shallow');
mkdir($t->testdir() . '/min3/a');
mkdir($t->testdir() . '/min3/a/b');
$t->write_file('min3/a/b/deep', 'x');
$r = http_delete('/min3/a/b/deep');
like($r, qr/204 No Content/, 'min_delete_depth deep enough');
mkdir($t->testdir() . '/min0');
$t->write_file('min0/top', 'x');
$r = http_delete('/min0/top');
like($r, qr/204 No Content/, 'min_delete_depth 0');
# MKCOL edge cases
$r = http_mkcol('/no_slash');
like($r, qr/409/, 'mkcol without trailing slash');
http_mkcol('/newcol/');
$r = http_mkcol('/newcol/');
like($r, qr/405/, 'mkcol existing directory');
$r = http_mkcol('/nonexist_parent/child/');
like($r, qr/409/, 'mkcol missing parent');
# Destination header validation
$t->write_file('src_file', 'COPYDATA');
$r = http(<<EOF);
COPY /src_file HTTP/1.1
Host: localhost
Connection: close
EOF
like($r, qr/400/, 'copy no destination');
$r = http_copy('/src_file', 'ftp://localhost/bad_scheme');
like($r, qr/400/, 'copy invalid destination scheme');
$r = http_copy('/src_file', 'http://otherhost/file');
like($r, qr/400/, 'copy different host');
$r = http_copy('/src_file', 'http://localhost/full_url_copy');
like($r, qr/20[14]/, 'copy with full url destination');
$r = http_copy('/src_file', 'http://localhost:8080/port_copy');
like($r, qr/20[14]/, 'copy with port in destination');
$r = http_copy('/src_file', 'http://localhost');
like($r, qr/400/, 'copy destination no path after host');
$r = http(<<EOF);
MOVE /src_file HTTP/1.1
Host: localhost
Connection: close
EOF
like($r, qr/400/, 'move no destination');
# Overwrite header
$t->write_file('src_ow', 'OWDATA');
$t->write_file('existing_dst', 'OLD');
$r = http_copy('/src_ow', '/existing_dst', 'Overwrite: F');
like($r, qr/412/, 'copy overwrite false');
is($t->read_file('existing_dst'), 'OLD', 'copy overwrite false preserved');
$t->write_file('existing_dst2', 'OLD2');
$r = http_copy('/src_ow', '/existing_dst2', 'Overwrite: f');
like($r, qr/412/, 'copy overwrite lowercase f');
$r = http_copy('/src_ow', '/existing_dst', 'Overwrite: T');
like($r, qr/204 No Content/, 'copy overwrite true');
is($t->read_file('existing_dst'), 'OWDATA', 'copy overwrite true replaced');
$t->write_file('ow_lower', 'OLD');
$r = http_copy('/src_ow', '/ow_lower', 'Overwrite: t');
like($r, qr/204 No Content/, 'copy overwrite lowercase t');
$r = http_copy('/src_ow', '/inv_ow_dst', 'Overwrite: X');
like($r, qr/400/, 'copy invalid overwrite');
$r = http_copy('/src_ow', '/inv_ow_dst2', 'Overwrite: TRUE');
like($r, qr/400/, 'copy invalid overwrite multi-char');
# COPY Depth header
$r = http_copy('/src_ow', '/depth0_copy', 'Depth: 0');
like($r, qr/204 No Content/, 'copy file depth 0');
$r = http_copy('/src_ow', '/depth1_copy', 'Depth: 1');
like($r, qr/400/, 'copy depth 1 invalid');
# collection/non-collection mismatch
mkdir($t->testdir() . '/srcdir_mm');
$t->write_file('srcdir_mm/inner', 'INNER');
$r = http_copy('/srcdir_mm/', '/mismatch_noncol');
like($r, qr/409/, 'copy collection to non-collection');
$r = http_copy('/src_ow', '/mismatch_col/');
like($r, qr/409/, 'copy non-collection to collection');
# directory tree operations
mkdir($t->testdir() . '/srcdir_tree');
mkdir($t->testdir() . '/srcdir_tree/subdir');
$t->write_file('srcdir_tree/file1', 'FILE1');
$t->write_file('srcdir_tree/subdir/file2', 'FILE2');
$r = http_copy('/srcdir_tree/', '/dstdir_tree/');
like($r, qr/201 Created/, 'copy directory tree');
is($t->read_file('dstdir_tree/file1'), 'FILE1', 'copy tree file');
is($t->read_file('dstdir_tree/subdir/file2'), 'FILE2', 'copy tree subdir file');
mkdir($t->testdir() . '/ow_src');
$t->write_file('ow_src/data', 'NEWDATA');
mkdir($t->testdir() . '/ow_dst');
$t->write_file('ow_dst/old', 'OLDDATA');
$r = http_copy('/ow_src/', '/ow_dst/', 'Overwrite: T');
like($r, qr/201 Created/, 'copy dir overwrite existing');
is($t->read_file('ow_dst/data'), 'NEWDATA', 'copy dir overwrite content');
$r = http_copy('/nonexistent_src', '/some_dst');
like($r, qr/404/, 'copy source not found');
mkdir($t->testdir() . '/existing_dir_dst');
$r = http_copy('/src_ow', 'http://localhost/existing_dir_dst');
like($r, qr/409/, 'copy to existing dir no slash');
# MOVE
$t->write_file('moveme', 'MOVEDATA');
$r = http_move('/moveme', '/moved');
like($r, qr/204 No Content/, 'move file');
ok(!-f $t->testdir() . '/moveme', 'move file source removed');
is($t->read_file('moved'), 'MOVEDATA', 'move file content');
$t->write_file('movedepth', 'x');
$r = http_move('/movedepth', '/movedepth_dst', 'Depth: 0');
like($r, qr/400/, 'move depth 0 invalid');
mkdir($t->testdir() . '/movedir_nested');
mkdir($t->testdir() . '/movedir_nested/sub');
$t->write_file('movedir_nested/a', 'A');
$t->write_file('movedir_nested/sub/b', 'B');
mkdir($t->testdir() . '/movedir_nested_dst');
$r = http_move('/movedir_nested/', '/movedir_nested_dst/');
like($r, qr/201 Created/, 'move nested directory');
ok(!-d $t->testdir() . '/movedir_nested', 'move nested source removed');
# dav_access
mkdir($t->testdir() . '/access');
$r = http_put('/access/afile', 'accessdata');
like($r, qr/201 Created/, 'dav_access put');
SKIP: {
skip 'permissions on win32', 1 if $^O eq 'MSWin32';
my $mode = (stat($t->testdir() . '/access/afile'))[2] & 07777;
is($mode & 0644, 0644, 'dav_access file permissions');
}
$r = http_mkcol('/access/subdir/');
like($r, qr/201 Created/, 'dav_access mkcol');
SKIP: {
skip 'permissions on win32', 1 if $^O eq 'MSWin32';
my $mode = (stat($t->testdir() . '/access/subdir'))[2] & 07777;
ok($mode & 0755, 'dav_access mkcol permissions');
}
###############################################################################
sub http_put {
my ($uri, $body, $extra) = @_;
my $length = length($body);
my $headers = <<EOF;
PUT $uri HTTP/1.1
Host: localhost
Connection: close
Content-Length: $length
EOF
if ($extra) {
$headers .= $extra . CRLF;
}
http($headers . CRLF . $body);
}
sub http_delete {
my ($uri, $extra) = @_;
$extra = '' if !defined $extra;
http(<<EOF);
DELETE $uri HTTP/1.1
Host: localhost
Connection: close
$extra
EOF
}
sub http_mkcol {
my ($uri) = @_;
http(<<EOF);
MKCOL $uri HTTP/1.1
Host: localhost
Connection: close
EOF
}
sub http_copy {
my ($uri, $destination, $extra) = @_;
$extra = '' if !defined $extra;
http(<<EOF);
COPY $uri HTTP/1.1
Host: localhost
Connection: close
Destination: $destination
$extra
EOF
}
sub http_move {
my ($uri, $destination, $extra) = @_;
$extra = '' if !defined $extra;
http(<<EOF);
MOVE $uri HTTP/1.1
Host: localhost
Connection: close
Destination: $destination
$extra
EOF
}
###############################################################################