1 | n/a | import netrc, os, unittest, sys, textwrap |
---|
2 | n/a | from test import support |
---|
3 | n/a | |
---|
4 | n/a | temp_filename = support.TESTFN |
---|
5 | n/a | |
---|
6 | n/a | class NetrcTestCase(unittest.TestCase): |
---|
7 | n/a | |
---|
8 | n/a | def make_nrc(self, test_data): |
---|
9 | n/a | test_data = textwrap.dedent(test_data) |
---|
10 | n/a | mode = 'w' |
---|
11 | n/a | if sys.platform != 'cygwin': |
---|
12 | n/a | mode += 't' |
---|
13 | n/a | with open(temp_filename, mode) as fp: |
---|
14 | n/a | fp.write(test_data) |
---|
15 | n/a | self.addCleanup(os.unlink, temp_filename) |
---|
16 | n/a | return netrc.netrc(temp_filename) |
---|
17 | n/a | |
---|
18 | n/a | def test_default(self): |
---|
19 | n/a | nrc = self.make_nrc("""\ |
---|
20 | n/a | machine host1.domain.com login log1 password pass1 account acct1 |
---|
21 | n/a | default login log2 password pass2 |
---|
22 | n/a | """) |
---|
23 | n/a | self.assertEqual(nrc.hosts['host1.domain.com'], |
---|
24 | n/a | ('log1', 'acct1', 'pass1')) |
---|
25 | n/a | self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2')) |
---|
26 | n/a | |
---|
27 | n/a | def test_macros(self): |
---|
28 | n/a | nrc = self.make_nrc("""\ |
---|
29 | n/a | macdef macro1 |
---|
30 | n/a | line1 |
---|
31 | n/a | line2 |
---|
32 | n/a | |
---|
33 | n/a | macdef macro2 |
---|
34 | n/a | line3 |
---|
35 | n/a | line4 |
---|
36 | n/a | """) |
---|
37 | n/a | self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'], |
---|
38 | n/a | 'macro2': ['line3\n', 'line4\n']}) |
---|
39 | n/a | |
---|
40 | n/a | def _test_passwords(self, nrc, passwd): |
---|
41 | n/a | nrc = self.make_nrc(nrc) |
---|
42 | n/a | self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd)) |
---|
43 | n/a | |
---|
44 | n/a | def test_password_with_leading_hash(self): |
---|
45 | n/a | self._test_passwords("""\ |
---|
46 | n/a | machine host.domain.com login log password #pass account acct |
---|
47 | n/a | """, '#pass') |
---|
48 | n/a | |
---|
49 | n/a | def test_password_with_trailing_hash(self): |
---|
50 | n/a | self._test_passwords("""\ |
---|
51 | n/a | machine host.domain.com login log password pass# account acct |
---|
52 | n/a | """, 'pass#') |
---|
53 | n/a | |
---|
54 | n/a | def test_password_with_internal_hash(self): |
---|
55 | n/a | self._test_passwords("""\ |
---|
56 | n/a | machine host.domain.com login log password pa#ss account acct |
---|
57 | n/a | """, 'pa#ss') |
---|
58 | n/a | |
---|
59 | n/a | def _test_comment(self, nrc, passwd='pass'): |
---|
60 | n/a | nrc = self.make_nrc(nrc) |
---|
61 | n/a | self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd)) |
---|
62 | n/a | self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass')) |
---|
63 | n/a | |
---|
64 | n/a | def test_comment_before_machine_line(self): |
---|
65 | n/a | self._test_comment("""\ |
---|
66 | n/a | # comment |
---|
67 | n/a | machine foo.domain.com login bar password pass |
---|
68 | n/a | machine bar.domain.com login foo password pass |
---|
69 | n/a | """) |
---|
70 | n/a | |
---|
71 | n/a | def test_comment_before_machine_line_no_space(self): |
---|
72 | n/a | self._test_comment("""\ |
---|
73 | n/a | #comment |
---|
74 | n/a | machine foo.domain.com login bar password pass |
---|
75 | n/a | machine bar.domain.com login foo password pass |
---|
76 | n/a | """) |
---|
77 | n/a | |
---|
78 | n/a | def test_comment_before_machine_line_hash_only(self): |
---|
79 | n/a | self._test_comment("""\ |
---|
80 | n/a | # |
---|
81 | n/a | machine foo.domain.com login bar password pass |
---|
82 | n/a | machine bar.domain.com login foo password pass |
---|
83 | n/a | """) |
---|
84 | n/a | |
---|
85 | n/a | def test_comment_at_end_of_machine_line(self): |
---|
86 | n/a | self._test_comment("""\ |
---|
87 | n/a | machine foo.domain.com login bar password pass # comment |
---|
88 | n/a | machine bar.domain.com login foo password pass |
---|
89 | n/a | """) |
---|
90 | n/a | |
---|
91 | n/a | def test_comment_at_end_of_machine_line_no_space(self): |
---|
92 | n/a | self._test_comment("""\ |
---|
93 | n/a | machine foo.domain.com login bar password pass #comment |
---|
94 | n/a | machine bar.domain.com login foo password pass |
---|
95 | n/a | """) |
---|
96 | n/a | |
---|
97 | n/a | def test_comment_at_end_of_machine_line_pass_has_hash(self): |
---|
98 | n/a | self._test_comment("""\ |
---|
99 | n/a | machine foo.domain.com login bar password #pass #comment |
---|
100 | n/a | machine bar.domain.com login foo password pass |
---|
101 | n/a | """, '#pass') |
---|
102 | n/a | |
---|
103 | n/a | |
---|
104 | n/a | @unittest.skipUnless(os.name == 'posix', 'POSIX only test') |
---|
105 | n/a | def test_security(self): |
---|
106 | n/a | # This test is incomplete since we are normally not run as root and |
---|
107 | n/a | # therefore can't test the file ownership being wrong. |
---|
108 | n/a | d = support.TESTFN |
---|
109 | n/a | os.mkdir(d) |
---|
110 | n/a | self.addCleanup(support.rmtree, d) |
---|
111 | n/a | fn = os.path.join(d, '.netrc') |
---|
112 | n/a | with open(fn, 'wt') as f: |
---|
113 | n/a | f.write("""\ |
---|
114 | n/a | machine foo.domain.com login bar password pass |
---|
115 | n/a | default login foo password pass |
---|
116 | n/a | """) |
---|
117 | n/a | with support.EnvironmentVarGuard() as environ: |
---|
118 | n/a | environ.set('HOME', d) |
---|
119 | n/a | os.chmod(fn, 0o600) |
---|
120 | n/a | nrc = netrc.netrc() |
---|
121 | n/a | self.assertEqual(nrc.hosts['foo.domain.com'], |
---|
122 | n/a | ('bar', None, 'pass')) |
---|
123 | n/a | os.chmod(fn, 0o622) |
---|
124 | n/a | self.assertRaises(netrc.NetrcParseError, netrc.netrc) |
---|
125 | n/a | |
---|
126 | n/a | def test_main(): |
---|
127 | n/a | support.run_unittest(NetrcTestCase) |
---|
128 | n/a | |
---|
129 | n/a | if __name__ == "__main__": |
---|
130 | n/a | test_main() |
---|