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