test_netrc.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import netrc, os, unittest, sys, textwrap
  2. from test import test_support
  3. temp_filename = test_support.TESTFN
  4. class NetrcTestCase(unittest.TestCase):
  5. def make_nrc(self, test_data):
  6. test_data = textwrap.dedent(test_data)
  7. mode = 'w'
  8. if sys.platform != 'cygwin':
  9. mode += 't'
  10. with open(temp_filename, mode) as fp:
  11. fp.write(test_data)
  12. self.addCleanup(os.unlink, temp_filename)
  13. return netrc.netrc(temp_filename)
  14. def test_default(self):
  15. nrc = self.make_nrc("""\
  16. machine host1.domain.com login log1 password pass1 account acct1
  17. default login log2 password pass2
  18. """)
  19. self.assertEqual(nrc.hosts['host1.domain.com'],
  20. ('log1', 'acct1', 'pass1'))
  21. self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))
  22. def test_macros(self):
  23. nrc = self.make_nrc("""\
  24. macdef macro1
  25. line1
  26. line2
  27. macdef macro2
  28. line3
  29. line4
  30. """)
  31. self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'],
  32. 'macro2': ['line3\n', 'line4\n']})
  33. def _test_passwords(self, nrc, passwd):
  34. nrc = self.make_nrc(nrc)
  35. self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd))
  36. def test_password_with_leading_hash(self):
  37. self._test_passwords("""\
  38. machine host.domain.com login log password #pass account acct
  39. """, '#pass')
  40. def test_password_with_trailing_hash(self):
  41. self._test_passwords("""\
  42. machine host.domain.com login log password pass# account acct
  43. """, 'pass#')
  44. def test_password_with_internal_hash(self):
  45. self._test_passwords("""\
  46. machine host.domain.com login log password pa#ss account acct
  47. """, 'pa#ss')
  48. def _test_comment(self, nrc, passwd='pass'):
  49. nrc = self.make_nrc(nrc)
  50. self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd))
  51. self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass'))
  52. def test_comment_before_machine_line(self):
  53. self._test_comment("""\
  54. # comment
  55. machine foo.domain.com login bar password pass
  56. machine bar.domain.com login foo password pass
  57. """)
  58. def test_comment_before_machine_line_no_space(self):
  59. self._test_comment("""\
  60. #comment
  61. machine foo.domain.com login bar password pass
  62. machine bar.domain.com login foo password pass
  63. """)
  64. def test_comment_before_machine_line_hash_only(self):
  65. self._test_comment("""\
  66. #
  67. machine foo.domain.com login bar password pass
  68. machine bar.domain.com login foo password pass
  69. """)
  70. def test_comment_at_end_of_machine_line(self):
  71. self._test_comment("""\
  72. machine foo.domain.com login bar password pass # comment
  73. machine bar.domain.com login foo password pass
  74. """)
  75. def test_comment_at_end_of_machine_line_no_space(self):
  76. self._test_comment("""\
  77. machine foo.domain.com login bar password pass #comment
  78. machine bar.domain.com login foo password pass
  79. """)
  80. def test_comment_at_end_of_machine_line_pass_has_hash(self):
  81. self._test_comment("""\
  82. machine foo.domain.com login bar password #pass #comment
  83. machine bar.domain.com login foo password pass
  84. """, '#pass')
  85. @unittest.skipUnless(os.name == 'posix', 'POSIX only test')
  86. def test_security(self):
  87. # This test is incomplete since we are normally not run as root and
  88. # therefore can't test the file ownership being wrong.
  89. d = test_support.TESTFN
  90. os.mkdir(d)
  91. self.addCleanup(test_support.rmtree, d)
  92. fn = os.path.join(d, '.netrc')
  93. with open(fn, 'wt') as f:
  94. f.write("""\
  95. machine foo.domain.com login bar password pass
  96. default login foo password pass
  97. """)
  98. with test_support.EnvironmentVarGuard() as environ:
  99. environ.set('HOME', d)
  100. os.chmod(fn, 0600)
  101. nrc = netrc.netrc()
  102. self.assertEqual(nrc.hosts['foo.domain.com'],
  103. ('bar', None, 'pass'))
  104. os.chmod(fn, 0o622)
  105. self.assertRaises(netrc.NetrcParseError, netrc.netrc)
  106. def test_main():
  107. test_support.run_unittest(NetrcTestCase)
  108. if __name__ == "__main__":
  109. test_main()