Skip to content

Commit

Permalink
net: lib: download_client: update tests
Browse files Browse the repository at this point in the history
Update tests to use new download client API.
Will be squashed.

Signed-off-by: Eivind Jølsgard <[email protected]>
  • Loading branch information
eivindj-nordic committed Nov 1, 2024
1 parent 25caecf commit 49e7000
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 226 deletions.
29 changes: 1 addition & 28 deletions tests/subsys/net/lib/download_client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
9 changes: 7 additions & 2 deletions tests/subsys/net/lib/download_client/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
96 changes: 53 additions & 43 deletions tests/subsys/net/lib/download_client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@
#include <zephyr/net/socket_offload.h>

#include <zephyr/ztest.h>
#include <download_client.h>
#include <net/download_client.h>

#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),
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand All @@ -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));
}
Expand All @@ -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));
Expand All @@ -133,15 +140,15 @@ 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)
{
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();

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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));
Expand All @@ -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)
Expand All @@ -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));
Expand All @@ -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)
Expand All @@ -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();

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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));
Expand Down
Loading

0 comments on commit 49e7000

Please sign in to comment.