Home | History | Annotate | Download | only in test
      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 tearDown(self):
      9         os.unlink(temp_filename)
     10 
     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'))
     28 
     29     def test_macros(self):
     30         nrc = self.make_nrc("""\
     31             macdef macro1
     32             line1
     33             line2
     34 
     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 
    105 
    106 def test_main():
    107     test_support.run_unittest(NetrcTestCase)
    108 
    109 if __name__ == "__main__":
    110     test_main()
    111