From: Michal Privoznik <[email protected]>

Our qemusecuritymock reimplements stat() (for the purpose of
testing our DAC secdriver) and so far, for every path S_IFREG
mode is set (i.e. pretending every path thrown into stat() is a
regular file). Extend the mock so that a path can be directory
(S_IFDIR).

Signed-off-by: Michal Privoznik <[email protected]>
---
 tests/qemusecuritymock.c | 7 ++++++-
 tests/qemusecuritytest.h | 3 +++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/tests/qemusecuritymock.c b/tests/qemusecuritymock.c
index cbd928ab38..51ba584853 100644
--- a/tests/qemusecuritymock.c
+++ b/tests/qemusecuritymock.c
@@ -232,6 +232,7 @@ int virFileRemoveXAttr(const char *path,
 #define VIR_MOCK_STAT_HOOK \
     do { \
         if (g_getenv(ENVVAR)) { \
+            const char *mimicDir = g_getenv(PATH_MIMIC_DIR); \
             uint32_t *val; \
 \
             virMutexLock(&m); \
@@ -239,7 +240,11 @@ int virFileRemoveXAttr(const char *path,
 \
             memset(sb, 0, sizeof(*sb)); \
 \
-            sb->st_mode = S_IFREG | 0666; \
+            if (STREQ_NULLABLE(path, mimicDir)) { \
+                sb->st_mode = S_IFDIR | 0666; \
+            } else { \
+                sb->st_mode = S_IFREG | 0666; \
+            } \
             sb->st_size = 123456; \
             sb->st_ino = 1; \
 \
diff --git a/tests/qemusecuritytest.h b/tests/qemusecuritytest.h
index 696cfb4b63..68b37352f3 100644
--- a/tests/qemusecuritytest.h
+++ b/tests/qemusecuritytest.h
@@ -19,6 +19,9 @@
 #pragma once
 
 #define ENVVAR "LIBVIRT_QEMU_SECURITY_TEST"
+/* If PATH_MIMIC_DIR is set then stat() mocked in qemusecuritymock.c pretends
+ * the value is a directory instead of a file. */
+#define PATH_MIMIC_DIR "LIBVIRT_QEMU_SECURITY_TEST_MIMIC_DIR"
 
 #include "internal.h"
 
-- 
2.56.0

Reply via email to