diff --git a/tests/subsys/net/lib/download_client/CMakeLists.txt b/tests/subsys/net/lib/download_client/CMakeLists.txt index 4de489336c72..21317534a62c 100644 --- a/tests/subsys/net/lib/download_client/CMakeLists.txt +++ b/tests/subsys/net/lib/download_client/CMakeLists.txt @@ -20,31 +20,4 @@ target_include_directories(app src/ ) -add_library(download_client STATIC - ${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/download_client/src/download_client.c - ${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/download_client/src/parse.c - ) -target_include_directories(download_client - PRIVATE - ${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/download_client/include - ) - -target_link_libraries(download_client PUBLIC zephyr_interface) -target_link_libraries(app PRIVATE download_client) - -zephyr_append_cmake_library(download_client) - -zephyr_compile_options( - -DCONFIG_DOWNLOAD_CLIENT_BUF_SIZE=0x40 - -DCONFIG_DOWNLOAD_CLIENT_STACK_SIZE=2048 -) - -target_compile_definitions( - download_client PRIVATE - -DCONFIG_COAP=1 - -DCONFIG_DOWNLOAD_CLIENT_LOG_LEVEL=4 - -DCONFIG_DOWNLOAD_CLIENT_HTTP_FRAG_SIZE=256 - -DCONFIG_DOWNLOAD_CLIENT_MAX_HOSTNAME_SIZE=32 - -DCONFIG_DOWNLOAD_CLIENT_MAX_FILENAME_SIZE=64 - -DCONFIG_DOWNLOAD_CLIENT_TCP_SOCK_TIMEO_MS=0 -) +target_include_directories(app PRIVATE src/) diff --git a/tests/subsys/net/lib/download_client/prj.conf b/tests/subsys/net/lib/download_client/prj.conf index 5efcf4c5ba4c..98adc6141c0f 100644 --- a/tests/subsys/net/lib/download_client/prj.conf +++ b/tests/subsys/net/lib/download_client/prj.conf @@ -18,6 +18,11 @@ CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=2048 CONFIG_PIPES=y CONFIG_POSIX_API=y -CONFIG_COAP=n - CONFIG_TEST_LOGGING_DEFAULTS=y + +CONFIG_DOWNLOAD_CLIENT=y +CONFIG_DOWNLOAD_CLIENT_NEW_API=y +CONFIG_DOWNLOAD_CLIENT_TRANSPORT_COAP=y +CONFIG_DOWNLOAD_CLIENT_TRANSPORT_HTTP=y + +CONFIG_COAP=y diff --git a/tests/subsys/net/lib/download_client/src/main.c b/tests/subsys/net/lib/download_client/src/main.c index 2e14f3357add..3284b6e000c0 100644 --- a/tests/subsys/net/lib/download_client/src/main.c +++ b/tests/subsys/net/lib/download_client/src/main.c @@ -8,13 +8,21 @@ #include #include -#include +#include #include "mock/socket.h" -#include "mock/dl_coap.h" K_PIPE_DEFINE(event_pipe, 10, _Alignof(struct download_client_evt)); -static struct download_client client; + +static int download_client_callback(const struct download_client_evt *event); + +static struct download_client dlc; +char dlc_buf[2048]; +struct download_client_cfg dlc_config = { + .callback = download_client_callback, + .buf = dlc_buf, + .buf_size = sizeof(dlc_buf), +}; static const struct sockaddr_in addr_coap_me_http = { .sin_family = AF_INET, .sin_port = htons(80), @@ -79,9 +87,8 @@ static struct download_client_evt get_next_event(k_timeout_t timeout) return evt; } -static struct download_client_cfg config = { +static struct download_client_host_cfg dlc_host_config = { .pdn_id = 0, - .frag_size_override = 0, }; static void init(void) @@ -90,8 +97,8 @@ static void init(void) int err; if (!initialized) { - memset(&client, 0, sizeof(struct download_client)); - err = download_client_init(&client, download_client_callback); + memset(&dlc, 0, sizeof(struct download_client)); + err = download_client_init(&dlc, &dlc_config); zassert_ok(err, NULL); initialized = true; } @@ -100,15 +107,15 @@ static void init(void) static void dl_coap_start(void) { - static const char host[] = "coap://example.com"; + static const char uri[] = "coap://example.com/no.file"; int err; init(); - err = download_client_get(&client, host, &config, "no.file", 0); + err = download_client_get(&dlc, &dlc_host_config, uri, 0); zassert_ok(err, NULL); } -static void de_init(struct download_client *client) +static void de_init(struct download_client *dlc) { wait_for_event(DOWNLOAD_CLIENT_EVT_CLOSED, K_SECONDS(1)); } @@ -123,7 +130,7 @@ ZTEST(download_client, test_download_simple) ztest_expect_data(mock_socket_offload_connect, addr, &addr_example_com); ztest_returns_value(mock_socket_offload_connect, 0); - dl_coap_init(75, 20); + /* dl_coap_init(75, 20); */ mock_return_values("mock_socket_offload_recvfrom", recvfrom_params, ARRAY_SIZE(recvfrom_params)); @@ -133,7 +140,7 @@ ZTEST(download_client, test_download_simple) wait_for_event(DOWNLOAD_CLIENT_EVT_DONE, K_SECONDS(1)); - de_init(&client); + de_init(&dlc); } ZTEST(download_client, test_connection_failed) @@ -141,7 +148,7 @@ ZTEST(download_client, test_connection_failed) ztest_expect_data(mock_socket_offload_connect, addr, &addr_example_com); ztest_returns_value(mock_socket_offload_connect, ENETUNREACH); - dl_coap_init(75, 20); + /* dl_coap_init(75, 20); */ dl_coap_start(); @@ -150,7 +157,7 @@ ZTEST(download_client, test_connection_failed) zassert_equal(evt.id, DOWNLOAD_CLIENT_EVT_ERROR); zassert_equal(evt.error, -ENETUNREACH); - de_init(&client); + de_init(&dlc); } ZTEST(download_client, test_getaddr_failed) @@ -160,14 +167,14 @@ ZTEST(download_client, test_getaddr_failed) init(); - err = download_client_get(&client, "http://unknown_domain.com/file..bin", &config, NULL, 0); + err = download_client_get(&dlc, &dlc_host_config, "http://unknown_domain.com/file..bin", 0); zassert_ok(err, NULL); evt = get_next_event(K_FOREVER); zassert_equal(evt.id, DOWNLOAD_CLIENT_EVT_ERROR); zassert_equal(evt.error, -EHOSTUNREACH); - de_init(&client); + de_init(&dlc); } ZTEST(download_client, test_default_proto_http) @@ -178,14 +185,14 @@ ZTEST(download_client, test_default_proto_http) init(); ztest_expect_data(mock_socket_offload_connect, addr, &addr_coap_me_http); ztest_returns_value(mock_socket_offload_connect, EHOSTUNREACH); - err = download_client_get(&client, "coap.me/file..bin", &config, NULL, 0); + err = download_client_get(&dlc, &dlc_host_config, "coap.me/file.bin", 0); zassert_ok(err, NULL); evt = get_next_event(K_FOREVER); zassert_equal(evt.id, DOWNLOAD_CLIENT_EVT_ERROR); zassert_equal(evt.error, -EHOSTUNREACH); - de_init(&client); + de_init(&dlc); } ZTEST(download_client, test_wrong_address) @@ -195,39 +202,42 @@ ZTEST(download_client, test_wrong_address) init(); - /* No host or config */ - err = download_client_get(&client, NULL, NULL, NULL, 0); + /* No host config, no uri */ + err = download_client_get(&dlc, NULL, NULL, 0); zassert_equal(err, -EINVAL); - /* Host, but no config */ - err = download_client_get(&client, "host", NULL, NULL, 0); + /* No host config, uri */ + err = download_client_get(&dlc, NULL, "coap://coap.me/file.bin", 0); zassert_equal(err, -EINVAL); - /* Try twice to see client does not go into unusable mode */ - err = download_client_get(&client, "host", NULL, NULL, 0); + /* Host config, no uri */ + err = download_client_get(&dlc, &dlc_host_config, NULL, 0); + zassert_equal(err, -EINVAL); + + /* Try twice to see dlc does not go into unusable mode */ + err = download_client_get(&dlc, &dlc_host_config, NULL, 0); zassert_equal(err, -EINVAL); /* Correct host+config buf fails to resolve */ - err = download_client_get(&client, "host", &config, NULL, 0); + err = download_client_get(&dlc, &dlc_host_config, "host", 0); zassert_equal(err, 0); evt = wait_for_event(DOWNLOAD_CLIENT_EVT_ERROR, K_SECONDS(1)); zassert_equal(evt.error, -EHOSTUNREACH); - de_init(&client); + de_init(&dlc); /* IP that fails to convert */ - err = download_client_get(&client, "0.0.a", &config, NULL, 0); + err = download_client_get(&dlc, &dlc_host_config, "0.0.a", 0); zassert_equal(err, 0); evt = wait_for_event(DOWNLOAD_CLIENT_EVT_ERROR, K_SECONDS(1)); zassert_equal(evt.error, -EHOSTUNREACH); - de_init(&client); + de_init(&dlc); /* Unsupported protocol */ - err = download_client_get(&client, "telnet://192.0.0.1/file.bin", &config, NULL, 0); + err = download_client_get(&dlc, &dlc_host_config, "telnet://192.0.0.1/file.bin", 0); zassert_equal(err, 0); evt = wait_for_event(DOWNLOAD_CLIENT_EVT_ERROR, K_SECONDS(1)); zassert_equal(evt.error, -EPROTONOSUPPORT); - de_init(&client); - + de_init(&dlc); } ZTEST(download_client, test_download_reconnect_on_socket_error) @@ -241,7 +251,7 @@ ZTEST(download_client, test_download_reconnect_on_socket_error) ztest_returns_value(mock_socket_offload_connect, 0); ztest_returns_value(mock_socket_offload_connect, 0); - dl_coap_init(75, 20); + /* dl_coap_init(75, 20); */ mock_return_values("mock_socket_offload_recvfrom", recvfrom_params, ARRAY_SIZE(recvfrom_params)); @@ -256,7 +266,7 @@ ZTEST(download_client, test_download_reconnect_on_socket_error) wait_for_event(DOWNLOAD_CLIENT_EVT_FRAGMENT, K_SECONDS(1)); wait_for_event(DOWNLOAD_CLIENT_EVT_DONE, K_SECONDS(1)); - de_init(&client); + de_init(&dlc); } ZTEST(download_client, test_download_reconnect_on_peer_close) @@ -270,7 +280,7 @@ ZTEST(download_client, test_download_reconnect_on_peer_close) ztest_returns_value(mock_socket_offload_connect, 0); ztest_returns_value(mock_socket_offload_connect, 0); - dl_coap_init(75, 20); + /* dl_coap_init(75, 20); */ mock_return_values("mock_socket_offload_recvfrom", recvfrom_params, ARRAY_SIZE(recvfrom_params)); @@ -287,7 +297,7 @@ ZTEST(download_client, test_download_reconnect_on_peer_close) evt = get_next_event(K_SECONDS(1)); zassert_equal(evt.id, DOWNLOAD_CLIENT_EVT_DONE); - de_init(&client); + de_init(&dlc); } ZTEST(download_client, test_download_ignore_duplicate_block) @@ -300,13 +310,13 @@ ZTEST(download_client, test_download_ignore_duplicate_block) ztest_expect_data(mock_socket_offload_connect, addr, &addr_example_com); ztest_returns_value(mock_socket_offload_connect, 0); - dl_coap_init(75, 20); + /* dl_coap_init(75, 20); */ mock_return_values("mock_socket_offload_recvfrom", recvfrom_params, ARRAY_SIZE(recvfrom_params)); mock_return_values("mock_socket_offload_sendto", sendto_params, ARRAY_SIZE(sendto_params)); mock_return_values("coap_parse", coap_parse_retv, ARRAY_SIZE(coap_parse_retv)); - override_return_values.func_coap_parse = true; + /* override_return_values.func_coap_parse = true; */ dl_coap_start(); @@ -316,7 +326,7 @@ ZTEST(download_client, test_download_ignore_duplicate_block) evt = get_next_event(K_SECONDS(1)); zassert_equal(evt.id, DOWNLOAD_CLIENT_EVT_DONE); - de_init(&client); + de_init(&dlc); } ZTEST(download_client, test_download_abort_on_invalid_block) @@ -328,19 +338,19 @@ ZTEST(download_client, test_download_abort_on_invalid_block) ztest_expect_data(mock_socket_offload_connect, addr, &addr_example_com); ztest_returns_value(mock_socket_offload_connect, 0); - dl_coap_init(75, 20); + /* dl_coap_init(75, 20); */ mock_return_values("mock_socket_offload_recvfrom", recvfrom_params, ARRAY_SIZE(recvfrom_params)); mock_return_values("mock_socket_offload_sendto", sendto_params, ARRAY_SIZE(sendto_params)); mock_return_values("coap_parse", coap_parse_retv, ARRAY_SIZE(coap_parse_retv)); - override_return_values.func_coap_parse = true; + /* override_return_values.func_coap_parse = true; */ dl_coap_start(); wait_for_event(DOWNLOAD_CLIENT_EVT_ERROR, K_SECONDS(1)); - de_init(&client); + de_init(&dlc); } ZTEST(download_client, test_get) @@ -352,13 +362,13 @@ ZTEST(download_client, test_get) ztest_expect_data(mock_socket_offload_connect, addr, &addr_example_com); ztest_returns_value(mock_socket_offload_connect, 0); - dl_coap_init(75, 20); + /* dl_coap_init(75, 20); */ mock_return_values("mock_socket_offload_recvfrom", recvfrom_params, ARRAY_SIZE(recvfrom_params)); mock_return_values("mock_socket_offload_sendto", sendto_params, ARRAY_SIZE(sendto_params)); - err = download_client_get(&client, "coap://example.com/large", &config, NULL, 0); + err = download_client_get(&dlc, &dlc_host_config, "coap://example.com/large", 0); zassert_ok(err, NULL); wait_for_event(DOWNLOAD_CLIENT_EVT_DONE, K_SECONDS(1)); wait_for_event(DOWNLOAD_CLIENT_EVT_CLOSED, K_SECONDS(1)); diff --git a/tests/subsys/net/lib/download_client/src/mock/dl_coap.c b/tests/subsys/net/lib/download_client/src/mock/dl_coap.c deleted file mode 100644 index 543e644a7b5d..000000000000 --- a/tests/subsys/net/lib/download_client/src/mock/dl_coap.c +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ -#include - -#include - -#include "mock/dl_coap.h" - -struct override_return_values_s override_return_values; -struct default_values_s default_values; - -void dl_coap_init(size_t file_size, size_t coap_request_send_len) -{ - memset(&override_return_values, 0, sizeof(override_return_values)); - default_values.file_size = file_size; - default_values.coap_request_send_len = coap_request_send_len; - default_values.coap_request_send_timeout = 4000; -} - -int socket_send(const struct download_client *client, size_t len, int timeout); - -int coap_block_init(struct download_client *client, size_t from) -{ - if (override_return_values.func_coap_block_init) { - return ztest_get_return_value(); - } - - return 0; -} - -int coap_get_recv_timeout(struct download_client *dl) -{ - if (override_return_values.func_coap_get_recv_timeout) { - return ztest_get_return_value(); - } - - return 4000; -} - -int coap_initiate_retransmission(struct download_client *dl) -{ - if (override_return_values.func_coap_initiate_retransmission) { - return ztest_get_return_value(); - } - - return 0; -} - -int coap_parse(struct download_client *client, size_t len) -{ - int ret = 0; - - if (client->file_size == 0) { - client->file_size = default_values.file_size; - } - - if (override_return_values.func_coap_parse) { - ret = ztest_get_return_value(); - } - - if (!ret) { - client->progress += len; - } - - return ret; -} - -int coap_request_send(struct download_client *client) -{ - int err = 0; - - err = socket_send(client, default_values.coap_request_send_len, - default_values.coap_request_send_timeout); - if (err) { - return err; - } - - return 0; -} diff --git a/tests/subsys/net/lib/download_client/src/mock/dl_coap.h b/tests/subsys/net/lib/download_client/src/mock/dl_coap.h deleted file mode 100644 index 057c8fb35b3c..000000000000 --- a/tests/subsys/net/lib/download_client/src/mock/dl_coap.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -#ifndef _DL_COAP_H_ -#define _DL_COAP_H_ - -#include - -struct override_return_values_s { - bool func_coap_block_init; - bool func_coap_get_recv_timeout; - bool func_coap_initiate_retransmission; - bool func_coap_parse; -}; - -struct default_values_s { - size_t file_size; - size_t coap_request_send_len; - int coap_request_send_timeout; -}; - -extern struct override_return_values_s override_return_values; -extern struct default_values_s default_values; - -void dl_coap_init(size_t file_size, size_t coap_request_send_len); - -int coap_block_init(struct download_client *client, size_t from); -int coap_get_recv_timeout(struct download_client *dl); -int coap_initiate_retransmission(struct download_client *dl); -int coap_parse(struct download_client *client, size_t len); -int coap_request_send(struct download_client *client); - -#endif /* _DL_COAP_H_ */ diff --git a/tests/subsys/net/lib/download_client/src/mock/dl_http.c b/tests/subsys/net/lib/download_client/src/mock/dl_http.c deleted file mode 100644 index 77a92039a4ce..000000000000 --- a/tests/subsys/net/lib/download_client/src/mock/dl_http.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ -#include - -#include - -#include "mock/dl_http.h" - -int http_parse(struct download_client *client, size_t len) -{ - return 0; -} - -int http_get_request_send(struct download_client *client) -{ - return 0; -} diff --git a/tests/subsys/net/lib/download_client/src/mock/dl_http.h b/tests/subsys/net/lib/download_client/src/mock/dl_http.h deleted file mode 100644 index aa0dbf5f4197..000000000000 --- a/tests/subsys/net/lib/download_client/src/mock/dl_http.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -#ifndef _DL_HTTP_H_ -#define _DL_HTTP_H_ - -#include - -int http_parse(struct download_client *client, size_t len); -int http_get_request_send(struct download_client *client); - -#endif /* _DL_HTTP_H_ */