mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			Adds UDP Greentea test cases
udpsocket_echotest udpsocket_echotest_nonblockpull/6665/head
							parent
							
								
									a2f5ffa752
								
							
						
					
					
						commit
						1fef1602a2
					
				| 
						 | 
					@ -55,6 +55,17 @@ static void _ifdown() {
 | 
				
			||||||
    printf("MBED: ifdown\n");
 | 
					    printf("MBED: ifdown\n");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void drop_bad_packets(UDPSocket& sock) {
 | 
				
			||||||
 | 
					    nsapi_error_t err;
 | 
				
			||||||
 | 
					    sock.set_timeout(0);
 | 
				
			||||||
 | 
					    while (true) {
 | 
				
			||||||
 | 
					        err = sock.recvfrom(NULL, 0, 0);
 | 
				
			||||||
 | 
					        if (err == NSAPI_ERROR_WOULD_BLOCK) {
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void fill_tx_buffer_ascii(char *buff, size_t len)
 | 
					void fill_tx_buffer_ascii(char *buff, size_t len)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    for (size_t i = 0; i<len; ++i) {
 | 
					    for (size_t i = 0; i<len; ++i) {
 | 
				
			||||||
| 
						 | 
					@ -76,8 +87,9 @@ void greentea_teardown(const size_t passed, const size_t failed, const failure_t
 | 
				
			||||||
    return greentea_test_teardown_handler(passed, failed, failure);
 | 
					    return greentea_test_teardown_handler(passed, failed, failure);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
Case cases[] = {
 | 
					Case cases[] = {
 | 
				
			||||||
 | 
					        Case("Echo", test_udpsocket_echotest),
 | 
				
			||||||
 | 
					        Case("Echo non-block", test_udpsocket_echotest_nonblock),
 | 
				
			||||||
        Case("Echo burst", test_udpsocket_echotest_burst),
 | 
					        Case("Echo burst", test_udpsocket_echotest_burst),
 | 
				
			||||||
        Case("Echo burst non-block", test_udpsocket_echotest_burst_nonblock),
 | 
					        Case("Echo burst non-block", test_udpsocket_echotest_burst_nonblock),
 | 
				
			||||||
        Case("Reuse a socket", test_udpsocket_open_close_repeat),
 | 
					        Case("Reuse a socket", test_udpsocket_open_close_repeat),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,11 +19,14 @@
 | 
				
			||||||
#define UDP_TESTS_H
 | 
					#define UDP_TESTS_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
NetworkInterface* get_interface();
 | 
					NetworkInterface* get_interface();
 | 
				
			||||||
 | 
					void drop_bad_packets(UDPSocket& sock);
 | 
				
			||||||
void fill_tx_buffer_ascii(char *buff, size_t len);
 | 
					void fill_tx_buffer_ascii(char *buff, size_t len);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Test cases
 | 
					 * Test cases
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					void test_udpsocket_echotest();
 | 
				
			||||||
 | 
					void test_udpsocket_echotest_nonblock();
 | 
				
			||||||
void test_udpsocket_echotest_burst();
 | 
					void test_udpsocket_echotest_burst();
 | 
				
			||||||
void test_udpsocket_echotest_burst_nonblock();
 | 
					void test_udpsocket_echotest_burst_nonblock();
 | 
				
			||||||
void test_udpsocket_open_close_repeat();
 | 
					void test_udpsocket_open_close_repeat();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,153 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (c) 2018, ARM Limited, All Rights Reserved
 | 
				
			||||||
 | 
					 * SPDX-License-Identifier: Apache-2.0
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * 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.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "mbed.h"
 | 
				
			||||||
 | 
					#include MBED_CONF_APP_HEADER_FILE
 | 
				
			||||||
 | 
					#include "UDPSocket.h"
 | 
				
			||||||
 | 
					#include "greentea-client/test_env.h"
 | 
				
			||||||
 | 
					#include "unity/unity.h"
 | 
				
			||||||
 | 
					#include "utest.h"
 | 
				
			||||||
 | 
					#include "udp_tests.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					using namespace utest::v1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    static const int SIGNAL_SIGIO = 0x1;
 | 
				
			||||||
 | 
					    static const int SIGIO_TIMEOUT = 5000; //[ms]
 | 
				
			||||||
 | 
					    static const int WAIT2RECV_TIMEOUT = 1000; //[ms]
 | 
				
			||||||
 | 
					    static const int RETRIES = 2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    UDPSocket sock;
 | 
				
			||||||
 | 
					    Semaphore tx_sem(0, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    static const int BUFF_SIZE = 1200;
 | 
				
			||||||
 | 
					    char rx_buffer[BUFF_SIZE] = {0};
 | 
				
			||||||
 | 
					    char tx_buffer[BUFF_SIZE] = {0};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    static const int PKTS = 22;
 | 
				
			||||||
 | 
					    static const int pkt_sizes[PKTS] = {1,2,3,4,5,6,7,8,9,10, \
 | 
				
			||||||
 | 
					                                        100,200,300,400,500,600,700,800,900,1000,\
 | 
				
			||||||
 | 
					                                        1100,1200};
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void _sigio_handler(osThreadId id) {
 | 
				
			||||||
 | 
					    osSignalSet(id, SIGNAL_SIGIO);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void test_udpsocket_echotest()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    SocketAddress udp_addr;
 | 
				
			||||||
 | 
					    get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
 | 
				
			||||||
 | 
					    udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    UDPSocket sock;
 | 
				
			||||||
 | 
					    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int recvd;
 | 
				
			||||||
 | 
					    int sent;
 | 
				
			||||||
 | 
					    int s_idx = 0;
 | 
				
			||||||
 | 
					    for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; pkt_s = ++s_idx) {
 | 
				
			||||||
 | 
					        pkt_s = pkt_sizes[s_idx];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fill_tx_buffer_ascii(tx_buffer, BUFF_SIZE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int retry_cnt = 0; retry_cnt <= 2; retry_cnt++) {
 | 
				
			||||||
 | 
					            sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
 | 
				
			||||||
 | 
					            if (sent != pkt_s) {
 | 
				
			||||||
 | 
					                printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            recvd = sock.recvfrom(NULL, rx_buffer, pkt_s);
 | 
				
			||||||
 | 
					            if (recvd == pkt_s) {
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, pkt_s));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void udpsocket_echotest_nonblock_receiver(void *receive_bytes)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    int expt2recv = *(int*)receive_bytes;
 | 
				
			||||||
 | 
					    int recvd;
 | 
				
			||||||
 | 
					    for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) {
 | 
				
			||||||
 | 
					        recvd = sock.recvfrom(NULL, rx_buffer, expt2recv);
 | 
				
			||||||
 | 
					        if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
 | 
				
			||||||
 | 
					            wait_ms(WAIT2RECV_TIMEOUT);
 | 
				
			||||||
 | 
					            continue;
 | 
				
			||||||
 | 
					        } else if (recvd == expt2recv) {
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, expt2recv));
 | 
				
			||||||
 | 
					    drop_bad_packets(sock);
 | 
				
			||||||
 | 
					    sock.set_blocking(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    tx_sem.release();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void test_udpsocket_echotest_nonblock()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    SocketAddress udp_addr;
 | 
				
			||||||
 | 
					    get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
 | 
				
			||||||
 | 
					    udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
 | 
				
			||||||
 | 
					    sock.set_blocking(false);
 | 
				
			||||||
 | 
					    sock.sigio(callback(_sigio_handler, Thread::gettid()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int sent;
 | 
				
			||||||
 | 
					    int s_idx = 0;
 | 
				
			||||||
 | 
					    Thread *thread;
 | 
				
			||||||
 | 
					    unsigned char *stack_mem = (unsigned char *)malloc(OS_STACK_SIZE);
 | 
				
			||||||
 | 
					    TEST_ASSERT_NOT_NULL(stack_mem);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) {
 | 
				
			||||||
 | 
					        pkt_s = pkt_sizes[s_idx];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        thread = new Thread(osPriorityNormal,
 | 
				
			||||||
 | 
					                            OS_STACK_SIZE,
 | 
				
			||||||
 | 
					                            stack_mem,
 | 
				
			||||||
 | 
					                            "receiver");
 | 
				
			||||||
 | 
					        TEST_ASSERT_EQUAL(osOK, thread->start(callback(udpsocket_echotest_nonblock_receiver, &pkt_s)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) {
 | 
				
			||||||
 | 
					            fill_tx_buffer_ascii(tx_buffer, pkt_s);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
 | 
				
			||||||
 | 
					            if (sent == NSAPI_ERROR_WOULD_BLOCK) {
 | 
				
			||||||
 | 
					                if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
 | 
				
			||||||
 | 
					                    continue;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            } else if (sent != pkt_s) {
 | 
				
			||||||
 | 
					                printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (tx_sem.wait(WAIT2RECV_TIMEOUT*2) == 0) { // RX might wait up to WAIT2RECV_TIMEOUT before recvfrom
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        thread->join();
 | 
				
			||||||
 | 
					        delete thread;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    free(stack_mem);
 | 
				
			||||||
 | 
					    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -56,18 +56,6 @@ void free_tx_buffers() {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void drop_bad_packets(UDPSocket& sock) {
 | 
					 | 
				
			||||||
    nsapi_error_t err;
 | 
					 | 
				
			||||||
    sock.set_timeout(0);
 | 
					 | 
				
			||||||
    while (true) {
 | 
					 | 
				
			||||||
        err = sock.recvfrom(NULL, NULL, 0);
 | 
					 | 
				
			||||||
        if (err == NSAPI_ERROR_WOULD_BLOCK) {
 | 
					 | 
				
			||||||
            break;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void test_udpsocket_echotest_burst()
 | 
					void test_udpsocket_echotest_burst()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    SocketAddress udp_addr;
 | 
					    SocketAddress udp_addr;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue