This is an automated email from the ASF dual-hosted git repository. pnoltes pushed a commit to branch feature/674-improve-properties in repository https://gitbox.apache.org/repos/asf/celix.git
commit 301064f6730cd28fbff450364fcaf7eecce3275c Author: Pepijn Noltes <[email protected]> AuthorDate: Sun Jan 21 16:35:01 2024 +0100 Add rsa_utils static lib --- bundles/remote_services/CMakeLists.txt | 1 + bundles/remote_services/rsa_utils/CMakeLists.txt | 39 +++++++++++++ .../remote_services/rsa_utils/gtest/CMakeLists.txt | 35 ++++++++++++ .../gtest/src/RsaUtilsErrorInjectionTestSuite.cc | 53 ++++++++++++++++++ .../rsa_utils/gtest/src/RsaUtilsTestSuite.cc | 65 ++++++++++++++++++++++ .../rsa_utils/include/celix_rsa_utils.h | 51 +++++++++++++++++ .../rsa_utils/src/celix_rsa_utils.c | 57 +++++++++++++++++++ 7 files changed, 301 insertions(+) diff --git a/bundles/remote_services/CMakeLists.txt b/bundles/remote_services/CMakeLists.txt index 1d069192..b1ba1db7 100644 --- a/bundles/remote_services/CMakeLists.txt +++ b/bundles/remote_services/CMakeLists.txt @@ -20,6 +20,7 @@ if (REMOTE_SERVICE_ADMIN) add_subdirectory(remote_services_api) add_subdirectory(rsa_spi) add_subdirectory(rsa_common) + add_subdirectory(rsa_utils) add_subdirectory(rsa_dfi_utils) add_subdirectory(discovery_common) add_subdirectory(discovery_configured) diff --git a/bundles/remote_services/rsa_utils/CMakeLists.txt b/bundles/remote_services/rsa_utils/CMakeLists.txt new file mode 100644 index 00000000..b9fd94d9 --- /dev/null +++ b/bundles/remote_services/rsa_utils/CMakeLists.txt @@ -0,0 +1,39 @@ +# 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. + +set(RSA_UTILS_SRC + src/celix_rsa_utils.c +) + +add_library(rsa_utils STATIC ${RSA_UTILS_SRC}) +set_target_properties(rsa_utils PROPERTIES OUTPUT_NAME "celix_rsa_utils") +target_include_directories(rsa_utils PRIVATE src) +target_include_directories(rsa_utils PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>) +target_link_libraries(rsa_utils PUBLIC Celix::framework) + +install(TARGETS rsa_utils EXPORT celix LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT rsa + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/celix/rsa_utils) +install(DIRECTORY include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/celix/rsa_utils/ + COMPONENT rsa) + +#Setup target aliases to match external usage +add_library(Celix::rsa_utils ALIAS rsa_utils) + +if (ENABLE_TESTING) + add_subdirectory(gtest) +endif () diff --git a/bundles/remote_services/rsa_utils/gtest/CMakeLists.txt b/bundles/remote_services/rsa_utils/gtest/CMakeLists.txt new file mode 100644 index 00000000..3d36df1c --- /dev/null +++ b/bundles/remote_services/rsa_utils/gtest/CMakeLists.txt @@ -0,0 +1,35 @@ +# 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_executable(test_rsa_utils + src/RsaUtilsTestSuite.cc +) + +target_link_libraries(test_rsa_utils PRIVATE rsa_utils GTest::gtest GTest::gtest_main) +target_include_directories(test_utils PRIVATE ../src) #for version_private (needs refactoring of test) +add_test(NAME test_rsa_utils COMMAND test_rsa_utils) +setup_target_for_coverage(test_rsa_utils SCAN_DIR ..) + +if (EI_TESTS) + add_executable(test_rsa_utils_ei + src/RsaUtilsErrorInjectionTestSuite.cc + ) + target_link_libraries(test_rsa_utils_ei PRIVATE rsa_utils GTest::gtest GTest::gtest_main) + target_link_libraries(test_rsa_utils_ei PRIVATE properties_ei) + add_test(NAME test_rsa_utils_ei COMMAND test_rsa_utils_ei) + setup_target_for_coverage(test_rsa_utils_ei SCAN_DIR ..) +endif () diff --git a/bundles/remote_services/rsa_utils/gtest/src/RsaUtilsErrorInjectionTestSuite.cc b/bundles/remote_services/rsa_utils/gtest/src/RsaUtilsErrorInjectionTestSuite.cc new file mode 100644 index 00000000..52895667 --- /dev/null +++ b/bundles/remote_services/rsa_utils/gtest/src/RsaUtilsErrorInjectionTestSuite.cc @@ -0,0 +1,53 @@ +/* +* 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_rsa_utils.h" +#include "celix_properties_ei.h" +#include "celix_err.h" + +class RsaUtilsErrorInjectionTestSuite : public ::testing::Test { + public: + RsaUtilsErrorInjectionTestSuite() { + celix_ei_expect_celix_properties_copy(nullptr, 0, nullptr); + celix_err_printErrors(stderr, nullptr, nullptr); + } + ~RsaUtilsErrorInjectionTestSuite() override = default; +}; + +TEST_F(RsaUtilsErrorInjectionTestSuite, PropertiesCopyFailureTest) { + // Given an error injection for celix_properties_copy + celix_ei_expect_celix_properties_copy((void*)celix_rsaUtils_createServicePropertiesFromEndpointProperties, 0, nullptr); + + // And an endpointProperties + celix_autoptr(celix_properties_t) endpointProperties = celix_properties_create(); + EXPECT_NE(endpointProperties, nullptr); + + // When calling celix_rsaUtils_createServicePropertiesFromEndpointProperties + celix_properties_t* serviceProperties = nullptr; + celix_status_t status = + celix_rsaUtils_createServicePropertiesFromEndpointProperties(endpointProperties, &serviceProperties); + + // Then the status is CELIX_ENOMEM + EXPECT_EQ(status, CELIX_ENOMEM); + + // And the serviceProperties is nullptr + EXPECT_EQ(serviceProperties, nullptr); +} diff --git a/bundles/remote_services/rsa_utils/gtest/src/RsaUtilsTestSuite.cc b/bundles/remote_services/rsa_utils/gtest/src/RsaUtilsTestSuite.cc new file mode 100644 index 00000000..4183296c --- /dev/null +++ b/bundles/remote_services/rsa_utils/gtest/src/RsaUtilsTestSuite.cc @@ -0,0 +1,65 @@ +/* +* 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_rsa_utils.h" +#include "celix_properties.h" +#include "celix_constants.h" + +class RsaUtilsTestSuite : public ::testing::Test { + public: + RsaUtilsTestSuite() = default; + ~RsaUtilsTestSuite() override = default; +}; + +TEST_F(RsaUtilsTestSuite, CreateServicePropertiesFromEndpointPropertiesTest) { + celix_autoptr(celix_properties_t) endpointProperties = celix_properties_create(); + celix_properties_set(endpointProperties, CELIX_FRAMEWORK_SERVICE_RANKING, "10"); + celix_properties_set(endpointProperties, CELIX_FRAMEWORK_SERVICE_VERSION, "1.0.0"); + + celix_autoptr(celix_properties_t) serviceProperties = nullptr; + celix_status_t status = + celix_rsaUtils_createServicePropertiesFromEndpointProperties(endpointProperties, &serviceProperties); + ASSERT_EQ(status, CELIX_SUCCESS); + ASSERT_TRUE(serviceProperties != nullptr); + + const auto* entry = celix_properties_getEntry(serviceProperties, CELIX_FRAMEWORK_SERVICE_RANKING); + ASSERT_NE(entry, nullptr); + EXPECT_EQ(entry->valueType, CELIX_PROPERTIES_VALUE_TYPE_LONG); + EXPECT_EQ(entry->typed.longValue, 10L); + + entry = celix_properties_getEntry(serviceProperties, CELIX_FRAMEWORK_SERVICE_VERSION); + ASSERT_NE(entry, nullptr); + EXPECT_EQ(entry->valueType, CELIX_PROPERTIES_VALUE_TYPE_VERSION); + EXPECT_EQ(1, celix_version_getMajor(entry->typed.versionValue)); + EXPECT_EQ(0, celix_version_getMinor(entry->typed.versionValue)); + EXPECT_EQ(0, celix_version_getMicro(entry->typed.versionValue)); + EXPECT_STREQ("", celix_version_getQualifier(entry->typed.versionValue)); +} + +TEST_F(RsaUtilsTestSuite, CreateServicePropertiesFromEndpointPropertiesWithNullArgTest) { + // NULL argument will result in an empty service properties set + celix_autoptr(celix_properties_t) serviceProperties = nullptr; + celix_status_t status = + celix_rsaUtils_createServicePropertiesFromEndpointProperties(nullptr, &serviceProperties); + ASSERT_EQ(status, CELIX_SUCCESS); + ASSERT_NE(nullptr, serviceProperties); + EXPECT_EQ(0, celix_properties_size(serviceProperties)); +} diff --git a/bundles/remote_services/rsa_utils/include/celix_rsa_utils.h b/bundles/remote_services/rsa_utils/include/celix_rsa_utils.h new file mode 100644 index 00000000..f22e04ed --- /dev/null +++ b/bundles/remote_services/rsa_utils/include/celix_rsa_utils.h @@ -0,0 +1,51 @@ +/* + * 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_CELIX_RSA_UTILS_H +#define CELIX_CELIX_RSA_UTILS_H + +#include "celix_errno.h" +#include "celix_properties_type.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Create a typed service properties from endpoint properties. + * + * The conversion ensures that "service.ranking" and "bundle.id" (if present) are set as long values and that the + * "service.version" (if present) is set as version. + * + * Note that the "service.id" long "service.bundleid" properties are set during service registration and + * therefore not set by this function. + * + * @param[in] endpointProperties + * @param[out] serviceProperties + * @return CELIX_SUCCESS if successfully, CELIX_ENOMEM if out of memory. + */ +celix_status_t +celix_rsaUtils_createServicePropertiesFromEndpointProperties(const celix_properties_t* endpointProperties, + celix_properties_t** serviceProperties); + +#ifdef __cplusplus +}; +#endif + +#endif // CELIX_CELIX_RSA_UTILS_H diff --git a/bundles/remote_services/rsa_utils/src/celix_rsa_utils.c b/bundles/remote_services/rsa_utils/src/celix_rsa_utils.c new file mode 100644 index 00000000..d20e69b9 --- /dev/null +++ b/bundles/remote_services/rsa_utils/src/celix_rsa_utils.c @@ -0,0 +1,57 @@ +/* +* 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_rsa_utils.h" +#include "celix_properties.h" +#include "celix_constants.h" + +celix_status_t +celix_rsaUtils_createServicePropertiesFromEndpointProperties(const celix_properties_t* endpointProperties, + celix_properties_t** serviceProperties) { + celix_autoptr(celix_properties_t) props = celix_properties_copy(endpointProperties); + if (!props) { + return CELIX_ENOMEM; + } + + celix_status_t status = CELIX_SUCCESS; + long bundleId = celix_properties_getAsLong(props, CELIX_FRAMEWORK_BUNDLE_ID, -1L); + if (bundleId >= 0) { + status = celix_properties_setLong(props, CELIX_FRAMEWORK_BUNDLE_ID, bundleId); + } + + const celix_properties_entry_t* entry = celix_properties_getEntry(props, CELIX_FRAMEWORK_SERVICE_RANKING); + if (status == CELIX_SUCCESS && entry && entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_STRING) { + long ranking = celix_properties_getAsLong(props, CELIX_FRAMEWORK_SERVICE_RANKING, 0L); + status = celix_properties_setLong(props, CELIX_FRAMEWORK_SERVICE_RANKING, ranking); + } + + const char* versionStr = celix_properties_get(props, CELIX_FRAMEWORK_SERVICE_VERSION, NULL); + if (status == CELIX_SUCCESS && versionStr) { + celix_autoptr(celix_version_t) version = NULL; + status = celix_version_parse(versionStr, &version); + if (status == CELIX_SUCCESS) { + status = celix_properties_assignVersion(props, CELIX_FRAMEWORK_SERVICE_VERSION, celix_steal_ptr(version)); + } + } + + if (status == CELIX_SUCCESS) { + *serviceProperties = celix_steal_ptr(props); + } + return status; +}
