1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport netrc, os, unittest, sys, textwrap 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptemp_filename = test_support.TESTFN 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass NetrcTestCase(unittest.TestCase): 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def tearDown(self): 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.unlink(temp_filename) 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def make_nrc(self, test_data): 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_data = textwrap.dedent(test_data) 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep mode = 'w' 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if sys.platform != 'cygwin': 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep mode += 't' 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep with open(temp_filename, mode) as fp: 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fp.write(test_data) 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return netrc.netrc(temp_filename) 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_default(self): 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep nrc = self.make_nrc("""\ 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine host1.domain.com login log1 password pass1 account acct1 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep default login log2 password pass2 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """) 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(nrc.hosts['host1.domain.com'], 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ('log1', 'acct1', 'pass1')) 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2')) 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_macros(self): 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep nrc = self.make_nrc("""\ 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep macdef macro1 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep line1 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep line2 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep macdef macro2 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep line3 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep line4 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """) 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'], 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'macro2': ['line3\n', 'line4\n']}) 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _test_passwords(self, nrc, passwd): 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep nrc = self.make_nrc(nrc) 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd)) 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_password_with_leading_hash(self): 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._test_passwords("""\ 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine host.domain.com login log password #pass account acct 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """, '#pass') 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_password_with_trailing_hash(self): 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._test_passwords("""\ 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine host.domain.com login log password pass# account acct 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """, 'pass#') 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_password_with_internal_hash(self): 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._test_passwords("""\ 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine host.domain.com login log password pa#ss account acct 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """, 'pa#ss') 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _test_comment(self, nrc, passwd='pass'): 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep nrc = self.make_nrc(nrc) 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd)) 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass')) 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_comment_before_machine_line(self): 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._test_comment("""\ 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # comment 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine foo.domain.com login bar password pass 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine bar.domain.com login foo password pass 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """) 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_comment_before_machine_line_no_space(self): 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._test_comment("""\ 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep #comment 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine foo.domain.com login bar password pass 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine bar.domain.com login foo password pass 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """) 79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_comment_before_machine_line_hash_only(self): 81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._test_comment("""\ 82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine foo.domain.com login bar password pass 84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine bar.domain.com login foo password pass 85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """) 86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_comment_at_end_of_machine_line(self): 88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._test_comment("""\ 89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine foo.domain.com login bar password pass # comment 90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine bar.domain.com login foo password pass 91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """) 92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_comment_at_end_of_machine_line_no_space(self): 94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._test_comment("""\ 95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine foo.domain.com login bar password pass #comment 96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine bar.domain.com login foo password pass 97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """) 98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_comment_at_end_of_machine_line_pass_has_hash(self): 100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._test_comment("""\ 101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine foo.domain.com login bar password #pass #comment 102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep machine bar.domain.com login foo password pass 103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """, '#pass') 104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(): 107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_support.run_unittest(NetrcTestCase) 108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__": 110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_main() 111