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