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