From 69e11c5a68e75c9f48953e320cddbdf3e7f9eb46 Mon Sep 17 00:00:00 2001
From: Vladimir Serbinenko <phcoder@gmail.com>
Date: Fri, 12 Jun 2020 17:09:44 +0200
Subject: [PATCH 3/3] Add tests for cmdline module

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
---
 grub-core/Makefile.core.def           | 12 +++++
 grub-core/tests/loader_cmdline_test.c | 69 +++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
 create mode 100644 grub-core/tests/loader_cmdline_test.c

diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 166b444c7..5038e03be 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1823,6 +1823,13 @@ module = {
   enable = noemu;
 };
 
+/* Special case for emu so we always have cmdline available for the tests.  */
+module = {
+  name = cmdline_lib;
+  common = lib/cmdline.c;
+  enable = emu;
+};
+
 module = {
   name = fdt;
   efi = loader/efi/fdt.c;
@@ -2119,6 +2126,11 @@ module = {
   common = tests/strtoull_test.c;
 };
 
+module = {
+  name = loader_cmdline_test;
+  common = tests/loader_cmdline_test.c;
+};
+
 module = {
   name = setjmp_test;
   common = tests/setjmp_test.c;
diff --git a/grub-core/tests/loader_cmdline_test.c b/grub-core/tests/loader_cmdline_test.c
new file mode 100644
index 000000000..c3f6bcebe
--- /dev/null
+++ b/grub-core/tests/loader_cmdline_test.c
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2013  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/misc.h>
+#include <grub/dl.h>
+#include <grub/command.h>
+#include <grub/env.h>
+#include <grub/test.h>
+#include <grub/mm.h>
+#include <grub/lib/cmdline.h>
+#include <grub/misc.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+struct cmdline_test {
+  int argc;
+  const char *const *argv;
+  const char *result;
+};
+
+static struct cmdline_test tests[] = {
+  {0, (const char *const[]) {"dummy"}, ""},
+  {1, (const char *const[]) {"bzimage"}, "bzimage"},
+  {2, (const char *const[]) {"bzimage", "root=/dev/sda1"}, "bzimage root=/dev/sda1"},
+  {3, (const char *const[]) {"bzimage", "root=/dev/sda1", "acpi_osi=Windows 2006"}, "bzimage root=/dev/sda1 acpi_osi=\"Windows 2006\""},
+  {3, (const char *const[]) {"bzimage", "root=/dev/sda1", "Hello World"}, "bzimage root=/dev/sda1 \"Hello World\""},
+  {3, (const char *const[]) {"bzimage", "root=/dev/sda1", "Hello World=Hi"}, "bzimage root=/dev/sda1 \"Hello World=Hi\""},
+};
+
+static void
+loader_cmdline_test (void)
+{
+  unsigned i;
+  for (i = 0; i < ARRAY_SIZE(tests); i++)
+    {
+      static char resbuf[512];
+      grub_err_t err;
+      int sz = grub_loader_cmdline_size (tests[i].argc, tests[i].argv);
+      grub_test_assert(sz == (int) grub_strlen(tests[i].result) + 1,
+		       "Wrong computed size for %s: should be %d, got %d",
+		       tests[i].result,
+		       (int) grub_strlen(tests[i].result) + 1, sz);
+      err = grub_create_loader_cmdline (tests[i].argc, tests[i].argv, resbuf,
+					sz, GRUB_VERIFY_KERNEL_CMDLINE);
+      grub_test_assert(err == 0,
+		       "grub_create_loader_cmdline returned error: %d: %s",
+		       err, grub_errmsg);
+      grub_test_assert(grub_strcmp(resbuf, tests[i].result) == 0,
+		       "grub_create_loader_cmdline made wrong string: expected %s, got %s",
+		       tests[i].result, resbuf);
+    }
+}
+
+GRUB_FUNCTIONAL_TEST (loader_cmdline_test, loader_cmdline_test);
-- 
2.20.1

