[PATCH 20/40] qemusecuritytest: Store 'notRestored' files in a hash table

Peter Krempa pkrempa at redhat.com
Sat Feb 6 08:32:42 UTC 2021


The validation code looks whether certain paths are in the 'notRestored'
list. For the purpose of lookup it's better to use a hash table rather
than a string list.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 tests/qemusecuritymock.c | 19 +++++++++++--------
 tests/qemusecuritytest.c | 14 ++++++--------
 tests/qemusecuritytest.h |  4 +++-
 3 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/tests/qemusecuritymock.c b/tests/qemusecuritymock.c
index 1fa4e522cc..5daf27ccd7 100644
--- a/tests/qemusecuritymock.c
+++ b/tests/qemusecuritymock.c
@@ -398,7 +398,7 @@ int virFileUnlock(int fd G_GNUC_UNUSED,

 typedef struct _checkOwnerData checkOwnerData;
 struct _checkOwnerData {
-    const char **paths;
+    GHashTable *paths;
     bool chown_fail;
     bool selinux_fail;
 };
@@ -413,7 +413,7 @@ checkSELinux(void *payload,
     char *label = payload;

     if (STRNEQ(label, DEFAULT_SELINUX_LABEL) &&
-        !virStringListHasString(data->paths, name)) {
+        !g_hash_table_contains(data->paths, name)) {
         fprintf(stderr,
                 "Path %s wasn't restored back to its original SELinux label\n",
                 name);
@@ -434,7 +434,7 @@ checkOwner(void *payload,

     if ((owner % 16 != DEFAULT_UID ||
          owner >> 16 != DEFAULT_GID) &&
-        !virStringListHasString(data->paths, name)) {
+        !g_hash_table_contains(data->paths, name)) {
         fprintf(stderr,
                 "Path %s wasn't restored back to its original owner\n",
                 name);
@@ -473,19 +473,22 @@ printXATTR(void *payload,
  * can be passed in @paths argument. If a path is not restored
  * but it's on the list no error is indicated.
  */
-int checkPaths(const char **paths)
+int checkPaths(GHashTable *paths)
 {
     int ret = -1;
     checkOwnerData data = { .paths = paths, .chown_fail = false, .selinux_fail = false };
     bool xattr_fail = false;
-    size_t i;
+    GHashTableIter htitr;
+    void *key;

     virMutexLock(&m);
     init_hash();

-    for (i = 0; paths && paths[i]; i++) {
-        if (!virHashLookup(chown_paths, paths[i])) {
-            fprintf(stderr, "Unexpected path restored: %s\n", paths[i]);
+    g_hash_table_iter_init(&htitr, paths);
+
+    while (g_hash_table_iter_next(&htitr, &key, NULL)) {
+        if (!virHashLookup(chown_paths, key)) {
+            fprintf(stderr, "Unexpected path restored: %s\n", (const char *) key);
             goto cleanup;
         }
     }
diff --git a/tests/qemusecuritytest.c b/tests/qemusecuritytest.c
index 1750018137..74a25f2be1 100644
--- a/tests/qemusecuritytest.c
+++ b/tests/qemusecuritytest.c
@@ -87,7 +87,7 @@ testDomain(const void *opaque)
 {
     const struct testData *data = opaque;
     g_autoptr(virDomainObj) vm = NULL;
-    g_auto(GStrv) notRestored = NULL;
+    g_autoptr(GHashTable) notRestored = virHashNew(NULL);
     size_t i;
     int ret = -1;

@@ -102,14 +102,12 @@ testDomain(const void *opaque)
             continue;

         if (virStorageSourceIsLocalStorage(src) && src->path &&
-            (src->shared || src->readonly) &&
-            virStringListAdd(&notRestored, src->path) < 0)
-            return -1;
+            (src->shared || src->readonly))
+            g_hash_table_insert(notRestored, g_strdup(src->path), NULL);

         for (n = src->backingStore; virStorageSourceIsBacking(n); n = n->backingStore) {
-            if (virStorageSourceIsLocalStorage(n) && n->path &&
-                virStringListAdd(&notRestored, n->path) < 0)
-                return -1;
+            if (virStorageSourceIsLocalStorage(n) && n->path)
+                g_hash_table_insert(notRestored, g_strdup(n->path), NULL);
         }
     }

@@ -123,7 +121,7 @@ testDomain(const void *opaque)

     qemuSecurityRestoreAllLabel(data->driver, vm, false);

-    if (checkPaths((const char **) notRestored) < 0)
+    if (checkPaths(notRestored) < 0)
         goto cleanup;

     ret = 0;
diff --git a/tests/qemusecuritytest.h b/tests/qemusecuritytest.h
index cc3918ddf5..696cfb4b63 100644
--- a/tests/qemusecuritytest.h
+++ b/tests/qemusecuritytest.h
@@ -20,6 +20,8 @@

 #define ENVVAR "LIBVIRT_QEMU_SECURITY_TEST"

-extern int checkPaths(const char **paths);
+#include "internal.h"
+
+extern int checkPaths(GHashTable *paths);

 extern void freePaths(void);
-- 
2.29.2




More information about the libvir-list mailing list