7fc0182
From d50b7bf2b66b7b67ee332c1af63bc1f5fdfba7ad Mon Sep 17 00:00:00 2001
8298c36
From: "Brian C. Lane" <bcl@redhat.com>
7fc0182
Date: Mon, 8 Aug 2022 13:49:09 -0700
7fc0182
Subject: [PATCH 10/13] tests: Add a libparted test for
7fc0182
 ped_partition_set_system on gpt
8298c36
8298c36
Test the libparted API to make sure the flag is not cleared by calling
8298c36
ped_partition_set_system.
8298c36
---
7fc0182
 libparted/tests/Makefile.am    |  6 ++-
7fc0182
 libparted/tests/flags.c        | 81 ++++++++++++++++++++++++++++++++++
7fc0182
 libparted/tests/t1001-flags.sh | 23 ++++++++++
7fc0182
 3 files changed, 108 insertions(+), 2 deletions(-)
7fc0182
 create mode 100644 libparted/tests/flags.c
7fc0182
 create mode 100755 libparted/tests/t1001-flags.sh
8298c36
7fc0182
diff --git a/libparted/tests/Makefile.am b/libparted/tests/Makefile.am
7fc0182
index fd5cba5..260b692 100644
7fc0182
--- a/libparted/tests/Makefile.am
7fc0182
+++ b/libparted/tests/Makefile.am
7fc0182
@@ -3,9 +3,10 @@
7fc0182
 #
7fc0182
 # This file may be modified and/or distributed without restriction.
7fc0182
 
7fc0182
-TESTS = t1000-label.sh t2000-disk.sh t2100-zerolen.sh t3000-symlink.sh t4000-volser.sh
7fc0182
+TESTS = t1000-label.sh t1001-flags.sh t2000-disk.sh t2100-zerolen.sh \
7fc0182
+	t3000-symlink.sh t4000-volser.sh
7fc0182
 EXTRA_DIST = $(TESTS)
7fc0182
-check_PROGRAMS = label disk zerolen symlink volser
7fc0182
+check_PROGRAMS = label disk zerolen symlink volser flags
7fc0182
 AM_CFLAGS = $(WARN_CFLAGS) $(WERROR_CFLAGS)
7fc0182
 
7fc0182
 LDADD = \
7fc0182
@@ -24,6 +25,7 @@ disk_SOURCES  = common.h common.c disk.c
7fc0182
 zerolen_SOURCES = common.h common.c zerolen.c
7fc0182
 symlink_SOURCES = common.h common.c symlink.c
7fc0182
 volser_SOURCES = common.h common.c volser.c
7fc0182
+flags_SOURCES = common.h common.c flags.c
7fc0182
 
7fc0182
 # Arrange to symlink to tests/init.sh.
7fc0182
 CLEANFILES = init.sh
8298c36
diff --git a/libparted/tests/flags.c b/libparted/tests/flags.c
7fc0182
new file mode 100644
7fc0182
index 0000000..c83a361
7fc0182
--- /dev/null
8298c36
+++ b/libparted/tests/flags.c
7fc0182
@@ -0,0 +1,81 @@
7fc0182
+#include <config.h>
7fc0182
+#include <unistd.h>
7fc0182
+
7fc0182
+#include <check.h>
7fc0182
+
7fc0182
+#include <parted/parted.h>
7fc0182
+
7fc0182
+#include "common.h"
7fc0182
+#include "progname.h"
7fc0182
+
7fc0182
+#define STREQ(a, b) (strcmp (a, b) == 0)
7fc0182
+
7fc0182
+static char* temporary_disk;
7fc0182
+
7fc0182
+static void
7fc0182
+create_disk (void)
7fc0182
+{
7fc0182
+        temporary_disk = _create_disk (80 * 1024 * 1024);
7fc0182
+        fail_if (temporary_disk == NULL, "Failed to create temporary disk");
7fc0182
+}
7fc0182
+
7fc0182
+static void
7fc0182
+destroy_disk (void)
7fc0182
+{
7fc0182
+        unlink (temporary_disk);
7fc0182
+        free (temporary_disk);
7fc0182
+}
7fc0182
+
7fc0182
+/* TEST: Test partition type flag on gpt disklabel */
7fc0182
+START_TEST (test_gpt_flag)
8298c36
+{
8298c36
+        PedDevice* dev = ped_device_get (temporary_disk);
8298c36
+        if (dev == NULL)
8298c36
+                return;
8298c36
+
7fc0182
+        PedDisk* disk = ped_disk_new_fresh (dev, ped_disk_type_get ("gpt"));
8298c36
+        PedConstraint *constraint = ped_constraint_any (dev);
8298c36
+        PedPartition *part = ped_partition_new (disk, PED_PARTITION_NORMAL,
8298c36
+            ped_file_system_type_get("ext4"), 2048, 4096);
7fc0182
+        ped_partition_set_flag(part, PED_PARTITION_BIOS_GRUB, 1);
8298c36
+        // Type should remain set to BIOS_GRUB
8298c36
+        ped_partition_set_system(part, ped_file_system_type_get("ext4"));
8298c36
+
8298c36
+        ped_disk_add_partition (disk, part, constraint);
8298c36
+        ped_disk_commit (disk);
8298c36
+        ped_constraint_destroy (constraint);
8298c36
+
8298c36
+        // Check flag to confirm it is still set
8298c36
+        part = ped_disk_get_partition (disk, 1);
7fc0182
+        fail_if (ped_partition_get_flag(part, PED_PARTITION_BIOS_GRUB) != 1, "BIOS_GRUB flag not set");
8298c36
+
8298c36
+        ped_disk_destroy (disk);
8298c36
+        ped_device_destroy (dev);
8298c36
+}
8298c36
+END_TEST
8298c36
+
7fc0182
+int
7fc0182
+main (int argc, char **argv)
7fc0182
+{
7fc0182
+        set_program_name (argv[0]);
7fc0182
+        int number_failed;
7fc0182
+        Suite* suite = suite_create ("Partition Flags");
7fc0182
+        TCase* tcase_gpt = tcase_create ("GPT");
7fc0182
+
7fc0182
+        /* Fail when an exception is raised */
7fc0182
+        ped_exception_set_handler (_test_exception_handler);
7fc0182
+
7fc0182
+        tcase_add_checked_fixture (tcase_gpt, create_disk, destroy_disk);
7fc0182
+        tcase_add_test (tcase_gpt, test_gpt_flag);
8298c36
+        /* Disable timeout for this test */
7fc0182
+        tcase_set_timeout (tcase_gpt, 0);
7fc0182
+        suite_add_tcase (suite, tcase_gpt);
8298c36
+
7fc0182
+        SRunner* srunner = srunner_create (suite);
7fc0182
+        srunner_run_all (srunner, CK_VERBOSE);
7fc0182
+
7fc0182
+        number_failed = srunner_ntests_failed (srunner);
7fc0182
+        srunner_free (srunner);
7fc0182
+
7fc0182
+        return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
7fc0182
+}
7fc0182
diff --git a/libparted/tests/t1001-flags.sh b/libparted/tests/t1001-flags.sh
7fc0182
new file mode 100755
7fc0182
index 0000000..60a6248
7fc0182
--- /dev/null
7fc0182
+++ b/libparted/tests/t1001-flags.sh
7fc0182
@@ -0,0 +1,23 @@
7fc0182
+#!/bin/sh
7fc0182
+# run the flags unittest
7fc0182
+
7fc0182
+# Copyright (C) 2007-2014, 2019-2022 Free Software Foundation, Inc.
7fc0182
+
7fc0182
+# This program is free software; you can redistribute it and/or modify
7fc0182
+# it under the terms of the GNU General Public License as published by
7fc0182
+# the Free Software Foundation; either version 3 of the License, or
7fc0182
+# (at your option) any later version.
7fc0182
+
7fc0182
+# This program is distributed in the hope that it will be useful,
7fc0182
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
7fc0182
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7fc0182
+# GNU General Public License for more details.
7fc0182
+
7fc0182
+# You should have received a copy of the GNU General Public License
7fc0182
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
7fc0182
+
7fc0182
+. "${top_srcdir=../..}/tests/init.sh"; path_prepend_ .
7fc0182
+
7fc0182
+flags || fail=1
7fc0182
+
7fc0182
+Exit $fail
8298c36
-- 
7fc0182
2.37.3
8298c36