<commit_before>/*
 * 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 "UDPSocket.h"
#include "EventFlags.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_RX = 0x1;
static const int SIGNAL_SIGIO_TX = 0x2;
static const int SIGIO_TIMEOUT = 5000; //[ms]
static const int WAIT2RECV_TIMEOUT = 2000; //[ms]
static const int RETRIES = 2;

static const double EXPECTED_LOSS_RATIO = 0.0;
static const double TOLERATED_LOSS_RATIO = 0.3;

UDPSocket sock;
Semaphore tx_sem(0, 1);
EventFlags signals;

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
                                   };
Timer tc_exec_time;
int time_allotted;
}

static void _sigio_handler()
{
    signals.set(SIGNAL_SIGIO_RX | SIGNAL_SIGIO_TX);
}

void UDPSOCKET_ECHOTEST()
{
    SocketAddress udp_addr;
    NetworkInterface::get_default_instance()->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(NetworkInterface::get_default_instance()));

    int recvd;
    int sent;
    int packets_sent = 0;
    int packets_recv = 0;
    for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); ++s_idx) {
        int pkt_s = pkt_sizes[s_idx];

        fill_tx_buffer_ascii(tx_buffer, BUFF_SIZE);
        int packets_sent_prev = packets_sent;

        for (int retry_cnt = 0; retry_cnt <= 2; retry_cnt++) {
            memset(rx_buffer, 0, BUFF_SIZE);
            sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
            if (check_oversized_packets(sent, pkt_s)) {
                TEST_IGNORE_MESSAGE("This device does not handle oversized packets");
            } else if (sent == pkt_s) {
                packets_sent++;
            } else {
                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;
            } else {
                printf("[Round#%02d - Receiver] error, returned %d\n", s_idx, recvd);
            }
        }
        if (memcmp(tx_buffer, rx_buffer, pkt_s) == 0) {
            packets_recv++;
        }
        // Make sure that at least one packet of every size was sent.
        TEST_ASSERT_TRUE(packets_sent > packets_sent_prev);
    }

    // Packet loss up to 30% tolerated
    if (packets_sent > 0) {
        double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
        printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
        TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
    }
    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) {
            if (tc_exec_time.read() >= time_allotted) {
                break;
            }
            signals.wait_all(SIGNAL_SIGIO_RX, WAIT2RECV_TIMEOUT);
            --retry_cnt;
            continue;
        } else if (recvd < 0) {
            printf("sock.recvfrom returned %d\n", recvd);
            TEST_FAIL();
            break;
        } else if (recvd == expt2recv) {
            break;
        }
    }

    drop_bad_packets(sock, 0); // timeout equivalent to set_blocking(false)

    tx_sem.release();
}

void UDPSOCKET_ECHOTEST_NONBLOCK()
{
    tc_exec_time.start();
    time_allotted = split2half_rmng_udp_test_time(); // [s]

    SocketAddress udp_addr;
    NetworkInterface::get_default_instance()->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(NetworkInterface::get_default_instance()));
    sock.set_blocking(false);
    sock.sigio(callback(_sigio_handler));

    int sent;
    int packets_sent = 0;
    int packets_recv = 0;
    Thread *thread;
    unsigned char *stack_mem = (unsigned char *)malloc(OS_STACK_SIZE);
    TEST_ASSERT_NOT_NULL(stack_mem);

    for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); ++s_idx) {
        int pkt_s = pkt_sizes[s_idx];
        int packets_sent_prev = packets_sent;

        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 == pkt_s) {
                packets_sent++;
            } else if (sent == NSAPI_ERROR_WOULD_BLOCK) {
                if (tc_exec_time.read() >= time_allotted ||
                        osSignalWait(SIGNAL_SIGIO_TX, SIGIO_TIMEOUT).status == osEventTimeout) {
                    continue;
                }
                --retry_cnt;
            } else {
                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;
        }
        // Make sure that at least one packet of every size was sent.
        TEST_ASSERT_TRUE(packets_sent > packets_sent_prev);
        thread->join();
        delete thread;
        if (memcmp(tx_buffer, rx_buffer, pkt_s) == 0) {
            packets_recv++;
        }
    }
    free(stack_mem);

    // Packet loss up to 30% tolerated
    if (packets_sent > 0) {
        double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
        printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
        TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
        int count = fetch_stats();
        int j = 0;
        for (; j < count; j++) {
            if ((NSAPI_UDP == udp_stats[j].proto) && (SOCK_OPEN == udp_stats[j].state)) {
                TEST_ASSERT(udp_stats[j].sent_bytes != 0);
                TEST_ASSERT(udp_stats[j].recv_bytes != 0);
                break;
            }
        }
        loss_ratio = 1 - ((double)udp_stats[j].recv_bytes / (double)udp_stats[j].sent_bytes);
        printf("Bytes sent: %d, bytes received %d, loss ratio %.2lf\r\n", udp_stats[j].sent_bytes, udp_stats[j].recv_bytes, loss_ratio);
        TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);

#endif
    }
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
    tc_exec_time.stop();
}
<commit_msg>Cellular: WISE-1570 needs greater receiving timeout to pass UDPSOCKET_ECHOTEST_NONBLOCK test<commit_after>/*
 * 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 "UDPSocket.h"
#include "EventFlags.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_RX = 0x1;
static const int SIGNAL_SIGIO_TX = 0x2;
static const int SIGIO_TIMEOUT = 5000; //[ms]
static const int WAIT2RECV_TIMEOUT = 5000; //[ms]
static const int RETRIES = 2;

static const double EXPECTED_LOSS_RATIO = 0.0;
static const double TOLERATED_LOSS_RATIO = 0.3;

UDPSocket sock;
Semaphore tx_sem(0, 1);
EventFlags signals;

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
                                   };
Timer tc_exec_time;
int time_allotted;
}

static void _sigio_handler()
{
    signals.set(SIGNAL_SIGIO_RX | SIGNAL_SIGIO_TX);
}

void UDPSOCKET_ECHOTEST()
{
    SocketAddress udp_addr;
    NetworkInterface::get_default_instance()->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(NetworkInterface::get_default_instance()));

    int recvd;
    int sent;
    int packets_sent = 0;
    int packets_recv = 0;
    for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); ++s_idx) {
        int pkt_s = pkt_sizes[s_idx];

        fill_tx_buffer_ascii(tx_buffer, BUFF_SIZE);
        int packets_sent_prev = packets_sent;

        for (int retry_cnt = 0; retry_cnt <= 2; retry_cnt++) {
            memset(rx_buffer, 0, BUFF_SIZE);
            sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
            if (check_oversized_packets(sent, pkt_s)) {
                TEST_IGNORE_MESSAGE("This device does not handle oversized packets");
            } else if (sent == pkt_s) {
                packets_sent++;
            } else {
                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;
            } else {
                printf("[Round#%02d - Receiver] error, returned %d\n", s_idx, recvd);
            }
        }
        if (memcmp(tx_buffer, rx_buffer, pkt_s) == 0) {
            packets_recv++;
        }
        // Make sure that at least one packet of every size was sent.
        TEST_ASSERT_TRUE(packets_sent > packets_sent_prev);
    }

    // Packet loss up to 30% tolerated
    if (packets_sent > 0) {
        double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
        printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
        TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
    }
    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) {
            if (tc_exec_time.read() >= time_allotted) {
                break;
            }
            signals.wait_all(SIGNAL_SIGIO_RX, WAIT2RECV_TIMEOUT);
            --retry_cnt;
            continue;
        } else if (recvd < 0) {
            printf("sock.recvfrom returned %d\n", recvd);
            TEST_FAIL();
            break;
        } else if (recvd == expt2recv) {
            break;
        }
    }

    drop_bad_packets(sock, 0); // timeout equivalent to set_blocking(false)

    tx_sem.release();
}

void UDPSOCKET_ECHOTEST_NONBLOCK()
{
    tc_exec_time.start();
    time_allotted = split2half_rmng_udp_test_time(); // [s]

    SocketAddress udp_addr;
    NetworkInterface::get_default_instance()->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(NetworkInterface::get_default_instance()));
    sock.set_blocking(false);
    sock.sigio(callback(_sigio_handler));

    int sent;
    int packets_sent = 0;
    int packets_recv = 0;
    Thread *thread;
    unsigned char *stack_mem = (unsigned char *)malloc(OS_STACK_SIZE);
    TEST_ASSERT_NOT_NULL(stack_mem);

    for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); ++s_idx) {
        int pkt_s = pkt_sizes[s_idx];
        int packets_sent_prev = packets_sent;

        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 == pkt_s) {
                packets_sent++;
            } else if (sent == NSAPI_ERROR_WOULD_BLOCK) {
                if (tc_exec_time.read() >= time_allotted ||
                        osSignalWait(SIGNAL_SIGIO_TX, SIGIO_TIMEOUT).status == osEventTimeout) {
                    continue;
                }
                --retry_cnt;
            } else {
                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;
        }
        // Make sure that at least one packet of every size was sent.
        TEST_ASSERT_TRUE(packets_sent > packets_sent_prev);
        thread->join();
        delete thread;
        if (memcmp(tx_buffer, rx_buffer, pkt_s) == 0) {
            packets_recv++;
        }
    }
    free(stack_mem);

    // Packet loss up to 30% tolerated
    if (packets_sent > 0) {
        double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
        printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
        TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
        int count = fetch_stats();
        int j = 0;
        for (; j < count; j++) {
            if ((NSAPI_UDP == udp_stats[j].proto) && (SOCK_OPEN == udp_stats[j].state)) {
                TEST_ASSERT(udp_stats[j].sent_bytes != 0);
                TEST_ASSERT(udp_stats[j].recv_bytes != 0);
                break;
            }
        }
        loss_ratio = 1 - ((double)udp_stats[j].recv_bytes / (double)udp_stats[j].sent_bytes);
        printf("Bytes sent: %d, bytes received %d, loss ratio %.2lf\r\n", udp_stats[j].sent_bytes, udp_stats[j].recv_bytes, loss_ratio);
        TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);

#endif
    }
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
    tc_exec_time.stop();
}

{'fraction_non_alphanumeric': 0.10801802357879144, 'fraction_numerical': 0.016789087093389297, 'mean_word_length': 2.5076856462437758, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4657698', 'n_tokens_mistral': 5664, 'n_tokens_neox': 5417, 'n_words': 1213}