This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/685-remove-bundle-revision
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 60a5d6a707c8cf54a1968d790ac923597b15164a
Author: Pepijn Noltes <[email protected]>
AuthorDate: Sat Jan 4 17:35:50 2025 +0100

    gh-685: Add dirent_ei and use dirent_ei in bnd cache test
---
 libs/error_injector/CMakeLists.txt                 |  1 +
 libs/error_injector/dirent/CMakeLists.txt          | 25 ++++++++++++++++
 libs/error_injector/dirent/include/dirent_ei.h     | 34 ++++++++++++++++++++++
 libs/error_injector/dirent/src/dirent_ei.cc        | 34 ++++++++++++++++++++++
 libs/framework/gtest/CMakeLists.txt                |  1 +
 .../src/CelixBundleCacheErrorInjectionTestSuite.cc | 12 +++++++-
 libs/framework/src/celix_bundle_cache.c            | 12 ++++----
 7 files changed, 111 insertions(+), 8 deletions(-)

diff --git a/libs/error_injector/CMakeLists.txt 
b/libs/error_injector/CMakeLists.txt
index 76a9f99f7..1fb2048f1 100644
--- a/libs/error_injector/CMakeLists.txt
+++ b/libs/error_injector/CMakeLists.txt
@@ -37,6 +37,7 @@ add_subdirectory(sys_shm)
 add_subdirectory(socket)
 add_subdirectory(pthread)
 add_subdirectory(unistd)
+add_subdirectory(dirent)
 
 celix_subproject(ERROR_INJECTOR_MDNSRESPONDER "Option to enable building the 
mdnsresponder error injector" OFF)
 if (ERROR_INJECTOR_MDNSRESPONDER)
diff --git a/libs/error_injector/dirent/CMakeLists.txt 
b/libs/error_injector/dirent/CMakeLists.txt
new file mode 100644
index 000000000..8d777d026
--- /dev/null
+++ b/libs/error_injector/dirent/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(dirent_ei STATIC src/dirent_ei.cc)
+
+target_include_directories(dirent_ei PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
+target_link_libraries(dirent_ei PUBLIC Celix::error_injector)
+target_link_options(dirent_ei INTERFACE
+        LINKER:--wrap,opendir
+        )
+add_library(Celix::dirent_ei ALIAS dirent_ei)
diff --git a/libs/error_injector/dirent/include/dirent_ei.h 
b/libs/error_injector/dirent/include/dirent_ei.h
new file mode 100644
index 000000000..26e89c428
--- /dev/null
+++ b/libs/error_injector/dirent/include/dirent_ei.h
@@ -0,0 +1,34 @@
+/*
+ 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_DIRENT_EI_H
+#define CELIX_DIRENT_EI_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "celix_error_injector.h"
+#include <dirent.h>
+
+CELIX_EI_DECLARE(opendir, DIR*);
+
+#ifdef __cplusplus
+}
+#endif
+#endif //CELIX_DIRENT_EI_H
diff --git a/libs/error_injector/dirent/src/dirent_ei.cc 
b/libs/error_injector/dirent/src/dirent_ei.cc
new file mode 100644
index 000000000..26896368b
--- /dev/null
+++ b/libs/error_injector/dirent/src/dirent_ei.cc
@@ -0,0 +1,34 @@
+/*
+ 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 <errno.h>
+#include "dirent_ei.h"
+
+extern "C" {
+
+DIR* __real_opendir(const char* __name);
+CELIX_EI_DEFINE(opendir, DIR*)
+DIR* __wrap_opendir(const char* __name) {
+    errno = EMFILE;
+    CELIX_EI_IMPL(opendir);
+    errno = 0;
+    return __real_opendir(__name);
+}
+
+}
\ No newline at end of file
diff --git a/libs/framework/gtest/CMakeLists.txt 
b/libs/framework/gtest/CMakeLists.txt
index 3a5016756..08e0c4038 100644
--- a/libs/framework/gtest/CMakeLists.txt
+++ b/libs/framework/gtest/CMakeLists.txt
@@ -173,6 +173,7 @@ if (EI_TESTS)
             Celix::hmap_ei
             Celix::stdio_ei
             Celix::string_ei
+            Celix::dirent_ei
             GTest::gtest GTest::gtest_main
     )
 
diff --git 
a/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc 
b/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc
index 591f11cbc..3f2e07dfe 100644
--- a/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc
+++ b/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc
@@ -37,6 +37,7 @@
 #include "malloc_ei.h"
 #include "celix_bundle_manifest.h"
 #include "unistd_ei.h"
+#include "dirent_ei.h"
 
 class CelixBundleCacheErrorInjectionTestSuite : public ::testing::Test {
   public:
@@ -55,15 +56,18 @@ class CelixBundleCacheErrorInjectionTestSuite : public 
::testing::Test {
         celix_ei_expect_celix_stringHashMap_create(nullptr, 0, nullptr);
         celix_ei_expect_malloc(nullptr, 0, nullptr);
         celix_ei_expect_calloc(nullptr, 0, nullptr);
+        celix_ei_expect_opendir(nullptr, 0, nullptr);
         celix_ei_expect_celix_properties_load(nullptr, 0, CELIX_SUCCESS);
         celix_frameworkLogger_destroy(fw.logger);
         celix_properties_destroy(fw.configurationMap);
     }
+
     void createCache(celix_bundle_cache_t** cache) {
         celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CACHE_USE_TMP_DIR, true);
         EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_create(&fw, &fw.cache));
         *cache = fw.cache;
     }
+
     struct celix_framework fw {};
 };
 
@@ -103,13 +107,19 @@ TEST_F(CelixBundleCacheErrorInjectionTestSuite, 
CacheDeleteErrorTest) {
 
 TEST_F(CelixBundleCacheErrorInjectionTestSuite, ArchiveCreateErrorTest) {
     celix_bundle_cache_t* cache = nullptr;
+    long bndId;
     createCache(&cache);
 
+    //note needs to be the first ei test, to ensure that the lookup bundle id 
map is not yet loaded.
+    celix_ei_expect_opendir((void*)celix_bundleCache_findBundleIdForLocation, 
1, nullptr);
+    EXPECT_EQ(celix_bundleCache_findBundleIdForLocation(cache, 
SIMPLE_TEST_BUNDLE1_LOCATION, &bndId),
+              CELIX_FILE_IO_EXCEPTION);
+
     celix_bundle_archive_t* archive = nullptr;
     
celix_ei_expect_celix_utils_writeOrCreateString((void*)celix_bundleCache_createArchive,
 0, nullptr);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createArchive(cache, 1, 
SIMPLE_TEST_BUNDLE1_LOCATION, &archive));
     EXPECT_EQ(nullptr, archive);
-    long bndId;
+
     auto status = celix_bundleCache_findBundleIdForLocation(cache, 
SIMPLE_TEST_BUNDLE1_LOCATION, &bndId);
     EXPECT_EQ(CELIX_SUCCESS, status);
     EXPECT_EQ(-1, bndId);
diff --git a/libs/framework/src/celix_bundle_cache.c 
b/libs/framework/src/celix_bundle_cache.c
index b63f16473..29968c682 100644
--- a/libs/framework/src/celix_bundle_cache.c
+++ b/libs/framework/src/celix_bundle_cache.c
@@ -55,13 +55,13 @@ struct celix_bundle_cache {
     bool locationToBundleIdLookupMapLoaded; //true if the 
locationToBundleIdLookupMap is loaded from disk
 };
 
-static const char* bundleCache_progamName() {
+static const char* celix_bundleCache_programName() {
 #if defined(__APPLE__) || defined(__FreeBSD__)
     return getprogname();
 #elif defined(_GNU_SOURCE)
     return program_invocation_short_name;
 #else
-    return "";
+    return NULL;
 #endif
 }
 
@@ -109,10 +109,8 @@ celix_status_t celix_bundleCache_create(celix_framework_t* 
fw, celix_bundle_cach
     if (useTmpDir) {
         //Using /tmp dir for cache, so that multiple frameworks can be launched
         //instead of cacheDir = ".cache";
-        const char* pg = bundleCache_progamName();
-        if (pg == NULL) {
-            pg = "";
-        }
+        const char* pg = celix_bundleCache_programName();
+        pg = !pg ? "no-program-name" : pg;
         asprintf(&cache->cacheDir, "/tmp/celix-cache-%s-%s", pg, 
celix_framework_getUUID(fw));
     } else {
         const char* cacheDir = celix_bundleCache_cacheDirPath(fw);
@@ -220,7 +218,7 @@ static celix_status_t 
celix_bundleCache_updateIdForLocationLookupMap(celix_bundl
     celix_autoptr(DIR) dir = opendir(cache->cacheDir);
     if (dir == NULL) {
         fw_logCode(cache->fw->logger, CELIX_LOG_LEVEL_ERROR, 
CELIX_BUNDLE_EXCEPTION,
-                   "Cannot open bundle cache directory %s", cache->cacheDir);
+                   "Cannot open bundle cache directory %s: %s", 
cache->cacheDir, strerror(errno));
         return CELIX_FILE_IO_EXCEPTION;
     }
     char archiveRootBuffer[CELIX_DEFAULT_STRING_CREATE_BUFFER_SIZE];

Reply via email to