summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libparted/disk.c17
-rw-r--r--libparted/tests/Makefile.am8
-rw-r--r--libparted/tests/disk.c111
-rwxr-xr-xlibparted/tests/t2000-disk.sh27
4 files changed, 153 insertions, 10 deletions
diff --git a/libparted/disk.c b/libparted/disk.c
index e2e55c3..34b1677 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -223,23 +223,24 @@ static int
_add_duplicate_part (PedDisk* disk, PedPartition* old_part)
{
PedPartition* new_part;
- PedConstraint* constraint_exact;
+ int ret;
new_part = disk->type->ops->partition_duplicate (old_part);
if (!new_part)
goto error;
new_part->disk = disk;
- constraint_exact = ped_constraint_exact (&new_part->geom);
- if (!constraint_exact)
+ _disk_push_update_mode (disk);
+ ret = _disk_raw_add (disk, new_part);
+ _disk_pop_update_mode (disk);
+ if (!ret)
goto error_destroy_new_part;
- if (!ped_disk_add_partition (disk, new_part, constraint_exact))
- goto error_destroy_constraint_exact;
- ped_constraint_destroy (constraint_exact);
+#ifdef DEBUG
+ if (!_disk_check_sanity (disk))
+ goto error_destroy_new_part;
+#endif
return 1;
-error_destroy_constraint_exact:
- ped_constraint_destroy (constraint_exact);
error_destroy_new_part:
ped_partition_destroy (new_part);
error:
diff --git a/libparted/tests/Makefile.am b/libparted/tests/Makefile.am
index 12ad29f..d526207 100644
--- a/libparted/tests/Makefile.am
+++ b/libparted/tests/Makefile.am
@@ -3,13 +3,17 @@
#
# This file may be modified and/or distributed without restriction.
-TESTS = t1000-label.sh
+TESTS = t1000-label.sh t2000-disk.sh
EXTRA_DIST = $(TESTS)
-bin_PROGRAMS = label
+bin_PROGRAMS = label disk
label_CFLAGS = $(CHECK_CFLAGS) -I$(top_srcdir)/include
label_LDADD = $(CHECK_LIBS) $(top_builddir)/libparted/libparted.la
label_SOURCES = common.h common.c label.c
+disk_CFLAGS = $(CHECK_CFLAGS) -I$(top_srcdir)/include
+disk_LDADD = $(CHECK_LIBS) $(top_builddir)/libparted/libparted.la
+disk_SOURCES = common.h common.c disk.c
+
MAINTAINERCLEANFILES = Makefile.in
CLEANFILES = init.sh
diff --git a/libparted/tests/disk.c b/libparted/tests/disk.c
new file mode 100644
index 0000000..295ec05
--- /dev/null
+++ b/libparted/tests/disk.c
@@ -0,0 +1,111 @@
+#include <config.h>
+#include <unistd.h>
+
+#include <check.h>
+
+#include <parted/parted.h>
+
+#include "common.h"
+
+static char* temporary_disk;
+
+static void
+create_disk (void)
+{
+ temporary_disk = _create_disk (20);
+ fail_if (temporary_disk == NULL, "Failed to create temporary disk");
+}
+
+static void
+destroy_disk (void)
+{
+ unlink (temporary_disk);
+ free (temporary_disk);
+}
+
+/* TEST: Create a disklabel on a simple disk image */
+START_TEST (test_duplicate)
+{
+ PedDevice* dev = ped_device_get (temporary_disk);
+ if (dev == NULL)
+ return;
+
+ PedDiskType* type;
+ PedDisk* disk;
+ PedDisk* disk_dup;
+ PedPartition *part;
+ PedPartition *part_dup;
+ PedConstraint *constraint;
+
+ int part_num[] = {1, 5, 6, 0};
+
+ disk = _create_disk_label (dev, ped_disk_type_get ("msdos"));
+
+ constraint = ped_constraint_any (dev);
+
+ /* Primary partition from 16,4kB to 15MB */
+ part = ped_partition_new (disk, PED_PARTITION_EXTENDED,
+ NULL,
+ 32, 29311);
+ ped_disk_add_partition (disk, part, constraint);
+
+ /* Logical partition from 10MB to 15MB */
+ part = ped_partition_new (disk, PED_PARTITION_LOGICAL,
+ ped_file_system_type_get ("ext2"),
+ 19584, 29311);
+ ped_disk_add_partition (disk, part, constraint);
+
+ /* Logical partition from 16,4kB to 4981kB */
+ part = ped_partition_new (disk, PED_PARTITION_LOGICAL,
+ ped_file_system_type_get ("ext2"),
+ 32, 9727);
+ ped_disk_add_partition (disk, part, constraint);
+
+ ped_disk_commit (disk);
+
+ ped_constraint_destroy (constraint);
+
+ disk_dup = ped_disk_duplicate (disk);
+
+ /* Checks if both partitions match */
+ for (int *i = part_num; *i != 0; i++) {
+ 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,
+ "Duplicated partition %d doesn't match. "
+ "Details are start: %d/%d end: %d/%d\n",
+ *i, part->geom.start, part_dup->geom.start,
+ part->geom.end, part_dup->geom.end);
+ }
+
+ ped_disk_destroy (disk);
+ ped_device_destroy (dev);
+}
+END_TEST
+
+int
+main (void)
+{
+ int number_failed;
+ Suite* suite = suite_create ("Disk");
+ TCase* tcase_duplicate = tcase_create ("Duplicate");
+
+ /* Fail when an exception is raised */
+ ped_exception_set_handler (_test_exception_handler);
+
+ tcase_add_checked_fixture (tcase_duplicate, create_disk, destroy_disk);
+ tcase_add_test (tcase_duplicate, test_duplicate);
+ /* Disable timeout for this test */
+ tcase_set_timeout (tcase_duplicate, 0);
+ suite_add_tcase (suite, tcase_duplicate);
+
+ SRunner* srunner = srunner_create (suite);
+ srunner_run_all (srunner, CK_VERBOSE);
+
+ number_failed = srunner_ntests_failed (srunner);
+ srunner_free (srunner);
+
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
diff --git a/libparted/tests/t2000-disk.sh b/libparted/tests/t2000-disk.sh
new file mode 100755
index 0000000..7a85b98
--- /dev/null
+++ b/libparted/tests/t2000-disk.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+# Copyright (C) 2007 Free Software Foundation, Inc.
+
+# This program 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.
+
+# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+
+test_description='run the disk unit tests in a directory supporting O_DIRECT'
+# This wrapper around the ./label binary is used to find a directory
+# in which one can open a file with the O_DIRECT flag.
+
+. ./init.sh
+
+test_expect_success \
+ 'run the actual tests' 'disk'
+
+test_done