xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/filetest.t (revision 0:68f95e015346)
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
61    my $skip_eff_user_tests = (!$Config{d_setreuid} && !$Config{d_setresuid})
62	                                            ||
63			      (!$Config{d_setregid} && !$Config{d_setresgid});
64
65    eval {
66	if (!-e $tstfile) {
67	    open(T, ">$tstfile") or die "Can't create $tstfile: $!";
68	    close T;
69	}
70	system($chflags, "uchg", $tstfile);
71	die "Can't exec $chflags uchg" if $? != 0;
72    };
73    skip("Errors in test using chflags: $@", 4) if $@;
74
75    {
76	use filetest 'access';
77    SKIP: {
78	    skip("No tests on effective user id", 1)
79		if $skip_eff_user_tests;
80	    is(-w $tstfile, undef, "$tstfile should not be recognized as writable");
81	}
82	is(-W $tstfile, undef, "$tstfile should not be recognized as writable");
83    }
84
85    {
86	no filetest 'access';
87    SKIP: {
88	    skip("No tests on effective user id", 1)
89		if $skip_eff_user_tests;
90	    is(-w $tstfile, 1, "$tstfile should be recognized as writable");
91	}
92	is(-W $tstfile, 1, "$tstfile should be recognized as writable");
93    }
94
95    # cleanup
96    system($chflags, "nouchg", $tstfile);
97    unlink $tstfile;
98    warn "Can't remove $tstfile: $!" if -e $tstfile;
99}
100