Skip to content

Commit

Permalink
gh-685: Add celix launcher test suite with error injection
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Jun 2, 2024
1 parent fe5aa59 commit 25df868
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 11 deletions.
1 change: 1 addition & 0 deletions libs/framework/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ if (EI_TESTS)
src/FrameworkBundleWithErrorInjectionTestSuite.cc
src/FrameworkFactoryWithErrorInjectionTestSuite.cc
src/ManifestErrorInjectionTestSuite.cc
src/CelixLauncherErrorInjectionTestSuite.cc
)
target_compile_definitions(test_framework_with_ei PRIVATE
SIMPLE_TEST_BUNDLE1_LOCATION="${SIMPLE_TEST_BUNDLE1}"
Expand Down
81 changes: 81 additions & 0 deletions libs/framework/gtest/src/CelixLauncherErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <gtest/gtest.h>

#include "celix_launcher.h"
#include "celix_stdlib_cleanup.h"
#include "celix_utils.h"
#include "framework.h"
#include "celix_launcher_private.h"

#include "celix_properties_ei.h"
#include "malloc_ei.h"

#define LAUNCH_WAIT_TIMEOUT 100

class CelixLauncherErrorInjectionTestSuite : public ::testing::Test {
public:
CelixLauncherErrorInjectionTestSuite() {
celix_ei_expect_calloc(nullptr, 0, nullptr);
celix_ei_expect_celix_properties_setEntry(nullptr, 0, CELIX_SUCCESS);
}

static int launch(const std::vector<std::string>& args, celix_properties_t* props) {
celix_autofree char* str = nullptr;
if (props) {
EXPECT_EQ(CELIX_SUCCESS, celix_properties_saveToString(props, 0, &str));
}

char** argv = new char*[args.size()];
for (size_t i = 0; i < args.size(); ++i) {
argv[i] = celix_utils_strdup(args[i].c_str());
}

auto rc = celix_launcher_launchAndWait((int)args.size(), argv, str);

for (size_t i = 0; i < args.size(); ++i) {
free(argv[i]);
}
delete [] argv;
return rc;
}
};

TEST_F(CelixLauncherErrorInjectionTestSuite, CreateBundleCacheErrorTest) {
//Given an error injection for calloc is primed when called from framework_create
celix_ei_expect_calloc((void*)framework_create, 0, nullptr);

//When calling celix_launcher_launchAndWait
auto rc = launch({"programName", "-c"}, nullptr);

//Then an exception is expected
EXPECT_EQ(1, rc);
}

TEST_F(CelixLauncherErrorInjectionTestSuite, CombinePropertiesErrorTest) {
//Given an error injection for celix_properties_setEntry is primed when called from celix_launcher_combineProperties
celix_ei_expect_celix_properties_setEntry((void*)celix_launcher_combineProperties, 0, ENOMEM);

//When calling celix_launcher_launchAndWait with configuration properties
auto rc = launch({"programName"}, nullptr);

//Then an exception is expected
EXPECT_EQ(1, rc);
}
13 changes: 3 additions & 10 deletions libs/framework/src/celix_launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ static celix_status_t celix_launcher_createFramework(celix_properties_t* embedde
*/
static celix_status_t celix_launcher_parseOptions(int argc, char* argv[], celix_launcher_options_t* opts);

/**
* @brief Create a combined configuration properties set by combining the embedded properties with the runtime properties.
* @param[in,out] embeddedProps The embedded properties to use and extend with the runtime properties.
* @param[in] configFile The optional runtime properties file to load and use.
*/
static celix_status_t celix_launcher_combineProperties(celix_properties_t* embeddedProps, const celix_properties_t* runtimeProps);

/**
* @brief Print the usage of the Celix launcher command arguments.
*/
Expand Down Expand Up @@ -170,7 +163,7 @@ int celix_launcher_launchAndWait(int argc, char* argv[], const char* embeddedCon
curl_global_cleanup();
#endif
}
return CELIX_LAUNCHER_OK_EXIT_CODE;
return status == CELIX_SUCCESS ? CELIX_LAUNCHER_OK_EXIT_CODE : CELIX_LAUNCHER_ERROR_EXIT_CODE;
}

static celix_status_t celix_launcher_setGlobalFramework(celix_framework_t* fw) {
Expand Down Expand Up @@ -247,7 +240,7 @@ static celix_status_t celix_launcher_createFramework(celix_properties_t* embedde
#endif

*frameworkOut = celix_frameworkFactory_createFramework(embeddedProps);
return *frameworkOut != NULL ? CELIX_SUCCESS : CELIX_ENOMEM;
return *frameworkOut != NULL ? CELIX_SUCCESS : CELIX_FRAMEWORK_EXCEPTION;
}

/**
Expand Down Expand Up @@ -334,7 +327,7 @@ static celix_status_t celix_launcher_createBundleCache(celix_properties_t* embed
return CELIX_SUCCESS;
}

static celix_status_t celix_launcher_combineProperties(celix_properties_t* embeddedProps,
celix_status_t celix_launcher_combineProperties(celix_properties_t* embeddedProps,
const celix_properties_t* runtimeProps) {
if (embeddedProps && runtimeProps) {
CELIX_PROPERTIES_ITERATE(runtimeProps, visit) {
Expand Down
9 changes: 9 additions & 0 deletions libs/framework/src/celix_launcher_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ extern "C" {
*/
void celix_launcher_stopInternal(const int* signal);

/**
* @brief Create a combined configuration properties set by combining the embedded properties with the runtime
* properties.
* @param[in,out] embeddedProps The embedded properties to use and extend with the runtime properties.
* @param[in] configFile The optional runtime properties file to load and use.
*/
celix_status_t celix_launcher_combineProperties(celix_properties_t* embeddedProps,
const celix_properties_t* runtimeProps);

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions libs/utils/error_injector/celix_properties/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ target_link_options(properties_ei INTERFACE
LINKER:--wrap,celix_properties_set
LINKER:--wrap,celix_properties_setLong
LINKER:--wrap,celix_properties_setVersion
LINKER:--wrap,celix_properties_setEntry
LINKER:--wrap,celix_properties_save
LINKER:--wrap,celix_properties_load
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ CELIX_EI_DECLARE(celix_properties_copy, celix_properties_t*);
CELIX_EI_DECLARE(celix_properties_set, celix_status_t);
CELIX_EI_DECLARE(celix_properties_setLong, celix_status_t);
CELIX_EI_DECLARE(celix_properties_setVersion, celix_status_t);
CELIX_EI_DECLARE(celix_properties_setEntry, celix_status_t);
CELIX_EI_DECLARE(celix_properties_save, celix_status_t);
CELIX_EI_DECLARE(celix_properties_load, celix_status_t);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ celix_status_t __wrap_celix_properties_setVersion(celix_properties_t *properties
return __real_celix_properties_setVersion(properties, key, version);
}

celix_status_t __real_celix_properties_setEntry(celix_properties_t* properties,
const char* key,
const celix_properties_entry_t* entry);
CELIX_EI_DEFINE(celix_properties_setEntry, celix_status_t)
celix_status_t __wrap_celix_properties_setEntry(celix_properties_t* properties,
const char* key,
const celix_properties_entry_t* entry) {
CELIX_EI_IMPL(celix_properties_setEntry);
return __real_celix_properties_setEntry(properties, key, entry);
}

celix_status_t
__real_celix_properties_save(const celix_properties_t* properties, const char* filename, int encodeFlags);
CELIX_EI_DEFINE(celix_properties_save, celix_status_t)
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/src/properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ const char* celix_properties_getAsString(const celix_properties_t* properties,
celix_status_t celix_properties_setString(celix_properties_t* properties,
const char* key,
const char* value) {
if (!properties) {
if (!properties) {
return CELIX_SUCCESS; // silently ignore NULL properties
}
char* copy = celix_properties_createString(properties, value);
Expand Down

0 comments on commit 25df868

Please sign in to comment.