Skip to content

Commit

Permalink
gh-685: Add additional manifest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Jul 26, 2024
1 parent 75aafbc commit 7b4b0cc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
15 changes: 14 additions & 1 deletion libs/framework/gtest/src/ManifestErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,18 @@ TEST_F(ManifestErrorInjectionTestSuite, NoMemoryForManifestCreateTest) {
celix_status_t status = celix_bundleManifest_create(attributes, &manifest);
EXPECT_EQ(CELIX_ENOMEM, status);
EXPECT_EQ(nullptr, manifest);
celix_err_printErrors(stdout, "Errors are expected[", "]\n");
celix_err_printErrors(stdout, "Errors are expected [", "]\n");
}

TEST_F(ManifestErrorInjectionTestSuite, NoMemoryForFrameworkManifestCreateTest) {
celix_bundle_manifest_t* manifest = nullptr;
celix_ei_expect_celix_properties_create((void*)celix_bundleManifest_createFrameworkManifest, 0, nullptr);
auto status = celix_bundleManifest_createFrameworkManifest(&manifest);
EXPECT_EQ(CELIX_ENOMEM, status);

celix_ei_expect_celix_properties_set((void*)celix_bundleManifest_createFrameworkManifest, 0, CELIX_ENOMEM, 3);
status = celix_bundleManifest_createFrameworkManifest(&manifest);
EXPECT_EQ(CELIX_ENOMEM, status);

celix_err_printErrors(stdout, "Errors are expected [", "]\n");
}
32 changes: 32 additions & 0 deletions libs/framework/gtest/src/ManifestTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "celix_err.h"
#include "celix_properties.h"
#include "celix_stdlib_cleanup.h"
#include "celix_framework_version.h"

class ManifestTestSuite : public ::testing::Test {
public:
Expand Down Expand Up @@ -60,6 +61,15 @@ TEST_F(ManifestTestSuite, CreateManifestTest) {
EXPECT_EQ(4, celix_properties_size(celix_bundleManifest_getAttributes(manifest)));
}

TEST_F(ManifestTestSuite, InvalidArgumentTest) {
celix_bundle_manifest_t* man = nullptr;
auto status = celix_bundleManifest_create(nullptr, &man);
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, status);

status = celix_bundleManifest_createFromFile("invalid-file", &man);
EXPECT_EQ(CELIX_FILE_IO_EXCEPTION, status);
}

TEST_F(ManifestTestSuite, MissingOrInvalidMandatoryManifestAttributesTest) {
//Given an empty properties set
celix_properties_t *properties = celix_properties_create();
Expand Down Expand Up @@ -196,3 +206,25 @@ TEST_F(ManifestTestSuite, GetBuiltinAttributes) {
EXPECT_STREQ("my_group", celix_bundleManifest_getBundleGroup(manifest2));
EXPECT_STREQ("my_description", celix_bundleManifest_getBundleDescription(manifest2));
}

TEST_F(ManifestTestSuite, CreateFrameworkManifestTest) {
//When creating a framework manifest
celix_autoptr(celix_bundle_manifest_t) manifest = nullptr;
celix_status_t status = celix_bundleManifest_createFrameworkManifest(&manifest);

//When the creation is successful
ASSERT_EQ(CELIX_SUCCESS, status);

//And the manifest contains at least the mandatory attributes for a framework bundle
EXPECT_GE(celix_properties_size(celix_bundleManifest_getAttributes(manifest)), 4);
auto manifestVersion = celix_bundleManifest_getManifestVersion(manifest);
ASSERT_NE(nullptr, manifestVersion);
auto bundleVersion = celix_bundleManifest_getBundleVersion(manifest);
ASSERT_NE(nullptr, bundleVersion);
celix_autofree char* mv = celix_version_toString(manifestVersion);
celix_autofree char* bv = celix_version_toString(bundleVersion);
EXPECT_STREQ("2.0.0", mv);
EXPECT_STREQ(CELIX_FRAMEWORK_VERSION, bv);
EXPECT_STREQ("Apache Celix Framework", celix_bundleManifest_getBundleName(manifest));
EXPECT_STREQ("apache_celix_framework", celix_bundleManifest_getBundleSymbolicName(manifest));
}
16 changes: 8 additions & 8 deletions libs/framework/src/celix_bundle_manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ static celix_status_t celix_bundleManifest_setMandatoryAttributes(celix_bundle_m
celix_autoptr(celix_version_t) manifestVersion = NULL;
celix_status_t getVersionStatus =
celix_properties_getAsVersion(manifest->attributes, CELIX_BUNDLE_MANIFEST_VERSION, NULL, &manifestVersion);
CELIX_ERR_RET_IF_ENOMEM(getVersionStatus);
CELIX_RETURN_IF_ENOMEM(getVersionStatus);

celix_autoptr(celix_version_t) bundleVersion = NULL;
getVersionStatus = celix_properties_getAsVersion(manifest->attributes, CELIX_BUNDLE_VERSION, NULL, &bundleVersion);
CELIX_ERR_RET_IF_ENOMEM(getVersionStatus);
CELIX_RETURN_IF_ENOMEM(getVersionStatus);

celix_status_t status = CELIX_SUCCESS;
if (!bundleName) {
Expand Down Expand Up @@ -191,9 +191,9 @@ static celix_status_t celix_bundleManifest_setMandatoryAttributes(celix_bundle_m

if (status == CELIX_SUCCESS) {
manifest->symbolicName = celix_utils_strdup(symbolicName);
CELIX_ERR_RET_IF_NULL(manifest->symbolicName);
CELIX_RETURN_IF_NULL(manifest->symbolicName);
manifest->bundleName = celix_utils_strdup(bundleName);
CELIX_ERR_RET_IF_NULL(manifest->bundleName);
CELIX_RETURN_IF_NULL(manifest->bundleName);
manifest->manifestVersion = celix_steal_ptr(manifestVersion);
manifest->bundleVersion = celix_steal_ptr(bundleVersion);
}
Expand All @@ -208,27 +208,27 @@ static celix_status_t celix_bundleManifest_setOptionalAttributes(celix_bundle_ma
celix_autofree char* activatorLib = NULL;
if (lib) {
activatorLib = celix_utils_strdup(lib);
CELIX_ERR_RET_IF_NULL(activatorLib);
CELIX_RETURN_IF_NULL(activatorLib);
}

const char* group = celix_properties_getAsString(manifest->attributes, CELIX_BUNDLE_GROUP, NULL);
celix_autofree char* bundleGroup = NULL;
if (group) {
bundleGroup = celix_utils_strdup(group);
CELIX_ERR_RET_IF_NULL(bundleGroup);
CELIX_RETURN_IF_NULL(bundleGroup);
}

const char* desc = celix_properties_getAsString(manifest->attributes, CELIX_BUNDLE_DESCRIPTION, NULL);
celix_autofree char* description = NULL;
if (desc) {
description = celix_utils_strdup(desc);
CELIX_ERR_RET_IF_NULL(description);
CELIX_RETURN_IF_NULL(description);
}

celix_autoptr(celix_array_list_t) privateLibraries = NULL;
celix_status_t getStatus = celix_properties_getAsStringArrayList(
manifest->attributes, CELIX_BUNDLE_PRIVATE_LIBRARIES, NULL, &privateLibraries);
CELIX_ERR_RET_IF_ENOMEM(getStatus);
CELIX_RETURN_IF_ENOMEM(getStatus);
if (celix_properties_hasKey(manifest->attributes, CELIX_BUNDLE_PRIVATE_LIBRARIES) && !privateLibraries) {
celix_err_pushf(CELIX_BUNDLE_PRIVATE_LIBRARIES " is not a string array. Got: '%s'",
celix_properties_get(manifest->attributes, CELIX_BUNDLE_PRIVATE_LIBRARIES, NULL));
Expand Down
8 changes: 4 additions & 4 deletions libs/utils/include/celix_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ CELIX_UTILS_EXPORT void celix_err_printErrors(FILE* stream, const char* prefix,
CELIX_UTILS_EXPORT int celix_err_dump(char* buf, size_t size, const char* prefix, const char* postfix);

/*!
* Helper macro that return with ENOMEM if the status is ENOMEM and logs an "<func>: Out of memory" error to celix_err.
* Helper macro that returns with ENOMEM if the status is ENOMEM and logs an "<func>: Out of memory" error to celix_err.
*/
#define CELIX_ERR_RET_IF_ENOMEM(status) \
#define CELIX_RETURN_IF_ENOMEM(status) \
do { \
if ((status) == ENOMEM) { \
celix_err_pushf("%s: Out of memory", __func__); \
Expand All @@ -106,9 +106,9 @@ CELIX_UTILS_EXPORT int celix_err_dump(char* buf, size_t size, const char* prefix
} while (0)

/*!
* Helper macro that return with ENOMEM if the arg is NULL and logs an "<func>: Out of memory" error to celix_err.
* Helper macro that returns with ENOMEM if the arg is NULL and logs an "<func>: Out of memory" error to celix_err.
*/
#define CELIX_ERR_RET_IF_NULL(arg) \
#define CELIX_RETURN_IF_NULL(arg) \
do { \
if ((arg) == NULL) { \
celix_err_pushf("%s: Out of memory", __func__); \
Expand Down

0 comments on commit 7b4b0cc

Please sign in to comment.