Skip to content

lwip - Add more network tests #3265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions features/FEATURE_LWIP/TESTS/mbedmicro-net/connectivity/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#endif

#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"

#include "EthernetInterface.h"

using namespace utest::v1;


// Bringing the network up and down
template <int COUNT>
void test_bring_up_down() {
EthernetInterface eth;

for (int i = 0; i < COUNT; i++) {
int err = eth.connect();
TEST_ASSERT_EQUAL(0, err);

printf("MBED: IP Address %s\r\n", eth.get_ip_address());
printf("MBED: Netmask %s\r\n", eth.get_netmask());
printf("MBED: Gateway %s\r\n", eth.get_gateway());
TEST_ASSERT(eth.get_ip_address());
TEST_ASSERT(eth.get_netmask());
TEST_ASSERT(eth.get_gateway());

UDPSocket udp;
err = udp.open(&eth);
TEST_ASSERT_EQUAL(0, err);
err = udp.close();
TEST_ASSERT_EQUAL(0, err);

TCPSocket tcp;
err = tcp.open(&eth);
TEST_ASSERT_EQUAL(0, err);
err = tcp.close();
TEST_ASSERT_EQUAL(0, err);

err = eth.disconnect();
TEST_ASSERT_EQUAL(0, err);
}
}


// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(60, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Testing bringing the network up and down", test_bring_up_down<1>),
Case("Testing bringing the network up and down twice", test_bring_up_down<2>),
};

Specification specification(test_setup, cases);

int main() {
return !Harness::run(specification);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif
#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,18 @@ def handle(self):
Note: reason for not echoing data back after receiving {{end}} is that send
fails raising a SocketError as client closes connection.
"""
print ("HOST: TCPEchoClient_Handler: Connection received...")
while self.server.isrunning():
try:
data = self.recv()
if not data: break
except Exception as e:
print ('HOST: TCPEchoClient_Handler recv error: %s' % str(e))
break

print ('HOST: TCPEchoClient_Handler: Rx: \n%s\n' % data)

try:
# echo data back to the client
self.send(data)
except Exception as e:
print ('HOST: TCPEchoClient_Handler send error: %s' % str(e))
break
print 'Connection finished'

def recv(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def handle(self):
""" UDP packet handler. Echoes data back to sender's address.
"""
data, sock = self.request
print ('HOST: UDPEchoClientHandler: Rx: \n%s\n' % data)
sock.sendto(data, self.client_address)


Expand Down
135 changes: 135 additions & 0 deletions features/FEATURE_LWIP/TESTS/mbedmicro-net/host_tests/udp_shotgun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
"""
mbed SDK
Copyright (c) 2011-2013 ARM Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import sys
import socket
import json
import random
import itertools
from sys import stdout
from threading import Thread
from SocketServer import BaseRequestHandler, UDPServer
from mbed_host_tests import BaseHostTest, event_callback


class UDPEchoClientHandler(BaseRequestHandler):
def handle(self):
""" UDP packet handler. Responds with multiple simultaneous packets
"""
data, sock = self.request
pattern = [ord(d) << 4 for d in data]

# Each byte in request indicates size of packet to recieve
# Each packet size is shifted over by 4 to fit in a byte, which
# avoids any issues with endianess or decoding
for packet in pattern:
data = [random.randint(0, 255) for _ in range(packet-1)]
data.append(reduce(lambda a,b: a^b, data))
data = ''.join(map(chr, data))
sock.sendto(data, self.client_address)


class UDPEchoClientTest(BaseHostTest):
def __init__(self):
"""
Initialise test parameters.

:return:
"""
BaseHostTest.__init__(self)
self.SERVER_IP = None # Will be determined after knowing the target IP
self.SERVER_PORT = 0 # Let TCPServer choose an arbitrary port
self.server = None
self.server_thread = None
self.target_ip = None

@staticmethod
def find_interface_to_target_addr(target_ip):
"""
Finds IP address of the interface through which it is connected to the target.

:return:
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((target_ip, 0)) # Target IP, Any port
ip = s.getsockname()[0]
s.close()
return ip

def setup_udp_server(self):
"""
sets up a UDP server for target to connect and send test data.

:return:
"""
# !NOTE: There should mechanism to assert in the host test
if self.SERVER_IP is None:
self.log("setup_udp_server() called before determining server IP!")
self.notify_complete(False)

# Returning none will suppress host test from printing success code
self.server = UDPServer((self.SERVER_IP, self.SERVER_PORT), UDPEchoClientHandler)
ip, port = self.server.server_address
self.SERVER_PORT = port
self.server.allow_reuse_address = True
self.log("HOST: Listening for UDP packets: " + self.SERVER_IP + ":" + str(self.SERVER_PORT))
self.server_thread = Thread(target=UDPEchoClientTest.server_thread_func, args=(self,))
self.server_thread.start()

@staticmethod
def server_thread_func(this):
"""
Thread function to run TCP server forever.

:param this:
:return:
"""
this.server.serve_forever()

@event_callback("target_ip")
def _callback_target_ip(self, key, value, timestamp):
"""
Callback to handle reception of target's IP address.

:param key:
:param value:
:param timestamp:
:return:
"""
self.target_ip = value
self.SERVER_IP = self.find_interface_to_target_addr(self.target_ip)
self.setup_udp_server()

@event_callback("host_ip")
def _callback_host_ip(self, key, value, timestamp):
"""
Callback for request for host IP Addr

"""
self.send_kv("host_ip", self.SERVER_IP)

@event_callback("host_port")
def _callback_host_port(self, key, value, timestamp):
"""
Callback for request for host port
"""
self.send_kv("host_port", self.SERVER_PORT)

def teardown(self):
if self.server:
self.server.shutdown()
self.server_thread.join()

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif

#if DEVICE_EMAC
#error [NOT_SUPPORTED] Not supported for WiFi targets
#endif
Expand Down Expand Up @@ -30,7 +29,7 @@ void prep_buffer(char *tx_buffer, size_t tx_size) {
}

int main() {
GREENTEA_SETUP(20, "tcp_echo_client");
GREENTEA_SETUP(20, "tcp_echo");

EthernetInterface eth;
eth.connect();
Expand Down
Loading