test_strerror.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test strerror
  5. """
  6. import socket
  7. import os
  8. from twisted.trial.unittest import TestCase
  9. from twisted.internet.tcp import ECONNABORTED
  10. from twisted.python.win32 import _ErrorFormatter, formatError
  11. from twisted.python.runtime import platform
  12. class _MyWindowsException(OSError):
  13. """
  14. An exception type like L{ctypes.WinError}, but available on all platforms.
  15. """
  16. class ErrorFormatingTests(TestCase):
  17. """
  18. Tests for C{_ErrorFormatter.formatError}.
  19. """
  20. probeErrorCode = ECONNABORTED
  21. probeMessage = "correct message value"
  22. def test_strerrorFormatting(self):
  23. """
  24. L{_ErrorFormatter.formatError} should use L{os.strerror} to format
  25. error messages if it is constructed without any better mechanism.
  26. """
  27. formatter = _ErrorFormatter(None, None, None)
  28. message = formatter.formatError(self.probeErrorCode)
  29. self.assertEqual(message, os.strerror(self.probeErrorCode))
  30. def test_emptyErrorTab(self):
  31. """
  32. L{_ErrorFormatter.formatError} should use L{os.strerror} to format
  33. error messages if it is constructed with only an error tab which does
  34. not contain the error code it is called with.
  35. """
  36. error = 1
  37. # Sanity check
  38. self.assertNotEqual(self.probeErrorCode, error)
  39. formatter = _ErrorFormatter(None, None, {error: 'wrong message'})
  40. message = formatter.formatError(self.probeErrorCode)
  41. self.assertEqual(message, os.strerror(self.probeErrorCode))
  42. def test_errorTab(self):
  43. """
  44. L{_ErrorFormatter.formatError} should use C{errorTab} if it is supplied
  45. and contains the requested error code.
  46. """
  47. formatter = _ErrorFormatter(
  48. None, None, {self.probeErrorCode: self.probeMessage})
  49. message = formatter.formatError(self.probeErrorCode)
  50. self.assertEqual(message, self.probeMessage)
  51. def test_formatMessage(self):
  52. """
  53. L{_ErrorFormatter.formatError} should return the return value of
  54. C{formatMessage} if it is supplied.
  55. """
  56. formatCalls = []
  57. def formatMessage(errorCode):
  58. formatCalls.append(errorCode)
  59. return self.probeMessage
  60. formatter = _ErrorFormatter(
  61. None, formatMessage, {self.probeErrorCode: 'wrong message'})
  62. message = formatter.formatError(self.probeErrorCode)
  63. self.assertEqual(message, self.probeMessage)
  64. self.assertEqual(formatCalls, [self.probeErrorCode])
  65. def test_winError(self):
  66. """
  67. L{_ErrorFormatter.formatError} should return the message argument from
  68. the exception L{winError} returns, if L{winError} is supplied.
  69. """
  70. winCalls = []
  71. def winError(errorCode):
  72. winCalls.append(errorCode)
  73. return _MyWindowsException(errorCode, self.probeMessage)
  74. formatter = _ErrorFormatter(
  75. winError,
  76. lambda error: 'formatMessage: wrong message',
  77. {self.probeErrorCode: 'errorTab: wrong message'})
  78. message = formatter.formatError(self.probeErrorCode)
  79. self.assertEqual(message, self.probeMessage)
  80. def test_fromEnvironment(self):
  81. """
  82. L{_ErrorFormatter.fromEnvironment} should create an L{_ErrorFormatter}
  83. instance with attributes populated from available modules.
  84. """
  85. formatter = _ErrorFormatter.fromEnvironment()
  86. if formatter.winError is not None:
  87. from ctypes import WinError
  88. self.assertEqual(
  89. formatter.formatError(self.probeErrorCode),
  90. WinError(self.probeErrorCode).strerror)
  91. formatter.winError = None
  92. if formatter.formatMessage is not None:
  93. from win32api import FormatMessage
  94. self.assertEqual(
  95. formatter.formatError(self.probeErrorCode),
  96. FormatMessage(self.probeErrorCode))
  97. formatter.formatMessage = None
  98. if formatter.errorTab is not None:
  99. from socket import errorTab
  100. self.assertEqual(
  101. formatter.formatError(self.probeErrorCode),
  102. errorTab[self.probeErrorCode])
  103. if platform.getType() != "win32":
  104. test_fromEnvironment.skip = "Test will run only on Windows."
  105. def test_correctLookups(self):
  106. """
  107. Given a known-good errno, make sure that formatMessage gives results
  108. matching either C{socket.errorTab}, C{ctypes.WinError}, or
  109. C{win32api.FormatMessage}.
  110. """
  111. acceptable = [socket.errorTab[ECONNABORTED]]
  112. try:
  113. from ctypes import WinError
  114. acceptable.append(WinError(ECONNABORTED).strerror)
  115. except ImportError:
  116. pass
  117. try:
  118. from win32api import FormatMessage
  119. acceptable.append(FormatMessage(ECONNABORTED))
  120. except ImportError:
  121. pass
  122. self.assertIn(formatError(ECONNABORTED), acceptable)
  123. if platform.getType() != "win32":
  124. test_correctLookups.skip = "Test will run only on Windows."