[parted-devel] [PATCH] tests: Fixing libparted test framework usage

Brian C. Lane bcl at redhat.com
Tue Feb 7 18:55:21 GMT 2023


The fail and fail_if functions from libcheck are deprecated, replace
them with ck_abort_msg and ck_assert_msg. Note that the logic of assert
is the opposite of fail_if.
---
 libparted/tests/common.c  |  8 ++++----
 libparted/tests/disk.c    |  6 +++---
 libparted/tests/flags.c   |  6 +++---
 libparted/tests/label.c   | 14 ++++++--------
 libparted/tests/symlink.c | 31 +++++++++++++++++++++----------
 libparted/tests/volser.c  |  2 +-
 libparted/tests/zerolen.c |  2 +-
 7 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/libparted/tests/common.c b/libparted/tests/common.c
index 2be0e3a..8c42ece 100644
--- a/libparted/tests/common.c
+++ b/libparted/tests/common.c
@@ -27,7 +27,7 @@ size_t get_sector_size (void)
 PedExceptionOption
 _test_exception_handler (PedException* e)
 {
-        fail ("Exception of type %s has been raised: %s",
+        ck_abort_msg("Exception of type %s has been raised: %s",
               ped_exception_get_type_string (e->type),
               e->message);
 
@@ -69,10 +69,10 @@ _create_disk_label (PedDevice *dev, PedDiskType *type)
 
         /* Create the label */
         disk = ped_disk_new_fresh (dev, type);
-        fail_if (!disk, "Failed to create a label of type: %s",
+        ck_assert_msg(disk != NULL, "Failed to create a label of type: %s",
                  type->name);
-        fail_if (!ped_disk_commit(disk),
-		 "Failed to commit label to device");
+        ck_assert_msg(ped_disk_commit(disk) != 0,
+                 "Failed to commit label to device");
 
         return disk;
 }
diff --git a/libparted/tests/disk.c b/libparted/tests/disk.c
index 62d20c1..f7b16a5 100644
--- a/libparted/tests/disk.c
+++ b/libparted/tests/disk.c
@@ -14,7 +14,7 @@ static void
 create_disk (void)
 {
         temporary_disk = _create_disk (get_sector_size () * 4 * 10 * 1024);
-        fail_if (temporary_disk == NULL, "Failed to create temporary disk");
+        ck_assert_msg(temporary_disk != NULL, "Failed to create temporary disk");
 }
 
 static void
@@ -72,8 +72,8 @@ START_TEST (test_duplicate)
                 part = ped_disk_get_partition (disk, *i);
                 part_dup = ped_disk_get_partition (disk_dup, *i);
 
-                fail_if (part->geom.start != part_dup->geom.start ||
-                         part->geom.end != part_dup->geom.end,
+                ck_assert_msg(part->geom.start == part_dup->geom.start &&
+                         part->geom.end == part_dup->geom.end,
                          "Duplicated partition %d doesn't match. "
                          "Details are start: %d/%d end: %d/%d\n",
                          *i, part->geom.start, part_dup->geom.start,
diff --git a/libparted/tests/flags.c b/libparted/tests/flags.c
index c4b290b..ff4ae71 100644
--- a/libparted/tests/flags.c
+++ b/libparted/tests/flags.c
@@ -16,7 +16,7 @@ static void
 create_disk (void)
 {
         temporary_disk = _create_disk (80 * 1024 * 1024);
-        fail_if (temporary_disk == NULL, "Failed to create temporary disk");
+        ck_assert_msg(temporary_disk != NULL, "Failed to create temporary disk");
 }
 
 static void
@@ -47,7 +47,7 @@ START_TEST (test_gpt_flag)
 
         // Check flag to confirm it is still set
         part = ped_disk_get_partition (disk, 1);
-        fail_if (ped_partition_get_flag(part, PED_PARTITION_BIOS_GRUB) != 1, "BIOS_GRUB flag not set");
+        ck_assert_msg(ped_partition_get_flag(part, PED_PARTITION_BIOS_GRUB) == 1, "BIOS_GRUB flag not set");
 
         ped_disk_destroy (disk);
         ped_device_destroy (dev);
@@ -75,7 +75,7 @@ START_TEST (test_msdos_flag)
 
         // Check flag to confirm it is still set
         part = ped_disk_get_partition (disk, 1);
-        fail_if (ped_partition_get_flag(part, PED_PARTITION_BLS_BOOT) != 1, "BLS_BOOT flag not set");
+        ck_assert_msg(ped_partition_get_flag(part, PED_PARTITION_BLS_BOOT) == 1, "BLS_BOOT flag not set");
 
         ped_disk_destroy (disk);
         ped_device_destroy (dev);
diff --git a/libparted/tests/label.c b/libparted/tests/label.c
index e0d63c7..67b1b07 100644
--- a/libparted/tests/label.c
+++ b/libparted/tests/label.c
@@ -16,7 +16,7 @@ static void
 create_disk (void)
 {
         temporary_disk = _create_disk (80 * 1024 * 1024);
-        fail_if (temporary_disk == NULL, "Failed to create temporary disk");
+        ck_assert_msg(temporary_disk != NULL, "Failed to create temporary disk");
 }
 
 static void
@@ -72,12 +72,11 @@ START_TEST (test_probe_label)
 
                 /* Try to probe the disk label. */
                 probed = ped_disk_probe (dev);
-                fail_if (!probed,
+                ck_assert_msg(probed,
                          "Failed to probe the just created label of type: %s",
                          type->name);
                 if (probed && !STREQ (probed->name, type->name))
-                        fail_if (1,
-                                 "Probe returned label of type: %s as type: %s",
+                        ck_abort_msg("Probe returned label of type: %s as type: %s",
                                  type->name, probed->name);
         }
         ped_device_destroy (dev);
@@ -105,12 +104,11 @@ START_TEST (test_read_label)
 
                 /* Try to read the disk label. */
                 disk = ped_disk_new (dev);
-                fail_if (!disk,
+                ck_assert_msg(disk,
                          "Failed to read the just created label of type: %s",
                          type->name);
                 if (disk && !STREQ (disk->type->name, type->name))
-                        fail_if (1,
-                                 "Read returned label of type: %s as type: %s",
+                        ck_abort_msg("Read returned label of type: %s as type: %s",
                                  type->name, disk->type->name);
 
                 ped_disk_destroy (disk);
@@ -138,7 +136,7 @@ START_TEST (test_clone_label)
 
                 /* Try to clone the disk label. */
                 PedDisk* clone = ped_disk_duplicate (disk);
-                fail_if (!clone,
+                ck_assert_msg(clone,
                          "Failed to clone the just created label of type: %s",
                          type->name);
 
diff --git a/libparted/tests/symlink.c b/libparted/tests/symlink.c
index 52e99ca..da6bef8 100644
--- a/libparted/tests/symlink.c
+++ b/libparted/tests/symlink.c
@@ -30,7 +30,7 @@ static void
 create_disk (void)
 {
         temporary_disk = _create_disk (4096 * 1024);
-        fail_if (temporary_disk == NULL, "Failed to create temporary disk");
+        ck_assert_msg(temporary_disk != NULL, "Failed to create temporary disk");
 }
 
 static void
@@ -45,7 +45,7 @@ START_TEST (test_symlink)
         char cwd[256], ln[256] = "/dev/mapper/parted-test-XXXXXX";
 
         if (!getcwd (cwd, sizeof cwd)) {
-                fail ("Could not get cwd");
+                ck_abort_msg("Could not get cwd");
                 return;
         }
 
@@ -53,7 +53,7 @@ START_TEST (test_symlink)
            temporary disk */
         int tmp_fd = mkstemp (ln);
         if (tmp_fd == -1) {
-                fail ("Could not create tempfile");
+                ck_abort_msg("Could not create tempfile");
                 return;
         }
 
@@ -62,11 +62,17 @@ START_TEST (test_symlink)
         close (tmp_fd);
         unlink (ln);
         char temp_disk_path[256];
-        snprintf (temp_disk_path, sizeof temp_disk_path, "%s/%s", cwd,
-                  temporary_disk);
+        int r = snprintf(temp_disk_path, sizeof temp_disk_path, "%s/%s",
+                          cwd,
+                          temporary_disk);
+        if (r < 0 || r >= sizeof temp_disk_path) {
+                ck_abort_msg("symlink truncated");
+                return;
+        }
+
         int res = symlink (temp_disk_path, ln);
         if (res) {
-                fail ("could not create symlink");
+                ck_abort_msg("could not create symlink");
                 return;
         }
 
@@ -77,7 +83,7 @@ START_TEST (test_symlink)
         /* Create a second temporary_disk */
         char *temporary_disk2 = _create_disk (4096 * 1024);
         if (temporary_disk2 == NULL) {
-                fail ("Failed to create 2nd temporary disk");
+                ck_abort_msg("Failed to create 2nd temporary disk");
                 goto exit_destroy_dev;
         }
 
@@ -89,11 +95,16 @@ START_TEST (test_symlink)
 
         /* Update symlink to point to our new / second temporary disk */
         unlink (ln);
-        snprintf (temp_disk_path, sizeof temp_disk_path, "%s/%s", cwd,
-                  temporary_disk);
+        r = snprintf (temp_disk_path, sizeof temp_disk_path, "%s/%s",
+                          cwd, temporary_disk);
+        if (r < 0 || r >= sizeof temp_disk_path) {
+                ck_abort_msg("2nd symlink truncated");
+                goto exit_destroy_dev;
+        }
+
         res = symlink (temp_disk_path, ln);
         if (res) {
-                fail ("could not create 2nd symlink");
+                ck_abort_msg("could not create 2nd symlink");
                 goto exit_destroy_dev;
         }
 
diff --git a/libparted/tests/volser.c b/libparted/tests/volser.c
index c6efa5f..4b6e2d1 100644
--- a/libparted/tests/volser.c
+++ b/libparted/tests/volser.c
@@ -34,7 +34,7 @@ static void set_test (void)
         type = ped_disk_type_get ("dasd");
 
         tmp_disk = _create_disk (20*1024*1024);
-        fail_if (tmp_disk == NULL, "Failed to create temporary disk");
+        ck_assert_msg(tmp_disk != NULL, "Failed to create temporary disk");
         dev = ped_device_get (tmp_disk);
         if (dev == NULL)
                 return;
diff --git a/libparted/tests/zerolen.c b/libparted/tests/zerolen.c
index cf2bd1c..2d9b424 100644
--- a/libparted/tests/zerolen.c
+++ b/libparted/tests/zerolen.c
@@ -28,7 +28,7 @@ main (int argc, char **argv)
         TCase* tcase_probe = tcase_create ("Probe");
 
         if (argc < 2) {
-                fail ("Insufficient arguments");
+                ck_abort_msg("Insufficient arguments");
                 return EXIT_FAILURE;
         }
         temporary_disk = argv[1];
-- 
2.39.1




More information about the parted-devel mailing list