You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2023/04/06 21:46:48 UTC
[celix] 02/02: Add malloc/strup error ei for rcm lib tests
This is an automated email from the ASF dual-hosted git repository.
pnoltes pushed a commit to branch feature/cap_req_model_lib
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 9d5dfa83494fe80ba4c1efe1d028e8a91d4d1a89
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Thu Apr 6 23:46:34 2023 +0200
Add malloc/strup error ei for rcm lib tests
---
libs/rcm/gtest/CMakeLists.txt | 15 ++--
...ntCapabilityModelWithErrorInjectionTestSuite.cc | 85 ++++++++++++++++++++++
libs/rcm/src/celix_capability.c | 24 +++---
libs/rcm/src/celix_requirement.c | 30 ++++----
libs/rcm/src/celix_resource.c | 45 ++++++------
5 files changed, 145 insertions(+), 54 deletions(-)
diff --git a/libs/rcm/gtest/CMakeLists.txt b/libs/rcm/gtest/CMakeLists.txt
index bfe6ef45..baa924f3 100644
--- a/libs/rcm/gtest/CMakeLists.txt
+++ b/libs/rcm/gtest/CMakeLists.txt
@@ -25,11 +25,10 @@ target_link_libraries(test_rcm PRIVATE Celix::rcm GTest::gtest GTest::gtest_main
add_test(NAME test_rcm COMMAND test_rcm)
setup_target_for_coverage(test_rcm SCAN_DIR ..)
-#TODO
-#add_executable(test_rcm_with_error_injection
-# src/RequirementCapabilityModelWithErrorInjectionTestSuite.cc
-#)
-#target_link_libraries(test_rcm PRIVATE Celix::rcm GTest::gtest GTest::gtest_main)
-#add_test(NAME test_rcm_with_error_injection COMMAND test_rcm_with_error_injection)
-#setup_target_for_coverage(test_rcm_with_error_injection SCAN_DIR ..)
-
+add_executable(test_rcm_with_error_injection
+ src/RequirementCapabilityModelWithErrorInjectionTestSuite.cc
+)
+target_link_libraries(test_rcm_with_error_injection PRIVATE Celix::rcm GTest::gtest GTest::gtest_main)
+target_link_libraries(test_rcm_with_error_injection PRIVATE Celix::malloc_ei Celix::utils_ei)
+add_test(NAME test_rcm_with_error_injection COMMAND test_rcm_with_error_injection)
+setup_target_for_coverage(test_rcm_with_error_injection SCAN_DIR ..)
diff --git a/libs/rcm/gtest/src/RequirementCapabilityModelWithErrorInjectionTestSuite.cc b/libs/rcm/gtest/src/RequirementCapabilityModelWithErrorInjectionTestSuite.cc
new file mode 100644
index 00000000..40c08741
--- /dev/null
+++ b/libs/rcm/gtest/src/RequirementCapabilityModelWithErrorInjectionTestSuite.cc
@@ -0,0 +1,85 @@
+/*
+ * 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 "malloc_ei.h"
+#include "celix_utils_ei.h"
+
+#include "celix/Properties.h"
+#include "celix_capability.h"
+#include "celix_requirement.h"
+#include "celix_resource.h"
+#include "celix_rcm_err.h"
+
+class RequirementCapabilityModelWithErrorInjectionTestSuite : public ::testing::Test {
+public:
+ RequirementCapabilityModelWithErrorInjectionTestSuite() = default;
+
+ ~RequirementCapabilityModelWithErrorInjectionTestSuite() override {
+ celix_ei_expect_malloc(nullptr, 0, nullptr);
+ celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
+ //TODO add celix_ei_expect_celix_arrayList_create(nullptr, 0, nullptr);
+ celix_rcmErr_resetErrors();
+ }
+};
+
+TEST_F(RequirementCapabilityModelWithErrorInjectionTestSuite, TestRequirementErrorHandling) {
+ //inject error on first malloc call from celix_requirement_create
+ celix_ei_expect_malloc((void*)celix_requirement_create, 0, nullptr);
+ celix_requirement_t* req = celix_requirement_create(nullptr, "test", nullptr);
+ EXPECT_EQ(nullptr, req);
+ EXPECT_EQ(1, celix_rcmErr_getErrorCount());
+
+
+ //inject error on first celix_utils_strdup call from celix_requirement_create
+ celix_ei_expect_celix_utils_strdup((void*)celix_requirement_create, 0, nullptr);
+ req = celix_requirement_create(nullptr, "test", nullptr);
+ EXPECT_EQ(nullptr, req);
+ EXPECT_EQ(2, celix_rcmErr_getErrorCount());
+}
+
+TEST_F(RequirementCapabilityModelWithErrorInjectionTestSuite, TestCapabilityErrorHandling) {
+ //inject error on first malloc call from celix_capability_create
+ celix_ei_expect_malloc((void*)celix_capability_create, 0, nullptr);
+ celix_capability_t* cap = celix_capability_create(nullptr, "test");
+ EXPECT_EQ(nullptr, cap);
+ EXPECT_EQ(1, celix_rcmErr_getErrorCount());
+
+
+ //inject error on first celix_utils_strdup call from celix_capability_create
+ celix_ei_expect_celix_utils_strdup((void*)celix_capability_create, 0, nullptr);
+ cap = celix_capability_create(nullptr, "test");
+ EXPECT_EQ(nullptr, cap);
+ EXPECT_EQ(2, celix_rcmErr_getErrorCount());
+}
+
+TEST_F(RequirementCapabilityModelWithErrorInjectionTestSuite, TestResourceErrorHandling) {
+ //inject error on first malloc call from celix_resource_create
+ celix_ei_expect_malloc((void*)celix_resource_create, 0, nullptr);
+ celix_resource_t* res = celix_resource_create();
+ EXPECT_EQ(nullptr, res);
+ EXPECT_EQ(1, celix_rcmErr_getErrorCount());
+
+ //TODO inject error on first celix_arrayList_create call from celix_resource_create
+
+ //TODO inject error on first celix_arrayList_create call from celix_resource_addCapability
+
+ //TODO inject error on first celix_arrayList_create call from celix_resource_addRequirement
+}
\ No newline at end of file
diff --git a/libs/rcm/src/celix_capability.c b/libs/rcm/src/celix_capability.c
index 7f11f95c..692b1acf 100644
--- a/libs/rcm/src/celix_capability.c
+++ b/libs/rcm/src/celix_capability.c
@@ -40,19 +40,21 @@ celix_capability_t* celix_capability_create(
}
celix_capability_t* cap = malloc(sizeof(*cap));
- if (cap != NULL) {
- cap->resource = resource;
- cap->ns = celix_utils_strdup(ns);
- cap->attributes = celix_properties_create();
- cap->directives = celix_properties_create();
- if (cap->ns == NULL || cap->attributes == NULL || cap->directives == NULL) {
- celix_rcmErr_push("Failed to allocate celix_capability_t fields. Out of memory.");
- celix_capability_destroy(cap);
- cap = NULL;
- }
- } else {
+ if (cap == NULL) {
celix_rcmErr_push("Failed to allocate celix_capability_t. Out of memory.");
+ return NULL;
+ }
+
+ cap->resource = resource;
+ cap->ns = celix_utils_strdup(ns);
+ cap->attributes = celix_properties_create();
+ cap->directives = celix_properties_create();
+ if (cap->ns == NULL || cap->attributes == NULL || cap->directives == NULL) {
+ celix_rcmErr_push("Failed to allocate celix_capability_t fields. Out of memory.");
+ celix_capability_destroy(cap);
+ return NULL;
}
+
return cap;
}
diff --git a/libs/rcm/src/celix_requirement.c b/libs/rcm/src/celix_requirement.c
index 38b940cc..6e796f88 100644
--- a/libs/rcm/src/celix_requirement.c
+++ b/libs/rcm/src/celix_requirement.c
@@ -43,21 +43,25 @@ celix_requirement_t* celix_requirement_create(
}
celix_requirement_t* req = malloc(sizeof(*req));
- if (req != NULL) {
- req->resource = resource;
- req->ns = celix_utils_strdup(ns);
- req->attributes = celix_properties_create();
- req->directives = celix_properties_create();
- if (req->ns == NULL || req->attributes == NULL || req->directives == NULL) {
- celix_rcmErr_push("Failed to allocate celix_requirement_t fields. Out of memory.");
- celix_requirement_destroy(req);
- req = NULL;
- } else if (filter != NULL) {
- celix_properties_set(req->directives, CELIX_REQUIREMENT_DIRECTIVE_FILTER, filter);
- }
- } else {
+ if (req == NULL) {
celix_rcmErr_push("Failed to allocate celix_requirement_t. Out of memory.");
+ return NULL;
+ }
+
+ req->resource = resource;
+ req->ns = celix_utils_strdup(ns);
+ req->attributes = celix_properties_create();
+ req->directives = celix_properties_create();
+ if (req->ns == NULL || req->attributes == NULL || req->directives == NULL) {
+ celix_rcmErr_push("Failed to allocate celix_requirement_t fields. Out of memory.");
+ celix_requirement_destroy(req);
+ return NULL;
+ }
+
+ if (filter != NULL) {
+ celix_properties_set(req->directives, CELIX_REQUIREMENT_DIRECTIVE_FILTER, filter);
}
+
return req;
}
diff --git a/libs/rcm/src/celix_resource.c b/libs/rcm/src/celix_resource.c
index e981e0ee..fbe51083 100644
--- a/libs/rcm/src/celix_resource.c
+++ b/libs/rcm/src/celix_resource.c
@@ -46,29 +46,30 @@ static void celix_resource_freeReqOrCapList(void* list) {
celix_resource_t* celix_resource_create() {
celix_resource_t* res = malloc(sizeof(*res));
- if (res != NULL) {
- celix_array_list_create_options_t opts = CELIX_EMPTY_ARRAY_LIST_CREATE_OPTIONS;
- opts.simpleRemovedCallback = celix_resource_destroyCapability;
- res->allCapabilities = celix_arrayList_createWithOptions(&opts);
-
- opts.simpleRemovedCallback = celix_resource_destroyRequirement;
- res->allRequirements = celix_arrayList_createWithOptions(&opts);
-
- celix_string_hash_map_create_options_t mapOpts = CELIX_EMPTY_STRING_HASH_MAP_CREATE_OPTIONS;
- mapOpts.simpleRemovedCallback = celix_resource_freeReqOrCapList;
- res->capabilitiesByNamespace = celix_stringHashMap_createWithOptions(&mapOpts);
- res->requirementsByNamespace = celix_stringHashMap_createWithOptions(&mapOpts);
-
- if ( res->allCapabilities == NULL ||
- res->allRequirements == NULL ||
- res->capabilitiesByNamespace == NULL ||
- res->requirementsByNamespace == NULL) {
- celix_rcmErr_push("Failed to allocate celix_resource_t fields. Out of memory.");
- celix_resource_destroy(res);
- res = NULL;
- }
- } else {
+ if (res == NULL) {
celix_rcmErr_push("Failed to allocate celix_resource_t. Out of memory.");
+ return NULL;
+ }
+
+ celix_array_list_create_options_t opts = CELIX_EMPTY_ARRAY_LIST_CREATE_OPTIONS;
+ opts.simpleRemovedCallback = celix_resource_destroyCapability;
+ res->allCapabilities = celix_arrayList_createWithOptions(&opts);
+
+ opts.simpleRemovedCallback = celix_resource_destroyRequirement;
+ res->allRequirements = celix_arrayList_createWithOptions(&opts);
+
+ celix_string_hash_map_create_options_t mapOpts = CELIX_EMPTY_STRING_HASH_MAP_CREATE_OPTIONS;
+ mapOpts.simpleRemovedCallback = celix_resource_freeReqOrCapList;
+ res->capabilitiesByNamespace = celix_stringHashMap_createWithOptions(&mapOpts);
+ res->requirementsByNamespace = celix_stringHashMap_createWithOptions(&mapOpts);
+
+ if ( res->allCapabilities == NULL ||
+ res->allRequirements == NULL ||
+ res->capabilitiesByNamespace == NULL ||
+ res->requirementsByNamespace == NULL) {
+ celix_rcmErr_push("Failed to allocate celix_resource_t fields. Out of memory.");
+ celix_resource_destroy(res);
+ return NULL;
}
return res;
}