b971b8
From 8b59b9dab54c094dfc9bafd9d9f2c18f25877f36 Mon Sep 17 00:00:00 2001
b971b8
Message-Id: <8b59b9dab54c094dfc9bafd9d9f2c18f25877f36@dist-git>
b971b8
From: Paulo de Rezende Pinatti <ppinatti@linux.ibm.com>
b971b8
Date: Wed, 24 Jun 2020 13:16:17 +0200
b971b8
Subject: [PATCH] util: Introduce a parser for kernel cmdline arguments
b971b8
MIME-Version: 1.0
b971b8
Content-Type: text/plain; charset=UTF-8
b971b8
Content-Transfer-Encoding: 8bit
b971b8
b971b8
Introduce two utility functions to parse a kernel command
b971b8
line string according to the kernel code parsing rules in
b971b8
order to enable the caller to perform operations such as
b971b8
verifying whether certain argument=value combinations are
b971b8
present or retrieving an argument's value.
b971b8
b971b8
Signed-off-by: Paulo de Rezende Pinatti <ppinatti@linux.ibm.com>
b971b8
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
b971b8
Reviewed-by: Erik Skultety <eskultet@redhat.com>
b971b8
(cherry picked from commit c5fffb959d93b83d87e70b21d19424e9722700b0)
b971b8
b971b8
https://bugzilla.redhat.com/show_bug.cgi?id=1848997
b971b8
https://bugzilla.redhat.com/show_bug.cgi?id=1850351
b971b8
b971b8
Conflicts:
b971b8
	src/util/virutil.c
b971b8
            - unrelated commits db72866310d and ab36f729470 were not
b971b8
              backported
b971b8
b971b8
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
b971b8
Message-Id: <784fbc062d41f991b6321ac051b05e6c80a470cd.1592996194.git.jdenemar@redhat.com>
b971b8
Reviewed-by: Ján Tomko <jtomko@redhat.com>
b971b8
---
b971b8
 src/libvirt_private.syms |   2 +
b971b8
 src/util/virutil.c       | 185 +++++++++++++++++++++++++++++++++++++++
b971b8
 src/util/virutil.h       |  34 +++++++
b971b8
 tests/utiltest.c         | 136 ++++++++++++++++++++++++++++
b971b8
 4 files changed, 357 insertions(+)
b971b8
b971b8
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
b971b8
index a3fe49ae33..9e290c7bdf 100644
b971b8
--- a/src/libvirt_private.syms
b971b8
+++ b/src/libvirt_private.syms
b971b8
@@ -3395,6 +3395,8 @@ virHexToBin;
b971b8
 virHostGetDRMRenderNode;
b971b8
 virHostHasIOMMU;
b971b8
 virIndexToDiskName;
b971b8
+virKernelCmdlineMatchParam;
b971b8
+virKernelCmdlineNextParam;
b971b8
 virMemoryLimitIsSet;
b971b8
 virMemoryLimitTruncate;
b971b8
 virMemoryMaxValue;
b971b8
diff --git a/src/util/virutil.c b/src/util/virutil.c
b971b8
index 261b2d2af6..17fd06dbb2 100644
b971b8
--- a/src/util/virutil.c
b971b8
+++ b/src/util/virutil.c
b971b8
@@ -1673,3 +1673,188 @@ virHostGetDRMRenderNode(void)
b971b8
     VIR_DIR_CLOSE(driDir);
b971b8
     return ret;
b971b8
 }
b971b8
+
b971b8
+
b971b8
+static const char *virKernelCmdlineSkipQuote(const char *cmdline,
b971b8
+                                             bool *is_quoted)
b971b8
+{
b971b8
+    if (cmdline[0] == '"') {
b971b8
+        *is_quoted = !(*is_quoted);
b971b8
+        cmdline++;
b971b8
+    }
b971b8
+    return cmdline;
b971b8
+}
b971b8
+
b971b8
+
b971b8
+/**
b971b8
+ * virKernelCmdlineFindEqual:
b971b8
+ * @cmdline: target kernel command line string
b971b8
+ * @is_quoted: indicates whether the string begins with quotes
b971b8
+ * @res: pointer to the position immediately after the parsed parameter,
b971b8
+ * can be used in subsequent calls to process further parameters until
b971b8
+ * the end of the string.
b971b8
+ *
b971b8
+ * Iterate over the provided kernel command line string while honoring
b971b8
+ * the kernel quoting rules and returns the index of the equal sign
b971b8
+ * separating argument and value.
b971b8
+ *
b971b8
+ * Returns 0 for the cases where no equal sign is found or the argument
b971b8
+ * itself begins with the equal sign (both cases indicating that the
b971b8
+ * argument has no value). Otherwise, returns the index of the equal
b971b8
+ * sign in the string.
b971b8
+ */
b971b8
+static size_t virKernelCmdlineFindEqual(const char *cmdline,
b971b8
+                                        bool is_quoted,
b971b8
+                                        const char **res)
b971b8
+{
b971b8
+    size_t i;
b971b8
+    size_t equal_index = 0;
b971b8
+
b971b8
+    for (i = 0; cmdline[i]; i++) {
b971b8
+        if (!(is_quoted) && g_ascii_isspace(cmdline[i]))
b971b8
+            break;
b971b8
+        if (equal_index == 0 && cmdline[i] == '=') {
b971b8
+            equal_index = i;
b971b8
+            continue;
b971b8
+        }
b971b8
+        virKernelCmdlineSkipQuote(cmdline + i, &is_quoted);
b971b8
+    }
b971b8
+    *res = cmdline + i;
b971b8
+    return equal_index;
b971b8
+}
b971b8
+
b971b8
+
b971b8
+static char* virKernelArgNormalize(const char *arg)
b971b8
+{
b971b8
+    return virStringReplace(arg, "_", "-");
b971b8
+}
b971b8
+
b971b8
+
b971b8
+/**
b971b8
+ * virKernelCmdlineNextParam:
b971b8
+ * @cmdline: kernel command line string to be checked for next parameter
b971b8
+ * @param: pointer to hold retrieved parameter, will be NULL if none found
b971b8
+ * @val: pointer to hold retrieved value of @param
b971b8
+ *
b971b8
+ * Parse the kernel cmdline and store the next parameter in @param
b971b8
+ * and the value of @param in @val which can be NULL if @param has
b971b8
+ * no value. In addition returns the address right after @param=@value
b971b8
+ * for possible further processing.
b971b8
+ *
b971b8
+ * Returns a pointer to address right after @param=@val in the
b971b8
+ * kernel command line, will point to the string's end (NULL)
b971b8
+ * in case no next parameter is found
b971b8
+ */
b971b8
+const char *virKernelCmdlineNextParam(const char *cmdline,
b971b8
+                                      char **param,
b971b8
+                                      char **val)
b971b8
+{
b971b8
+    const char *next;
b971b8
+    int equal_index;
b971b8
+    bool is_quoted = false;
b971b8
+    *param = NULL;
b971b8
+    *val = NULL;
b971b8
+
b971b8
+    virSkipSpaces(&cmdline);
b971b8
+    cmdline = virKernelCmdlineSkipQuote(cmdline, &is_quoted);
b971b8
+    equal_index = virKernelCmdlineFindEqual(cmdline, is_quoted, &next;;
b971b8
+
b971b8
+    if (next == cmdline)
b971b8
+        return next;
b971b8
+
b971b8
+    /* param has no value */
b971b8
+    if (equal_index == 0) {
b971b8
+        if (is_quoted && next[-1] == '"')
b971b8
+            *param = g_strndup(cmdline, next - cmdline - 1);
b971b8
+        else
b971b8
+            *param = g_strndup(cmdline, next - cmdline);
b971b8
+        return next;
b971b8
+    }
b971b8
+
b971b8
+    *param = g_strndup(cmdline, equal_index);
b971b8
+
b971b8
+    if (cmdline[equal_index + 1] == '"') {
b971b8
+        is_quoted = true;
b971b8
+        equal_index++;
b971b8
+    }
b971b8
+
b971b8
+    if (is_quoted && next[-1] == '"')
b971b8
+        *val = g_strndup(cmdline + equal_index + 1,
b971b8
+                         next - cmdline - equal_index - 2);
b971b8
+    else
b971b8
+        *val = g_strndup(cmdline + equal_index + 1,
b971b8
+                         next - cmdline - equal_index - 1);
b971b8
+    return next;
b971b8
+}
b971b8
+
b971b8
+
b971b8
+static bool virKernelCmdlineStrCmp(const char *kernel_val,
b971b8
+                                   const char *caller_val,
b971b8
+                                   virKernelCmdlineFlags flags)
b971b8
+{
b971b8
+    if (flags & VIR_KERNEL_CMDLINE_FLAGS_CMP_PREFIX)
b971b8
+        return STRPREFIX(kernel_val, caller_val);
b971b8
+    return STREQ(kernel_val, caller_val);
b971b8
+}
b971b8
+
b971b8
+
b971b8
+/**
b971b8
+ * virKernelCmdlineMatchParam:
b971b8
+ * @cmdline: kernel command line string to be checked for @arg
b971b8
+ * @arg: kernel command line argument
b971b8
+ * @values: array of possible values to match @arg
b971b8
+ * @len_values: size of array, it can be 0 meaning a match will be positive if
b971b8
+ *              the argument has no value.
b971b8
+ * @flags: bitwise-OR of virKernelCmdlineFlags
b971b8
+ *
b971b8
+ * Try to match the provided kernel cmdline string with the provided @arg
b971b8
+ * and the list @values of possible values according to the matching strategy
b971b8
+ * defined in @flags.
b971b8
+ *
b971b8
+ *
b971b8
+ * Returns true if a match is found, false otherwise
b971b8
+ */
b971b8
+bool virKernelCmdlineMatchParam(const char *cmdline,
b971b8
+                                const char *arg,
b971b8
+                                const char **values,
b971b8
+                                size_t len_values,
b971b8
+                                virKernelCmdlineFlags flags)
b971b8
+{
b971b8
+    bool match = false;
b971b8
+    size_t i;
b971b8
+    const char *next = cmdline;
b971b8
+    g_autofree char *arg_norm = virKernelArgNormalize(arg);
b971b8
+
b971b8
+    while (next[0] != '\0') {
b971b8
+        g_autofree char *kparam = NULL;
b971b8
+        g_autofree char *kparam_norm = NULL;
b971b8
+        g_autofree char *kval = NULL;
b971b8
+
b971b8
+        next = virKernelCmdlineNextParam(next, &kparam, &kval);
b971b8
+
b971b8
+        if (!kparam)
b971b8
+            break;
b971b8
+
b971b8
+        kparam_norm = virKernelArgNormalize(kparam);
b971b8
+
b971b8
+        if (STRNEQ(kparam_norm, arg_norm))
b971b8
+            continue;
b971b8
+
b971b8
+        if (!kval) {
b971b8
+            match = (len_values == 0) ? true : false;
b971b8
+        } else {
b971b8
+            match = false;
b971b8
+            for (i = 0; i < len_values; i++) {
b971b8
+                if (virKernelCmdlineStrCmp(kval, values[i], flags)) {
b971b8
+                    match = true;
b971b8
+                    break;
b971b8
+                }
b971b8
+            }
b971b8
+        }
b971b8
+
b971b8
+        if (match && (flags & VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST))
b971b8
+            break;
b971b8
+    }
b971b8
+
b971b8
+    return match;
b971b8
+}
b971b8
diff --git a/src/util/virutil.h b/src/util/virutil.h
b971b8
index 0dcaff79ac..f1d2ccdd1f 100644
b971b8
--- a/src/util/virutil.h
b971b8
+++ b/src/util/virutil.h
b971b8
@@ -145,6 +145,40 @@ bool virHostHasIOMMU(void);
b971b8
 
b971b8
 char *virHostGetDRMRenderNode(void) G_GNUC_NO_INLINE;
b971b8
 
b971b8
+/* Kernel cmdline match and comparison strategy for arg=value pairs */
b971b8
+typedef enum {
b971b8
+    /* substring comparison of argument values */
b971b8
+    VIR_KERNEL_CMDLINE_FLAGS_CMP_PREFIX = 1,
b971b8
+
b971b8
+    /* strict string comparison of argument values */
b971b8
+    VIR_KERNEL_CMDLINE_FLAGS_CMP_EQ = 2,
b971b8
+
b971b8
+    /* look for any occurrence of the argument with the expected value,
b971b8
+     * this should be used when an argument set to the expected value overrides
b971b8
+     * all the other occurrences of the argument, e.g. when looking for 'arg=1'
b971b8
+     * in 'arg=0 arg=1 arg=0' the search would succeed with this flag
b971b8
+     */
b971b8
+    VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST = 4,
b971b8
+
b971b8
+    /* look for the last occurrence of argument with the expected value,
b971b8
+     * this should be used when the last occurrence of the argument overrides
b971b8
+     * all the other ones, e.g. when looking for 'arg=1' in 'arg=0 arg=1' the
b971b8
+     * search would succeed with this flag, but in 'arg=1 arg=0' it would not,
b971b8
+     * because 'arg=0' overrides all the previous occurrences of 'arg'
b971b8
+     */
b971b8
+    VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST = 8,
b971b8
+} virKernelCmdlineFlags;
b971b8
+
b971b8
+const char *virKernelCmdlineNextParam(const char *cmdline,
b971b8
+                                      char **param,
b971b8
+                                      char **val);
b971b8
+
b971b8
+bool virKernelCmdlineMatchParam(const char *cmdline,
b971b8
+                                const char *arg,
b971b8
+                                const char **values,
b971b8
+                                size_t len_values,
b971b8
+                                virKernelCmdlineFlags flags);
b971b8
+
b971b8
 /**
b971b8
  * VIR_ASSIGN_IS_OVERFLOW:
b971b8
  * @rvalue: value that is checked (evaluated twice)
b971b8
diff --git a/tests/utiltest.c b/tests/utiltest.c
b971b8
index 5ae04585cb..2bff7859dc 100644
b971b8
--- a/tests/utiltest.c
b971b8
+++ b/tests/utiltest.c
b971b8
@@ -254,6 +254,140 @@ testOverflowCheckMacro(const void *data G_GNUC_UNUSED)
b971b8
 }
b971b8
 
b971b8
 
b971b8
+struct testKernelCmdlineNextParamData
b971b8
+{
b971b8
+    const char *cmdline;
b971b8
+    const char *param;
b971b8
+    const char *val;
b971b8
+    const char *next;
b971b8
+};
b971b8
+
b971b8
+static struct testKernelCmdlineNextParamData kEntries[] = {
b971b8
+    { "arg1 arg2 arg3=val1",                        "arg1",              NULL,                  " arg2 arg3=val1"      },
b971b8
+    { "arg1=val1 arg2 arg3=val3 arg4",              "arg1",              "val1",                " arg2 arg3=val3 arg4" },
b971b8
+    { "arg1=sub1=val1,sub2=val2 arg3=val3 arg4",    "arg1",              "sub1=val1,sub2=val2", " arg3=val3 arg4"      },
b971b8
+    { "arg3=val3 ",                                 "arg3",              "val3",                " "                    },
b971b8
+    { "arg3=val3",                                  "arg3",              "val3",                ""                     },
b971b8
+    { "arg-3=val3 arg4",                            "arg-3",             "val3",                " arg4"                },
b971b8
+    { " arg_3=val3 arg4",                           "arg_3",             "val3",                " arg4"                },
b971b8
+    { "arg2=\"value with space\" arg3=val3",        "arg2",              "value with space",    " arg3=val3"           },
b971b8
+    { " arg2=\"value with space\"   arg3=val3",     "arg2",              "value with space",    "   arg3=val3"         },
b971b8
+    { "  \"arg2=value with space\" arg3=val3",      "arg2",              "value with space",    " arg3=val3"           },
b971b8
+    { "arg2=\"val\"ue arg3",                        "arg2",              "val\"ue",             " arg3"                },
b971b8
+    { "arg2=value\" long\" arg3",                   "arg2",              "value\" long\"",      " arg3"                },
b971b8
+    { " \"arg2 with space=value with space\" arg3", "arg2 with space",   "value with space",    " arg3"                },
b971b8
+    { " arg2\" with space=val2\" arg3",             "arg2\" with space", "val2\"",              " arg3"                },
b971b8
+    { " arg2longer=someval\" long\" arg2=val2",     "arg2longer",        "someval\" long\"",    " arg2=val2"           },
b971b8
+    { "=val1 arg2=val2",                            "=val1",             NULL,                  " arg2=val2"           },
b971b8
+    { " ",                                          NULL,                NULL,                  ""                     },
b971b8
+    { "",                                           NULL,                NULL,                  ""                     },
b971b8
+};
b971b8
+
b971b8
+static int
b971b8
+testKernelCmdlineNextParam(const void *data G_GNUC_UNUSED)
b971b8
+{
b971b8
+    const char *next;
b971b8
+    size_t i;
b971b8
+
b971b8
+    for (i = 0; i < G_N_ELEMENTS(kEntries); ++i) {
b971b8
+        g_autofree char * param = NULL;
b971b8
+        g_autofree char * val = NULL;
b971b8
+
b971b8
+        next = virKernelCmdlineNextParam(kEntries[i].cmdline, &param, &val;;
b971b8
+
b971b8
+        if (STRNEQ_NULLABLE(param, kEntries[i].param) ||
b971b8
+            STRNEQ_NULLABLE(val, kEntries[i].val) ||
b971b8
+            STRNEQ(next, kEntries[i].next)) {
b971b8
+            VIR_TEST_DEBUG("\nKernel cmdline [%s]", kEntries[i].cmdline);
b971b8
+            VIR_TEST_DEBUG("Expect param [%s]", kEntries[i].param);
b971b8
+            VIR_TEST_DEBUG("Actual param [%s]", param);
b971b8
+            VIR_TEST_DEBUG("Expect value [%s]", kEntries[i].val);
b971b8
+            VIR_TEST_DEBUG("Actual value [%s]", val);
b971b8
+            VIR_TEST_DEBUG("Expect next [%s]", kEntries[i].next);
b971b8
+            VIR_TEST_DEBUG("Actual next [%s]", next);
b971b8
+
b971b8
+            return -1;
b971b8
+        }
b971b8
+    }
b971b8
+
b971b8
+    return 0;
b971b8
+}
b971b8
+
b971b8
+
b971b8
+struct testKernelCmdlineMatchData
b971b8
+{
b971b8
+    const char *cmdline;
b971b8
+    const char *arg;
b971b8
+    const char *values[2];
b971b8
+    virKernelCmdlineFlags flags;
b971b8
+    bool result;
b971b8
+};
b971b8
+
b971b8
+static struct testKernelCmdlineMatchData kMatchEntries[] = {
b971b8
+    {"arg1 myarg=no arg2=val2 myarg=yes arg4=val4 myarg=no arg5", "myarg",  {"1", "y"},          VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST | VIR_KERNEL_CMDLINE_FLAGS_CMP_EQ,     false },
b971b8
+    {"arg1 myarg=no arg2=val2 myarg=yes arg4=val4 myarg=no arg5", "myarg",  {"on", "yes"},       VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST | VIR_KERNEL_CMDLINE_FLAGS_CMP_EQ,     true  },
b971b8
+    {"arg1 myarg=no arg2=val2 myarg=yes arg4=val4 myarg=no arg5", "myarg",  {"1", "y"},          VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST | VIR_KERNEL_CMDLINE_FLAGS_CMP_PREFIX, true  },
b971b8
+    {"arg1 myarg=no arg2=val2 myarg=yes arg4=val4 myarg=no arg5", "myarg",  {"a", "b"},          VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST | VIR_KERNEL_CMDLINE_FLAGS_CMP_PREFIX, false },
b971b8
+    {"arg1 myarg=no arg2=val2 myarg=yes arg4=val4 myarg=no arg5", "myarg",  {"on", "yes"},       VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST | VIR_KERNEL_CMDLINE_FLAGS_CMP_EQ,      false },
b971b8
+    {"arg1 myarg=no arg2=val2 myarg=yes arg4=val4 myarg=no arg5", "myarg",  {"1", "y"},          VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST | VIR_KERNEL_CMDLINE_FLAGS_CMP_PREFIX,  false },
b971b8
+    {"arg1 myarg=no arg2=val2 arg4=val4 myarg=yes arg5",          "myarg",  {"on", "yes"},       VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST | VIR_KERNEL_CMDLINE_FLAGS_CMP_EQ,      true  },
b971b8
+    {"arg1 myarg=no arg2=val2 arg4=val4 myarg=yes arg5",          "myarg",  {"1", "y"},          VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST | VIR_KERNEL_CMDLINE_FLAGS_CMP_PREFIX,  true  },
b971b8
+    {"arg1 myarg=no arg2=val2 arg4=val4 myarg arg5",              "myarg",  {NULL, NULL},        VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST,                                        true  },
b971b8
+    {"arg1 myarg arg2=val2 arg4=val4 myarg=yes arg5",             "myarg",  {NULL, NULL},        VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST,                                       true  },
b971b8
+    {"arg1 myarg arg2=val2 arg4=val4 myarg=yes arg5",             "myarg",  {NULL, NULL},        VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST,                                        false },
b971b8
+    {"arg1 my-arg=no arg2=val2 arg4=val4 my_arg=yes arg5",        "my-arg", {"on", "yes"},       VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST,                                        true  },
b971b8
+    {"arg1 my-arg=no arg2=val2 arg4=val4 my_arg=yes arg5 ",       "my-arg", {"on", "yes"},       VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST | VIR_KERNEL_CMDLINE_FLAGS_CMP_EQ,      true  },
b971b8
+    {"arg1 my-arg arg2=val2 arg4=val4 my_arg=yes arg5",           "my_arg", {NULL, NULL},        VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST,                                       true  },
b971b8
+    {"arg1 my-arg arg2=val2 arg4=val4 my-arg=yes arg5",           "my_arg", {NULL, NULL},        VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST,                                       true  },
b971b8
+    {"=arg1 my-arg arg2=val2 arg4=val4 my-arg=yes arg5",          "my_arg", {NULL, NULL},        VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST,                                       true  },
b971b8
+    {"my-arg =arg1 arg2=val2 arg4=val4 my-arg=yes arg5",          "=arg1",  {NULL, NULL},        VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST,                                        true  },
b971b8
+    {"arg1 arg2=val2 myarg=sub1=val1 arg5",                       "myarg",  {"sub1=val1", NULL}, VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST,                                        true  },
b971b8
+    {"arg1 arg2=",                                                "arg2",   {"", ""},            VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST | VIR_KERNEL_CMDLINE_FLAGS_CMP_EQ,      true  },
b971b8
+    {" ",                                                         "myarg",  {NULL, NULL},        VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST,                                        false },
b971b8
+    {"",                                                          "",       {NULL, NULL},        VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST,                                        false },
b971b8
+};
b971b8
+
b971b8
+
b971b8
+static int
b971b8
+testKernelCmdlineMatchParam(const void *data G_GNUC_UNUSED)
b971b8
+{
b971b8
+    bool result;
b971b8
+    size_t i, lenValues;
b971b8
+
b971b8
+    for (i = 0; i < G_N_ELEMENTS(kMatchEntries); ++i) {
b971b8
+        if (kMatchEntries[i].values[0] == NULL)
b971b8
+            lenValues = 0;
b971b8
+        else
b971b8
+            lenValues = G_N_ELEMENTS(kMatchEntries[i].values);
b971b8
+
b971b8
+        result = virKernelCmdlineMatchParam(kMatchEntries[i].cmdline,
b971b8
+                                            kMatchEntries[i].arg,
b971b8
+                                            kMatchEntries[i].values,
b971b8
+                                            lenValues,
b971b8
+                                            kMatchEntries[i].flags);
b971b8
+
b971b8
+        if (result != kMatchEntries[i].result) {
b971b8
+            VIR_TEST_DEBUG("\nKernel cmdline [%s]", kMatchEntries[i].cmdline);
b971b8
+            VIR_TEST_DEBUG("Kernel argument [%s]", kMatchEntries[i].arg);
b971b8
+            VIR_TEST_DEBUG("Kernel values [%s] [%s]", kMatchEntries[i].values[0],
b971b8
+                           kMatchEntries[i].values[1]);
b971b8
+            if (kMatchEntries[i].flags & VIR_KERNEL_CMDLINE_FLAGS_CMP_PREFIX)
b971b8
+                VIR_TEST_DEBUG("Flag [VIR_KERNEL_CMDLINE_FLAGS_CMP_PREFIX]");
b971b8
+            if (kMatchEntries[i].flags & VIR_KERNEL_CMDLINE_FLAGS_CMP_EQ)
b971b8
+                VIR_TEST_DEBUG("Flag [VIR_KERNEL_CMDLINE_FLAGS_CMP_EQ]");
b971b8
+            if (kMatchEntries[i].flags & VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST)
b971b8
+                VIR_TEST_DEBUG("Flag [VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST]");
b971b8
+            if (kMatchEntries[i].flags & VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST)
b971b8
+                VIR_TEST_DEBUG("Flag [VIR_KERNEL_CMDLINE_FLAGS_SEARCH_LAST]");
b971b8
+            VIR_TEST_DEBUG("Expect result [%d]", kMatchEntries[i].result);
b971b8
+            VIR_TEST_DEBUG("Actual result [%d]", result);
b971b8
+
b971b8
+            return -1;
b971b8
+        }
b971b8
+    }
b971b8
+
b971b8
+    return 0;
b971b8
+}
b971b8
 
b971b8
 
b971b8
 static int
b971b8
@@ -277,6 +411,8 @@ mymain(void)
b971b8
     DO_TEST(ParseVersionString);
b971b8
     DO_TEST(RoundValueToPowerOfTwo);
b971b8
     DO_TEST(OverflowCheckMacro);
b971b8
+    DO_TEST(KernelCmdlineNextParam);
b971b8
+    DO_TEST(KernelCmdlineMatchParam);
b971b8
 
b971b8
     return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
b971b8
 }
b971b8
-- 
b971b8
2.27.0
b971b8