10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport netrc, os, unittest, sys, textwrap 20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom test import test_support 30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yitemp_filename = test_support.TESTFN 50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass NetrcTestCase(unittest.TestCase): 70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def tearDown(self): 90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi os.unlink(temp_filename) 100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def make_nrc(self, test_data): 120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi test_data = textwrap.dedent(test_data) 130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi mode = 'w' 140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if sys.platform != 'cygwin': 150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi mode += 't' 160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with open(temp_filename, mode) as fp: 170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi fp.write(test_data) 180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return netrc.netrc(temp_filename) 190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_default(self): 210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi nrc = self.make_nrc("""\ 220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine host1.domain.com login log1 password pass1 account acct1 230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi default login log2 password pass2 240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """) 250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(nrc.hosts['host1.domain.com'], 260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ('log1', 'acct1', 'pass1')) 270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2')) 280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_macros(self): 300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi nrc = self.make_nrc("""\ 310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi macdef macro1 320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi line1 330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi line2 340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi macdef macro2 360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi line3 370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi line4 380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """) 390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'], 400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 'macro2': ['line3\n', 'line4\n']}) 410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def _test_passwords(self, nrc, passwd): 430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi nrc = self.make_nrc(nrc) 440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd)) 450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_password_with_leading_hash(self): 470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._test_passwords("""\ 480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine host.domain.com login log password #pass account acct 490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """, '#pass') 500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_password_with_trailing_hash(self): 520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._test_passwords("""\ 530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine host.domain.com login log password pass# account acct 540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """, 'pass#') 550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_password_with_internal_hash(self): 570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._test_passwords("""\ 580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine host.domain.com login log password pa#ss account acct 590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """, 'pa#ss') 600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def _test_comment(self, nrc, passwd='pass'): 620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi nrc = self.make_nrc(nrc) 630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd)) 640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass')) 650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_comment_before_machine_line(self): 670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._test_comment("""\ 680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # comment 690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine foo.domain.com login bar password pass 700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine bar.domain.com login foo password pass 710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """) 720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_comment_before_machine_line_no_space(self): 740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._test_comment("""\ 750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi #comment 760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine foo.domain.com login bar password pass 770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine bar.domain.com login foo password pass 780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """) 790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_comment_before_machine_line_hash_only(self): 810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._test_comment("""\ 820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine foo.domain.com login bar password pass 840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine bar.domain.com login foo password pass 850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """) 860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_comment_at_end_of_machine_line(self): 880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._test_comment("""\ 890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine foo.domain.com login bar password pass # comment 900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine bar.domain.com login foo password pass 910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """) 920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_comment_at_end_of_machine_line_no_space(self): 940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._test_comment("""\ 950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine foo.domain.com login bar password pass #comment 960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine bar.domain.com login foo password pass 970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """) 980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_comment_at_end_of_machine_line_pass_has_hash(self): 1000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self._test_comment("""\ 1010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine foo.domain.com login bar password #pass #comment 1020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi machine bar.domain.com login foo password pass 1030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi """, '#pass') 1040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef test_main(): 1070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi test_support.run_unittest(NetrcTestCase) 1080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif __name__ == "__main__": 1100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi test_main() 111