File: label.c

package info (click to toggle)
parted 3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 17,324 kB
  • sloc: ansic: 73,015; sh: 11,628; makefile: 645; perl: 179; python: 44; asm: 36; sed: 16
file content (197 lines) | stat: -rw-r--r-- 6,322 bytes parent folder | download | duplicates (6)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <config.h>
#include <unistd.h>

#include <check.h>

#include <parted/parted.h>

#include "common.h"
#include "progname.h"

#define STREQ(a, b) (strcmp (a, b) == 0)

static char* temporary_disk;

static void
create_disk (void)
{
        temporary_disk = _create_disk (80 * 1024 * 1024);
        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_create_label)
{
        PedDevice* dev = ped_device_get (temporary_disk);
        if (dev == NULL)
                return;

        PedDiskType* type;
        PedDisk* disk;

        for (type = ped_disk_type_get_next (NULL); type;
             type = ped_disk_type_get_next (type)) {
          fprintf (stderr, "create label: %s\n", type->name); fflush (stderr);

                if (!_implemented_disk_label (type->name))
                        continue;

                disk = _create_disk_label (dev, type);
                ped_disk_destroy (disk);
        }
        ped_device_destroy (dev);
}
END_TEST

/* TEST: Probe the disk label of a loop device. */
START_TEST (test_probe_label)
{
        PedDevice* dev = ped_device_get (temporary_disk);
        if (dev == NULL)
                return;

        PedDiskType* probed;
        PedDiskType* type;
        PedDisk* disk;

        for (type = ped_disk_type_get_next (NULL); type;
             type = ped_disk_type_get_next (type)) {
          fprintf (stderr, "PROBE label: %s\n", type->name); fflush (stderr);
                if (!_implemented_disk_label (type->name))
                        continue;

                disk = _create_disk_label (dev, type);
                ped_disk_destroy (disk);

                /* Try to probe the disk label. */
                probed = ped_disk_probe (dev);
                fail_if (!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",
                                 type->name, probed->name);
        }
        ped_device_destroy (dev);
}
END_TEST

/* TEST: Read the disk label of a loop device. */
START_TEST (test_read_label)
{
        PedDevice* dev = ped_device_get (temporary_disk);
        if (dev == NULL)
                return;

        PedDiskType* type;
        PedDisk* disk;

        for (type = ped_disk_type_get_next (NULL); type;
             type = ped_disk_type_get_next (type)) {
          fprintf (stderr, "read label: %s\n", type->name); fflush (stderr);
                if (!_implemented_disk_label (type->name))
                        continue;

                disk = _create_disk_label (dev, type);
                ped_disk_destroy (disk);

                /* Try to read the disk label. */
                disk = ped_disk_new (dev);
                fail_if (!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",
                                 type->name, disk->type->name);

                ped_disk_destroy (disk);
        }
        ped_device_destroy (dev);
}
END_TEST

/* TEST: Clone the disk label of a loop device. */
START_TEST (test_clone_label)
{
        PedDevice* dev = ped_device_get (temporary_disk);
        if (dev == NULL)
                return;

        PedDiskType* type;

        for (type = ped_disk_type_get_next (NULL); type;
             type = ped_disk_type_get_next (type)) {
          fprintf (stderr, "clone label: %s\n", type->name); fflush (stderr);
                if (!_implemented_disk_label (type->name))
                        continue;

                PedDisk* disk = _create_disk_label (dev, type);

                /* Try to clone the disk label. */
                PedDisk* clone = ped_disk_duplicate (disk);
                fail_if (!clone,
                         "Failed to clone the just created label of type: %s",
                         type->name);

                ped_disk_destroy (clone);
                ped_disk_destroy (disk);
        }
        ped_device_destroy (dev);
}
END_TEST

int
main (int argc, char **argv)
{
        set_program_name (argv[0]);
        int number_failed;
        Suite* suite = suite_create ("Disk Label");
        TCase* tcase_basic = tcase_create ("Create");
        TCase* tcase_probe = tcase_create ("Probe");
        TCase* tcase_read = tcase_create ("Read");
        TCase* tcase_clone = tcase_create ("Clone");

        /* Fail when an exception is raised */
        ped_exception_set_handler (_test_exception_handler);

        tcase_add_checked_fixture (tcase_basic, create_disk, destroy_disk);
        tcase_add_test (tcase_basic, test_create_label);
        /* Disable timeout for this test */
        tcase_set_timeout (tcase_basic, 0);
        suite_add_tcase (suite, tcase_basic);

        tcase_add_checked_fixture (tcase_probe, create_disk, destroy_disk);
        tcase_add_test (tcase_probe, test_probe_label);
        /* Disable timeout for this test. */
        tcase_set_timeout (tcase_probe, 0);
        suite_add_tcase (suite, tcase_probe);

        tcase_add_checked_fixture (tcase_read, create_disk, destroy_disk);
        tcase_add_test (tcase_read, test_read_label);
        /* Disable timeout for this test. */
        tcase_set_timeout (tcase_read, 0);
        suite_add_tcase (suite, tcase_read);

        tcase_add_checked_fixture (tcase_clone, create_disk, destroy_disk);
        tcase_add_test (tcase_clone, test_clone_label);
        /* Disable timeout for this test. */
        tcase_set_timeout (tcase_clone, 0);
        suite_add_tcase (suite, tcase_clone);

        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;
}