diff --git a/tests/net/lib/coap_client/CMakeLists.txt b/tests/net/lib/coap_client/CMakeLists.txt index 60506c23c2d3f3..9486b914706eb2 100644 --- a/tests/net/lib/coap_client/CMakeLists.txt +++ b/tests/net/lib/coap_client/CMakeLists.txt @@ -24,5 +24,6 @@ add_compile_definitions(CONFIG_COAP_CLIENT_MESSAGE_HEADER_SIZE=48) add_compile_definitions(CONFIG_COAP_CLIENT_STACK_SIZE=1024) add_compile_definitions(CONFIG_COAP_CLIENT_THREAD_PRIORITY=10) add_compile_definitions(CONFIG_COAP_LOG_LEVEL=4) -add_compile_definitions(CONFIG_NET_SOCKETS_POSIX_NAMES=y) add_compile_definitions(CONFIG_COAP_INIT_ACK_TIMEOUT_MS=2000) +add_compile_definitions(CONFIG_COAP_CLIENT_MAX_REQUESTS=2) +add_compile_definitions(CONFIG_COAP_CLIENT_MAX_INSTANCES=2) diff --git a/tests/net/lib/coap_client/src/main.c b/tests/net/lib/coap_client/src/main.c index cb77c7c0cfe36d..9e66015dbee0d4 100644 --- a/tests/net/lib/coap_client/src/main.c +++ b/tests/net/lib/coap_client/src/main.c @@ -17,7 +17,8 @@ DEFINE_FFF_GLOBALS; static uint8_t last_response_code; static const char *test_path = "test"; -static uint16_t last_message_id; + +static uint16_t messages_needing_response[2]; static struct coap_client client; @@ -33,11 +34,21 @@ static char *long_payload = "Lorem ipsum dolor sit amet, consectetur adipiscing static ssize_t z_impl_zsock_recvfrom_custom_fake(int sock, void *buf, size_t max_len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) { + uint16_t last_message_id = 0; + LOG_INF("Recvfrom"); - static uint8_t ack_data[] = { + uint8_t ack_data[] = { 0x68, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + if (messages_needing_response[0] != 0) { + last_message_id = messages_needing_response[0]; + messages_needing_response[0] = 0; + } else { + last_message_id = messages_needing_response[1]; + messages_needing_response[1] = 0; + } + ack_data[2] = (uint8_t) (last_message_id >> 8); ack_data[3] = (uint8_t) last_message_id; @@ -50,11 +61,17 @@ static ssize_t z_impl_zsock_sendto_custom_fake(int sock, void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { - LOG_INF("Sendto"); - last_message_id = 0; + uint16_t last_message_id = 0; + last_message_id |= ((uint8_t *) buf)[2] << 8; last_message_id |= ((uint8_t *) buf)[3]; + if (messages_needing_response[0] == 0) { + messages_needing_response[0] = last_message_id; + } else { + messages_needing_response[1] = last_message_id; + } + last_response_code = ((uint8_t *) buf)[1]; LOG_INF("Latest message ID: %d", last_message_id); @@ -65,10 +82,20 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_response(int sock, void *buf, s int flags, struct sockaddr *src_addr, socklen_t *addrlen) { + uint16_t last_message_id = 0; + static uint8_t ack_data[] = { 0x48, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + if (messages_needing_response[0] != 0) { + last_message_id = messages_needing_response[0]; + messages_needing_response[0] = 0; + } else { + last_message_id = messages_needing_response[1]; + messages_needing_response[1] = 0; + } + ack_data[2] = (uint8_t) (last_message_id >> 8); ack_data[3] = (uint8_t) last_message_id; @@ -81,10 +108,20 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_empty_ack(int sock, void *buf, int flags, struct sockaddr *src_addr, socklen_t *addrlen) { + uint16_t last_message_id = 0; + static uint8_t ack_data[] = { 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + if (messages_needing_response[0] != 0) { + last_message_id = messages_needing_response[0]; + messages_needing_response[0] = 0; + } else { + last_message_id = messages_needing_response[1]; + messages_needing_response[1] = 0; + } + ack_data[2] = (uint8_t) (last_message_id >> 8); ack_data[3] = (uint8_t) last_message_id; @@ -99,10 +136,20 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_unmatching(int sock, void *buf, int flags, struct sockaddr *src_addr, socklen_t *addrlen) { + uint16_t last_message_id = 0; + static uint8_t ack_data[] = { 0x68, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }; + if (messages_needing_response[0] != 0) { + last_message_id = messages_needing_response[0]; + messages_needing_response[0] = 0; + } else { + last_message_id = messages_needing_response[1]; + messages_needing_response[1] = 0; + } + ack_data[2] = (uint8_t) (last_message_id >> 8); ack_data[3] = (uint8_t) last_message_id; @@ -242,7 +289,7 @@ ZTEST(coap_client, test_no_response) LOG_INF("Send request"); clear_socket_events(); - ret = coap_client_req(&client, 0, &address, &client_request, -1); + ret = coap_client_req(&client, 0, &address, &client_request, 0); zassert_true(ret >= 0, "Sending request failed, %d", ret); k_sleep(K_MSEC(1000)); @@ -307,15 +354,12 @@ ZTEST(coap_client, test_multiple_requests) zassert_true(ret >= 0, "Sending request failed, %d", ret); ret = coap_client_req(&client, 0, &address, &client_request, -1); - zassert_equal(ret, -EAGAIN, "Shouldn't be able to send 2 requests at same time"); + zassert_true(ret >= 0, "Sending request failed, %d", ret); k_sleep(K_MSEC(5)); k_sleep(K_MSEC(1000)); zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response"); - ret = coap_client_req(&client, 0, &address, &client_request, -1); - zassert_true(ret >= 0, "Sending request failed, %d", ret); - k_sleep(K_MSEC(5)); k_sleep(K_MSEC(1000)); zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");