xref: /openbsd/gnu/usr.bin/perl/lib/filetest.t (revision 09467b48)
1#!./perl
2
3BEGIN {
4	chdir 't' if -d 't';
5	@INC = '../lib';
6}
7
8use Config;
9use Test::More tests => 15;
10
11# these two should be kept in sync with the pragma itself
12# if hint bits are changed there, other things *will* break
13my $hint_bits = 0x00400000;
14my $error = "filetest: the only implemented subpragma is 'access'.\n";
15
16# can't use it yet, because of the import death
17ok( require filetest, 'required pragma successfully' );
18
19# and here's one culprit, right here
20eval { filetest->import('bad subpragma') };
21is( $@, $error, 'filetest dies with bad subpragma on import' );
22
23is( $^H & $hint_bits, 0, 'hint bits not set without pragma in place' );
24
25# now try the normal usage
26# can't check $^H here; it's lexically magic (see perlvar)
27# the test harness unintentionally hoards the goodies for itself
28use_ok( 'filetest', 'access' );
29
30# and import again, to see it here
31filetest->import('access');
32ok( $^H & $hint_bits, 'hint bits set with pragma loaded' );
33
34# and now get rid of it
35filetest->unimport('access');
36is( $^H & $hint_bits, 0, 'hint bits not set with pragma unimported' );
37
38eval { filetest->unimport() };
39is( $@, $error, 'filetest dies without subpragma on unimport' );
40
41# there'll be a compilation aborted failure here, with the eval string
42eval "no filetest 'fake pragma'";
43like( $@, qr/^$error/, 'filetest dies with bad subpragma on unuse' );
44
45eval "use filetest 'bad subpragma'";
46like( $@, qr/^$error/, 'filetest dies with bad subpragma on use' );
47
48eval "use filetest";
49like( $@, qr/^$error/, 'filetest dies with missing subpragma on use' );
50
51eval "no filetest";
52like( $@, qr/^$error/, 'filetest dies with missing subpragma on unuse' );
53
54SKIP: {
55    # A real test for filetest.
56    # This works for systems with /usr/bin/chflags (i.e. BSD4.4 systems).
57    my $chflags = "/usr/bin/chflags";
58    my $tstfile = "filetest.tst";
59    skip("No $chflags available", 4) if !-x $chflags;
60    skip("Dragonfly filetests seem non-chflags aware", 4) if $^O eq 'dragonfly';
61
62    my $skip_eff_user_tests = (!$Config{d_setreuid} && !$Config{d_setresuid})
63	                                            ||
64			      (!$Config{d_setregid} && !$Config{d_setresgid});
65
66    eval {
67	if (!-e $tstfile) {
68	    open(T, ">$tstfile") or die "Can't create $tstfile: $!";
69	    close T;
70	}
71	system($chflags, "uchg", $tstfile);
72	die "Can't exec $chflags uchg" if $? != 0;
73    };
74    skip("Errors in test using chflags: $@", 4) if $@;
75
76    {
77	use filetest 'access';
78    SKIP: {
79	    skip("No tests on effective user id", 1)
80		if $skip_eff_user_tests;
81	    is(-w $tstfile, undef, "$tstfile should not be recognized as writable");
82	}
83	is(-W $tstfile, undef, "$tstfile should not be recognized as writable");
84    }
85
86    {
87	no filetest 'access';
88    SKIP: {
89	    skip("No tests on effective user id", 1)
90		if $skip_eff_user_tests;
91	    is(-w $tstfile, 1, "$tstfile should be recognized as writable");
92	}
93	is(-W $tstfile, 1, "$tstfile should be recognized as writable");
94    }
95
96    # cleanup
97    system($chflags, "nouchg", $tstfile);
98    unlink $tstfile;
99    warn "Can't remove $tstfile: $!" if -e $tstfile;
100}
101