1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
11 from twisted.trial.unittest import TestCase
12 from twisted.internet.tcp import ECONNABORTED
13 from twisted.python.win32 import _ErrorFormatter, formatError
14 from twisted.python.runtime import platform
17 class _MyWindowsException(OSError):
19 An exception type like L{ctypes.WinError}, but available on all platforms.
24 class ErrorFormatingTestCase(TestCase):
26 Tests for C{_ErrorFormatter.formatError}.
28 probeErrorCode = ECONNABORTED
29 probeMessage = "correct message value"
31 def test_strerrorFormatting(self):
33 L{_ErrorFormatter.formatError} should use L{os.strerror} to format
34 error messages if it is constructed without any better mechanism.
36 formatter = _ErrorFormatter(None, None, None)
37 message = formatter.formatError(self.probeErrorCode)
38 self.assertEqual(message, os.strerror(self.probeErrorCode))
41 def test_emptyErrorTab(self):
43 L{_ErrorFormatter.formatError} should use L{os.strerror} to format
44 error messages if it is constructed with only an error tab which does
45 not contain the error code it is called with.
49 self.assertNotEqual(self.probeErrorCode, error)
50 formatter = _ErrorFormatter(None, None, {error: 'wrong message'})
51 message = formatter.formatError(self.probeErrorCode)
52 self.assertEqual(message, os.strerror(self.probeErrorCode))
55 def test_errorTab(self):
57 L{_ErrorFormatter.formatError} should use C{errorTab} if it is supplied
58 and contains the requested error code.
60 formatter = _ErrorFormatter(
61 None, None, {self.probeErrorCode: self.probeMessage})
62 message = formatter.formatError(self.probeErrorCode)
63 self.assertEqual(message, self.probeMessage)
66 def test_formatMessage(self):
68 L{_ErrorFormatter.formatError} should return the return value of
69 C{formatMessage} if it is supplied.
72 def formatMessage(errorCode):
73 formatCalls.append(errorCode)
74 return self.probeMessage
75 formatter = _ErrorFormatter(
76 None, formatMessage, {self.probeErrorCode: 'wrong message'})
77 message = formatter.formatError(self.probeErrorCode)
78 self.assertEqual(message, self.probeMessage)
79 self.assertEqual(formatCalls, [self.probeErrorCode])
82 def test_winError(self):
84 L{_ErrorFormatter.formatError} should return the message argument from
85 the exception L{winError} returns, if L{winError} is supplied.
88 def winError(errorCode):
89 winCalls.append(errorCode)
90 return _MyWindowsException(errorCode, self.probeMessage)
91 formatter = _ErrorFormatter(
93 lambda error: 'formatMessage: wrong message',
94 {self.probeErrorCode: 'errorTab: wrong message'})
95 message = formatter.formatError(self.probeErrorCode)
96 self.assertEqual(message, self.probeMessage)
99 def test_fromEnvironment(self):
101 L{_ErrorFormatter.fromEnvironment} should create an L{_ErrorFormatter}
102 instance with attributes populated from available modules.
104 formatter = _ErrorFormatter.fromEnvironment()
106 if formatter.winError is not None:
107 from ctypes import WinError
109 formatter.formatError(self.probeErrorCode),
110 WinError(self.probeErrorCode).strerror)
111 formatter.winError = None
113 if formatter.formatMessage is not None:
114 from win32api import FormatMessage
116 formatter.formatError(self.probeErrorCode),
117 FormatMessage(self.probeErrorCode))
118 formatter.formatMessage = None
120 if formatter.errorTab is not None:
121 from socket import errorTab
123 formatter.formatError(self.probeErrorCode),
124 errorTab[self.probeErrorCode])
126 if platform.getType() != "win32":
127 test_fromEnvironment.skip = "Test will run only on Windows."
130 def test_correctLookups(self):
132 Given an known-good errno, make sure that formatMessage gives results
133 matching either C{socket.errorTab}, C{ctypes.WinError}, or
134 C{win32api.FormatMessage}.
136 acceptable = [socket.errorTab[ECONNABORTED]]
138 from ctypes import WinError
139 acceptable.append(WinError(ECONNABORTED).strerror)
143 from win32api import FormatMessage
144 acceptable.append(FormatMessage(ECONNABORTED))
148 self.assertIn(formatError(ECONNABORTED), acceptable)
150 if platform.getType() != "win32":
151 test_correctLookups.skip = "Test will run only on Windows."