You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@celix.apache.org by "pnoltes (via GitHub)" <gi...@apache.org> on 2023/01/23 18:59:04 UTC

[GitHub] [celix] pnoltes commented on a diff in pull request #473: Add basic error injector mechanism.

pnoltes commented on code in PR #473:
URL: https://github.com/apache/celix/pull/473#discussion_r1084393570


##########
misc/error_injector/CMakeLists.txt:
##########
@@ -0,0 +1,25 @@
+# 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.
+
+add_library(error_injector INTERFACE error_injector.h)

Review Comment:
   nitpick: IMO this library can be subdir of the libs directory and also has as name `Celix::error_injector` 



##########
misc/error_injector/error_injector.h:
##########
@@ -0,0 +1,84 @@
+/*
+ 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.
+ */
+
+#ifndef CELIX_ERROR_INJECTOR_H
+#define CELIX_ERROR_INJECTOR_H

Review Comment:
   nitpick: please rename to celix_error_injector.h and move it to a include dir.



##########
misc/error_injector/celix_properties/CMakeLists.txt:
##########
@@ -0,0 +1,23 @@
+# 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.
+
+add_library(celix_properties_ei STATIC celix_properties_ei.cpp)

Review Comment:
   nitpick: In Celix most resent C++ sources files use the `cc` extension



##########
bundles/pubsub/pubsub_protocol/pubsub_protocol_lib/gtest/CMakeLists.txt:
##########
@@ -17,10 +17,16 @@
 
 add_executable(celix_pswp_common_tests src/PS_WP_common_tests.cc)
 target_include_directories(celix_pswp_common_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
-target_link_libraries(celix_pswp_common_tests PRIVATE celix_pubsub_protocol_lib GTest::gtest Celix::pubsub_spi GTest::gtest_main ${CMAKE_DL_LIBS})
-if (NOT ENABLE_ADDRESS_SANITIZER)
-    target_compile_definitions(celix_pswp_common_tests PRIVATE ENABLE_MALLOC_RETURN_NULL_TESTS)
-endif ()
+target_link_libraries(celix_pswp_common_tests PRIVATE celix_pubsub_protocol_lib GTest::gtest Celix::pubsub_spi GTest::gtest_main)
 
 add_test(NAME celix_pswp_common_tests COMMAND celix_pswp_common_tests)
-setup_target_for_coverage(celix_pswp_common_tests SCAN_DIR ..)
\ No newline at end of file
+setup_target_for_coverage(celix_pswp_common_tests SCAN_DIR ..)
+
+if (LINKER_WRAP_SUPPORTED)

Review Comment:
   Nice. Yeah so only if wrap is supported this is used and this includes the coverage build.



##########
bundles/pubsub/pubsub_protocol/pubsub_protocol_lib/gtest/src/PS_WP_common_ei_tests.cc:
##########
@@ -0,0 +1,233 @@
+/**
+ *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 <iostream>
+#include <cstring>
+#include <celix_properties_ei.h>
+#include <malloc_ei.h>
+
+#include "pubsub_wire_protocol_common.h"
+
+class WireProtocolCommonEiTest : public ::testing::Test {
+public:
+    WireProtocolCommonEiTest() = default;
+    ~WireProtocolCommonEiTest() override {
+        celix_ei_expect_realloc(nullptr, 0, nullptr);
+        celix_ei_expect_calloc(nullptr, 0, nullptr);
+        celix_ei_expect_celix_properties_create(nullptr, 0, nullptr);
+    };
+};
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_EncodeMetadataWithNoMemoryLeft) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 0;
+    message.metadata.metadata = celix_properties_create();
+    celix_properties_set(message.metadata.metadata, "key1", "value1");
+
+    //Scenario: No mem with no pre-allocated data
+    //Given (mocked) realloc is forced to return NULL
+    celix_ei_expect_realloc((void *)pubsubProtocol_encodeMetadata, 0, nullptr);
+
+    //When I try to encode a metadata
+    char *data = nullptr;
+    size_t length = 0;
+    size_t contentLength = 0;
+    auto status = pubsubProtocol_encodeMetadata(&message, &data, &length, &contentLength);
+    //Then I expect a failure
+    EXPECT_NE(status, CELIX_SUCCESS);
+
+    //Scenario: No mem with some pre-allocated data
+    //Given a data set with some space
+    data = (char*)malloc(16);
+    length = 16;
+
+    //And (mocked) realloc is forced to return NULL
+    celix_ei_expect_realloc((void *)pubsubProtocol_encodeMetadata, 0, nullptr);
+
+    //When I try to encode a metadata
+    status = pubsubProtocol_encodeMetadata(&message, &data, &length, &contentLength);
+
+    //Then I expect a failure
+    EXPECT_NE(status, CELIX_SUCCESS);
+
+    free(data);
+    celix_properties_destroy(message.metadata.metadata);
+}
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_DecodeMetadataWithSingleEntries) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 0;
+    message.metadata.metadata = nullptr;
+
+    char* data = strdup("ABCD4:key1,6:value1,"); //note 1 entry
+    auto len = strlen(data);
+    pubsubProtocol_writeInt((unsigned char*)data, 0, message.header.convertEndianess, 1);
+    auto status = pubsubProtocol_decodeMetadata((void*)data, len, &message);
+
+    EXPECT_EQ(status, CELIX_SUCCESS);
+    EXPECT_EQ(1, celix_properties_size(message.metadata.metadata));
+    EXPECT_STREQ("value1", celix_properties_get(message.metadata.metadata, "key1", "not-found"));
+
+    free(data);
+    celix_properties_destroy(message.metadata.metadata);
+}
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_DencodeMetadataWithMultipleEntries) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 1;
+    message.metadata.metadata = nullptr;
+
+    char* data = strdup("ABCD4:key1,6:value1,4:key2,6:value2,6:key111,8:value111,"); //note 3 entries
+    auto len = strlen(data);
+    pubsubProtocol_writeInt((unsigned char*)data, 0, message.header.convertEndianess, 3);
+    auto status = pubsubProtocol_decodeMetadata((void*)data, len, &message);
+
+    EXPECT_EQ(status, CELIX_SUCCESS);
+    EXPECT_EQ(3, celix_properties_size(message.metadata.metadata));
+    EXPECT_STREQ("value1", celix_properties_get(message.metadata.metadata, "key1", "not-found"));
+    EXPECT_STREQ("value2", celix_properties_get(message.metadata.metadata, "key2", "not-found"));
+    EXPECT_STREQ("value111", celix_properties_get(message.metadata.metadata, "key111", "not-found"));
+
+    free(data);
+    celix_properties_destroy(message.metadata.metadata);
+}
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_DencodeMetadataWithExtraEntries) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 1;
+    message.metadata.metadata = nullptr;
+
+    char* data = strdup("ABCD4:key1,6:value1,4:key2,6:value2,6:key111,8:value111,"); //note 3 entries
+    auto len = strlen(data);
+    pubsubProtocol_writeInt((unsigned char*)data, 0, message.header.convertEndianess, 2);
+    auto status = pubsubProtocol_decodeMetadata((void*)data, len, &message);
+    // the 3rd entry should be ignored
+    EXPECT_EQ(status, CELIX_SUCCESS);
+    EXPECT_EQ(2, celix_properties_size(message.metadata.metadata));
+    EXPECT_STREQ("value1", celix_properties_get(message.metadata.metadata, "key1", "not-found"));
+    EXPECT_STREQ("value2", celix_properties_get(message.metadata.metadata, "key2", "not-found"));
+    EXPECT_STREQ("not-found", celix_properties_get(message.metadata.metadata, "key111", "not-found"));
+
+    free(data);
+    celix_properties_destroy(message.metadata.metadata);
+}
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_DencodeMetadataMissingEntries) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 1;
+    message.metadata.metadata = nullptr;
+
+    char* data = strdup("ABCD4:key1,6:value1,4:key2,6:value2,6:key111,8:value111,"); //note 3 entries
+    auto len = strlen(data);
+    pubsubProtocol_writeInt((unsigned char*)data, 0, message.header.convertEndianess, 4);
+    auto status = pubsubProtocol_decodeMetadata((void*)data, len, &message);
+
+    EXPECT_EQ(status, CELIX_INVALID_SYNTAX);
+    EXPECT_EQ(nullptr, message.metadata.metadata);
+
+    free(data);
+}
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_DecodeMetadataWithSingleEntryWithIncompleteValue) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 0;
+    message.metadata.metadata = nullptr;
+
+    char* data = strdup("ABCD4:key1,6:val,"); //note 1 entry with short value
+    auto len = strlen(data);
+    pubsubProtocol_writeInt((unsigned char*)data, 0, message.header.convertEndianess, 1);
+    auto status = pubsubProtocol_decodeMetadata((void*)data, len, &message);
+
+    EXPECT_EQ(status, CELIX_INVALID_SYNTAX);
+    EXPECT_EQ(nullptr, message.metadata.metadata);
+
+    free(data);
+}
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_DecodeMetadataTooShort) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 0;
+    message.metadata.metadata = nullptr;
+
+    char* data = strdup("ABCD4:key1,6:value1,"); //note 1 entry
+    pubsubProtocol_writeInt((unsigned char*)data, 0, message.header.convertEndianess, 1);
+    auto status = pubsubProtocol_decodeMetadata((void*)data, 3, &message); // not enough data for `nOfElements`
+
+    EXPECT_EQ(status, CELIX_INVALID_SYNTAX);
+    EXPECT_EQ(nullptr, message.metadata.metadata);
+
+    free(data);
+}
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_DecodeEmptyMetadata) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 0;
+    message.metadata.metadata = nullptr;
+
+    uint32_t data = 0;
+    auto status = pubsubProtocol_decodeMetadata((void*)&data, 4, &message);
+
+    EXPECT_EQ(status, CELIX_SUCCESS);
+    EXPECT_EQ(nullptr, message.metadata.metadata);
+
+    // incorrect `nOfElements`
+    data = 4;
+    status = pubsubProtocol_decodeMetadata((void*)&data, 4, &message);
+
+    EXPECT_EQ(status, CELIX_INVALID_SYNTAX);
+    EXPECT_EQ(nullptr, message.metadata.metadata);
+}
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_NotEnoughMemoryForMultipleEntries) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 1;
+    message.metadata.metadata = nullptr;
+
+    char* data = strdup("ABCD4:key1,6:value1,4:key2,6:value2,6:key111,8:value111,"); //note 3 entries
+    auto len = strlen(data);
+    pubsubProtocol_writeInt((unsigned char*)data, 0, message.header.convertEndianess, 3);
+    for (int i = 0; i < 6; ++i) {
+        celix_ei_expect_calloc((void *)pubsubProtocol_decodeMetadata, 0, nullptr, i+1);

Review Comment:
   Just to check.
   
   So the `i+1` call to realloc from pubsubProtocol_encodeMetadata (0 level deep) should fail and return a nullptr?



##########
misc/error_injector/celix_properties/celix_properties_ei.cpp:
##########
@@ -0,0 +1,29 @@
+/*
+ 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 <celix_properties_ei.h>

Review Comment:
   nitpick: `#include "celix_properties_ei.h"`



##########
misc/error_injector/error_injector.h:
##########
@@ -0,0 +1,84 @@
+/*
+ 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.
+ */
+
+#ifndef CELIX_ERROR_INJECTOR_H
+#define CELIX_ERROR_INJECTOR_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <assert.h>
+#include <dlfcn.h>
+#include <stddef.h>
+

Review Comment:
   Could this macro get some documentation. Something like:
   
   ```
   /**
    * @brief Get the caller library address based on the return address of level frame up t he call stack.
    */
   #define CELIX_EI_GET_CALLER(addr, level)                                   \
   do {                                                                       \
       Dl_info dlinfo;                                                        \
       dladdr(__builtin_return_address(level), &dlinfo);                     \
       (addr) = dlinfo.dli_saddr;                                             \
   } while(0)
   ```



##########
misc/error_injector/malloc/malloc_ei.cpp:
##########
@@ -0,0 +1,43 @@
+/*
+ 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 <malloc_ei.h>
+
+extern "C" {
+void *__real_malloc(size_t);

Review Comment:
   Just to check. the symbol `__real_malloc` which pont to `malloc` due to ld `--wrap`?
   And the any usage of `malloc` will result in symbol `__warp_malloc` due to ld `--wrap`?



##########
bundles/pubsub/pubsub_protocol/pubsub_protocol_lib/gtest/src/PS_WP_common_ei_tests.cc:
##########
@@ -0,0 +1,233 @@
+/**
+ *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 <iostream>
+#include <cstring>
+#include <celix_properties_ei.h>
+#include <malloc_ei.h>
+
+#include "pubsub_wire_protocol_common.h"
+
+class WireProtocolCommonEiTest : public ::testing::Test {
+public:
+    WireProtocolCommonEiTest() = default;
+    ~WireProtocolCommonEiTest() override {
+        celix_ei_expect_realloc(nullptr, 0, nullptr);
+        celix_ei_expect_calloc(nullptr, 0, nullptr);
+        celix_ei_expect_celix_properties_create(nullptr, 0, nullptr);
+    };
+};
+
+TEST_F(WireProtocolCommonEiTest, WireProtocolCommonTest_EncodeMetadataWithNoMemoryLeft) {
+    pubsub_protocol_message_t message;
+    message.header.convertEndianess = 0;
+    message.metadata.metadata = celix_properties_create();
+    celix_properties_set(message.metadata.metadata, "key1", "value1");
+
+    //Scenario: No mem with no pre-allocated data
+    //Given (mocked) realloc is forced to return NULL
+    celix_ei_expect_realloc((void *)pubsubProtocol_encodeMetadata, 0, nullptr);

Review Comment:
   Just to check. 
   
   So the first call to realloc from pubsubProtocol_encodeMetadata (0 level deep) should fail and return a nullptr?



##########
misc/error_injector/error_injector.h:
##########
@@ -0,0 +1,84 @@
+/*
+ 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.
+ */
+
+#ifndef CELIX_ERROR_INJECTOR_H
+#define CELIX_ERROR_INJECTOR_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <assert.h>
+#include <dlfcn.h>
+#include <stddef.h>
+

Review Comment:
   Or maybe a small readme how to use this (declare, define, etc)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org