1
# Copyright (c) 2007 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
18
class ErrorFormatingTestCase(TestCase):
20
Tests for C{_ErrorFormatter.formatError}.
22
probeErrorCode = ECONNABORTED
23
probeMessage = "correct message value"
25
def test_strerrorFormatting(self):
27
L{_ErrorFormatter.formatError} should use L{os.strerror} to format
28
error messages if it is constructed without any better mechanism.
30
formatter = _ErrorFormatter(None, None, None)
31
message = formatter.formatError(self.probeErrorCode)
32
self.assertEqual(message, os.strerror(self.probeErrorCode))
35
def test_emptyErrorTab(self):
37
L{_ErrorFormatter.formatError} should use L{os.strerror} to format
38
error messages if it is constructed with only an error tab which does
39
not contain the error code it is called with.
43
self.assertNotEqual(self.probeErrorCode, error)
44
formatter = _ErrorFormatter(None, None, {error: 'wrong message'})
45
message = formatter.formatError(self.probeErrorCode)
46
self.assertEqual(message, os.strerror(self.probeErrorCode))
49
def test_errorTab(self):
51
L{_ErrorFormatter.formatError} should use C{errorTab} if it is supplied
52
and contains the requested error code.
54
formatter = _ErrorFormatter(
55
None, None, {self.probeErrorCode: self.probeMessage})
56
message = formatter.formatError(self.probeErrorCode)
57
self.assertEqual(message, self.probeMessage)
60
def test_formatMessage(self):
62
L{_ErrorFormatter.formatError} should return the return value of
63
C{formatMessage} if it is supplied.
66
def formatMessage(errorCode):
67
formatCalls.append(errorCode)
68
return self.probeMessage
69
formatter = _ErrorFormatter(
70
None, formatMessage, {self.probeErrorCode: 'wrong message'})
71
message = formatter.formatError(self.probeErrorCode)
72
self.assertEqual(message, self.probeMessage)
73
self.assertEqual(formatCalls, [self.probeErrorCode])
76
def test_winError(self):
78
L{_ErrorFormatter.formatError} should return the message argument from
79
the exception L{winError} returns, if L{winError} is supplied.
82
def winError(errorCode):
83
winCalls.append(errorCode)
84
return (errorCode, self.probeMessage)
85
formatter = _ErrorFormatter(
87
lambda error: 'formatMessage: wrong message',
88
{self.probeErrorCode: 'errorTab: wrong message'})
89
message = formatter.formatError(self.probeErrorCode)
90
self.assertEqual(message, self.probeMessage)
93
def test_fromEnvironment(self):
95
L{_ErrorFormatter.fromEnvironment} should create an L{_ErrorFormatter}
96
instance with attributes populated from available modules.
98
formatter = _ErrorFormatter.fromEnvironment()
100
if formatter.winError is not None:
101
from ctypes import WinError
103
formatter.formatError(self.probeErrorCode),
104
WinError(self.probeErrorCode)[1])
105
formatter.winError = None
107
if formatter.formatMessage is not None:
108
from win32api import FormatMessage
110
formatter.formatError(self.probeErrorCode),
111
FormatMessage(self.probeErrorCode))
112
formatter.formatMessage = None
114
if formatter.errorTab is not None:
115
from socket import errorTab
117
formatter.formatError(self.probeErrorCode),
118
errorTab[self.probeErrorCode])
120
if platform.getType() != "win32":
121
test_fromEnvironment.skip = "This error lookup only works on Windows"
124
def test_correctLookups(self):
126
Given an known-good errno, make sure that formatMessage gives results
127
matching either C{socket.errorTab}, C{ctypes.WinError}, or
128
C{win32api.FormatMessage}.
130
acceptable = [socket.errorTab[ECONNABORTED]]
132
from ctypes import WinError
133
acceptable.append(WinError(ECONNABORTED)[1])
137
from win32api import FormatMessage
138
acceptable.append(FormatMessage(ECONNABORTED))
142
self.assertIn(formatError(ECONNABORTED), acceptable)
144
if platform.getType() != "win32":
145
test_correctLookups.skip = "This error lookup only works on Windows"