diff --git a/TESTS/netsocket/udp/main.cpp b/TESTS/netsocket/udp/main.cpp index 0d61c53b89..77a87d8f61 100644 --- a/TESTS/netsocket/udp/main.cpp +++ b/TESTS/netsocket/udp/main.cpp @@ -83,8 +83,8 @@ Case cases[] = { Case("Reuse a socket", test_udpsocket_open_close_repeat), Case("Open at least 3 sockets", test_udpsocket_open_limit), Case("Receive in given time", test_udpsocket_recv_timeout), + Case("Detect sendto invalid params", test_udpsocket_sendto_invalid), Case("Send in given time", test_udpsocket_sendto_timeout), - }; Specification specification(greentea_setup, cases, greentea_teardown); diff --git a/TESTS/netsocket/udp/udp_tests.h b/TESTS/netsocket/udp/udp_tests.h index 3544442142..049f0d8b6c 100644 --- a/TESTS/netsocket/udp/udp_tests.h +++ b/TESTS/netsocket/udp/udp_tests.h @@ -29,6 +29,7 @@ void test_udpsocket_echotest_burst_nonblock(); void test_udpsocket_open_close_repeat(); void test_udpsocket_open_limit(); void test_udpsocket_recv_timeout(); +void test_udpsocket_sendto_invalid(); void test_udpsocket_sendto_timeout(); #endif //UDP_TESTS_H diff --git a/TESTS/netsocket/udp/udpsocket_sendto_invalid.cpp b/TESTS/netsocket/udp/udpsocket_sendto_invalid.cpp new file mode 100644 index 0000000000..283ba1eca7 --- /dev/null +++ b/TESTS/netsocket/udp/udpsocket_sendto_invalid.cpp @@ -0,0 +1,41 @@ +/* + * 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; + +void test_udpsocket_sendto_invalid() +{ + UDPSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface())); + + TEST_ASSERT(sock.sendto(NULL, 9, NULL, 0) < 0); + TEST_ASSERT(sock.sendto("", 9, NULL, 0) < 0); + TEST_ASSERT(sock.sendto("", 0, NULL, 0) < 0); + TEST_ASSERT_EQUAL(5, sock.sendto(MBED_CONF_APP_ECHO_SERVER_ADDR, 0, "hello", 5)); + TEST_ASSERT_EQUAL(0, sock.sendto(MBED_CONF_APP_ECHO_SERVER_ADDR, 9, NULL, 0)); + TEST_ASSERT_EQUAL(5, sock.sendto(MBED_CONF_APP_ECHO_SERVER_ADDR, 9, "hello", 5)); + + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/udp/udpsocket_sendto_repeat.cpp b/TESTS/netsocket/udp/udpsocket_sendto_repeat.cpp new file mode 100644 index 0000000000..b5e3b084b2 --- /dev/null +++ b/TESTS/netsocket/udp/udpsocket_sendto_repeat.cpp @@ -0,0 +1,56 @@ +/* + * 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; + +void test_udpsocket_sendto_repeat() +{ + UDPSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface())); + + SocketAddress udp_addr; + get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr); + udp_addr.set_port(9); + + int sent; + Timer timer; + int i; + static const char tx_buffer[] = {'h','e','l','l','o'}; + bool oom_earlier = false; // 2 times in a row -> time to give up + for (i = 0; i < 100; i++) { + sent = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer)); + if (sent == NSAPI_ERROR_NO_MEMORY) { + if (oom_earlier) { + break; + } + oom_earlier = true; + wait(1); + continue; + } + oom_earlier = false; + TEST_ASSERT_EQUAL(sizeof(tx_buffer), sent); + } + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +}