File: pathops.test

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (515 lines) | stat: -rw-r--r-- 17,474 bytes parent folder | download | duplicates (7)
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
# -*- tcl -*-
# Tests for the find function.
#
# Sourcing this file into Tcl runs the tests and generates output for errors.
# No output means no errors were found.
#
# Copyright (c) 1998-2000 by Ajuba Solutions.
# Copyright (c) 2001 by ActiveState Tool Corp.
# Copyright (c) 2005-2009 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
# All rights reserved.
#
# RCS: @(#) $Id: pathops.test,v 1.2 2009/10/27 19:16:34 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.2
testsNeedTcltest 1.0

testing {
    useLocal fileutil.tcl fileutil
}

# -------------------------------------------------------------------------

set dir $::tcltest::temporaryDirectory

# -------------------------------------------------------------------------

test jail-1.0 {jail error} {
    catch {::fileutil::jail} res
    set res
} [tcltest::wrongNumArgs {::fileutil::jail} {jail filename} 0]

test jail-1.2 {jail error} {
    catch {::fileutil::jail a} res
    set res
} [tcltest::wrongNumArgs {::fileutil::jail} {jail filename} 1]

test jail-1.3 {jail error} {
    catch {::fileutil::jail a b c} res
    set res
} [tcltest::tooManyArgs {::fileutil::jail} {jail filename}]

test jail-2.0 {jail relative} {
    ::fileutil::jail /var/www a/b/c
} /var/www/a/b/c

test jail-2.1 {jail absolute outside} {
    ::fileutil::jail /var/www /a/b/c
} /var/www/a/b/c

test jail-2.1.1 {jail absolute outside, spaces} {
    ::fileutil::jail /var/www {/a/b/c d}
} {/var/www/a/b/c d}

test jail-2.2 {jail absolute inside} {
    ::fileutil::jail /var/www /var/www/a/b/c
} /var/www/a/b/c

test jail-2.2.1 {jail absolute inside} {
    ::fileutil::jail /var/www {/var/www/a/b/c d}
} {/var/www/a/b/c d}

test jail-2.3 {try to escape from jail} {
    ::fileutil::jail /var/www ../../etc/passwd
} /var/www/etc/passwd

test jail-2.4 {jail is relative itself} {
    ::fileutil::jail a b
} [file join $dir a b]

test jail-2.4.1 {jail is relative itself, spaces in path} {
    ::fileutil::jail a {b c}
} [file join $dir a {b c}]

test jail-2.4.2 {jail is relative itself, spaces in path} {
    ::fileutil::jail {a b} {c d}
} [file join $dir {a b} {c d}]


# Need tests using non-existing paths for sure. Similar tests for
# 'normalize' as well.

# Tests for the internal 'Normalize' command. This is our forward
# compatibility wrapper and it should behave identical to the
# 8.4. builtin 'file normalize'. We pilfered the test cases from the
# test suite for 'file normalize' in the Tcl core.

if {![string equal $::tcl_platform(platform) windows]} {

    set dirfile    [makeDirectory dir.file]
    set dirbfile   [makeDirectory dir2.file]
    set insidefile [makeFile "test file in directory" dir.file/inside.file]
    set gorpfile   [makeFile "test file" gorp.file]

    # Paths for the links.

    set linkfile       [tempPath link.file]
    set dirlink        [tempPath dir.link]
    set dirblink       [tempPath dir2.link]
    set linkinsidefile [tempPath $dirfile/linkinside.file]
    set dirbblink      [tempPath $dirbfile/dir2.link]]

    # Create the links. Unix specific.

    exec ln -s gorp.file    $linkfile
    exec ln -s inside.file  $linkinsidefile
    exec ln -s dir.file     $dirlink
    exec ln -s dir.link     $dirblink
    exec ln -s ../dir2.link $dirbblink

    # File/Directory structure created by the above.
    #
    #    /FOO/dir2.link -> dir.link
    #    /FOO/dir.link  -> dir.file
    #    /FOO/dir.file/
    #    /FOO/dir.file/linkinside.file -> inside.file
    #    /FOO/dir.file/inside.file
    #
    #    /FOO/link.file -> gorp.file
    #    /FOO/gorp.file
    #
    #    /FOO/dir2.file/
    #    /FOO/dir2.file/dir2.link -> ../dir2.link
}


test fu-normalize-1.0 {link normalisation} {unixOnly} {
    # Symlink of last path element is not resolved.
    string equal \
	    [::fileutil::Normalize $gorpfile] \
	    [::fileutil::Normalize $linkfile]
} {0}

test fu-normalize-1.1 {link normalisation} {unixOnly} {
    # Symlink of last path element is not resolved.
    string equal \
	    [::fileutil::Normalize $dirfile] \
	    [::fileutil::Normalize $dirlink]
} {0}

test fu-normalize-1.2 {link normalisation} {unixOnly} {
    # Link higher in path is resolved (File!, non-existing last component).
    string equal \
	    [::fileutil::Normalize [file join $gorpfile foo]] \
	    [::fileutil::Normalize [file join $linkfile foo]]
} {1}

test fu-normalize-1.3 {link normalisation} {unixOnly} {
    # Link higher in path is resolved (Directory, non-existing last component).
    string equal \
	    [::fileutil::Normalize [file join $dirfile foo]] \
	    [::fileutil::Normalize [file join $dirlink foo]]
} {1}

test fu-normalize-1.4 {link normalisation} {unixOnly} {
    # Link higher in path is resolved (Directory, existing last component).
    string equal \
	    [::fileutil::Normalize $insidefile] \
	    [::fileutil::Normalize [file join $dirlink inside.file]]
} {1}

test fu-normalize-1.5 {link normalisation} {unixOnly} {
    # Identical paths.
    string equal \
	    [::fileutil::Normalize $linkinsidefile] \
	    [::fileutil::Normalize $linkinsidefile]
} {1}

test fu-normalize-1.6 {link normalisation} {unixOnly} {
    # Double link, one in last component, that one not resolved.
    string equal \
	    [::fileutil::Normalize $linkinsidefile] \
	    [::fileutil::Normalize [file join $dirlink inside.file]]
} {0}

test fu-normalize-1.7 {link normalisation} {unixOnly} {
    # Double link, both higher up, second is file!, both resolved
    string equal \
	    [::fileutil::Normalize [file join $dirlink linkinside.file foo]] \
	    [::fileutil::Normalize [file join $insidefile foo]]
} {1}

test fu-normalize-1.8 {link normalisation} {unixOnly} {
    # Directory link, and bad last component
    string equal \
	    [::fileutil::Normalize ${linkinsidefile}foo] \
	    [::fileutil::Normalize [file join $dirlink inside.filefoo]]
} {0}

if 0 {
    test fu-normalize-1.9 {link normalisation} {unixOnly} {
	file delete -force $dirlink
	file link $dirlink [file nativename $dirfile]
	string equal \
		[::fileutil::Normalize [file join $linkinsidefile foo]] \
		[::fileutil::Normalize [file join $dirlink inside.file foo]]
    } {1}
}

test fu-normalize-1.10 {link normalisation: double link} {unixOnly} {
    # Double symlink in one component.
    string equal \
	    [::fileutil::Normalize [file join $linkinsidefile foo]] \
	    [::fileutil::Normalize [file join $dirblink inside.file foo]]
} {1}


test fu-normalize-1.11 {link normalisation: double link, back in tree} {unixOnly} {
    # Double link and back up in the tree.

    string equal \
	    [::fileutil::Normalize [file join $linkinsidefile foo]] \
	    [::fileutil::Normalize [file join $dirbblink inside.file foo]]
} {1}


test fu-normalize-2.0 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::Normalize /a/b/c
} /a/b/c

test fu-normalize-2.1 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::Normalize /a/../b/c
} /b/c

test fu-normalize-2.2 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::Normalize /a/./b/c
} /a/b/c

test fu-normalize-2.3 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::Normalize /../b/c
} /b/c

test fu-normalize-2.4 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::Normalize /a/../../b/c
} /b/c



# Based on the internal Normalize, a fullnormalize (which resolves a
# link in the last element as well.

test fu-fullnormalize-1.0 {link normalisation} {unixOnly} {
    # Symlink of last path element _is_ resolved.
    string equal \
	    [::fileutil::fullnormalize $gorpfile] \
	    [::fileutil::fullnormalize $linkfile]
} {1}

test fu-fullnormalize-1.1 {link normalisation} {unixOnly} {
    # Symlink of last path element _is_ resolved.
    string equal \
	    [::fileutil::fullnormalize $dirfile] \
	    [::fileutil::fullnormalize $dirlink]
} {1}

test fu-fullnormalize-1.2 {link normalisation} {unixOnly} {
    # Link higher in path is resolved (File!, non-existing last component).
    string equal \
	    [::fileutil::fullnormalize [file join $gorpfile foo]] \
	    [::fileutil::fullnormalize [file join $linkfile foo]]
} {1}

test fu-fullnormalize-1.3 {link normalisation} {unixOnly} {
    # Link higher in path is resolved (Directory, non-existing last component).
    string equal \
	    [::fileutil::fullnormalize [file join $dirfile foo]] \
	    [::fileutil::fullnormalize [file join $dirlink foo]]
} {1}

test fu-fullnormalize-1.4 {link normalisation} {unixOnly} {
    # Link higher in path is resolved (Directory, existing last component).
    string equal \
	    [::fileutil::fullnormalize $insidefile] \
	    [::fileutil::fullnormalize [file join $dirlink inside.file]]
} {1}

test fu-fullnormalize-1.5 {link normalisation} {unixOnly} {
    # Identical paths.
    string equal \
	    [::fileutil::fullnormalize $linkinsidefile] \
	    [::fileutil::fullnormalize $linkinsidefile]
} {1}

test fu-fullnormalize-1.6 {link normalisation} {unixOnly} {
    # Double link, one in last component, this one is resolved.
    string equal \
	    [::fileutil::fullnormalize $linkinsidefile] \
	    [::fileutil::fullnormalize [file join $dirlink inside.file]]
} {1}

test fu-fullnormalize-1.7 {link normalisation} {unixOnly} {
    # Double link, both higher up, second is file!, both resolved
    string equal \
	    [::fileutil::fullnormalize [file join $dirlink linkinside.file foo]] \
	    [::fileutil::fullnormalize [file join $insidefile foo]]
} {1}

test fu-fullnormalize-1.8 {link normalisation} {unixOnly} {
    # Directory link, and bad last component
    string equal \
	    [::fileutil::fullnormalize ${linkinsidefile}foo] \
	    [::fileutil::fullnormalize [file join $dirlink inside.filefoo]]
} {0}

test fu-fullnormalize-1.10 {link normalisation: double link} {unixOnly} {
    # Double symlink in one component.
    string equal \
	    [::fileutil::fullnormalize [file join $linkinsidefile foo]] \
	    [::fileutil::fullnormalize [file join $dirblink inside.file foo]]
} {1}


test fu-fullnormalize-2.0 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::fullnormalize /a/b/c
} /a/b/c

test fu-fullnormalize-2.1 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::fullnormalize /a/../b/c
} /b/c

test fu-fullnormalize-2.2 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::fullnormalize /a/./b/c
} /a/b/c

test fu-fullnormalize-2.3 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::fullnormalize /../b/c
} /b/c

test fu-fullnormalize-2.4 {normalisation, non-existing paths} {unixOnly} {
    ::fileutil::fullnormalize /a/../../b/c
} /b/c

# Cleaning up after.

removeFile      find3/find4/file5
removeDirectory find3/find4
removeDirectory find3
removeDirectory touchTest
removeDirectory installDst
removeDirectory installSrc
removeDirectory {find 1}
removeDirectory dotfiles
removeDirectory grepTest

if {![string equal $::tcl_platform(platform) windows]} {
    file delete -force $linkfile
    file delete -force $dirlink
    file delete -force $dirblink
    file delete -force $linkinsidefile
    file delete -force $dirbblink

    removeFile dir.file/inside.file
    removeFile gorp.file
    removeDirectory dir.file
    removeDirectory dir2.file
}

# -------------------------------------------------------------------------
# Computation of paths relative to a base.

test fu-relative-1.0 {fileutil::relative, wrong#args} {
    catch {fileutil::relative} msg
    set msg
} [tcltest::wrongNumArgs fileutil::relative {base dst} 0]

test fu-relative-1.1 {fileutil::relative, wrong#args} {
    catch {fileutil::relative a} msg
    set msg
} [tcltest::wrongNumArgs fileutil::relative {base dst} 1]

test fu-relative-1.2 {fileutil::relative, wrong#args} {
    catch {fileutil::relative a b c} msg
    set msg
} [tcltest::tooManyArgs fileutil::relative {base dst}]

foreach {n base dst result} {
    0  /base         /base/destination    destination
    1  /base         /destination         ../destination
    2   base          base/destination    destination
    3   base          destination         ../destination
    4  /sub/base     /sub/sub/destination ../sub/destination
    5  /sub/sub/base /sub/destination     ../../destination
    6   sub/base      sub/sub/destination ../sub/destination
    7   sub/sub/base  sub/destination     ../../destination
    8  /base         /base                .
    9   base          base                .
    10 /base/sub     /base/sub            .
    11  base/sub      base/sub            .
    12 /base/sub     /base                ..
    13  base/sub      base                ..
    14  base/sub      destination         ../../destination
    15  base/tcl      base/common         ../common
    16  base/tcl/x    base/common         ../../common
    17 /base/tcl     /base/common         ../common
    18 /base/tcl/x   /base/common         ../../common
} {
    test fu-relative-2.$n {fileutil::relative} {
	fileutil::relative $base $dst
    } $result
}

foreach {n base dst ra rb} {
    0 /base          base/destination absolute relative
    1  base         /destination      relative absolute
} {
    test fu-relative-3.$n {fileutil::relative, bad mix} unixOnly {
	catch {fileutil::relative $base $dst} msg
	set msg
    } "Unable to compute relation for paths of different pathtypes: $ra vs. $rb, ($base vs. $dst)"
}

foreach {n base dst ra rb} {
    0 /base          base/destination volumerelative relative
    1  base         /destination      relative volumerelative
} {
    test fu-relative-4.$n {fileutil::relative, bad mix} winOnly {
	catch {fileutil::relative $base $dst} msg
	set msg
    } "Unable to compute relation for paths of different pathtypes: $ra vs. $rb, ($base vs. $dst)"
}

test fu-relativeurl-1.0 {fileutil::relativeUrl, wrong#args} {
    catch {fileutil::relativeUrl} msg
    set msg
} [tcltest::wrongNumArgs fileutil::relativeUrl {base dst} 0]

test fu-relativeurl-1.1 {fileutil::relativeUrl, wrong#args} {
    catch {fileutil::relativeUrl a} msg
    set msg
} [tcltest::wrongNumArgs fileutil::relativeUrl {base dst} 1]

test fu-relativeurl-1.2 {fileutil::relativeUrl, wrong#args} {
    catch {fileutil::relativeUrl a b c} msg
    set msg
} [tcltest::tooManyArgs fileutil::relativeUrl {base dst}]

foreach {n base dst result} {
    0  /base/file.html         /base/destination/xx.html    destination/xx.html
    1  /base/file.html         /destination/xx.html         ../destination/xx.html
    2   base/file.html          base/destination/xx.html    destination/xx.html
    3   base/file.html          destination/xx.html         ../destination/xx.html
    4  /sub/base/file.html     /sub/sub/destination/xx.html ../sub/destination/xx.html
    5  /sub/sub/base/file.html /sub/destination/xx.html     ../../destination/xx.html
    6   sub/base/file.html      sub/sub/destination/xx.html ../sub/destination/xx.html
    7   sub/sub/base/file.html  sub/destination/xx.html     ../../destination/xx.html
    8  /base/file.html         /base/xx.html                xx.html
    9   base/file.html          base/xx.html                xx.html
    10 /base/sub/file.html     /base/sub/xx.html            xx.html
    11  base/sub/file.html      base/sub/xx.html            xx.html
    12 /base/sub/file.html     /base/xx.html                ../xx.html
    13  base/sub/file.html      base/xx.html                ../xx.html
    14  base/sub/file.html      xx.html                     ../../xx.html
    15  base/tcl/a.html         base/common/../common/./style.css       ../common/style.css
    16  base/tcl/x/a.html       base/common/../common/./style.css       ../../common/style.css
    17 /base/tcl/a.html        /base/common/../common/./style.css       ../common/style.css
    18 /base/tcl/x/a.html      /base/common/../common/./style.css       ../../common/style.css
} {
    test fu-relativeurl-2.$n {fileutil::relativeUrl} {
	fileutil::relativeUrl $base $dst
    } $result
}

foreach {n base dst ra rb} {
    0 /base/file.html          base/destination/xx.html absolute relative
    1  base/file.html         /destination/xx.html      relative absolute
} {
    test fu-relativeurl-3.$n {fileutil::relativeUrl, bad mix} unixOnly {
	catch {fileutil::relativeUrl $base $dst} msg
	set msg
    } "Unable to compute relation for paths of different pathtypes: $ra vs. $rb, ($base vs. $dst)"
}

foreach {n base dst ra rb} {
    0 /base/file.html          base/destination/xx.html volumerelative relative
    1  base/file.html         /destination/xx.html      relative volumerelative
} {
    test fu-relativeurl-4.$n {fileutil::relativeUrl, bad mix} winOnly {
	catch {fileutil::relativeUrl $base $dst} msg
	set msg
    } "Unable to compute relation for paths of different pathtypes: $ra vs. $rb, ($base vs. $dst)"
}

if {[llength [info commands ::fileutil::LexNormalize]]} {

    # Check an internal command. May not exist (i.e. an accelerator
    # may not define it).

    foreach {n base dst} {
	0 a/../b  b
	1 a/./b   a/b
	2 a       a
	3 a/b     a/b
	4 ./a     a
	5 ../a    a
	6 /../a   /a
	7 /./a    /a
	8 /a/../b /b
	9 /foo/bar/../snafu/../gobble /foo/gobble
    } {
	test fu-lexnormalize-1.$n "fileutil::LexNormalize $base" {
	    fileutil::LexNormalize $base
	} $dst
    }
}

# -------------------------------------------------------------------------

unset dir
testsuiteCleanup
return