blob: e36724f54bce0d52ec2af99d554d7ffc751855c2 [file] [log] [blame]
Richard Jones64b02de2010-08-03 06:39:33 +00001"""Mock socket module used by the smtpd and smtplib tests.
2"""
3
4# imported for _GLOBAL_DEFAULT_TIMEOUT
5import socket as socket_module
6
7# Mock socket module
8_defaulttimeout = None
9_reply_data = None
10
11# This is used to queue up data to be read through socket.makefile, typically
12# *before* the socket object is even created. It is intended to handle a single
13# line which the socket will feed on recv() or makefile().
14def reply_with(line):
15 global _reply_data
16 _reply_data = line
17
18
19class MockFile:
20 """Mock file object returned by MockSocket.makefile().
21 """
22 def __init__(self, lines):
23 self.lines = lines
Georg Brandlc1143532014-01-25 09:02:18 +010024 def readline(self, limit=-1):
25 result = self.lines.pop(0) + b'\r\n'
26 if limit >= 0:
27 # Re-insert the line, removing the \r\n we added.
28 self.lines.insert(0, result[limit:-2])
29 result = result[:limit]
30 return result
Richard Jones64b02de2010-08-03 06:39:33 +000031 def close(self):
32 pass
33
34
35class MockSocket:
36 """Mock socket object used by smtpd and smtplib tests.
37 """
38 def __init__(self):
39 global _reply_data
40 self.output = []
41 self.lines = []
42 if _reply_data:
43 self.lines.append(_reply_data)
Richard Jones6a9e6bb2010-08-04 12:27:36 +000044 _reply_data = None
Richard Jones64b02de2010-08-03 06:39:33 +000045 self.conn = None
46 self.timeout = None
47
48 def queue_recv(self, line):
49 self.lines.append(line)
50
51 def recv(self, bufsize, flags=None):
52 data = self.lines.pop(0) + b'\r\n'
53 return data
54
55 def fileno(self):
56 return 0
57
58 def settimeout(self, timeout):
59 if timeout is None:
60 self.timeout = _defaulttimeout
61 else:
62 self.timeout = timeout
63
64 def gettimeout(self):
65 return self.timeout
66
67 def setsockopt(self, level, optname, value):
68 pass
69
70 def getsockopt(self, level, optname, buflen=None):
71 return 0
72
73 def bind(self, address):
74 pass
75
76 def accept(self):
77 self.conn = MockSocket()
78 return self.conn, 'c'
79
80 def getsockname(self):
81 return ('0.0.0.0', 0)
82
83 def setblocking(self, flag):
84 pass
85
86 def listen(self, backlog):
87 pass
88
89 def makefile(self, mode='r', bufsize=-1):
90 handle = MockFile(self.lines)
91 return handle
92
93 def sendall(self, buffer, flags=None):
94 self.last = data
95 self.output.append(data)
96 return len(data)
97
98 def send(self, data, flags=None):
99 self.last = data
100 self.output.append(data)
101 return len(data)
102
103 def getpeername(self):
104 return 'peer'
105
106 def close(self):
107 pass
108
109
110def socket(family=None, type=None, proto=None):
111 return MockSocket()
112
113
Senthil Kumaran3d23fd62011-07-30 10:56:50 +0800114def create_connection(address, timeout=socket_module._GLOBAL_DEFAULT_TIMEOUT,
115 source_address=None):
Richard Jones64b02de2010-08-03 06:39:33 +0000116 try:
117 int_port = int(address[1])
118 except ValueError:
119 raise error
120 ms = MockSocket()
121 if timeout is socket_module._GLOBAL_DEFAULT_TIMEOUT:
122 timeout = getdefaulttimeout()
123 ms.settimeout(timeout)
124 return ms
125
126
127def setdefaulttimeout(timeout):
128 global _defaulttimeout
129 _defaulttimeout = timeout
130
131
132def getdefaulttimeout():
133 return _defaulttimeout
134
135
136def getfqdn():
137 return ""
138
139
140def gethostname():
141 pass
142
143
144def gethostbyname(name):
145 return ""
146
147
Andrew Svetlov2ade6f22012-12-17 18:57:16 +0200148gaierror = socket_module.gaierror
149error = socket_module.error
Richard Jones64b02de2010-08-03 06:39:33 +0000150
151
152# Constants
153AF_INET = None
154SOCK_STREAM = None
155SOL_SOCKET = None
156SO_REUSEADDR = None