blob: 4d49a55cb64823a3a78efafe65a1c2f587d3314a [file] [log] [blame]
Benjamin Peterson43ee1a52011-06-10 11:32:52 -05001import netrc, os, unittest, sys, textwrap
Barry Warsaw04f357c2002-07-23 19:04:11 +00002from test import test_support
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +00003
Guido van Rossum3b0a3292002-08-09 16:38:32 +00004temp_filename = test_support.TESTFN
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +00005
6class NetrcTestCase(unittest.TestCase):
7
Steven Loria3b9173d2017-12-10 01:09:58 -05008 def make_nrc(self, test_data, cleanup=True):
Benjamin Peterson43ee1a52011-06-10 11:32:52 -05009 test_data = textwrap.dedent(test_data)
10 mode = 'w'
11 if sys.platform != 'cygwin':
12 mode += 't'
13 with open(temp_filename, mode) as fp:
14 fp.write(test_data)
Steven Loria3b9173d2017-12-10 01:09:58 -050015 if cleanup:
16 self.addCleanup(os.unlink, temp_filename)
Benjamin Peterson43ee1a52011-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
Steven Loria3b9173d2017-12-10 01:09:58 -050023 """, cleanup=False)
Benjamin Peterson43ee1a52011-06-10 11:32:52 -050024 self.assertEqual(nrc.hosts['host1.domain.com'],
25 ('log1', 'acct1', 'pass1'))
26 self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))
R. David Murrayd75cc912010-12-02 03:16:23 +000027
Steven Loria3b9173d2017-12-10 01:09:58 -050028 nrc2 = self.make_nrc(nrc.__repr__(), cleanup=True)
29 self.assertEqual(nrc.hosts, nrc2.hosts)
30
R. David Murrayd75cc912010-12-02 03:16:23 +000031 def test_macros(self):
Benjamin Peterson43ee1a52011-06-10 11:32:52 -050032 nrc = self.make_nrc("""\
33 macdef macro1
34 line1
35 line2
R. David Murrayd75cc912010-12-02 03:16:23 +000036
Benjamin Peterson43ee1a52011-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 Murray74213e42013-09-16 14:32:54 -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 = test_support.TESTFN
113 os.mkdir(d)
114 self.addCleanup(test_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 test_support.EnvironmentVarGuard() as environ:
122 environ.set('HOME', d)
123 os.chmod(fn, 0600)
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)
R David Murray4189b672013-09-16 13:48:44 -0400129
Neal Norwitz996acf12003-02-17 14:51:41 +0000130def test_main():
131 test_support.run_unittest(NetrcTestCase)
Tim Peters863ac442002-04-16 01:38:40 +0000132
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +0000133if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +0000134 test_main()