Skip to content

Commit 6af7b29

Browse files
authored
Merge pull request #3265 from geky/lwip-tests
lwip - Add more network tests
2 parents e7361eb + bbcf8de commit 6af7b29

File tree

16 files changed

+1690
-101
lines changed

16 files changed

+1690
-101
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#if !FEATURE_LWIP
2+
#error [NOT_SUPPORTED] LWIP not supported for this target
3+
#endif
4+
#if DEVICE_EMAC
5+
#error [NOT_SUPPORTED] Not supported for WiFi targets
6+
#endif
7+
8+
#include "mbed.h"
9+
#include "greentea-client/test_env.h"
10+
#include "unity.h"
11+
#include "utest.h"
12+
13+
#include "EthernetInterface.h"
14+
15+
using namespace utest::v1;
16+
17+
18+
// Bringing the network up and down
19+
template <int COUNT>
20+
void test_bring_up_down() {
21+
EthernetInterface eth;
22+
23+
for (int i = 0; i < COUNT; i++) {
24+
int err = eth.connect();
25+
TEST_ASSERT_EQUAL(0, err);
26+
27+
printf("MBED: IP Address %s\r\n", eth.get_ip_address());
28+
printf("MBED: Netmask %s\r\n", eth.get_netmask());
29+
printf("MBED: Gateway %s\r\n", eth.get_gateway());
30+
TEST_ASSERT(eth.get_ip_address());
31+
TEST_ASSERT(eth.get_netmask());
32+
TEST_ASSERT(eth.get_gateway());
33+
34+
UDPSocket udp;
35+
err = udp.open(&eth);
36+
TEST_ASSERT_EQUAL(0, err);
37+
err = udp.close();
38+
TEST_ASSERT_EQUAL(0, err);
39+
40+
TCPSocket tcp;
41+
err = tcp.open(&eth);
42+
TEST_ASSERT_EQUAL(0, err);
43+
err = tcp.close();
44+
TEST_ASSERT_EQUAL(0, err);
45+
46+
err = eth.disconnect();
47+
TEST_ASSERT_EQUAL(0, err);
48+
}
49+
}
50+
51+
52+
// Test setup
53+
utest::v1::status_t test_setup(const size_t number_of_cases) {
54+
GREENTEA_SETUP(60, "default_auto");
55+
return verbose_test_setup_handler(number_of_cases);
56+
}
57+
58+
Case cases[] = {
59+
Case("Testing bringing the network up and down", test_bring_up_down<1>),
60+
Case("Testing bringing the network up and down twice", test_bring_up_down<2>),
61+
};
62+
63+
Specification specification(test_setup, cases);
64+
65+
int main() {
66+
return !Harness::run(specification);
67+
}

features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#if !FEATURE_LWIP
2+
#error [NOT_SUPPORTED] LWIP not supported for this target
3+
#endif
14
#if DEVICE_EMAC
25
#error [NOT_SUPPORTED] Not supported for WiFi targets
36
#endif

features/FEATURE_LWIP/TESTS/mbedmicro-net/host_tests/tcp_echo_client.py renamed to features/FEATURE_LWIP/TESTS/mbedmicro-net/host_tests/tcp_echo.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,18 @@ def handle(self):
3333
Note: reason for not echoing data back after receiving {{end}} is that send
3434
fails raising a SocketError as client closes connection.
3535
"""
36-
print ("HOST: TCPEchoClient_Handler: Connection received...")
3736
while self.server.isrunning():
3837
try:
3938
data = self.recv()
4039
if not data: break
4140
except Exception as e:
42-
print ('HOST: TCPEchoClient_Handler recv error: %s' % str(e))
4341
break
4442

45-
print ('HOST: TCPEchoClient_Handler: Rx: \n%s\n' % data)
46-
4743
try:
4844
# echo data back to the client
4945
self.send(data)
5046
except Exception as e:
51-
print ('HOST: TCPEchoClient_Handler send error: %s' % str(e))
5247
break
53-
print 'Connection finished'
5448

5549
def recv(self):
5650
"""

features/FEATURE_LWIP/TESTS/mbedmicro-net/host_tests/udp_echo_client.py renamed to features/FEATURE_LWIP/TESTS/mbedmicro-net/host_tests/udp_echo.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def handle(self):
2828
""" UDP packet handler. Echoes data back to sender's address.
2929
"""
3030
data, sock = self.request
31-
print ('HOST: UDPEchoClientHandler: Rx: \n%s\n' % data)
3231
sock.sendto(data, self.client_address)
3332

3433

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 sys
19+
import socket
20+
import json
21+
import random
22+
import itertools
23+
from sys import stdout
24+
from threading import Thread
25+
from SocketServer import BaseRequestHandler, UDPServer
26+
from mbed_host_tests import BaseHostTest, event_callback
27+
28+
29+
class UDPEchoClientHandler(BaseRequestHandler):
30+
def handle(self):
31+
""" UDP packet handler. Responds with multiple simultaneous packets
32+
"""
33+
data, sock = self.request
34+
pattern = [ord(d) << 4 for d in data]
35+
36+
# Each byte in request indicates size of packet to recieve
37+
# Each packet size is shifted over by 4 to fit in a byte, which
38+
# avoids any issues with endianess or decoding
39+
for packet in pattern:
40+
data = [random.randint(0, 255) for _ in range(packet-1)]
41+
data.append(reduce(lambda a,b: a^b, data))
42+
data = ''.join(map(chr, data))
43+
sock.sendto(data, self.client_address)
44+
45+
46+
class UDPEchoClientTest(BaseHostTest):
47+
def __init__(self):
48+
"""
49+
Initialise test parameters.
50+
51+
:return:
52+
"""
53+
BaseHostTest.__init__(self)
54+
self.SERVER_IP = None # Will be determined after knowing the target IP
55+
self.SERVER_PORT = 0 # Let TCPServer choose an arbitrary port
56+
self.server = None
57+
self.server_thread = None
58+
self.target_ip = None
59+
60+
@staticmethod
61+
def find_interface_to_target_addr(target_ip):
62+
"""
63+
Finds IP address of the interface through which it is connected to the target.
64+
65+
:return:
66+
"""
67+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
68+
s.connect((target_ip, 0)) # Target IP, Any port
69+
ip = s.getsockname()[0]
70+
s.close()
71+
return ip
72+
73+
def setup_udp_server(self):
74+
"""
75+
sets up a UDP server for target to connect and send test data.
76+
77+
:return:
78+
"""
79+
# !NOTE: There should mechanism to assert in the host test
80+
if self.SERVER_IP is None:
81+
self.log("setup_udp_server() called before determining server IP!")
82+
self.notify_complete(False)
83+
84+
# Returning none will suppress host test from printing success code
85+
self.server = UDPServer((self.SERVER_IP, self.SERVER_PORT), UDPEchoClientHandler)
86+
ip, port = self.server.server_address
87+
self.SERVER_PORT = port
88+
self.server.allow_reuse_address = True
89+
self.log("HOST: Listening for UDP packets: " + self.SERVER_IP + ":" + str(self.SERVER_PORT))
90+
self.server_thread = Thread(target=UDPEchoClientTest.server_thread_func, args=(self,))
91+
self.server_thread.start()
92+
93+
@staticmethod
94+
def server_thread_func(this):
95+
"""
96+
Thread function to run TCP server forever.
97+
98+
:param this:
99+
:return:
100+
"""
101+
this.server.serve_forever()
102+
103+
@event_callback("target_ip")
104+
def _callback_target_ip(self, key, value, timestamp):
105+
"""
106+
Callback to handle reception of target's IP address.
107+
108+
:param key:
109+
:param value:
110+
:param timestamp:
111+
:return:
112+
"""
113+
self.target_ip = value
114+
self.SERVER_IP = self.find_interface_to_target_addr(self.target_ip)
115+
self.setup_udp_server()
116+
117+
@event_callback("host_ip")
118+
def _callback_host_ip(self, key, value, timestamp):
119+
"""
120+
Callback for request for host IP Addr
121+
122+
"""
123+
self.send_kv("host_ip", self.SERVER_IP)
124+
125+
@event_callback("host_port")
126+
def _callback_host_port(self, key, value, timestamp):
127+
"""
128+
Callback for request for host port
129+
"""
130+
self.send_kv("host_port", self.SERVER_PORT)
131+
132+
def teardown(self):
133+
if self.server:
134+
self.server.shutdown()
135+
self.server_thread.join()

features/FEATURE_LWIP/TESTS/mbedmicro-net/nist_internet_time_service/main.cpp

Lines changed: 0 additions & 79 deletions
This file was deleted.

features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_client_echo/main.cpp renamed to features/FEATURE_LWIP/TESTS/mbedmicro-net/tcp_echo/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#if !FEATURE_LWIP
22
#error [NOT_SUPPORTED] LWIP not supported for this target
33
#endif
4-
54
#if DEVICE_EMAC
65
#error [NOT_SUPPORTED] Not supported for WiFi targets
76
#endif
@@ -30,7 +29,7 @@ void prep_buffer(char *tx_buffer, size_t tx_size) {
3029
}
3130

3231
int main() {
33-
GREENTEA_SETUP(20, "tcp_echo_client");
32+
GREENTEA_SETUP(20, "tcp_echo");
3433

3534
EthernetInterface eth;
3635
eth.connect();

0 commit comments

Comments
 (0)