Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: lib: aws_fota: Add single url to aws_fota job doc parsing #10974

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion subsys/net/lib/aws_fota/src/aws_fota_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

#include "aws_fota_json.h"

#if defined(CONFIG_HTTP_PARSER_URL)
#include <zephyr/net/http/parser_url.h>
#endif

/**@brief Copy max maxlen bytes from src to dst. Insert null-terminator.
*/
static void strncpy_nullterm(char *dst, const char *src, size_t maxlen)
Expand Down Expand Up @@ -115,7 +119,24 @@ int aws_fota_parse_DescribeJobExecution_rsp(const char *job_document,

cJSON *hostname = cJSON_GetObjectItemCaseSensitive(location, "host");
cJSON *path = cJSON_GetObjectItemCaseSensitive(location, "path");

#if defined(CONFIG_HTTP_PARSER_URL)
cJSON *url = cJSON_GetObjectItemCaseSensitive(location, "url");

if (cJSON_GetStringValue(url) != NULL) {
struct http_parser_url u;

http_parser_url_init(&u);
http_parser_parse_url(url->valuestring,
strlen(url->valuestring), false, &u);
strncpy_nullterm(hostname_buf, url->valuestring
+ u.field_data[UF_HOST].off,
u.field_data[UF_HOST].len + 1);
strncpy_nullterm(file_path_buf, url->valuestring
+ u.field_data[UF_PATH].off + 1,
u.field_data[UF_PATH].len
+ u.field_data[UF_QUERY].len + 1);
} else
#endif
if ((cJSON_GetStringValue(hostname) != NULL)
&& (cJSON_GetStringValue(path) != NULL)) {
strncpy_nullterm(hostname_buf, hostname->valuestring,
Expand Down
2 changes: 2 additions & 0 deletions tests/subsys/net/lib/aws_fota/aws_fota_json/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
CONFIG_UNITY=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_CJSON_LIB=y
CONFIG_NETWORKING=y
CONFIG_HTTP_PARSER_URL=y
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
CONFIG_HEAP_MEM_POOL_SIZE=4096
29 changes: 28 additions & 1 deletion tests/subsys/net/lib/aws_fota/aws_fota_json/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,34 @@ void test_parse_job_execution(void)
TEST_ASSERT_EQUAL_STRING(expected_file_path, file_path);
}

void test_parse_malformed_job_execution(void)
#if !defined(CONFIG_HTTP_PARSER_URL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the relevant configuration options are enabled in the prj.conf for the test, this conditional is not needed.

void test_parse_job_execution_single_url(void)
{
int ret;
int version_number;
int expected_version_number = 1;
char expected_job_id[] = "9b5caac6-3e8a-45dd-9273-c1b995762f4a";
char expected_hostname[] = "fota-update-bucket.s3.eu-central-1.amazonaws.com";
char expected_file_path[] = "update.bin?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAWXEL53DXIU7W72AE%2F20190606%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20190606T081505Z&X-Amz-Expires=604800&X-Amz-Signature=913e00b97efe5565a901df4ff0b87e4878a406941d711f59d45915035989adcc&X-Amz-SignedHeaders=host";
char encoded[] = "{\"timestamp\":1559808907,\"execution\":{\"jobId\":\"9b5caac6-3e8a-45dd-9273-c1b995762f4a\",\"status\":\"QUEUED\",\"queuedAt\":1559808906,\"lastUpdatedAt\":1559808906,\"versionNumber\":1,\"executionNumber\":1,\"jobDocument\":{\"operation\":\"app_fw_update\",\"fwversion\":\"2\",\"size\":181124,\"location\":{\"url\":\"https://fota-update-bucket.s3.eu-central-1.amazonaws.com/update.bin?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAWXEL53DXIU7W72AE%2F20190606%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20190606T081505Z&X-Amz-Expires=604800&X-Amz-Signature=913e00b97efe5565a901df4ff0b87e4878a406941d711f59d45915035989adcc&X-Amz-SignedHeaders=host\"}}}}";
char job_id[100];
char hostname[100];
char file_path[1000];

ret = aws_fota_parse_DescribeJobExecution_rsp(encoded,
sizeof(encoded) - 1,
job_id, hostname,
file_path,
&version_number);
TEST_ASSERT_EQUAL(ret, 1);
TEST_ASSERT_EQUAL_STRING(job_id, expected_job_id);
TEST_ASSERT_EQUAL(version_number, expected_version_number);
TEST_ASSERT_EQUAL_STRING(hostname, expected_hostname);
TEST_ASSERT_EQUAL_STRING(file_path, expected_file_path);
}
#endif

static void test_parse_malformed_job_execution(void)
{
int ret;
char job_id[100];
Expand Down
Loading