This is an automated email from the ASF dual-hosted git repository.
pengzheng pushed a commit to branch feature/refactor_bundle_cache
in repository https://gitbox.apache.org/repos/asf/celix.git
The following commit(s) were added to refs/heads/feature/refactor_bundle_cache
by this push:
new 0444e0ec Improve error handling of celix_framework_utils.c and add
corresponding test cases.
new 64f2fa71 Merge remote-tracking branch
'apache/feature/refactor_bundle_cache' into feature/refactor_bundle_cache
0444e0ec is described below
commit 0444e0ecc9547bfca6e7811a0453f39c0f3da5a7
Author: PengZheng <[email protected]>
AuthorDate: Mon Apr 3 22:08:12 2023 +0800
Improve error handling of celix_framework_utils.c and add corresponding
test cases.
---
.../CelixFrameworkUtilsErrorInjectionTestSuite.cc | 29 ++++++++-
.../gtest/src/CelixFrameworkUtilsTestSuite.cc | 6 ++
libs/framework/src/celix_framework_utils.c | 71 +++++++++++-----------
3 files changed, 71 insertions(+), 35 deletions(-)
diff --git
a/libs/framework/gtest/src/CelixFrameworkUtilsErrorInjectionTestSuite.cc
b/libs/framework/gtest/src/CelixFrameworkUtilsErrorInjectionTestSuite.cc
index 1d1f8b3c..5a40cdbd 100644
--- a/libs/framework/gtest/src/CelixFrameworkUtilsErrorInjectionTestSuite.cc
+++ b/libs/framework/gtest/src/CelixFrameworkUtilsErrorInjectionTestSuite.cc
@@ -20,6 +20,7 @@
#include <gtest/gtest.h>
#include <dirent.h>
+#include <time.h>
#include "celix/FrameworkFactory.h"
#include "celix/FrameworkUtils.h"
@@ -37,6 +38,8 @@ public:
~CelixFrameworkUtilsErrorInjectionTestSuite () override {
celix_ei_expect_celix_utils_extractZipData(nullptr, 0, CELIX_SUCCESS);
celix_ei_expect_celix_utils_extractZipFile(nullptr, 0, CELIX_SUCCESS);
+ celix_ei_expect_celix_utils_writeOrCreateString(nullptr, 0, nullptr);
+ celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
}
std::shared_ptr<celix::Framework> framework{};
};
@@ -59,4 +62,28 @@ TEST_F(CelixFrameworkUtilsErrorInjectionTestSuite,
testExtractFileBundle) {
auto status =
celix_framework_utils_extractBundle(framework->getCFramework(),
SIMPLE_TEST_BUNDLE1_LOCATION, testExtractDir);
EXPECT_EQ(status, CELIX_ENOMEM);
celix_utils_deleteDirectory(testExtractDir, nullptr);
-}
\ No newline at end of file
+
+ // not enough memory for bundle url validation
+ celix_ei_expect_celix_utils_writeOrCreateString(CELIX_EI_UNKNOWN_CALLER,
0, nullptr, 1);
+ status = celix_framework_utils_extractBundle(framework->getCFramework(),
SIMPLE_TEST_BUNDLE1_LOCATION, testExtractDir);
+ EXPECT_EQ(status, CELIX_ILLEGAL_ARGUMENT);
+ celix_utils_deleteDirectory(testExtractDir, nullptr);
+
+ // not enough memory to resolve URL before unzipping
+ celix_ei_expect_celix_utils_writeOrCreateString(CELIX_EI_UNKNOWN_CALLER,
0, nullptr, 2);
+ status = celix_framework_utils_extractBundle(framework->getCFramework(),
SIMPLE_TEST_BUNDLE1_LOCATION, testExtractDir);
+ EXPECT_EQ(status, CELIX_ENOMEM);
+ celix_utils_deleteDirectory(testExtractDir, nullptr);
+}
+
+TEST_F(CelixFrameworkUtilsErrorInjectionTestSuite, CheckBundleAge) {
+ struct timespec now = {0, 0};
+ celix_ei_expect_celix_utils_writeOrCreateString(CELIX_EI_UNKNOWN_CALLER,
0, nullptr, 1);
+
EXPECT_FALSE(celix_framework_utils_isBundleUrlNewerThan(framework->getCFramework(),
SIMPLE_TEST_BUNDLE1_LOCATION, &now));
+}
+
+TEST_F(CelixFrameworkUtilsErrorInjectionTestSuite, testIsBundleUrlValid) {
+ celix_ei_expect_celix_utils_strdup(CELIX_EI_UNKNOWN_CALLER, 0, nullptr);
+ auto valid =
celix_framework_utils_isBundleUrlValid(framework->getCFramework(),
"non-existing.zip", false);
+ EXPECT_FALSE(valid);
+}
diff --git a/libs/framework/gtest/src/CelixFrameworkUtilsTestSuite.cc
b/libs/framework/gtest/src/CelixFrameworkUtilsTestSuite.cc
index 9d5b2cea..57c8a0da 100644
--- a/libs/framework/gtest/src/CelixFrameworkUtilsTestSuite.cc
+++ b/libs/framework/gtest/src/CelixFrameworkUtilsTestSuite.cc
@@ -133,6 +133,12 @@ TEST_F(CelixFrameworkUtilsTestSuite,
testExtractEmbeddedBundle) {
celix_utils_deleteDirectory(testExtractDir, nullptr);
}
+TEST_F(CelixFrameworkUtilsTestSuite, CheckBundleAge) {
+ struct timespec now = {0, 0};
+
EXPECT_TRUE(celix_framework_utils_isBundleUrlNewerThan(framework->getCFramework(),
SIMPLE_TEST_BUNDLE1_LOCATION, &now));
+
EXPECT_TRUE(celix_framework_utils_isBundleUrlNewerThan(framework->getCFramework(),
SIMPLE_TEST_BUNDLE1_LOCATION, nullptr));
+}
+
TEST_F(CelixFrameworkUtilsTestSuite, testListEmbeddedBundles) {
auto list = celix::listEmbeddedBundles();
ASSERT_EQ(2, list.size());
diff --git a/libs/framework/src/celix_framework_utils.c
b/libs/framework/src/celix_framework_utils.c
index ccbf2077..4248aa73 100644
--- a/libs/framework/src/celix_framework_utils.c
+++ b/libs/framework/src/celix_framework_utils.c
@@ -20,6 +20,7 @@
#include "celix_framework_utils.h"
#include "celix_framework_utils_private.h"
+#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@@ -57,43 +58,42 @@ static const char * const EMBEDDED_BUNDLE_END_POSTFIX =
"_end";
*/
static char* celix_framework_utils_resolveFileBundleUrl(char* pathBuffer,
size_t pathBufferSize, celix_framework_t* fw, const char* bundleLocation, bool
silent) {
char *result = NULL;
+ if (celix_utils_isStringNullOrEmpty(bundleLocation)) {
+ if (!silent) {
+ FW_LOG(CELIX_LOG_LEVEL_ERROR, "Invalid bundle empty bundle path.");
+ }
+ return NULL;
+ }
- const char *bundlePath = celix_framework_getConfigProperty(fw,
CELIX_BUNDLES_PATH_NAME, CELIX_BUNDLES_PATH_DEFAULT,
- NULL);
-
- if (!celix_utils_isStringNullOrEmpty(bundleLocation)) {
- if (celix_utils_fileExists(bundleLocation)) {
- //absolute/relative path to existing file.
- result = celix_utils_writeOrCreateString(pathBuffer,
pathBufferSize, "%s", bundleLocation);
- } else if (bundleLocation[0] != '/'){
- //relative path to a non-existing file, check the bundle paths.
- char *paths = celix_utils_strdup(bundlePath);
- const char *sep = ":";
- char *savePtr = NULL;
-
- for (char *path = strtok_r(paths, sep, &savePtr); path != NULL;
path = strtok_r(NULL, sep, &savePtr)){
- char* resolvedPath =
celix_utils_writeOrCreateString(pathBuffer, pathBufferSize, "%s/%s", path,
bundleLocation);
- if (celix_utils_fileExists(resolvedPath)) {
- result = resolvedPath;
- break;
- } else {
- celix_utils_freeStringIfNotEqual(pathBuffer, resolvedPath);
- }
- }
- free(paths);
+ const char *bundlePath = celix_framework_getConfigProperty(fw,
CELIX_BUNDLES_PATH_NAME, CELIX_BUNDLES_PATH_DEFAULT, NULL);
+ if (celix_utils_fileExists(bundleLocation)) {
+ //absolute/relative path to existing file.
+ result = celix_utils_writeOrCreateString(pathBuffer, pathBufferSize,
"%s", bundleLocation);
+ } else if (bundleLocation[0] != '/'){
+ errno = 0;
+ //relative path to a non-existing file, check the bundle paths.
+ char *paths = celix_utils_strdup(bundlePath);
+ const char *sep = ":";
+ char *savePtr = NULL;
+ if (paths == NULL) {
+ goto error_out;
+ }
- if (result == NULL && !silent) {
- FW_LOG(CELIX_LOG_LEVEL_ERROR, "Bundle location '%s' is not
valid. Relative path does not point to existing file taking into account the
cwd and Celix bundle path '%s'.", bundleLocation, bundlePath);
- }
- } else {
- // absolute path to non-existing file.
- if (!silent) {
- FW_LOG(CELIX_LOG_LEVEL_ERROR, "Bundle location '%s' is not
valid, file does not exist.", bundleLocation);
+ for (char *path = strtok_r(paths, sep, &savePtr); path != NULL; path =
strtok_r(NULL, sep, &savePtr)){
+ char* resolvedPath = celix_utils_writeOrCreateString(pathBuffer,
pathBufferSize, "%s/%s", path, bundleLocation);
+ if (celix_utils_fileExists(resolvedPath)) {
+ result = resolvedPath;
+ break;
+ } else {
+ celix_utils_freeStringIfNotEqual(pathBuffer, resolvedPath);
}
}
-
- } else if (!silent) {
- FW_LOG(CELIX_LOG_LEVEL_ERROR, "Invalid bundle empty bundle path.");
+ free(paths);
+ }
+error_out:
+ if (result == NULL && !silent) {
+ FW_LOG(CELIX_LOG_LEVEL_ERROR, "Failed(%s) to resolve bundle location
'%s', taking into account the cwd and Celix bundle path '%s'.",
+ strerror(errno), bundleLocation, bundlePath);
}
return result;
}
@@ -167,7 +167,10 @@ static celix_status_t
celix_framework_utils_extractBundlePath(celix_framework_t
char buffer[CELIX_DEFAULT_STRING_CREATE_BUFFER_SIZE];
char* resolvedPath = celix_framework_utils_resolveFileBundleUrl(buffer,
sizeof(buffer), fw, bundlePath, false);
- assert(resolvedPath != NULL); //should be caught by
celix_framework_utils_isBundleUrlValid
+ if (resolvedPath == NULL) {
+ //other errors should be caught by
celix_framework_utils_isBundleUrlValid
+ return CELIX_ENOMEM;
+ }
celix_status_t status = celix_utils_extractZipFile(resolvedPath,
extractPath, &err);
framework_logIfError(fw->logger, status, err, "Could not extract bundle
zip file `%s` to `%s`", resolvedPath, extractPath);
celix_utils_freeStringIfNotEqual(buffer, resolvedPath);