Skip to content

Commit 34e19e3

Browse files
committed
Added automation tests' implementation for TCP_Server, UDP_Server and STDIO
1 parent 53ce328 commit 34e19e3

File tree

7 files changed

+301
-29
lines changed

7 files changed

+301
-29
lines changed

libraries/tests/mbed/stdio/main.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,11 @@
55
* In the past we had an issue where the stdio retargeting was not linked in.
66
*/
77

8-
int main() {
9-
int Value = -1;
10-
11-
printf("\r\n\r\nGCC4MBED Test Suite\r\n");
12-
printf("Standard I/O Unit Tests\r\n");
13-
14-
printf("Test 1: printf() test\r\n");
15-
16-
printf("Test 2: scanf() test\r\n");
17-
printf(" Type number and press Enter: \n");
8+
/*
189
scanf("%d", &Value);
19-
printf("\n Your value was: %d\r\n", Value);
20-
10+
fprintf(stdout, "Test 3: fprintf(stdout, ...) test\r\n");
11+
fprintf(stderr, "Test 4: fprintf(stderr, ...) test\r\n");
12+
fscanf(stdin, "%d", &Value);
2113
fprintf(stdout, "Test 3: fprintf(stdout, ...) test\r\n");
2214
2315
fprintf(stderr, "Test 4: fprintf(stderr, ...) test\r\n");
@@ -28,4 +20,26 @@ int main() {
2820
printf("\n Your value was: %d\r\n", Value);
2921
3022
printf("Test complete\r\n");
23+
24+
*/
25+
26+
int main() {
27+
union {
28+
int value_int;
29+
};
30+
31+
while (true)
32+
{
33+
// SCANFm PRINTF family
34+
value_int = 0;
35+
scanf("%d", &value_int);
36+
printf("Your value was: %d\r\n", value_int);
37+
38+
// FSCANF, FPRINTF family
39+
value_int = 0;
40+
fscanf(stdin, "%d", &value_int);
41+
fprintf(stdout, "Your value was: %d\r\n", value_int);
42+
43+
//...
44+
}
3145
}
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
#include "mbed.h"
22
#include "EthernetInterface.h"
33

4-
#define ECHO_SERVER_PORT 7
4+
namespace {
5+
const int ECHO_SERVER_PORT = 7;
6+
const int BUFFER_SIZE = 256;
7+
}
58

69
int main (void) {
710
EthernetInterface eth;
811
eth.init(); //Use DHCP
912
eth.connect();
10-
printf("IP Address is %s\n", eth.getIPAddress());
11-
13+
printf("Server IP Address is %s:%d\n", eth.getIPAddress(), ECHO_SERVER_PORT);
14+
1215
TCPSocketServer server;
1316
server.bind(ECHO_SERVER_PORT);
1417
server.listen();
15-
18+
1619
while (true) {
17-
printf("\nWait for new connection...\n");
20+
printf("Wait for new connection...\n");
1821
TCPSocketConnection client;
1922
server.accept(client);
2023
client.set_blocking(false, 1500); // Timeout after (1.5)s
21-
2224
printf("Connection from: %s\n", client.get_address());
23-
char buffer[256];
25+
2426
while (true) {
27+
char buffer[BUFFER_SIZE] = {0};
2528
int n = client.receive(buffer, sizeof(buffer));
2629
if (n <= 0) break;
27-
30+
const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n;
31+
buffer[buffer_string_end_index] = '\0';
32+
printf("Server received: %s\n", buffer);
2833
client.send_all(buffer, n);
2934
if (n <= 0) break;
3035
}
31-
3236
client.close();
3337
}
3438
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
#include "mbed.h"
22
#include "EthernetInterface.h"
33

4-
#define ECHO_SERVER_PORT 7
4+
namespace {
5+
const int ECHO_SERVER_PORT = 7;
6+
const int BUFFER_SIZE = 256;
7+
}
58

69
int main (void) {
710
EthernetInterface eth;
811
eth.init(); //Use DHCP
912
eth.connect();
10-
printf("IP Address is %s\n", eth.getIPAddress());
13+
printf("Server IP Address is %s:%d\n", eth.getIPAddress(), ECHO_SERVER_PORT);
1114

1215
UDPSocket server;
1316
server.bind(ECHO_SERVER_PORT);
1417

1518
Endpoint client;
16-
char buffer[256];
19+
char buffer[BUFFER_SIZE] = {0};
1720
while (true) {
18-
printf("\nWait for packet...\n");
21+
printf("Wait for packet...\n");
1922
int n = server.receiveFrom(client, buffer, sizeof(buffer));
20-
21-
printf("Received packet from: %s\n", client.get_address());
22-
server.sendTo(client, buffer, n);
23+
if (n > 0) {
24+
printf("Received packet from: %s\n", client.get_address());
25+
const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n;
26+
buffer[buffer_string_end_index] = '\0';
27+
printf("Server received: %s\n", buffer);
28+
server.sendTo(client, buffer, n);
29+
}
2330
}
2431
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 random
19+
import re
20+
from host_test import Test, DefaultTest
21+
from time import time
22+
from sys import stdout
23+
24+
class StdioTest(DefaultTest):
25+
26+
pattern_int_value = "^Your value was: (-?\d+)"
27+
re_detect_int_value = re.compile(pattern_int_value)
28+
29+
def print_result(self, result):
30+
print "\n{%s}\n{end}" % result
31+
32+
def run(self):
33+
34+
test_result = True
35+
36+
for i in range(1, 5):
37+
random_integer = random.randint(-10000, 10000)
38+
print "Generated number: " + str(random_integer)
39+
self.mbed.serial.write(str(random_integer) + "\n")
40+
serial_stdio_msg = ""
41+
42+
ip_msg_timeout = self.mbed.options.timeout
43+
start_serial_pool = time();
44+
while (time() - start_serial_pool) < ip_msg_timeout:
45+
c = self.mbed.serial.read(512)
46+
stdout.write(c)
47+
stdout.flush()
48+
serial_stdio_msg += c
49+
# Searching for reply with scanned values
50+
m = self.re_detect_int_value.search(serial_stdio_msg)
51+
if m and len(m.groups()):
52+
duration = time() - start_serial_pool
53+
print "Number: " + str(m.groups()[0])
54+
test_result = test_result and (random_integer == int(m.groups()[0]))
55+
stdout.flush()
56+
break
57+
else:
58+
print "Error: No IP and port information sent from server"
59+
self.print_result('error')
60+
exit(-2)
61+
62+
if test_result: # All numbers are the same
63+
self.print_result('success')
64+
else:
65+
self.print_result('failure')
66+
stdout.flush()
67+
68+
if __name__ == '__main__':
69+
StdioTest().run()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 socket
19+
import re
20+
from host_test import Test, DefaultTest
21+
from time import time
22+
from sys import stdout
23+
24+
class TCPEchoServerTest(DefaultTest):
25+
ECHO_SERVER_ADDRESS = ""
26+
ECHO_PORT = 0
27+
s = None # Socket
28+
29+
pattern_server_ip = "^Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)"
30+
re_detect_server_ip = re.compile(pattern_server_ip)
31+
32+
def print_result(self, result):
33+
print "\n{%s}\n{end}" % result
34+
35+
def run(self):
36+
ip_msg_timeout = self.mbed.options.timeout
37+
serial_ip_msg = ""
38+
start_serial_pool = time();
39+
while (time() - start_serial_pool) < ip_msg_timeout:
40+
c = self.mbed.serial.read(512)
41+
stdout.write(c)
42+
stdout.flush()
43+
serial_ip_msg += c
44+
# Searching for IP address and port prompted by server
45+
m = self.re_detect_server_ip.search(serial_ip_msg)
46+
if m and len(m.groups()):
47+
self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4])
48+
self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method
49+
duration = time() - start_serial_pool
50+
print "TCP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT) + " after " + "%.2f" % duration + " sec"
51+
stdout.flush()
52+
break
53+
else:
54+
print "Error: No IP and port information sent from server"
55+
self.print_result('error')
56+
exit(-2)
57+
58+
# We assume this test fails so can't send 'error' message to server
59+
try:
60+
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
61+
self.s.connect((self.ECHO_SERVER_ADDRESS, self.ECHO_PORT))
62+
except Exception, e:
63+
print "Error: %s" % e
64+
self.print_result('error')
65+
exit(-1)
66+
67+
TEST_STRING = 'Hello, world !!!'
68+
self.s.sendall(TEST_STRING)
69+
70+
data = self.s.recv(1024)
71+
received_str = repr(data)[1:-1]
72+
73+
if TEST_STRING == received_str: # We need to cut not needed single quotes from the string
74+
print 'Received data: ' + received_str
75+
self.print_result('success')
76+
else:
77+
self.print_result('failure')
78+
self.s.close()
79+
80+
# Receiving
81+
try:
82+
while True:
83+
c = self.mbed.serial.read(512)
84+
stdout.write(c)
85+
stdout.flush()
86+
except KeyboardInterrupt, _:
87+
print "\n[CTRL+c] exit"
88+
89+
if __name__ == '__main__':
90+
TCPEchoServerTest().run()
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
from socket import socket, AF_INET, SOCK_DGRAM
19+
import re
20+
from host_test import Test, DefaultTest
21+
from time import time
22+
from sys import stdout
23+
24+
class UDPEchoServerTest(DefaultTest):
25+
ECHO_SERVER_ADDRESS = ""
26+
ECHO_PORT = 0
27+
s = None # Socket
28+
29+
pattern_server_ip = "^Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)"
30+
re_detect_server_ip = re.compile(pattern_server_ip)
31+
32+
def print_result(self, result):
33+
print "\n{%s}\n{end}" % result
34+
35+
def run(self):
36+
ip_msg_timeout = self.mbed.options.timeout
37+
serial_ip_msg = ""
38+
start_serial_pool = time();
39+
while (time() - start_serial_pool) < ip_msg_timeout:
40+
c = self.mbed.serial.read(512)
41+
stdout.write(c)
42+
stdout.flush()
43+
serial_ip_msg += c
44+
# Searching for IP address and port prompted by server
45+
m = self.re_detect_server_ip.search(serial_ip_msg)
46+
if m and len(m.groups()):
47+
self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4])
48+
self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method
49+
duration = time() - start_serial_pool
50+
print "UDP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT) + " after " + "%.2f" % duration + " sec"
51+
stdout.flush()
52+
break
53+
else:
54+
print "Error: No IP and port information sent from server"
55+
self.print_result('error')
56+
exit(-2)
57+
58+
# We assume this test fails so can't send 'error' message to server
59+
try:
60+
self.s = socket(AF_INET, SOCK_DGRAM)
61+
except Exception, e:
62+
print "Error: %s" % e
63+
self.print_result('error')
64+
exit(-1)
65+
66+
TEST_STRING = 'Hello, world !!!'
67+
self.s.sendto(TEST_STRING, (self.ECHO_SERVER_ADDRESS, self.ECHO_PORT))
68+
69+
data = self.s.recv(len(TEST_STRING))
70+
received_str = repr(data)[1:-1]
71+
72+
if TEST_STRING == received_str: # We need to cut not needed single quotes from the string
73+
print 'Received data: ' + received_str
74+
self.print_result('success')
75+
else:
76+
self.print_result('failure')
77+
self.s.close()
78+
79+
# Receiving
80+
try:
81+
while True:
82+
c = self.mbed.serial.read(512)
83+
stdout.write(c)
84+
stdout.flush()
85+
except KeyboardInterrupt, _:
86+
print "\n[CTRL+c] exit"
87+
88+
if __name__ == '__main__':
89+
UDPEchoServerTest().run()

workspace_tools/singletest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ def handle(self, test_spec, target_name, toolchain_name):
249249
return (test_result, target_name, toolchain_name,
250250
test_id, test_description, round(elapsed_time, 2), duration)
251251

252-
253252
def run_host_test(self, name, disk, port, duration, extra_serial=""):
254253
# print "{%s} port:%s disk:%s" % (name, port, disk),
255254
cmd = ["python", "%s.py" % name, '-p', port, '-d', disk, '-t', str(duration), "-e", extra_serial]

0 commit comments

Comments
 (0)