blob: f59e5371acad3ba367d3b59999ca81f35dc5df1c [file] [log] [blame]
James Sextonb24cd052017-09-30 02:10:31 -05001import netrc, os, unittest, sys, tempfile, textwrap
Berker Peksag8d9bb112017-11-25 13:37:22 +03002from unittest import mock
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +00004
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +00005
6class NetrcTestCase(unittest.TestCase):
7
Benjamin Peterson1df0f212011-06-10 11:32:52 -05008 def make_nrc(self, test_data):
9 test_data = textwrap.dedent(test_data)
10 mode = 'w'
11 if sys.platform != 'cygwin':
12 mode += 't'
James Sextonb24cd052017-09-30 02:10:31 -050013 temp_fd, temp_filename = tempfile.mkstemp()
14 with os.fdopen(temp_fd, mode=mode) as fp:
Benjamin Peterson1df0f212011-06-10 11:32:52 -050015 fp.write(test_data)
R David Murray104aab92013-09-17 20:30:02 -040016 self.addCleanup(os.unlink, temp_filename)
Benjamin Peterson1df0f212011-06-10 11:32:52 -050017 return netrc.netrc(temp_filename)
18
19 def test_default(self):
20 nrc = self.make_nrc("""\
21 machine host1.domain.com login log1 password pass1 account acct1
22 default login log2 password pass2
23 """)
24 self.assertEqual(nrc.hosts['host1.domain.com'],
25 ('log1', 'acct1', 'pass1'))
26 self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))
R. David Murray78a1a152010-12-02 03:10:43 +000027
James Sextonb24cd052017-09-30 02:10:31 -050028 nrc2 = self.make_nrc(nrc.__repr__())
29 self.assertEqual(nrc.hosts, nrc2.hosts)
30
R. David Murray78a1a152010-12-02 03:10:43 +000031 def test_macros(self):
Benjamin Peterson1df0f212011-06-10 11:32:52 -050032 nrc = self.make_nrc("""\
33 macdef macro1
34 line1
35 line2
R. David Murray78a1a152010-12-02 03:10:43 +000036
Benjamin Peterson1df0f212011-06-10 11:32:52 -050037 macdef macro2
38 line3
39 line4
40 """)
41 self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'],
42 'macro2': ['line3\n', 'line4\n']})
43
44 def _test_passwords(self, nrc, passwd):
45 nrc = self.make_nrc(nrc)
46 self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd))
47
48 def test_password_with_leading_hash(self):
49 self._test_passwords("""\
50 machine host.domain.com login log password #pass account acct
51 """, '#pass')
52
53 def test_password_with_trailing_hash(self):
54 self._test_passwords("""\
55 machine host.domain.com login log password pass# account acct
56 """, 'pass#')
57
58 def test_password_with_internal_hash(self):
59 self._test_passwords("""\
60 machine host.domain.com login log password pa#ss account acct
61 """, 'pa#ss')
62
63 def _test_comment(self, nrc, passwd='pass'):
64 nrc = self.make_nrc(nrc)
65 self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd))
66 self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass'))
67
68 def test_comment_before_machine_line(self):
69 self._test_comment("""\
70 # comment
71 machine foo.domain.com login bar password pass
72 machine bar.domain.com login foo password pass
73 """)
74
75 def test_comment_before_machine_line_no_space(self):
76 self._test_comment("""\
77 #comment
78 machine foo.domain.com login bar password pass
79 machine bar.domain.com login foo password pass
80 """)
81
82 def test_comment_before_machine_line_hash_only(self):
83 self._test_comment("""\
84 #
85 machine foo.domain.com login bar password pass
86 machine bar.domain.com login foo password pass
87 """)
88
89 def test_comment_at_end_of_machine_line(self):
90 self._test_comment("""\
91 machine foo.domain.com login bar password pass # comment
92 machine bar.domain.com login foo password pass
93 """)
94
95 def test_comment_at_end_of_machine_line_no_space(self):
96 self._test_comment("""\
97 machine foo.domain.com login bar password pass #comment
98 machine bar.domain.com login foo password pass
99 """)
100
101 def test_comment_at_end_of_machine_line_pass_has_hash(self):
102 self._test_comment("""\
103 machine foo.domain.com login bar password #pass #comment
104 machine bar.domain.com login foo password pass
105 """, '#pass')
106
Tim Peters863ac442002-04-16 01:38:40 +0000107
R David Murray104aab92013-09-17 20:30:02 -0400108 @unittest.skipUnless(os.name == 'posix', 'POSIX only test')
109 def test_security(self):
110 # This test is incomplete since we are normally not run as root and
111 # therefore can't test the file ownership being wrong.
112 d = support.TESTFN
113 os.mkdir(d)
114 self.addCleanup(support.rmtree, d)
115 fn = os.path.join(d, '.netrc')
116 with open(fn, 'wt') as f:
117 f.write("""\
118 machine foo.domain.com login bar password pass
119 default login foo password pass
120 """)
121 with support.EnvironmentVarGuard() as environ:
122 environ.set('HOME', d)
123 os.chmod(fn, 0o600)
124 nrc = netrc.netrc()
125 self.assertEqual(nrc.hosts['foo.domain.com'],
126 ('bar', None, 'pass'))
127 os.chmod(fn, 0o622)
128 self.assertRaises(netrc.NetrcParseError, netrc.netrc)
129
Berker Peksag8d9bb112017-11-25 13:37:22 +0300130 def test_file_not_found_in_home(self):
131 d = support.TESTFN
132 os.mkdir(d)
133 self.addCleanup(support.rmtree, d)
134 with support.EnvironmentVarGuard() as environ:
135 environ.set('HOME', d)
136 self.assertRaises(FileNotFoundError, netrc.netrc)
137
138 def test_file_not_found_explicit(self):
139 self.assertRaises(FileNotFoundError, netrc.netrc,
140 file='unlikely_netrc')
141
142 def test_home_not_set(self):
143 fake_home = support.TESTFN
144 os.mkdir(fake_home)
145 self.addCleanup(support.rmtree, fake_home)
146 fake_netrc_path = os.path.join(fake_home, '.netrc')
147 with open(fake_netrc_path, 'w') as f:
148 f.write('machine foo.domain.com login bar password pass')
149 os.chmod(fake_netrc_path, 0o600)
150
151 orig_expanduser = os.path.expanduser
152 called = []
153
154 def fake_expanduser(s):
155 called.append(s)
156 with support.EnvironmentVarGuard() as environ:
157 environ.set('HOME', fake_home)
158 result = orig_expanduser(s)
159 return result
160
161 with support.swap_attr(os.path, 'expanduser', fake_expanduser):
162 nrc = netrc.netrc()
163 login, account, password = nrc.authenticators('foo.domain.com')
164 self.assertEqual(login, 'bar')
165
166 self.assertTrue(called)
167
Tim Peters863ac442002-04-16 01:38:40 +0000168
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +0000169if __name__ == "__main__":
Berker Peksag8d9bb112017-11-25 13:37:22 +0300170 unittest.main()