diff --git a/TESTS/netsocket/nidd/main.cpp b/TESTS/netsocket/nidd/main.cpp new file mode 100644 index 0000000000..4c120c8dab --- /dev/null +++ b/TESTS/netsocket/nidd/main.cpp @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2019, 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. + */ + +#if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \ + (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE != CELLULAR) || \ + !MBED_CONF_CELLULAR_CONTROL_PLANE_OPT +#error [NOT_SUPPORTED] No network configuration found for this target. +#else + +#include "mbed.h" +#include "mbed_trace.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "utest/utest_stack_trace.h" +#include "nidd_tests.h" + +using namespace utest::v1; + +namespace { +Timer tc_bucket; // Timer to limit a test cases run time +} + +void drop_bad_packets(CellularNonIPSocket &sock, int orig_timeout) +{ + nsapi_error_t err; + sock.set_timeout(0); + while (true) { + err = sock.recv(NULL, 0); + if (err == NSAPI_ERROR_WOULD_BLOCK) { + break; + } + } + sock.set_timeout(orig_timeout); +} + +bool check_oversized_packets(nsapi_error_t error, int &size) +{ + if (error == NSAPI_ERROR_PARAMETER) { +#if MBED_CONF_QUECTEL_BG96_PROVIDE_DEFAULT + size = 100; // see BG96 driver +#else + size = 1280; // see TS 23.060 for MTU recommendations +#endif + return true; + } + return false; +} + +void fill_tx_buffer_ascii(char *buff, size_t len) +{ + for (size_t i = 0; i < len; ++i) { + buff[i] = (rand() % 43) + '0'; + } +} + +void poll_pending_messages(CellularNonIPSocket &sock, int count) +{ + uint8_t buf[100] = {0}; + sock.set_timeout(1000); + for (int i=0; i < count; i++) { + if (i == 0 || i == 2) { + (void) sock.send("", 0); // poll to clear any remaining MT messages + } + while (sock.recv(buf, sizeof(buf)) > 0) { + } + } +} + +int split2half_rmng_nidd_test_time() +{ + return (nidd_global::TESTS_TIMEOUT - tc_bucket.read()) / 2; +} + +// Test setup +utest::v1::status_t greentea_setup(const size_t number_of_cases) +{ + GREENTEA_SETUP(nidd_global::TESTS_TIMEOUT, "default_auto"); + tc_bucket.start(); + return greentea_test_setup_handler(number_of_cases); +} + +void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure) +{ + tc_bucket.stop(); + return greentea_test_teardown_handler(passed, failed, failure); +} + +utest::v1::status_t greentea_case_setup_handler_nidd(const Case *const source, const size_t index_of_case) +{ + return greentea_case_setup_handler(source, index_of_case); +} + +utest::v1::status_t greentea_case_teardown_handler_nidd(const Case *const source, const size_t passed, const size_t failed, const failure_t failure) +{ + return greentea_case_teardown_handler(source, passed, failed, failure); +} + +static void test_failure_handler(const failure_t failure) +{ + UTEST_LOG_FUNCTION(); + if (failure.location == LOCATION_TEST_SETUP || failure.location == LOCATION_TEST_TEARDOWN) { + verbose_test_failure_handler(failure); + GREENTEA_TESTSUITE_RESULT(false); + while (1) ; + } +} + +Case cases[] = { + Case("NIDDSOCKET_CONNECT", NIDDSOCKET_CONNECT), + Case("NIDDSOCKET_ECHOTEST_NONBLOCK", NIDDSOCKET_ECHOTEST_NONBLOCK), + Case("NIDDSOCKET_OPEN_CLOSE_REPEAT", NIDDSOCKET_OPEN_CLOSE_REPEAT), + Case("NIDDSOCKET_OPEN_LIMIT", NIDDSOCKET_OPEN_LIMIT), + Case("NIDDSOCKET_OPEN_DESTRUCT", NIDDSOCKET_OPEN_DESTRUCT), + Case("NIDDSOCKET_OPEN_TWICE", NIDDSOCKET_OPEN_TWICE), + Case("NIDDSOCKET_RECV_TIMEOUT", NIDDSOCKET_RECV_TIMEOUT), + Case("NIDDSOCKET_SEND_TIMEOUT", NIDDSOCKET_SEND_TIMEOUT), + Case("NIDDSOCKET_SEND_INVALID", NIDDSOCKET_SEND_INVALID), + Case("NIDDSOCKET_SEND_REPEAT", NIDDSOCKET_SEND_REPEAT), + Case("NIDDSOCKET_DISCONNECT", NIDDSOCKET_DISCONNECT), +}; + +handlers_t nidd_test_case_handlers = { + default_greentea_test_setup_handler, + greentea_test_teardown_handler, + test_failure_handler, + greentea_case_setup_handler_nidd, + greentea_case_teardown_handler_nidd, + greentea_case_failure_continue_handler +}; + +Specification specification(greentea_setup, cases, greentea_teardown, nidd_test_case_handlers); + +int main() +{ + int err = Harness::run(specification); + return !err; +} + +#endif // !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) + diff --git a/TESTS/netsocket/nidd/nidd_tests.h b/TESTS/netsocket/nidd/nidd_tests.h new file mode 100644 index 0000000000..5348330d27 --- /dev/null +++ b/TESTS/netsocket/nidd/nidd_tests.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2019, 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. + */ + +#ifndef NIDD_TESTS_H +#define NIDD_TESTS_H + +#include "CellularNonIPSocket.h" +#include "CellularLog.h" +#include "../test_params.h" + +NetworkInterface *get_interface(); +void drop_bad_packets(CellularNonIPSocket &sock, int orig_timeout); +bool check_oversized_packets(nsapi_error_t error, int &size); +void fill_tx_buffer_ascii(char *buff, size_t len); +void poll_pending_messages(CellularNonIPSocket &sock, int count); + +/** + * Single testcase might take only half of the remaining execution time + */ +int split2half_rmng_nidd_test_time(); // [s] + +namespace nidd_global { +#ifdef MBED_GREENTEA_TEST_NIDDSOCKET_TIMEOUT_S +static const int TESTS_TIMEOUT = MBED_GREENTEA_TEST_NIDDSOCKET_TIMEOUT_S; +#else +static const int TESTS_TIMEOUT = (3 * 60); +#endif +static const int SOCKET_SEND_COUNT = 4; +} + +/* + * Test cases + */ +void NIDDSOCKET_CONNECT(); +void NIDDSOCKET_DISCONNECT(); +void NIDDSOCKET_OPEN_CLOSE_REPEAT(); +void NIDDSOCKET_OPEN_LIMIT(); +void NIDDSOCKET_RECV_TIMEOUT(); +void NIDDSOCKET_SEND_TIMEOUT(); +void NIDDSOCKET_OPEN_DESTRUCT(); +void NIDDSOCKET_OPEN_TWICE(); +void NIDDSOCKET_SEND_INVALID(); +void NIDDSOCKET_ECHOTEST_NONBLOCK(); +void NIDDSOCKET_SEND_REPEAT(); + +#endif //NIDD_TESTS_H diff --git a/TESTS/netsocket/nidd/niddsocket_connect.cpp b/TESTS/netsocket/nidd/niddsocket_connect.cpp new file mode 100644 index 0000000000..1fd9af8ddf --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_connect.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019, 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 "greentea-client/test_env.h" +#include "mbed.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +void NIDDSOCKET_CONNECT() +{ + // power off modem for an initial state + CellularDevice *dev = CellularDevice::get_default_instance(); + (void) dev->shutdown(); // modem may not be powered or ready yet + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, dev->soft_power_off()); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, dev->hard_power_off()); + + CellularContext *ctx = CellularContext::get_default_nonip_instance(); + ctx->set_default_parameters(); + nsapi_error_t err = ctx->connect(); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err); +} diff --git a/TESTS/netsocket/nidd/niddsocket_disconnect.cpp b/TESTS/netsocket/nidd/niddsocket_disconnect.cpp new file mode 100644 index 0000000000..951ac21fba --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_disconnect.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019, 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 "greentea-client/test_env.h" +#include "mbed.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +void NIDDSOCKET_DISCONNECT() +{ + nsapi_error_t err = CellularContext::get_default_nonip_instance()->disconnect(); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err); +} diff --git a/TESTS/netsocket/nidd/niddsocket_echotest.cpp b/TESTS/netsocket/nidd/niddsocket_echotest.cpp new file mode 100755 index 0000000000..22f1d42feb --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_echotest.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2019, 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 "EventFlags.h" +#include "greentea-client/test_env.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +namespace { +const int SIGNAL_SIGIO_RX = 0x1; +const int SIGNAL_SIGIO_TX = 0x2; +const int SIGIO_TIMEOUT = 1000; //[ms] +const int RETRIES = 2; + +const double EXPECTED_LOSS_RATIO = 0.0; +const double TOLERATED_LOSS_RATIO = 0.5; + +CellularNonIPSocket *sock; +EventFlags signals; + +const int NIDD_BUFF_SIZE = 100; +char rx_buffer[NIDD_BUFF_SIZE] = {0}; +char tx_buffer[NIDD_BUFF_SIZE] = {0}; + +const int PKTS = 11; +const int pkt_sizes[PKTS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, NIDD_BUFF_SIZE}; +Timer tc_exec_time; +int time_allotted; +} + +static void _sigio_handler() +{ + signals.set(SIGNAL_SIGIO_RX | SIGNAL_SIGIO_TX); +} + +void NIDDSOCKET_ECHOTEST_NONBLOCK() +{ + tc_exec_time.start(); + time_allotted = split2half_rmng_nidd_test_time(); // [s] + + sock = new CellularNonIPSocket(); + if (sock == NULL) { + TEST_FAIL_MESSAGE("NIDDSocket not created"); + return; + } + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(CellularContext::get_default_nonip_instance())); + sock->set_blocking(false); + sock->sigio(callback(_sigio_handler)); + + int sent; + int packets_sent = 0; + int packets_recv = 0; + int recvd = 0; + for (unsigned 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; + for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) { + fill_tx_buffer_ascii(tx_buffer, pkt_s); + tx_buffer[pkt_s] = '\0'; + sent = sock->send(tx_buffer, pkt_s); + if (sent == pkt_s) { + packets_sent++; + } else if (sent == NSAPI_ERROR_WOULD_BLOCK) { + if (tc_exec_time.read() >= time_allotted || + signals.wait_all(SIGNAL_SIGIO_TX, SIGIO_TIMEOUT) == osFlagsErrorTimeout) { + continue; + } + --retry_cnt; + } else { + tr_warn("send %d, error %d\n", s_idx, sent); + continue; + } + + for (int retry_recv = 0; retry_recv <= RETRIES; retry_recv++) { + recvd = sock->recv(rx_buffer, pkt_s); + rx_buffer[recvd] = '\0'; + if (recvd == NSAPI_ERROR_WOULD_BLOCK) { + if (tc_exec_time.read() >= time_allotted) { + tr_warn("recv timeout (%d)", time_allotted); + break; + } + signals.wait_all(SIGNAL_SIGIO_RX, SIGIO_TIMEOUT); + --retry_recv; + continue; + } else if (recvd < 0) { + tr_warn("sock.recvfrom error %d\n", recvd); + TEST_FAIL(); + break; + } else if (recvd == pkt_s) { + break; + } + } + + if (recvd == pkt_s) { + break; + } + } + // Make sure that at least one packet of every size was sent. + TEST_ASSERT_TRUE(packets_sent > packets_sent_prev); + if (recvd != pkt_s) { + tr_warn("send/recv size %d/%d", pkt_s, recvd); + } else if (memcmp(tx_buffer, rx_buffer, pkt_s) != 0) { + tr_warn("send/recv payload mismatch"); + } else { + packets_recv++; + } + } + + // Packet loss up to 30% tolerated + if (packets_sent > 0) { + double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent); + tr_info("Packets sent: %d, packets received %d, loss ratio %.2lf", 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()); + delete sock; + tc_exec_time.stop(); +} diff --git a/TESTS/netsocket/nidd/niddsocket_open_close_repeat.cpp b/TESTS/netsocket/nidd/niddsocket_open_close_repeat.cpp new file mode 100644 index 0000000000..9ac194c6fb --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_open_close_repeat.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019, 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 "greentea-client/test_env.h" +#include "mbed.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +void NIDDSOCKET_OPEN_CLOSE_REPEAT() +{ + CellularNonIPSocket *sock = new CellularNonIPSocket(); + if (!sock) { + TEST_FAIL(); + } + + for (int i = 0; i < 10; i++) { + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(CellularContext::get_default_nonip_instance())); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close()); + } + delete sock; +} diff --git a/TESTS/netsocket/nidd/niddsocket_open_destruct.cpp b/TESTS/netsocket/nidd/niddsocket_open_destruct.cpp new file mode 100644 index 0000000000..5f5060a11c --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_open_destruct.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019, 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 "greentea-client/test_env.h" +#include "mbed.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +void NIDDSOCKET_OPEN_DESTRUCT() +{ + for (int i = 0; i < 0; i++) { + CellularNonIPSocket *sock = new CellularNonIPSocket; + if (!sock) { + TEST_FAIL(); + } + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(CellularContext::get_default_nonip_instance())); + delete sock; + } +} diff --git a/TESTS/netsocket/nidd/niddsocket_open_limit.cpp b/TESTS/netsocket/nidd/niddsocket_open_limit.cpp new file mode 100644 index 0000000000..b87834a45c --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_open_limit.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2019, 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 "greentea-client/test_env.h" +#include "mbed.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +namespace { +typedef struct CellularNonIPSocketItem { + CellularNonIPSocket *sock; + CellularNonIPSocketItem *next; +} SocketItem; +} + +void NIDDSOCKET_OPEN_LIMIT() +{ + int open_sockets[2] = {0}; + + for (int i = 0; i < 2; i++) { + CellularNonIPSocketItem *socket_list_head = NULL; + CellularNonIPSocketItem *it; + + CellularNonIPSocket *sock; + int ret; + while (true) { + sock = new CellularNonIPSocket(); + if (!sock) { + break; + } + ret = sock->open(CellularContext::get_default_nonip_instance()); + if (ret == NSAPI_ERROR_NO_MEMORY || ret == NSAPI_ERROR_NO_SOCKET) { + delete sock; + break; + } + TEST_ASSERT_EQUAL(ret, NSAPI_ERROR_OK); + + // Hopefully this doesn't interfere when trying to allocate more sockets + it = new CellularNonIPSocketItem; + if (!it) { + delete sock; + break; + } + + it->sock = sock; + // Order of items in the list doesn't matter + it->next = socket_list_head; + socket_list_head = it; + } + + if (!socket_list_head) { + break; + } + CellularNonIPSocketItem *tmp; + for (CellularNonIPSocketItem *it = socket_list_head; it;) { + ++open_sockets[i]; + tmp = it; + it = it->next; + socket_list_head = it; + delete tmp->sock; + delete tmp; + } + } + TEST_ASSERT_EQUAL(open_sockets[0], open_sockets[1]); + TEST_ASSERT(open_sockets[0] == 1); +} diff --git a/TESTS/netsocket/nidd/niddsocket_open_twice.cpp b/TESTS/netsocket/nidd/niddsocket_open_twice.cpp new file mode 100644 index 0000000000..772900021e --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_open_twice.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019, 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 "greentea-client/test_env.h" +#include "mbed.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +void NIDDSOCKET_OPEN_TWICE() +{ + CellularNonIPSocket *sock = new CellularNonIPSocket(); + if (!sock) { + TEST_FAIL(); + } + + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(CellularContext::get_default_nonip_instance())); + TEST_ASSERT_EQUAL(NSAPI_ERROR_PARAMETER, sock->open(CellularContext::get_default_nonip_instance())); + + delete sock; +} diff --git a/TESTS/netsocket/nidd/niddsocket_recv_timeout.cpp b/TESTS/netsocket/nidd/niddsocket_recv_timeout.cpp new file mode 100644 index 0000000000..6a8785851a --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_recv_timeout.cpp @@ -0,0 +1,74 @@ +/* + * 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 "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "nidd_tests.h" + +using namespace utest::v1; + +namespace { +static const int SIGNAL_SIGIO = 0x1; +static const int SIGIO_TIMEOUT = 1000; //[ms] +static const int PKT_NUM = 2; +} + +static void _sigio_handler(osThreadId id) +{ + osSignalSet(id, SIGNAL_SIGIO); +} + +void NIDDSOCKET_RECV_TIMEOUT() +{ + CellularNonIPSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(CellularContext::get_default_nonip_instance())); + sock.set_timeout(100); + sock.sigio(callback(_sigio_handler, ThisThread::get_id())); + + static const int DATA_LEN = 4; + char buff[DATA_LEN] = {0}; + int recvd; + Timer timer; + int pkt_success = 0; + for (int i = 0; i < PKT_NUM; i++) { + memset(buff, 'A', sizeof(buff)); + TEST_ASSERT_EQUAL(DATA_LEN, sock.send(buff, DATA_LEN)); + timer.reset(); + timer.start(); + recvd = sock.recv(buff, sizeof(buff)); + timer.stop(); + + if (recvd == NSAPI_ERROR_WOULD_BLOCK) { + osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT); + if (timer.read_ms() > 150) { + TEST_ASSERT(150 - timer.read_ms() < 51); + } else { + TEST_ASSERT(timer.read_ms() - 150 < 51); + } + continue; + } else if (recvd < 0) { + tr_info("[bt#%02d] network error %d\n", i, recvd); + continue; + } + TEST_ASSERT_EQUAL(DATA_LEN, recvd); + pkt_success++; + } + poll_pending_messages(sock, PKT_NUM); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/nidd/niddsocket_send_invalid.cpp b/TESTS/netsocket/nidd/niddsocket_send_invalid.cpp new file mode 100644 index 0000000000..aebb816b54 --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_send_invalid.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019, 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 "greentea-client/test_env.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +void NIDDSOCKET_SEND_INVALID() +{ + CellularNonIPSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(CellularContext::get_default_nonip_instance())); + + TEST_ASSERT_EQUAL(sock.send(NULL, 0), NSAPI_ERROR_PARAMETER); + TEST_ASSERT_EQUAL(sock.send(NULL, MAX_CP_DATA_RECV_LEN + 1), NSAPI_ERROR_PARAMETER); + + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/nidd/niddsocket_send_repeat.cpp b/TESTS/netsocket/nidd/niddsocket_send_repeat.cpp new file mode 100644 index 0000000000..9b04c38ffd --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_send_repeat.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2019, 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 "greentea-client/test_env.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +void NIDDSOCKET_SEND_REPEAT() +{ + CellularNonIPSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(CellularContext::get_default_nonip_instance())); + + 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 < nidd_global::SOCKET_SEND_COUNT; i++) { + sent = sock.send(tx_buffer, sizeof(tx_buffer)); + if (sent == NSAPI_ERROR_NO_MEMORY) { + if (oom_earlier) { + break; + } + oom_earlier = true; + ThisThread::sleep_for(1000); + continue; + } + oom_earlier = false; + TEST_ASSERT_EQUAL(sizeof(tx_buffer), sent); + } + poll_pending_messages(sock, nidd_global::SOCKET_SEND_COUNT); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/nidd/niddsocket_send_timeout.cpp b/TESTS/netsocket/nidd/niddsocket_send_timeout.cpp new file mode 100644 index 0000000000..a3e2762ce7 --- /dev/null +++ b/TESTS/netsocket/nidd/niddsocket_send_timeout.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019, 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 "greentea-client/test_env.h" +#include "nidd_tests.h" +#include "unity/unity.h" +#include "utest.h" + +using namespace utest::v1; + +void NIDDSOCKET_SEND_TIMEOUT() +{ + char tx_buffer[4]; + fill_tx_buffer_ascii(tx_buffer, sizeof(tx_buffer)); + + CellularNonIPSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(CellularContext::get_default_nonip_instance())); + + Timer timer; + timer.start(); + int sent = sock.send(tx_buffer, sizeof(tx_buffer)); + timer.stop(); + TEST_ASSERT_EQUAL(sizeof(tx_buffer), sent); + sock.set_timeout(1000); + + timer.reset(); + timer.start(); + sent = sock.send(tx_buffer, sizeof(tx_buffer)); + timer.stop(); + TEST_ASSERT_EQUAL(sizeof(tx_buffer), sent); + + poll_pending_messages(sock, 2); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/tools/test_configs/NIDDInterface.json b/tools/test_configs/NIDDInterface.json new file mode 100644 index 0000000000..21beb203e9 --- /dev/null +++ b/tools/test_configs/NIDDInterface.json @@ -0,0 +1,59 @@ +{ + "config": { + "echo-server-addr" : { + "help" : "IP address of echo server", + "value" : "\"echo.mbedcloudtesting.com\"" + }, + "echo-server-port" : { + "help" : "Port of echo server", + "value" : "7" + }, + "echo-server-discard-port" : { + "help" : "Discard port of echo server", + "value" : "9" + }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" + }, + "trace-level": { + "help": "Note that excessive trace prints may mess up with Greentea parsing", + "macro_name": "MBED_TRACE_MAX_LEVEL", + "value": "TRACE_LEVEL_ERROR" + } + }, + "target_overrides": { + "*": { + "QUECTEL_BG96.provide-default": false, + "QUECTEL_BC95.provide-default": false, + "UBLOX_N2XX.provide-default": false, + + "cellular.clear-on-connect": true, + "cellular.radio-access-technology": 9, + + "nsapi.default-cellular-plmn": null, + "nsapi.default-cellular-sim-pin": null, + "nsapi.default-cellular-apn": "\"nonip\"", + "nsapi.default-cellular-username": null, + "nsapi.default-cellular-password": null, + + "target.network-default-interface-type": "CELLULAR", + "cellular.control-plane-opt": true, + "cellular.use-apn-lookup": false, + "platform.stdio-buffered-serial": true, + "platform.stdio-baud-rate": 115200, + "platform.default-serial-baud-rate": 115200, + "mbed-trace.enable": true, + "cellular.debug-at": true + }, + "MTB_STM_L475": { + "UBLOX_N2XX.tx": "TX1", + "UBLOX_N2XX.rx": "RX1" + } + } +} +