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