Just skip the readability test if running as root.
[p5sagit/p5-mst-13.2.git] / lib / Net / t / netrc.t
1 #!./perl
2
3 BEGIN {
4     if ($ENV{PERL_CORE}) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
7     }
8     if (!eval "require Socket") {
9         print "1..0 # no Socket\n"; exit 0;
10     }
11     if (ord('A') == 193 && !eval "require Convert::EBCDIC") {
12         print "1..0 # EBCDIC but no Convert::EBCDIC\n"; exit 0;
13     }
14 }
15
16 use strict;
17
18 use Cwd;
19 print "1..20\n";
20
21 # for testing _readrc
22 $ENV{HOME} = Cwd::cwd();
23
24 # avoid "used only once" warning
25 local (*CORE::GLOBAL::getpwuid, *CORE::GLOBAL::stat);
26
27 *CORE::GLOBAL::getpwuid = sub ($) {
28         ((undef) x 7, Cwd::cwd());
29 };
30
31 # for testing _readrc
32 my @stat;
33 *CORE::GLOBAL::stat = sub (*) {
34         return @stat;
35 };
36
37 # for testing _readrc
38 $INC{'FileHandle.pm'} = 1;
39
40 (my $libnet_t = __FILE__) =~ s/\w+.t$/libnet_t.pl/;
41 require $libnet_t;
42
43 # now that the tricks are out of the way...
44 eval { require Net::Netrc; };
45 ok( !$@, 'should be able to require() Net::Netrc safely' );
46 ok( exists $INC{'Net/Netrc.pm'}, 'should be able to use Net::Netrc' );
47
48 SKIP: {
49         skip('incompatible stat() handling for OS', 4), next SKIP 
50                 if ($^O =~ /os2|win32|macos|cygwin/i);
51         
52         my $warn;
53         local $SIG{__WARN__} = sub {
54                 $warn = shift;
55         };
56
57         # add write access for group/other
58         $stat[2] = 077;
59         ok( !defined(Net::Netrc::_readrc()),
60                 '_readrc() should not read world-writable file' );
61         ok( $warn =~ /^Bad permissions:/, '... and should warn about it' );
62
63         # the owner field should still not match
64         $stat[2] = 0;
65
66         if ($<) { 
67           ok( !defined(Net::Netrc::_readrc()), 
68               '_readrc() should not read file owned by someone else' ); 
69           ok( $warn =~ /^Not owner:/, '... and should warn about it' ); 
70         } else { 
71           ok(1, "Skip - testing as root") for 1..2; 
72         } 
73 }
74
75 # this field must now match, to avoid the last-tested warning
76 $stat[4] = $<;
77
78 # this curious mix of spaces and quotes tests a regex at line 79 (version 2.11)
79 FileHandle::set_lines(split(/\n/, <<LINES));
80 macdef bar
81 login   baz
82  machine "foo"
83 login   nigol "password" drowssap
84 machine foo "login"     l2
85         password p2
86 account tnuocca
87 default login "baz" password p2
88 default "login" baz password p3
89 macdef
90 LINES
91
92 # having set several lines and the uid, this should succeed
93 is( Net::Netrc::_readrc(), 1, '_readrc() should succeed now' );
94
95 # on 'foo', the login is 'nigol'
96 is( Net::Netrc->lookup('foo')->{login}, 'nigol', 
97         'lookup() should find value by host name' );
98
99 # on 'foo' with login 'l2', the password is 'p2'
100 is( Net::Netrc->lookup('foo', 'l2')->{password}, 'p2',
101         'lookup() should find value by hostname and login name' );
102
103 # the default password is 'p3', as later declarations have priority
104 is( Net::Netrc->lookup()->{password}, 'p3', 
105         'lookup() should find default value' );
106
107 # lookup() ignores the login parameter when using default data
108 is( Net::Netrc->lookup('default', 'baz')->{password}, 'p3',
109         'lookup() should ignore passed login when searching default' );
110
111 # lookup() goes to default data if hostname cannot be found in config data 
112 is( Net::Netrc->lookup('abadname')->{login}, 'baz',
113         'lookup() should use default for unknown machine name' );
114
115 # now test these accessors
116 my $instance = bless({}, 'Net::Netrc');
117 for my $accessor (qw( login account password )) {
118         is( $instance->$accessor(), undef, 
119                 "$accessor() should return undef if $accessor is not set" );
120         $instance->{$accessor} = $accessor;
121         is( $instance->$accessor(), $accessor,
122                 "$accessor() should return value when $accessor is set" );
123 }
124
125 # and the three-for-one accessor
126 is( scalar( () = $instance->lpa()), 3, 
127         'lpa() should return login, password, account');
128 is( join(' ', $instance->lpa), 'login password account', 
129         'lpa() should return appropriate values for l, p, and a' );
130
131 package FileHandle;
132
133 sub new {
134         tie *FH, 'FileHandle', @_;
135         bless \*FH, $_[0];
136 }
137
138 sub TIEHANDLE {
139         my ($class, undef, $file, $mode) = @_;
140         bless({ file => $file, mode => $mode }, $class);
141 }
142
143 my @lines;
144 sub set_lines {
145         @lines = @_;
146 }
147
148 sub READLINE {
149         shift @lines;
150 }
151
152 sub close { 1 }
153