-
Notifications
You must be signed in to change notification settings - Fork 0
gh #98 Extend kvp open function to also support URLs #99
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
base: develop
Are you sure you want to change the base?
Changes from all commits
129893f
2731cc3
82d755c
4d0dde5
f362689
0540b76
679e6f3
ff2c367
854e763
37babe1
889d5d5
5ea8838
9a66294
155307c
60483b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -37,6 +37,15 @@ ut_kvp_instance_t *gKVP_Instance = NULL; | |||
| #define UT_KVP_MAGIC (0xdeadbeef) | ||||
| #define UT_KVP_MAX_INCLUDE_DEPTH 5 | ||||
|
|
||||
| #define UT_KVP_HTTPS_PREFIX "https://" | ||||
| #define UT_KVP_HTTP_PREFIX "http://" | ||||
| #define UT_KVP_FILE_PREFIX "file://" | ||||
|
|
||||
| // Automatically calculate lengths by subtracting the '\0' null terminator | ||||
| #define UT_KVP_HTTPS_PREFIX_LEN (sizeof(UT_KVP_HTTPS_PREFIX) - 1) | ||||
| #define UT_KVP_HTTP_PREFIX_LEN (sizeof(UT_KVP_HTTP_PREFIX) - 1) | ||||
| #define UT_KVP_FILE_PREFIX_LEN (sizeof(UT_KVP_FILE_PREFIX) - 1) | ||||
|
|
||||
| typedef struct | ||||
| { | ||||
| uint32_t magic; | ||||
|
|
@@ -95,28 +104,86 @@ void ut_kvp_destroyInstance(ut_kvp_instance_t *pInstance) | |||
| pInternal = NULL; | ||||
| } | ||||
|
|
||||
| ut_kvp_status_t ut_kvp_open(ut_kvp_instance_t *pInstance, char *fileName) | ||||
| static bool is_url(const char *input) | ||||
| { | ||||
| if (pInstance == NULL) | ||||
| if (input == NULL) | ||||
| { | ||||
| return false; | ||||
| } | ||||
|
|
||||
| // Check for http:// | ||||
| if (strncmp(input, UT_KVP_HTTP_PREFIX, UT_KVP_HTTP_PREFIX_LEN) == 0) | ||||
| { | ||||
| return true; | ||||
| } | ||||
|
|
||||
| // Check for https:// | ||||
| if (strncmp(input, UT_KVP_HTTPS_PREFIX, UT_KVP_HTTPS_PREFIX_LEN) == 0) | ||||
| { | ||||
| return true; | ||||
| } | ||||
|
|
||||
| // No matches found | ||||
| return false; | ||||
| } | ||||
|
|
||||
| ut_kvp_status_t ut_kvp_open(ut_kvp_instance_t *pInstance, const char *fileNameOrUrl) | ||||
| { | ||||
| ut_kvp_instance_internal_t *pInternal = validateInstance(pInstance); | ||||
|
|
||||
| // Validate KVP instance handle | ||||
| if (pInternal == NULL) | ||||
| { | ||||
| return UT_KVP_STATUS_INVALID_INSTANCE; | ||||
| } | ||||
|
|
||||
| ut_kvp_instance_internal_t *pInternal = validateInstance(pInstance); | ||||
| if (fileName == NULL) | ||||
| // Validate fileNameOrUrl parameter | ||||
| if (fileNameOrUrl == NULL) | ||||
| { | ||||
| UT_LOG_ERROR("Invalid Param [fileName]"); | ||||
| return UT_KVP_STATUS_INVALID_PARAM; | ||||
| UT_LOG_ERROR("NULL PARAM [fileNameOrUrl]"); | ||||
| return UT_KVP_STATUS_NULL_PARAM; | ||||
kanjoe24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| } | ||||
|
|
||||
| // Determine if input is a URL(e.g., http:// or https://) | ||||
KarthikeyanR470 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| bool bFilenameIsAUrl = is_url(fileNameOrUrl); | ||||
|
|
||||
| // Handle URL-based input | ||||
| if(bFilenameIsAUrl == true) | ||||
| { | ||||
| char *pYaml = NULL; | ||||
|
|
||||
| pYaml = malloc(UT_KVP_MAX_ELEMENT_SIZE); | ||||
|
|
||||
| if ( pYaml == NULL ) | ||||
| { | ||||
| UT_LOG_ERROR("Malloc was not able to provide memory\n"); | ||||
| return UT_KVP_STATUS_PARSING_ERROR; | ||||
| } | ||||
|
|
||||
| snprintf(pYaml, UT_KVP_MAX_ELEMENT_SIZE, "include: %s\n", fileNameOrUrl); | ||||
KarthikeyanR470 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
|
||||
| // Pass the dynamically allocated YAML string to openMemory() for parsing | ||||
| ut_kvp_status_t status = ut_kvp_openMemory(pInstance, pYaml, strlen(pYaml)); | ||||
| free(pYaml); | ||||
| return status; | ||||
| } | ||||
|
|
||||
| // Handle file-based input | ||||
| if (strncmp(fileNameOrUrl, UT_KVP_FILE_PREFIX, UT_KVP_FILE_PREFIX_LEN) == 0) | ||||
| { | ||||
| // Skip "file://" | ||||
| fileNameOrUrl = fileNameOrUrl + UT_KVP_FILE_PREFIX_LEN; | ||||
| } | ||||
|
|
||||
| if (access(fileName, F_OK) != 0) | ||||
| // Verify that the file is accessible | ||||
| if (access(fileNameOrUrl, F_OK) != 0) | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't this fail if the filename is a URL? Since the URL hasn't been downloaded you can't call access on it? Why don't you get the error
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The URL path does not reach the access() check. Line 151 in 60483b5
1.If the input is a URL (is_url(...) == true), the function immediately enters the 'if' block, constructs an in-memory YAML string, calls ut_kvp_openMemory(), and returns. So URLs never fall through to the access() path. 2.The access(fileNameOrUrl, F_OK) check is executed only for non-URL file-based inputs. So the code does not attempt to call access() on a URL, and therefore we do not get UT_KVP_STATUS_FILE_OPEN_ERROR for URL cases. |
||||
| { | ||||
| UT_LOG_ERROR("[%s] cannot be accesed", fileName); | ||||
| UT_LOG_ERROR("[%s] cannot be accessed", fileNameOrUrl); | ||||
| return UT_KVP_STATUS_FILE_OPEN_ERROR; | ||||
| } | ||||
|
|
||||
| // Load the new document | ||||
| struct fy_document *newDoc = fy_document_build_from_file(NULL, fileName); | ||||
| struct fy_document *newDoc = fy_document_build_from_file(NULL, fileNameOrUrl); | ||||
| if (newDoc == NULL || fy_document_resolve(newDoc) != 0) | ||||
| { | ||||
| if (newDoc) | ||||
|
|
@@ -1109,7 +1176,7 @@ static struct fy_node* process_include(const char *filename, int depth, struct f | |||
| return NULL; | ||||
| } | ||||
|
|
||||
| if (strncmp(filename, "http:", 5) == 0 || strncmp(filename, "https:", 6) == 0) | ||||
| if (strncmp(filename, UT_KVP_HTTP_PREFIX, UT_KVP_HTTP_PREFIX_LEN) == 0 || strncmp(filename, UT_KVP_HTTPS_PREFIX, UT_KVP_HTTPS_PREFIX_LEN) == 0) | ||||
| { | ||||
| // URL include | ||||
| mChunk.memory = malloc(1); | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,12 @@ | |
| #define KVP_VALID_TEST_SEQUENCE_INCLUDE_YAML "assets/include/sequence-include.yaml" | ||
| #define KVP_VALID_TEST_RESOLVE_YAML_TAGS_YAML "assets/yaml_tags.yaml" | ||
| #define KVP_VALID_TEST_RESOLVE_YAML_TAGS_IN_SEQUENCE_YAML "assets/yaml_tags_in_sequence.yaml" | ||
| #define KVP_VALID_TEST_URL_HTTPS "https://raw.githubusercontent.com/rdkcentral/ut-control/main/tests/src/assets/include/2s.yaml" | ||
| #define KVP_VALID_TEST_URI_HTTP "http://localhost:8000/assets/yaml_tags.yaml" | ||
| #define KVP_VALID_TEST_URI_FILE "file://assets/yaml_tags.yaml" | ||
| #define KVP_VALID_TEST_NOT_VALID_URL_HTTPS "HTTPS://raw.githubusercontent.com/rdkcentral/ut-control/main/tests/src/assets/include/2s.yaml" | ||
| #define KVP_VALID_TEST_NOT_VALID_URI_HTTP "HTTP://localhost:8000/assets/yaml_tags.yaml" | ||
| #define KVP_VALID_TEST_NOT_VALID_URI_FILE "FILE://assets/yaml_tags.yaml" | ||
|
|
||
| static ut_kvp_instance_t *gpMainTestInstance = NULL; | ||
| static UT_test_suite_t *gpKVPSuite = NULL; | ||
|
|
@@ -108,7 +114,7 @@ void test_ut_kvp_open( void ) | |
| /* Negative Read Test, NULL PARAM */ | ||
| UT_LOG_STEP("ut_kvp_open( pInstance, NULL ) - Negative"); | ||
| status = ut_kvp_open( pInstance, NULL); | ||
| UT_ASSERT( status == UT_KVP_STATUS_INVALID_PARAM ); | ||
| UT_ASSERT( status == UT_KVP_STATUS_NULL_PARAM ); | ||
|
|
||
| /* Filename doesn't exist */ | ||
| UT_LOG_STEP("ut_kvp_open( pInstance, %s - filename doesn't exist ) - Negative", KVP_VALID_TEST_NO_FILE); | ||
|
|
@@ -120,11 +126,41 @@ void test_ut_kvp_open( void ) | |
| status = ut_kvp_open( pInstance, KVP_VALID_TEST_ZERO_LENGTH_YAML_FILE); | ||
| UT_ASSERT( status == UT_KVP_STATUS_PARSING_ERROR ); | ||
|
|
||
| /* Negative Read Test, KVP_VALID_TEST_NOT_VALID_URL_HTTPS PARAM */ | ||
| UT_LOG_STEP("ut_kvp_open( pInstance, KVP_VALID_TEST_NOT_VALID_URL_HTTPS ) - Negative"); | ||
| status = ut_kvp_open( pInstance, KVP_VALID_TEST_NOT_VALID_URL_HTTPS); | ||
| printf("Status = %d\n", status); | ||
| UT_ASSERT( status == UT_KVP_STATUS_FILE_OPEN_ERROR ); | ||
|
|
||
| /* Negative Read Test, KVP_VALID_TEST_NOT_VALID_URI_HTTP PARAM */ | ||
| UT_LOG_STEP("ut_kvp_open( pInstance, KVP_VALID_TEST_NOT_VALID_URI_HTTP ) - Negative"); | ||
| status = ut_kvp_open( pInstance, KVP_VALID_TEST_NOT_VALID_URI_HTTP); | ||
| printf("Status = %d\n", status); | ||
| UT_ASSERT( status == UT_KVP_STATUS_FILE_OPEN_ERROR ); | ||
|
|
||
| /* Negative Read Test, KVP_VALID_TEST_NOT_VALID_URI_FILE PARAM */ | ||
| UT_LOG_STEP("ut_kvp_open( pInstance, KVP_VALID_TEST_NOT_VALID_URI_FILE ) - Negative"); | ||
| status = ut_kvp_open( pInstance, KVP_VALID_TEST_NOT_VALID_URI_FILE); | ||
| printf("Status = %d\n", status); | ||
| UT_ASSERT( status == UT_KVP_STATUS_FILE_OPEN_ERROR ); | ||
|
|
||
| /* Positive Tests */ | ||
| UT_LOG_STEP("ut_kvp_open( pInstance, KVP_VALID_TEST_YAML_FILE ) - Positive"); | ||
| status = ut_kvp_open( pInstance, KVP_VALID_TEST_YAML_FILE); | ||
| UT_ASSERT( status == UT_KVP_STATUS_SUCCESS ); | ||
|
|
||
| UT_LOG_STEP("ut_kvp_open( pInstance, KVP_VALID_TEST_URL_HTTPS ) - Positive"); | ||
| status = ut_kvp_open( pInstance, KVP_VALID_TEST_URL_HTTPS); | ||
| UT_ASSERT( status == UT_KVP_STATUS_SUCCESS ); | ||
|
|
||
| UT_LOG_STEP("ut_kvp_open( pInstance, KVP_VALID_TEST_URI_HTTP ) - Positive"); | ||
| status = ut_kvp_open( pInstance, KVP_VALID_TEST_URI_HTTP); | ||
| UT_ASSERT( status == UT_KVP_STATUS_SUCCESS ); | ||
|
|
||
| UT_LOG_STEP("ut_kvp_open( pInstance, KVP_VALID_TEST_URI_FILE ) - Positive"); | ||
| status = ut_kvp_open( pInstance, KVP_VALID_TEST_URI_FILE); | ||
| UT_ASSERT( status == UT_KVP_STATUS_SUCCESS ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to add comments on the purpose of each test and what your trying to achieve. Both Positive & Negative testing are always required. And add comments to be the goals of the tests specifically.
Malformed URLS, and invalid filenames must also be checked.. CAPS HTTPS/HTTP/FILE should also be failing and tested. Ensure that both params are validated. Whilst you can test the ut_kvp_open() function, how do you know it's read the right data in? That's a L2 test, and also if not already existing will require a test, I would suggest having a test for points 1) & 2) above.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
|
|
||
| UT_LOG_STEP("ut_kvp_open( pInstance, %s ) - Postive", KVP_VALID_TEST_NOT_VALID_YAML_FORMATTED_FILE); | ||
KarthikeyanR470 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| status = ut_kvp_open( pInstance, KVP_VALID_TEST_NOT_VALID_YAML_FORMATTED_FILE); | ||
| UT_ASSERT( status == UT_KVP_STATUS_SUCCESS ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also update about url here