1 | import netrc, os, unittest, sys, textwrap
|
---|
2 | from test import test_support
|
---|
3 |
|
---|
4 | temp_filename = test_support.TESTFN
|
---|
5 |
|
---|
6 | class NetrcTestCase(unittest.TestCase):
|
---|
7 |
|
---|
8 | 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)
|
---|
15 | self.addCleanup(os.unlink, temp_filename)
|
---|
16 | 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'))
|
---|
26 |
|
---|
27 | def test_macros(self):
|
---|
28 | nrc = self.make_nrc("""\
|
---|
29 | macdef macro1
|
---|
30 | line1
|
---|
31 | line2
|
---|
32 |
|
---|
33 | 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 |
|
---|
103 |
|
---|
104 | @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 = test_support.TESTFN
|
---|
109 | os.mkdir(d)
|
---|
110 | self.addCleanup(test_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 test_support.EnvironmentVarGuard() as environ:
|
---|
118 | environ.set('HOME', d)
|
---|
119 | os.chmod(fn, 0600)
|
---|
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 |
|
---|
126 | def test_main():
|
---|
127 | test_support.run_unittest(NetrcTestCase)
|
---|
128 |
|
---|
129 | if __name__ == "__main__":
|
---|
130 | test_main()
|
---|