blob: 60a3ec9565b4cf77f265dde72706a09590358e08 [file] [log] [blame]
Benjamin Peterson1df0f212011-06-10 11:32:52 -05001import netrc, os, unittest, sys, textwrap
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +00003
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004temp_filename = support.TESTFN
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'
13 with open(temp_filename, mode) as fp:
14 fp.write(test_data)
R David Murray104aab92013-09-17 20:30:02 -040015 self.addCleanup(os.unlink, temp_filename)
Benjamin Peterson1df0f212011-06-10 11:32:52 -050016 return netrc.netrc(temp_filename)
17
18 def test_default(self):
19 nrc = self.make_nrc("""\
20 machine host1.domain.com login log1 password pass1 account acct1
21 default login log2 password pass2
22 """)
23 self.assertEqual(nrc.hosts['host1.domain.com'],
24 ('log1', 'acct1', 'pass1'))
25 self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))
R. David Murray78a1a152010-12-02 03:10:43 +000026
27 def test_macros(self):
Benjamin Peterson1df0f212011-06-10 11:32:52 -050028 nrc = self.make_nrc("""\
29 macdef macro1
30 line1
31 line2
R. David Murray78a1a152010-12-02 03:10:43 +000032
Benjamin Peterson1df0f212011-06-10 11:32:52 -050033 macdef macro2
34 line3
35 line4
36 """)
37 self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'],
38 'macro2': ['line3\n', 'line4\n']})
39
40 def _test_passwords(self, nrc, passwd):
41 nrc = self.make_nrc(nrc)
42 self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd))
43
44 def test_password_with_leading_hash(self):
45 self._test_passwords("""\
46 machine host.domain.com login log password #pass account acct
47 """, '#pass')
48
49 def test_password_with_trailing_hash(self):
50 self._test_passwords("""\
51 machine host.domain.com login log password pass# account acct
52 """, 'pass#')
53
54 def test_password_with_internal_hash(self):
55 self._test_passwords("""\
56 machine host.domain.com login log password pa#ss account acct
57 """, 'pa#ss')
58
59 def _test_comment(self, nrc, passwd='pass'):
60 nrc = self.make_nrc(nrc)
61 self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd))
62 self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass'))
63
64 def test_comment_before_machine_line(self):
65 self._test_comment("""\
66 # comment
67 machine foo.domain.com login bar password pass
68 machine bar.domain.com login foo password pass
69 """)
70
71 def test_comment_before_machine_line_no_space(self):
72 self._test_comment("""\
73 #comment
74 machine foo.domain.com login bar password pass
75 machine bar.domain.com login foo password pass
76 """)
77
78 def test_comment_before_machine_line_hash_only(self):
79 self._test_comment("""\
80 #
81 machine foo.domain.com login bar password pass
82 machine bar.domain.com login foo password pass
83 """)
84
85 def test_comment_at_end_of_machine_line(self):
86 self._test_comment("""\
87 machine foo.domain.com login bar password pass # comment
88 machine bar.domain.com login foo password pass
89 """)
90
91 def test_comment_at_end_of_machine_line_no_space(self):
92 self._test_comment("""\
93 machine foo.domain.com login bar password pass #comment
94 machine bar.domain.com login foo password pass
95 """)
96
97 def test_comment_at_end_of_machine_line_pass_has_hash(self):
98 self._test_comment("""\
99 machine foo.domain.com login bar password #pass #comment
100 machine bar.domain.com login foo password pass
101 """, '#pass')
102
Tim Peters863ac442002-04-16 01:38:40 +0000103
R David Murray104aab92013-09-17 20:30:02 -0400104 @unittest.skipUnless(os.name == 'posix', 'POSIX only test')
105 def test_security(self):
106 # This test is incomplete since we are normally not run as root and
107 # therefore can't test the file ownership being wrong.
108 d = support.TESTFN
109 os.mkdir(d)
110 self.addCleanup(support.rmtree, d)
111 fn = os.path.join(d, '.netrc')
112 with open(fn, 'wt') as f:
113 f.write("""\
114 machine foo.domain.com login bar password pass
115 default login foo password pass
116 """)
117 with support.EnvironmentVarGuard() as environ:
118 environ.set('HOME', d)
119 os.chmod(fn, 0o600)
120 nrc = netrc.netrc()
121 self.assertEqual(nrc.hosts['foo.domain.com'],
122 ('bar', None, 'pass'))
123 os.chmod(fn, 0o622)
124 self.assertRaises(netrc.NetrcParseError, netrc.netrc)
125
Neal Norwitz996acf12003-02-17 14:51:41 +0000126def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000127 support.run_unittest(NetrcTestCase)
Tim Peters863ac442002-04-16 01:38:40 +0000128
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +0000129if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +0000130 test_main()