summaryrefslogtreecommitdiff
path: root/libparted/tests/disk.c
blob: cf69b002359eb29ea6f922c199f026530be9931d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <config.h>
#include <unistd.h>

#include <check.h>

#include <parted/parted.h>

#include "common.h"
#include "progname.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;

        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)
{
        set_program_name (argv[0]);
        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;
}