| 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, cleanup=True): |
| 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 |
if cleanup: |
| 16 |
self.addCleanup(os.unlink, temp_filename) |
| 17 |
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 |
| 23 |
""", cleanup=False) |
| 24 |
self.assertEqual(nrc.hosts['host1.domain.com'], |
| 25 |
('log1', 'acct1', 'pass1')) |
| 26 |
self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2')) |
| 27 |
|
| 28 |
nrc2 = self.make_nrc(nrc.__repr__(), cleanup=True) |
| 29 |
self.assertEqual(nrc.hosts, nrc2.hosts) |
| 30 |
|
| 31 |
def test_macros(self): |
| 32 |
nrc = self.make_nrc("""\ |
| 33 |
macdef macro1 |
| 34 |
line1 |
| 35 |
line2 |
| 36 |
|
| 37 |
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 |
|
| 107 |
|
| 108 |
@unittest.skipUnless(os.name == 'posix', 'POSIX only test') |
| 109 |
def test_security(self): |
| 110 |
|
| 111 |
|
| 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) |
| 129 |
|
| 130 |
def test_main(): |
| 131 |
test_support.run_unittest(NetrcTestCase) |
| 132 |
|
| 133 |
if __name__ == "__main__": |
| 134 |
test_main() |