ยปCore Development>Code coverage>Lib/test/test_netrc.py

Python code coverage for Lib/test/test_netrc.py

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