]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/workspace_tools/host_tests/tcpecho_server_auto.py
Change .gitignore for ChibiOS
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / workspace_tools / host_tests / tcpecho_server_auto.py
1 """
2 mbed SDK
3 Copyright (c) 2011-2013 ARM Limited
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 """
17
18 import re
19 import sys
20 import uuid
21 import socket
22 from sys import stdout
23
24 class TCPEchoServerTest():
25 ECHO_SERVER_ADDRESS = ""
26 ECHO_PORT = 0
27 ECHO_LOOPs = 100
28 s = None # Socket
29
30 PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)"
31 re_detect_server_ip = re.compile(PATTERN_SERVER_IP)
32
33 def test(self, selftest):
34 result = False
35 c = selftest.mbed.serial_readline()
36 if c is None:
37 return selftest.RESULT_IO_SERIAL
38 selftest.notify(c)
39
40 m = self.re_detect_server_ip.search(c)
41 if m and len(m.groups()):
42 self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4])
43 self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method
44 selftest.notify("HOST: TCP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT))
45
46 # We assume this test fails so can't send 'error' message to server
47 try:
48 self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
49 self.s.connect((self.ECHO_SERVER_ADDRESS, self.ECHO_PORT))
50 except Exception, e:
51 self.s = None
52 selftest.notify("HOST: Socket error: %s"% e)
53 return selftest.RESULT_ERROR
54
55 print 'HOST: Sending %d echo strings...'% self.ECHO_LOOPs,
56 for i in range(0, self.ECHO_LOOPs):
57 TEST_STRING = str(uuid.uuid4())
58 try:
59 self.s.sendall(TEST_STRING)
60 data = self.s.recv(128)
61 except Exception, e:
62 self.s = None
63 selftest.notify("HOST: Socket error: %s"% e)
64 return selftest.RESULT_ERROR
65
66 received_str = repr(data)[1:-1]
67 if TEST_STRING == received_str: # We need to cut not needed single quotes from the string
68 sys.stdout.write('.')
69 stdout.flush()
70 result = True
71 else:
72 print "Expected: "
73 print "'%s'"% TEST_STRING
74 print "received: "
75 print "'%s'"% received_str
76 result = False
77 break
78
79 if self.s is not None:
80 self.s.close()
81 else:
82 selftest.notify("HOST: TCP Server not found")
83 result = False
84 return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE
Imprint / Impressum