Benjamin Peterson | 43ee1a5 | 2011-06-10 11:32:52 -0500 | [diff] [blame] | 1 | import netrc, os, unittest, sys, textwrap |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 2 | from test import test_support |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 4 | temp_filename = test_support.TESTFN |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 5 | |
| 6 | class NetrcTestCase(unittest.TestCase): |
| 7 | |
Benjamin Peterson | 43ee1a5 | 2011-06-10 11:32:52 -0500 | [diff] [blame] | 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) |
R David Murray | 74213e4 | 2013-09-16 14:32:54 -0400 | [diff] [blame] | 15 | self.addCleanup(os.unlink, temp_filename) |
Benjamin Peterson | 43ee1a5 | 2011-06-10 11:32:52 -0500 | [diff] [blame] | 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')) |
R. David Murray | d75cc91 | 2010-12-02 03:16:23 +0000 | [diff] [blame] | 26 | |
| 27 | def test_macros(self): |
Benjamin Peterson | 43ee1a5 | 2011-06-10 11:32:52 -0500 | [diff] [blame] | 28 | nrc = self.make_nrc("""\ |
| 29 | macdef macro1 |
| 30 | line1 |
| 31 | line2 |
R. David Murray | d75cc91 | 2010-12-02 03:16:23 +0000 | [diff] [blame] | 32 | |
Benjamin Peterson | 43ee1a5 | 2011-06-10 11:32:52 -0500 | [diff] [blame] | 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 | |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 103 | |
R David Murray | 74213e4 | 2013-09-16 14:32:54 -0400 | [diff] [blame] | 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) |
R David Murray | 4189b67 | 2013-09-16 13:48:44 -0400 | [diff] [blame] | 125 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 126 | def test_main(): |
| 127 | test_support.run_unittest(NetrcTestCase) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 128 | |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 129 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 130 | test_main() |