-
Notifications
You must be signed in to change notification settings - Fork 138
/
os.t
829 lines (674 loc) · 15.2 KB
/
os.t
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#! perl
# Copyright (C) 2001-2014, Parrot Foundation.
use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
use Parrot::Test tests => 36;
use Parrot::Config;
use Cwd;
use File::Spec;
use File::Path;
my $MSWin32 = $^O =~ m!MSWin32|msys!;
my $cygwin = $^O =~ m!cygwin!;
my $solaris = $^O =~ m!solaris!;
my $MSVC = $PConfig{cc} =~ m/\bcl(?:\.exe)?/i;
=head1 NAME
t/pmc/os.t - Files and Dirs
=head1 SYNOPSIS
% prove t/pmc/os.t
=head1 DESCRIPTION
Tests the C<OS> PMC.
=cut
END {
# Clean up environment on exit
rmdir "xpto" if -d "xpto";
unlink "xpto" if -f "xpto";
}
# test 'cwd'
my $cwd = File::Spec->canonpath(getcwd);
if (File::Spec->case_tolerant(substr($cwd,0,2))) {
$cwd = lc($cwd);
pir_output_is( <<'CODE', <<"OUT", 'Test cwd' );
.sub main :main
$P0 = new ['OS']
$S1 = $P0."cwd"()
# Unicode downcase needs ICU
$I0 = find_encoding "iso-8859-1"
$S1 = trans_encoding $S1, $I0
$S2 = downcase $S1
print $S2
print "\n"
end
.end
CODE
$cwd
OUT
}
else {
pir_output_is( <<'CODE', <<"OUT", 'Test cwd' );
.sub main :main
$P0 = new ['OS']
$S1 = $P0."cwd"()
print $S1
print "\n"
end
.end
CODE
$cwd
OUT
}
# test bad cwd
SKIP:
{
skip 'Some OSes don\'t allow to remove the current directory', 1 if $^O !~ /linux/i;
mkdir "test-bad-cwd";
pir_error_output_like( <<'CODE', <<"OUT", 'Test bad cwd' );
.sub main :main
$P0 = new ['OS']
$P0.'chdir'('test-bad-cwd')
$P0.'rm'('../test-bad-cwd')
$S0 = $P0.'cwd'()
.end
CODE
/getcwd failed/
OUT
}
# TEST chdir
chdir "src";
my $upcwd = File::Spec->canonpath(getcwd);
chdir '..';
if (File::Spec->case_tolerant(substr($cwd,0,2))) {
$cwd = lc($cwd);
$upcwd = lc($upcwd);
pir_output_is( <<'CODE', <<"OUT", 'Test chdir' );
.sub main :main
$P1 = new ['OS']
$S1 = "src"
$P1."chdir"($S1)
$S1 = $P1."cwd"()
# Unicode downcase needs ICU
$I0 = find_encoding "iso-8859-1"
$S1 = trans_encoding $S1, $I0
$S2 = downcase $S1
say $S2
$S1 = ".."
$P1."chdir"($S1)
$S1 = $P1."cwd"()
# Unicode downcase needs ICU
$I0 = find_encoding "iso-8859-1"
$S1 = trans_encoding $S1, $I0
$S2 = downcase $S1
say $S2
end
.end
CODE
$upcwd
$cwd
OUT
}
else {
pir_output_is( <<'CODE', <<"OUT", 'Test chdir' );
.sub main :main
$P1 = new ['OS']
$S1 = "src"
$P1."chdir"($S1)
$S1 = $P1."cwd"()
say $S1
$S1 = ".."
$P1."chdir"($S1)
$S1 = $P1."cwd"()
say $S1
end
.end
CODE
$upcwd
$cwd
OUT
}
# test bad chdir
pir_error_output_like( <<'CODE', <<"OUT", 'Test bad chdir' );
.sub main :main
$P1 = new ['OS']
$S1 = repeat "-!", 10
$P1."chdir"($S1)
.end
CODE
/chdir failed/
OUT
# Test mkdir
my $xpto = $upcwd;
$xpto =~ s/src([\/\\]?)$/xpto$1/;
if (File::Spec->case_tolerant(substr($cwd,0,2))) {
pir_output_is( <<'CODE', <<"OUT", 'Test mkdir' );
.sub main :main
$P1 = new ['OS']
$S1 = "xpto"
$I1 = 0o555
$P1."mkdir"($S1,$I1)
$P1."chdir"($S1)
$S1 = $P1."cwd"()
# Unicode downcase needs ICU
$I0 = find_encoding "iso-8859-1"
$S1 = trans_encoding $S1, $I0
$S2 = downcase $S1
say $S2
$S1 = ".."
$P1."chdir"($S1)
$S1 = $P1."cwd"()
# Unicode downcase needs ICU
$I0 = find_encoding "iso-8859-1"
$S1 = trans_encoding $S1, $I0
$S2 = downcase $S1
say $S2
end
.end
CODE
$xpto
$cwd
OUT
}
else {
pir_output_is( <<'CODE', <<"OUT", 'Test mkdir' );
.sub main :main
$P1 = new ['OS']
$S1 = "xpto"
$I1 = 0o555
$P1."mkdir"($S1,$I1)
$P1."chdir"($S1)
$S1 = $P1."cwd"()
say $S1
$S1 = ".."
$P1."chdir"($S1)
$S1 = $P1."cwd"()
say $S1
end
.end
CODE
$xpto
$cwd
OUT
}
pir_error_output_like( <<'CODE', <<"OUT", 'Test bad mkdir' );
.sub main :main
$P1 = new ['OS']
$P1."mkdir"(".", 0)
.end
CODE
/mkdir failed/i
OUT
# Test remove on a directory
mkdir "xpto" unless -d "xpto";
pir_output_is( <<'CODE', <<'OUT', 'Test rm call in a directory' );
.sub main :main
$P1 = new ['OS']
$S1 = "xpto"
$P1."rm"($S1)
print "ok\n"
end
.end
CODE
ok
OUT
ok( !-d $xpto, "Test that rm removed the directory" );
rmdir $xpto if -d $xpto; # this way next test doesn't fail if this one does
mkdir "test-bad-rm";
open my $testfile, '>', "test-bad-rm/bad";
close $testfile;
pir_output_like( <<'CODE', <<'OUT', 'Test bad rm calls' );
.sub main :main
$P1 = new ['OS']
push_eh eh1
$P1."rm"('test-bad-rm')
say "failed"
goto finally1
eh1:
.get_results($P2)
say $P2
finally1:
pop_eh
$P1."chdir"("..")
push_eh eh2
$P1."rm"('non-existent-directory')
say "failed"
goto finally2
eh2:
.get_results($P2)
say $P2
finally2:
pop_eh
.end
CODE
/rmdir failed.*
stat failed/
OUT
unlink "test-bad-rm/bad";
rmdir "test-bad-rm";
# test stat
open my $X, '>', "xpto";
print $X "xpto";
close $X;
my $stat;
my $count = $MSWin32 ? 11 : 13;
# [GH #820] Win32 stat() for mtime is broken. Try to use Win32::UTCFileTime
BEGIN {
if ($^O eq 'MSWin32') {
eval { require Win32::UTCFileTime; }
and Win32::UTCFileTime::import(':globally');
}
}
my @s = stat('xpto');
$s[6] = 0; # Parrot does this internally...
if ( $cygwin ) {
# Mask inode number (fudge it)
$s[1] &= 0xffffffff;
}
if ( $MSWin32 ) {
$s[0] = 0; # dev: we use dwVolumeSerialNumber instead of drive letter
$s[2] = 0777; # mode: always 0777 on Windows
$s[3] = 0; # nlink: only implemented in fstat for now
$s[6] = 0; # rdev: we use zero instead of drive letter
$s[10] = $s[9]; # ctime: equals mtime
$stat = sprintf("0x%08x\n" x 11, @s);
pir_output_is( <<'CODE', $stat, 'Test OS.stat' );
.sub main :main
$P1 = new ['OS']
$S1 = "xpto"
$P2 = $P1."stat"($S1)
$P2[0] = 0
$S1 = repeat "0x%08x\n", 11
$S2 = sprintf $S1, $P2
print $S2
done:
end
.end
CODE
}
else {
SKIP: {
skip 'broken test GH #457', 1 if $solaris;
skip 'cannot test stat on cygwin64 GH #1066', 1 if $cygwin and $PConfig{ptrsize} == 8;
$stat = sprintf("0x%08x\n" x 13, @s);
pir_output_is( <<'CODE', $stat, 'Test OS.stat' );
.sub main :main
$P1 = new ['OS']
$S1 = "xpto"
$P2 = $P1."stat"($S1)
$S1 = repeat "0x%08x\n", 13
$S2 = sprintf $S1, $P2
print $S2
done:
end
.end
CODE
}
}
pir_error_output_like( <<'CODE', <<'OUTPUT', 'test bad stat');
.sub main :main
$P1 = new ['OS']
$P2 = $P1."stat"("non-existent something")
.end
CODE
/stat failed/
OUTPUT
opendir my $IN, 'docs';
my @entries = readdir $IN;
closedir $IN;
my $entries = join( ' ', @entries ) . "\n";
pir_output_is( <<'CODE', $entries, 'Test OS.readdir' );
.sub main :main
$P1 = new ['OS']
$P2 = $P1.'readdir'('docs')
$S0 = join ' ', $P2
$I0 = find_encoding 'ascii'
$S0 = trans_encoding $S0, $I0
print $S0
print "\n"
.end
CODE
SKIP: {
skip 'not implemented on windows yet', 1 if $MSWin32;
mkdir 'silly-dir-with-silly-names';
open my $fileh, '>', "silly-dir-with-silly-names/sillyname\x{263A}";
close $fileh;
opendir my $IN2, 'silly-dir-with-silly-names';
my @entries2 = readdir $IN2;
closedir $IN2;
my $entries2 = join( ' ', @entries2 ) . "\n";
pir_output_is( <<'CODE', $entries2, 'Test OS.readdir with ord >127' );
.sub main :main
$P1 = new ['OS']
$P2 = $P1.'readdir'('silly-dir-with-silly-names')
$S0 = join ' ', $P2
print $S0
print "\n"
.end
CODE
unlink "silly-dir-with-silly-names/sillyname\x{263A}";
rmdir "silly-dir-with-silly-names";
}
pir_error_output_like( <<'CODE', <<'OUTPUT', 'Test bad OS.readdir' );
.sub main :main
$P1 = new ['OS']
$P2 = $P1.'readdir'('non-existent directory')
.end
CODE
/readdir failed/
OUTPUT
# test rename
open my $FILE, ">", "____some_test_file";
close $FILE;
pir_output_is( <<'CODE', <<"OUT", 'Test OS.rename' );
.sub main :main
$P1 = new ['OS']
$P1.'rename'('____some_test_file', '___some_other_file')
say "ok"
.end
CODE
ok
OUT
if(-f '___some_other_file')
{
ok(1, "file was really renamed");
unlink '___some_other_file';
}
else {
ok(0, "file wasn't renamed");
}
pir_error_output_like( <<'CODE', <<"OUT", 'Test bad OS.rename' );
.sub main :main
$P1 = new ['OS']
$P1.'rename'('some silly non-existent file name', 'arglblargl')
.end
CODE
/rename failed/
OUT
# test lstat
my $lstat;
SKIP: {
skip 'lstat not on Win32', 1 if $MSWin32;
skip 'broken test GH #457', 1 if $solaris;
skip 'cannot test lstat on cygwin64 GH #1066', 1 if $cygwin and $PConfig{ptrsize} == 8;
my @s = lstat('xpto');
$s[6] = 0; # Parrot does this internally
if ($cygwin) {
# Mask inode number (fudge it)
$s[1] &= 0xffffffff;
}
$lstat = sprintf( "0x%08x\n" x 13, @s );
pir_error_output_like( <<'CODE', <<"OUTPUT", "Test OS.lstat" );
.sub main :main
$P1 = new ['OS']
$S1 = "xpto"
$P2 = $P1."lstat"($S1)
$S1 = repeat "0x%08x\n", 13
$S2 = sprintf $S1, $P2
print $S2
$P3 = $P1."lstat"("non-existent file")
end
.end
CODE
/${lstat}stat failed/
OUTPUT
}
# Test remove on a file
pir_output_is( <<'CODE', <<"OUT", "Test rm call in a file" );
.sub main :main
$P1 = new ['OS']
$S1 = "xpto"
$P1."rm"($S1)
print "ok\n"
end
.end
CODE
ok
OUT
ok( !-f $xpto, "Test that rm removed file" );
rmdir $xpto if -f $xpto; # this way next test doesn't fail if this one does
# Test symlink
SKIP: {
skip "Admin rights and Vista needed for symlinks on Windows", 2 if $MSWin32;
pir_error_output_like( <<'CODE', <<"OUT", "Test symlink" );
.sub main :main
$P1 = new ['OS']
$S1 = "xpto"
$S2 = "MANIFEST"
$P1."symlink"($S2, $S1)
print "ok\n"
$P1."symlink"($S1, $S2)
end
.end
CODE
/ok
symlink failed/
OUT
ok( -l "xpto", "symlink was really created" );
unlink "xpto" if -f "xpto";
}
# Test readlink
SKIP: {
skip "Admin rights and Vista needed for symlinks on Windows", 1 if $MSWin32;
my $real_path = 'MANIFEST';
my $symlink_path = 'xpto';
symlink $real_path, $symlink_path
or warn "Failed to symlink: $!";
pir_output_is( <<'CODE', <<"OUT", 'Test readlink' );
.sub main :main
$P1 = new ['OS']
$S1 = $P1."readlink"("xpto")
say $S1
end
.end
CODE
$real_path
OUT
unlink $symlink_path if -f $symlink_path;
}
# Test link to file. May require root permissions
pir_output_is( <<'CODE', <<"OUT", "Test link" );
.sub main :main
$P1 = new ['OS']
$S1 = "xpto"
$S2 = "myconfig"
$P1."link"($S2, $S1)
print "ok\n"
end
.end
CODE
ok
OUT
my $nl = [ stat("myconfig") ]->[3];
if (-l "myconfig") {
ok( 1, "hard link on symlink skipped" );
}
else {
ok( $nl > 1, "hard link to file was really created" );
}
unlink "xpto" if -f "xpto";
my $prevnl = [ stat("tools") ]->[3];
pir_output_like( <<"CODE", <<"OUT", "Test dirlink" );
.sub main :main
.local pmc os
.local string xpto, tools
os = new ['OS']
xpto = "xpto"
tools = "tools"
push_eh no_root_perms
os."link"(tools, xpto)
pop_eh
.local pmc statvals
statvals = os.'stat'(tools)
# nlink
.local int nlink
nlink = statvals[3]
gt nlink, $prevnl, is_okay
end
no_root_perms:
.local pmc e
.local string message
.get_results( e )
pop_eh
message = e['message']
say message
end
is_okay:
say "ok"
end
.end
CODE
/link failed/
OUT
# Test umask
SKIP: {
skip "umask not available under Windows", 1 if $MSWin32;
my $umask = umask;
pir_output_like( <<'CODE', <<"OUT", "Test umask" );
.sub main :main
$P1 = new ['OS']
$I0 = $P1.'umask'(0)
say $I0
$I1 = $P1.'umask'($I0)
say $I1
end
.end
CODE
/$umask
0
/
OUT
}
use English '$UID';
# Test chroot
SKIP: {
skip "chroot not available under Windows", 1 if $MSWin32;
skip "chroot not available if not root", 1 if $UID != 0;
mkdir "my-super-chroot";
chdir "my-super-chroot";
symlink "loop", "loop";
chdir "..";
pir_error_output_like( <<'CODE', <<'OUT', "Test chroot" );
.sub main :main
$P1 = new ['OS']
$P1.'chdir'('my-super-chroot')
$P1.'chroot'('.')
$S0 = $P1.'cwd'()
say $S0
$P1.'chroot'('loop')
.end
CODE
/\/
chroot failed(: Too many levels of symbolic links)*
/
OUT
rmtree("my-super-chroot", 0, 1);
}
# test get_user_id
pir_output_is( <<'CODE', "$UID\n", 'Test get_user_id' );
.sub main :main
$P1 = new ['OS']
$I0 = $P1."get_user_id"()
say $I0
end
.end
CODE
SKIP: {
skip 'no file modes on Win32', 3 if ($MSWin32 || $cygwin);
open my $fa, ">", "test_f_a";
close $fa;
open my $fb, ">", "test_f_b";
close $fb;
open my $fc, ">", "test_f_c";
close $fc;
chmod 0777, "test_f_a";
chmod 0000, "test_f_b";
# test chmod
pir_output_is( <<'CODE', <<"OUT", 'Test chmod' );
.sub main :main
$P1 = new ['OS']
$P1."chmod"("test_f_c", 420)
say "ok"
end
.end
CODE
ok
OUT
my $filemode = (stat("test_f_c"))[2] & 07777;
ok($filemode == 420, "really did chmod");
unlink "test_f_c";
# test chmod
pir_error_output_like( <<'CODE', <<"OUT", 'Test chmod' );
.sub main :main
$P1 = new ['OS']
$P1."chmod"("this is another non-existent directory", 420)
say "ok"
end
.end
CODE
/chmod failed/
OUT
}
my ($ra, $rb, $wa, $wb, $xa, $xb);
# test can_read
$ra = -r "README" ? 1 : 0;
pir_output_is( <<'CODE', <<"OUT", 'Test can_read' );
.sub main :main
$P1 = new ['OS']
$I0 = $P1."can_read"("README")
say $I0
end
.end
CODE
$ra
OUT
# test can_write
$wa = -w "test_f_a" ? 1 : 0;
$wb = -w "test_f_b" ? 1 : 0;
pir_output_is( <<'CODE', <<"OUT", 'Test can_write' );
.sub main :main
$P1 = new ['OS']
$I0 = $P1."can_write"("test_f_a")
say $I0
$I1 = $P1."can_write"("test_f_b")
say $I1
end
.end
CODE
$wa
$wb
OUT
# test can_execute
$xa = -x "README" ? 1 : 0;
my $parrot_exe_name = $MSWin32 ? "parrot.exe" : "parrot";
$xb = -x $parrot_exe_name ? 1 : 0;
pir_output_is( <<"CODE", <<"OUT", 'Test can_execute' );
.sub main :main
\$P1 = new ['OS']
\$I0 = \$P1."can_execute"("README")
say \$I0
\$I1 = \$P1."can_execute"("$parrot_exe_name")
say \$I1
end
.end
CODE
$xa
$xb
OUT
# Support loadlib 'os' for a while
pir_output_is( <<'CODE', <<"OUT", "loadlib 'os' deprecation" );
.sub main :main
$P0 = loadlib 'os'
$P1 = new 'OS'
$S1 = $P1."cwd"()
print "ok\n"
end
.end
CODE
ok
OUT
unlink "test_f_a", "test_f_b", "test_f_c";
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: