Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / test / test_strerror.py
1 # Copyright (c) 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 class _MyWindowsException(OSError):
18     """
19     An exception type like L{ctypes.WinError}, but available on all platforms.
20     """
21
22
23
24 class ErrorFormatingTestCase(TestCase):
25     """
26     Tests for C{_ErrorFormatter.formatError}.
27     """
28     probeErrorCode = ECONNABORTED
29     probeMessage = "correct message value"
30
31     def test_strerrorFormatting(self):
32         """
33         L{_ErrorFormatter.formatError} should use L{os.strerror} to format
34         error messages if it is constructed without any better mechanism.
35         """
36         formatter = _ErrorFormatter(None, None, None)
37         message = formatter.formatError(self.probeErrorCode)
38         self.assertEqual(message, os.strerror(self.probeErrorCode))
39
40
41     def test_emptyErrorTab(self):
42         """
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.
46         """
47         error = 1
48         # Sanity check
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))
53
54
55     def test_errorTab(self):
56         """
57         L{_ErrorFormatter.formatError} should use C{errorTab} if it is supplied
58         and contains the requested error code.
59         """
60         formatter = _ErrorFormatter(
61             None, None, {self.probeErrorCode: self.probeMessage})
62         message = formatter.formatError(self.probeErrorCode)
63         self.assertEqual(message, self.probeMessage)
64
65
66     def test_formatMessage(self):
67         """
68         L{_ErrorFormatter.formatError} should return the return value of
69         C{formatMessage} if it is supplied.
70         """
71         formatCalls = []
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])
80
81
82     def test_winError(self):
83         """
84         L{_ErrorFormatter.formatError} should return the message argument from
85         the exception L{winError} returns, if L{winError} is supplied.
86         """
87         winCalls = []
88         def winError(errorCode):
89             winCalls.append(errorCode)
90             return _MyWindowsException(errorCode, self.probeMessage)
91         formatter = _ErrorFormatter(
92             winError,
93             lambda error: 'formatMessage: wrong message',
94             {self.probeErrorCode: 'errorTab: wrong message'})
95         message = formatter.formatError(self.probeErrorCode)
96         self.assertEqual(message, self.probeMessage)
97
98
99     def test_fromEnvironment(self):
100         """
101         L{_ErrorFormatter.fromEnvironment} should create an L{_ErrorFormatter}
102         instance with attributes populated from available modules.
103         """
104         formatter = _ErrorFormatter.fromEnvironment()
105
106         if formatter.winError is not None:
107             from ctypes import WinError
108             self.assertEqual(
109                 formatter.formatError(self.probeErrorCode),
110                 WinError(self.probeErrorCode).strerror)
111             formatter.winError = None
112
113         if formatter.formatMessage is not None:
114             from win32api import FormatMessage
115             self.assertEqual(
116                 formatter.formatError(self.probeErrorCode),
117                 FormatMessage(self.probeErrorCode))
118             formatter.formatMessage = None
119
120         if formatter.errorTab is not None:
121             from socket import errorTab
122             self.assertEqual(
123                 formatter.formatError(self.probeErrorCode),
124                 errorTab[self.probeErrorCode])
125
126     if platform.getType() != "win32":
127         test_fromEnvironment.skip = "Test will run only on Windows."
128
129
130     def test_correctLookups(self):
131         """
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}.
135         """
136         acceptable = [socket.errorTab[ECONNABORTED]]
137         try:
138             from ctypes import WinError
139             acceptable.append(WinError(ECONNABORTED).strerror)
140         except ImportError:
141             pass
142         try:
143             from win32api import FormatMessage
144             acceptable.append(FormatMessage(ECONNABORTED))
145         except ImportError:
146             pass
147
148         self.assertIn(formatError(ECONNABORTED), acceptable)
149
150     if platform.getType() != "win32":
151         test_correctLookups.skip = "Test will run only on Windows."