1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
#!/usr/bin/env python
'''
PEXPECT LICENSE
This license is approved by the OSI and FSF as GPL-compatible.
http://opensource.org/licenses/isc-license.txt
Copyright (c) 2012, Noah Spurrier <noah@noah.org>
PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
'''
import pexpect
from pexpect import fdpexpect
import unittest
from . import PexpectTestCase
import multiprocessing
import os
import signal
import socket
import time
import errno
class ExpectTestCase(PexpectTestCase.PexpectTestCase):
def setUp(self):
print(self.id())
PexpectTestCase.PexpectTestCase.setUp(self)
self.host = '127.0.0.1'
self.port = 49152 + 10000
self.motd = b"""\
------------------------------------------------------------------------------
* Welcome to the SOCKET UNIT TEST code! *
------------------------------------------------------------------------------
* *
* This unit test code is our best effort at testing the ability of the *
* pexpect library to handle sockets. We need some text to test buffer size *
* handling. *
* *
* A page is 1024 bytes or 1K. 80 x 24 = 1920. So a standard terminal window *
* contains more than one page. We actually want more than a page for our *
* tests. *
* *
* This is the twelfth line, and we need 24. So we need a few more paragraphs.*
* We can keep them short and just put lines between them. *
* *
* The 80 x 24 terminal size comes from the ancient past when computers were *
* only able to display text in cuneiform writing. *
* *
* The cunieform writing system used the edge of a reed to make marks on clay *
* tablets. *
* *
* It was the forerunner of the style of handwriting used by doctors to write *
* prescriptions. Thus the name: pre (before) script (writing) ion (charged *
* particle). *
------------------------------------------------------------------------------
""".replace(b'\n', b'\n\r') + b"\r\n"
self.prompt1 = 'Press Return to continue:'
self.prompt2 = 'Rate this unit test>'
self.prompt3 = 'Press X to exit:'
self.server_up = multiprocessing.Event()
self.server_process = multiprocessing.Process(target=self.socket_server, args=(self.server_up,))
self.server_process.daemon = True
self.server_process.start()
while not self.server_up.is_set():
time.sleep(0.250)
def tearDown(self):
os.kill(self.server_process.pid, signal.SIGINT)
self.server_process.join(timeout=5.0)
PexpectTestCase.PexpectTestCase.tearDown(self)
def socket_server(self, server_up):
sock = None
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self.host, self.port))
sock.listen(5)
server_up.set()
while True:
(conn, addr) = sock.accept()
conn.send(self.motd)
conn.send(self.prompt1)
result = conn.recv(1024)
if result != '\r\n':
break
conn.send(self.prompt2)
result = conn.recv(1024)
if result != '\r\n':
break
conn.send(self.prompt3)
result = conn.recv(1024)
if result.startswith('X'):
conn.shutdown(socket.SHUT_RDWR)
conn.close()
except KeyboardInterrupt:
pass
if sock is not None:
sock.shutdown(socket.SHUT_RDWR)
sock.close()
exit(0)
def test_socket(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
session = fdpexpect.fdspawn(sock.fileno(), timeout=10)
session.expect(self.prompt1)
self.assertEqual(session.before, self.motd)
session.send('\r\n')
session.expect(self.prompt2)
session.send('\r\n')
session.expect(self.prompt3)
session.send('X\r\n')
session.expect(pexpect.EOF)
self.assertEqual(session.before, b'')
def test_not_int(self):
with self.assertRaises(pexpect.ExceptionPexpect):
session = fdpexpect.fdspawn('bogus', timeout=10)
def test_not_file_descriptor(self):
with self.assertRaises(pexpect.ExceptionPexpect):
session = fdpexpect.fdspawn(-1, timeout=10)
def test_timeout(self):
with self.assertRaises(pexpect.TIMEOUT):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
session = fdpexpect.fdspawn(sock, timeout=10)
session.expect(b'Bogus response')
def test_interrupt(self):
def socket_fn(self, timed_out):
result = 0
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
session = fdpexpect.fdspawn(sock, timeout=10)
# Get all data from server
session.read_nonblocking(size=4096)
# This read should timeout
session.read_nonblocking(size=4096)
except pexpect.TIMEOUT:
timed_out.set()
result = errno.ETIMEDOUT
exit(result)
timed_out = multiprocessing.Event()
test_proc = multiprocessing.Process(target=socket_fn, args=(self, timed_out))
test_proc.daemon = True
test_proc.start()
while not timed_out.is_set():
time.sleep(0.250)
os.kill(test_proc.pid, signal.SIGWINCH)
test_proc.join(timeout=5.0)
self.assertEqual(test_proc.exitcode, errno.ETIMEDOUT)
def test_maxread(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
session = fdpexpect.fdspawn(sock.fileno(), timeout=10)
session.maxread = 1100
session.expect(self.prompt1)
self.assertEqual(session.before, self.motd)
session.send('\r\n')
session.expect(self.prompt2)
session.send('\r\n')
session.expect(self.prompt3)
session.send('X\r\n')
session.expect(pexpect.EOF)
self.assertEqual(session.before, b'')
def test_fd_isalive (self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
session = fdpexpect.fdspawn(sock.fileno(), timeout=10)
assert session.isalive()
sock.close()
assert not session.isalive(), "Should not be alive after close()"
def test_fd_isatty (self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
session = fdpexpect.fdspawn(sock.fileno(), timeout=10)
assert not session.isatty()
session.close()
def test_fileobj(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
session = fdpexpect.fdspawn(sock, timeout=10) # Should get the fileno from the socket
session.expect('Press Return to continue:')
session.close()
assert not session.isalive()
session.close() # Smoketest - should be able to call this again
if __name__ == '__main__':
unittest.main()
suite = unittest.makeSuite(ExpectTestCase, 'test')
|