| 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 tearDown(self): | 
| Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 9 |         os.unlink(temp_filename) | 
 | 10 |  | 
| Benjamin Peterson | 43ee1a5 | 2011-06-10 11:32:52 -0500 | [diff] [blame] | 11 |     def make_nrc(self, test_data): | 
 | 12 |         test_data = textwrap.dedent(test_data) | 
 | 13 |         mode = 'w' | 
 | 14 |         if sys.platform != 'cygwin': | 
 | 15 |             mode += 't' | 
 | 16 |         with open(temp_filename, mode) as fp: | 
 | 17 |             fp.write(test_data) | 
 | 18 |         return netrc.netrc(temp_filename) | 
 | 19 |  | 
 | 20 |     def test_default(self): | 
 | 21 |         nrc = self.make_nrc("""\ | 
 | 22 |             machine host1.domain.com login log1 password pass1 account acct1 | 
 | 23 |             default login log2 password pass2 | 
 | 24 |             """) | 
 | 25 |         self.assertEqual(nrc.hosts['host1.domain.com'], | 
 | 26 |                          ('log1', 'acct1', 'pass1')) | 
 | 27 |         self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2')) | 
| R. David Murray | d75cc91 | 2010-12-02 03:16:23 +0000 | [diff] [blame] | 28 |  | 
 | 29 |     def test_macros(self): | 
| Benjamin Peterson | 43ee1a5 | 2011-06-10 11:32:52 -0500 | [diff] [blame] | 30 |         nrc = self.make_nrc("""\ | 
 | 31 |             macdef macro1 | 
 | 32 |             line1 | 
 | 33 |             line2 | 
| R. David Murray | d75cc91 | 2010-12-02 03:16:23 +0000 | [diff] [blame] | 34 |  | 
| Benjamin Peterson | 43ee1a5 | 2011-06-10 11:32:52 -0500 | [diff] [blame] | 35 |             macdef macro2 | 
 | 36 |             line3 | 
 | 37 |             line4 | 
 | 38 |             """) | 
 | 39 |         self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'], | 
 | 40 |                                       'macro2': ['line3\n', 'line4\n']}) | 
 | 41 |  | 
 | 42 |     def _test_passwords(self, nrc, passwd): | 
 | 43 |         nrc = self.make_nrc(nrc) | 
 | 44 |         self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd)) | 
 | 45 |  | 
 | 46 |     def test_password_with_leading_hash(self): | 
 | 47 |         self._test_passwords("""\ | 
 | 48 |             machine host.domain.com login log password #pass account acct | 
 | 49 |             """, '#pass') | 
 | 50 |  | 
 | 51 |     def test_password_with_trailing_hash(self): | 
 | 52 |         self._test_passwords("""\ | 
 | 53 |             machine host.domain.com login log password pass# account acct | 
 | 54 |             """, 'pass#') | 
 | 55 |  | 
 | 56 |     def test_password_with_internal_hash(self): | 
 | 57 |         self._test_passwords("""\ | 
 | 58 |             machine host.domain.com login log password pa#ss account acct | 
 | 59 |             """, 'pa#ss') | 
 | 60 |  | 
 | 61 |     def _test_comment(self, nrc, passwd='pass'): | 
 | 62 |         nrc = self.make_nrc(nrc) | 
 | 63 |         self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd)) | 
 | 64 |         self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass')) | 
 | 65 |  | 
 | 66 |     def test_comment_before_machine_line(self): | 
 | 67 |         self._test_comment("""\ | 
 | 68 |             # comment | 
 | 69 |             machine foo.domain.com login bar password pass | 
 | 70 |             machine bar.domain.com login foo password pass | 
 | 71 |             """) | 
 | 72 |  | 
 | 73 |     def test_comment_before_machine_line_no_space(self): | 
 | 74 |         self._test_comment("""\ | 
 | 75 |             #comment | 
 | 76 |             machine foo.domain.com login bar password pass | 
 | 77 |             machine bar.domain.com login foo password pass | 
 | 78 |             """) | 
 | 79 |  | 
 | 80 |     def test_comment_before_machine_line_hash_only(self): | 
 | 81 |         self._test_comment("""\ | 
 | 82 |             # | 
 | 83 |             machine foo.domain.com login bar password pass | 
 | 84 |             machine bar.domain.com login foo password pass | 
 | 85 |             """) | 
 | 86 |  | 
 | 87 |     def test_comment_at_end_of_machine_line(self): | 
 | 88 |         self._test_comment("""\ | 
 | 89 |             machine foo.domain.com login bar password pass # comment | 
 | 90 |             machine bar.domain.com login foo password pass | 
 | 91 |             """) | 
 | 92 |  | 
 | 93 |     def test_comment_at_end_of_machine_line_no_space(self): | 
 | 94 |         self._test_comment("""\ | 
 | 95 |             machine foo.domain.com login bar password pass #comment | 
 | 96 |             machine bar.domain.com login foo password pass | 
 | 97 |             """) | 
 | 98 |  | 
 | 99 |     def test_comment_at_end_of_machine_line_pass_has_hash(self): | 
 | 100 |         self._test_comment("""\ | 
 | 101 |             machine foo.domain.com login bar password #pass #comment | 
 | 102 |             machine bar.domain.com login foo password pass | 
 | 103 |             """, '#pass') | 
 | 104 |  | 
| Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 105 |  | 
| Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 106 | def test_main(): | 
 | 107 |     test_support.run_unittest(NetrcTestCase) | 
| Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 108 |  | 
| Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 109 | if __name__ == "__main__": | 
| Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 110 |     test_main() |