| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2007 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 | |
| 4 """ | |
| 5 Test strerror | |
| 6 """ | |
| 7 | |
| 8 import socket | |
| 9 import os | |
| 10 | |
| 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 | |
| 15 | |
| 16 | |
| 17 | |
| 18 class ErrorFormatingTestCase(TestCase): | |
| 19 """ | |
| 20 Tests for C{_ErrorFormatter.formatError}. | |
| 21 """ | |
| 22 probeErrorCode = ECONNABORTED | |
| 23 probeMessage = "correct message value" | |
| 24 | |
| 25 def test_strerrorFormatting(self): | |
| 26 """ | |
| 27 L{_ErrorFormatter.formatError} should use L{os.strerror} to format | |
| 28 error messages if it is constructed without any better mechanism. | |
| 29 """ | |
| 30 formatter = _ErrorFormatter(None, None, None) | |
| 31 message = formatter.formatError(self.probeErrorCode) | |
| 32 self.assertEqual(message, os.strerror(self.probeErrorCode)) | |
| 33 | |
| 34 | |
| 35 def test_emptyErrorTab(self): | |
| 36 """ | |
| 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. | |
| 40 """ | |
| 41 error = 1 | |
| 42 # Sanity check | |
| 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)) | |
| 47 | |
| 48 | |
| 49 def test_errorTab(self): | |
| 50 """ | |
| 51 L{_ErrorFormatter.formatError} should use C{errorTab} if it is supplied | |
| 52 and contains the requested error code. | |
| 53 """ | |
| 54 formatter = _ErrorFormatter( | |
| 55 None, None, {self.probeErrorCode: self.probeMessage}) | |
| 56 message = formatter.formatError(self.probeErrorCode) | |
| 57 self.assertEqual(message, self.probeMessage) | |
| 58 | |
| 59 | |
| 60 def test_formatMessage(self): | |
| 61 """ | |
| 62 L{_ErrorFormatter.formatError} should return the return value of | |
| 63 C{formatMessage} if it is supplied. | |
| 64 """ | |
| 65 formatCalls = [] | |
| 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]) | |
| 74 | |
| 75 | |
| 76 def test_winError(self): | |
| 77 """ | |
| 78 L{_ErrorFormatter.formatError} should return the message argument from | |
| 79 the exception L{winError} returns, if L{winError} is supplied. | |
| 80 """ | |
| 81 winCalls = [] | |
| 82 def winError(errorCode): | |
| 83 winCalls.append(errorCode) | |
| 84 return (errorCode, self.probeMessage) | |
| 85 formatter = _ErrorFormatter( | |
| 86 winError, | |
| 87 lambda error: 'formatMessage: wrong message', | |
| 88 {self.probeErrorCode: 'errorTab: wrong message'}) | |
| 89 message = formatter.formatError(self.probeErrorCode) | |
| 90 self.assertEqual(message, self.probeMessage) | |
| 91 | |
| 92 | |
| 93 def test_fromEnvironment(self): | |
| 94 """ | |
| 95 L{_ErrorFormatter.fromEnvironment} should create an L{_ErrorFormatter} | |
| 96 instance with attributes populated from available modules. | |
| 97 """ | |
| 98 formatter = _ErrorFormatter.fromEnvironment() | |
| 99 | |
| 100 if formatter.winError is not None: | |
| 101 from ctypes import WinError | |
| 102 self.assertEqual( | |
| 103 formatter.formatError(self.probeErrorCode), | |
| 104 WinError(self.probeErrorCode)[1]) | |
| 105 formatter.winError = None | |
| 106 | |
| 107 if formatter.formatMessage is not None: | |
| 108 from win32api import FormatMessage | |
| 109 self.assertEqual( | |
| 110 formatter.formatError(self.probeErrorCode), | |
| 111 FormatMessage(self.probeErrorCode)) | |
| 112 formatter.formatMessage = None | |
| 113 | |
| 114 if formatter.errorTab is not None: | |
| 115 from socket import errorTab | |
| 116 self.assertEqual( | |
| 117 formatter.formatError(self.probeErrorCode), | |
| 118 errorTab[self.probeErrorCode]) | |
| 119 | |
| 120 if platform.getType() != "win32": | |
| 121 test_fromEnvironment.skip = "This error lookup only works on Windows" | |
| 122 | |
| 123 | |
| 124 def test_correctLookups(self): | |
| 125 """ | |
| 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}. | |
| 129 """ | |
| 130 acceptable = [socket.errorTab[ECONNABORTED]] | |
| 131 try: | |
| 132 from ctypes import WinError | |
| 133 acceptable.append(WinError(ECONNABORTED)[1]) | |
| 134 except ImportError: | |
| 135 pass | |
| 136 try: | |
| 137 from win32api import FormatMessage | |
| 138 acceptable.append(FormatMessage(ECONNABORTED)) | |
| 139 except ImportError: | |
| 140 pass | |
| 141 | |
| 142 self.assertIn(formatError(ECONNABORTED), acceptable) | |
| 143 | |
| 144 if platform.getType() != "win32": | |
| 145 test_correctLookups.skip = "This error lookup only works on Windows" | |
| OLD | NEW |