2011-06-10 13:32:52 -03:00
|
|
|
import netrc, os, unittest, sys, textwrap
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2002-03-21 22:48:57 -04:00
|
|
|
|
2002-08-09 13:38:32 -03:00
|
|
|
temp_filename = test_support.TESTFN
|
2002-03-21 22:48:57 -04:00
|
|
|
|
|
|
|
class NetrcTestCase(unittest.TestCase):
|
|
|
|
|
2011-06-10 13:32:52 -03:00
|
|
|
def tearDown(self):
|
2002-03-21 22:48:57 -04:00
|
|
|
os.unlink(temp_filename)
|
|
|
|
|
2011-06-10 13:32:52 -03:00
|
|
|
def make_nrc(self, test_data):
|
|
|
|
test_data = textwrap.dedent(test_data)
|
|
|
|
mode = 'w'
|
|
|
|
if sys.platform != 'cygwin':
|
|
|
|
mode += 't'
|
|
|
|
with open(temp_filename, mode) as fp:
|
|
|
|
fp.write(test_data)
|
|
|
|
return netrc.netrc(temp_filename)
|
|
|
|
|
|
|
|
def test_default(self):
|
|
|
|
nrc = self.make_nrc("""\
|
|
|
|
machine host1.domain.com login log1 password pass1 account acct1
|
|
|
|
default login log2 password pass2
|
|
|
|
""")
|
|
|
|
self.assertEqual(nrc.hosts['host1.domain.com'],
|
|
|
|
('log1', 'acct1', 'pass1'))
|
|
|
|
self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))
|
2010-12-01 23:16:23 -04:00
|
|
|
|
|
|
|
def test_macros(self):
|
2011-06-10 13:32:52 -03:00
|
|
|
nrc = self.make_nrc("""\
|
|
|
|
macdef macro1
|
|
|
|
line1
|
|
|
|
line2
|
|
|
|
|
|
|
|
macdef macro2
|
|
|
|
line3
|
|
|
|
line4
|
|
|
|
""")
|
|
|
|
self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'],
|
|
|
|
'macro2': ['line3\n', 'line4\n']})
|
|
|
|
|
|
|
|
def _test_passwords(self, nrc, passwd):
|
|
|
|
nrc = self.make_nrc(nrc)
|
|
|
|
self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd))
|
|
|
|
|
|
|
|
def test_password_with_leading_hash(self):
|
|
|
|
self._test_passwords("""\
|
|
|
|
machine host.domain.com login log password #pass account acct
|
|
|
|
""", '#pass')
|
|
|
|
|
|
|
|
def test_password_with_trailing_hash(self):
|
|
|
|
self._test_passwords("""\
|
|
|
|
machine host.domain.com login log password pass# account acct
|
|
|
|
""", 'pass#')
|
|
|
|
|
|
|
|
def test_password_with_internal_hash(self):
|
|
|
|
self._test_passwords("""\
|
|
|
|
machine host.domain.com login log password pa#ss account acct
|
|
|
|
""", 'pa#ss')
|
|
|
|
|
|
|
|
def _test_comment(self, nrc, passwd='pass'):
|
|
|
|
nrc = self.make_nrc(nrc)
|
|
|
|
self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd))
|
|
|
|
self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass'))
|
|
|
|
|
|
|
|
def test_comment_before_machine_line(self):
|
|
|
|
self._test_comment("""\
|
|
|
|
# comment
|
|
|
|
machine foo.domain.com login bar password pass
|
|
|
|
machine bar.domain.com login foo password pass
|
|
|
|
""")
|
|
|
|
|
|
|
|
def test_comment_before_machine_line_no_space(self):
|
|
|
|
self._test_comment("""\
|
|
|
|
#comment
|
|
|
|
machine foo.domain.com login bar password pass
|
|
|
|
machine bar.domain.com login foo password pass
|
|
|
|
""")
|
|
|
|
|
|
|
|
def test_comment_before_machine_line_hash_only(self):
|
|
|
|
self._test_comment("""\
|
|
|
|
#
|
|
|
|
machine foo.domain.com login bar password pass
|
|
|
|
machine bar.domain.com login foo password pass
|
|
|
|
""")
|
|
|
|
|
|
|
|
def test_comment_at_end_of_machine_line(self):
|
|
|
|
self._test_comment("""\
|
|
|
|
machine foo.domain.com login bar password pass # comment
|
|
|
|
machine bar.domain.com login foo password pass
|
|
|
|
""")
|
|
|
|
|
|
|
|
def test_comment_at_end_of_machine_line_no_space(self):
|
|
|
|
self._test_comment("""\
|
|
|
|
machine foo.domain.com login bar password pass #comment
|
|
|
|
machine bar.domain.com login foo password pass
|
|
|
|
""")
|
|
|
|
|
|
|
|
def test_comment_at_end_of_machine_line_pass_has_hash(self):
|
|
|
|
self._test_comment("""\
|
|
|
|
machine foo.domain.com login bar password #pass #comment
|
|
|
|
machine bar.domain.com login foo password pass
|
|
|
|
""", '#pass')
|
2010-12-01 23:16:23 -04:00
|
|
|
|
2002-04-15 22:38:40 -03:00
|
|
|
|
2003-02-17 10:51:41 -04:00
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(NetrcTestCase)
|
2002-04-15 22:38:40 -03:00
|
|
|
|
2002-03-21 22:48:57 -04:00
|
|
|
if __name__ == "__main__":
|
2003-02-17 10:51:41 -04:00
|
|
|
test_main()
|