]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemusecuritytest: Store 'notRestored' files in a hash table
authorPeter Krempa <pkrempa@redhat.com>
Fri, 5 Feb 2021 08:53:36 +0000 (09:53 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 11 Feb 2021 16:05:33 +0000 (17:05 +0100)
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@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tests/qemusecuritymock.c
tests/qemusecuritytest.c
tests/qemusecuritytest.h

index 1fa4e522cc5272e021c3d3eff02d72a244618519..5daf27ccd7c658e415eee204a0da54f6eeed6ff2 100644 (file)
@@ -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;
         }
     }
index 175001813701ef18622e3606d9f60ad29cca81c6..74a25f2be14475246919876d5239236966c7346e 100644 (file)
@@ -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;
index cc3918ddf5aec078456f06758ef496af68fefdee..696cfb4b63e105f492eb38104e8b2a9dc548630b 100644 (file)
@@ -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);