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