1 | /*
|
---|
2 | * Copyright (c) 2017 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <label/label.h>
|
---|
30 | #include <mem.h>
|
---|
31 | #include <pcut/pcut.h>
|
---|
32 | #include <stddef.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 |
|
---|
35 | PCUT_INIT
|
---|
36 |
|
---|
37 | PCUT_TEST_SUITE(label);
|
---|
38 |
|
---|
39 | static errno_t label_test_get_bsize(void *, size_t *);
|
---|
40 | static errno_t label_test_get_nblocks(void *, aoff64_t *);
|
---|
41 | static errno_t label_test_read(void *, aoff64_t, size_t, void *);
|
---|
42 | static errno_t label_test_write(void *, aoff64_t, size_t, const void *);
|
---|
43 |
|
---|
44 | label_bd_ops_t label_test_ops = {
|
---|
45 | .get_bsize = label_test_get_bsize,
|
---|
46 | .get_nblocks = label_test_get_nblocks,
|
---|
47 | .read = label_test_read,
|
---|
48 | .write = label_test_write
|
---|
49 | };
|
---|
50 |
|
---|
51 | /** Pretended block device for testing */
|
---|
52 | typedef struct {
|
---|
53 | char *data;
|
---|
54 | size_t bsize;
|
---|
55 | aoff64_t nblocks;
|
---|
56 | } test_bd_t;
|
---|
57 |
|
---|
58 | enum {
|
---|
59 | test_block_size = 512,
|
---|
60 | test_nblocks = 1024
|
---|
61 | };
|
---|
62 |
|
---|
63 | /** Create pretended block device.
|
---|
64 | *
|
---|
65 | * @param bsize Block size
|
---|
66 | * @param nblocks Number of blocks
|
---|
67 | * @param rbd Place to store pointer to new pretended block device
|
---|
68 | */
|
---|
69 | static errno_t test_bd_create(size_t bsize, aoff64_t nblocks, test_bd_t **rbd)
|
---|
70 | {
|
---|
71 | test_bd_t *bd;
|
---|
72 |
|
---|
73 | bd = calloc(1, sizeof(test_bd_t));
|
---|
74 | if (bd == NULL)
|
---|
75 | return ENOMEM;
|
---|
76 |
|
---|
77 | bd->data = calloc(bsize, nblocks);
|
---|
78 | if (bd->data == NULL) {
|
---|
79 | free(bd);
|
---|
80 | return ENOMEM;
|
---|
81 | }
|
---|
82 |
|
---|
83 | bd->bsize = bsize;
|
---|
84 | bd->nblocks = nblocks;
|
---|
85 | *rbd = bd;
|
---|
86 |
|
---|
87 | return EOK;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Destroy pretended block device.
|
---|
91 | *
|
---|
92 | * @param bd Pretended block device
|
---|
93 | */
|
---|
94 | static void test_bd_destroy(test_bd_t *bd)
|
---|
95 | {
|
---|
96 | free(bd->data);
|
---|
97 | free(bd);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Get block size wrapper for liblabel */
|
---|
101 | static errno_t label_test_get_bsize(void *arg, size_t *bsize)
|
---|
102 | {
|
---|
103 | test_bd_t *bd = (test_bd_t *)arg;
|
---|
104 |
|
---|
105 | *bsize = bd->bsize;
|
---|
106 | return EOK;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Get number of blocks wrapper for liblabel */
|
---|
110 | static errno_t label_test_get_nblocks(void *arg, aoff64_t *nblocks)
|
---|
111 | {
|
---|
112 | test_bd_t *bd = (test_bd_t *)arg;
|
---|
113 |
|
---|
114 | *nblocks = bd->nblocks;
|
---|
115 | return EOK;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Read blocks wrapper for liblabel */
|
---|
119 | static errno_t label_test_read(void *arg, aoff64_t ba, size_t cnt, void *buf)
|
---|
120 | {
|
---|
121 | test_bd_t *bd = (test_bd_t *)arg;
|
---|
122 |
|
---|
123 | if (ba >= bd->nblocks)
|
---|
124 | return EINVAL;
|
---|
125 |
|
---|
126 | memcpy(buf, bd->data + ba * bd->bsize, bd->bsize);
|
---|
127 | return EOK;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** Write blocks wrapper for liblabel */
|
---|
131 | static errno_t label_test_write(void *arg, aoff64_t ba, size_t cnt, const void *data)
|
---|
132 | {
|
---|
133 | test_bd_t *bd = (test_bd_t *)arg;
|
---|
134 |
|
---|
135 | if (ba >= bd->nblocks)
|
---|
136 | return EINVAL;
|
---|
137 |
|
---|
138 | memcpy(bd->data + ba * bd->bsize, data, bd->bsize);
|
---|
139 | return EOK;
|
---|
140 | }
|
---|
141 |
|
---|
142 | PCUT_TEST(open_empty) {
|
---|
143 | label_t *label;
|
---|
144 | label_bd_t lbd;
|
---|
145 | label_info_t linfo;
|
---|
146 | label_part_t *part;
|
---|
147 | test_bd_t *bd = NULL;
|
---|
148 | errno_t rc;
|
---|
149 |
|
---|
150 | rc = test_bd_create(test_block_size, test_nblocks, &bd);
|
---|
151 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
152 |
|
---|
153 | lbd.ops = &label_test_ops;
|
---|
154 | lbd.arg = (void *)bd;
|
---|
155 |
|
---|
156 | rc = label_open(&lbd, &label);
|
---|
157 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
158 |
|
---|
159 | rc = label_get_info(label, &linfo);
|
---|
160 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
161 |
|
---|
162 | PCUT_ASSERT_INT_EQUALS(lt_none, linfo.ltype);
|
---|
163 | PCUT_ASSERT_INT_EQUALS(0, linfo.flags);
|
---|
164 |
|
---|
165 | /* There should be exactly one pseudo partition */
|
---|
166 | part = label_part_first(label);
|
---|
167 | PCUT_ASSERT_NOT_NULL(part);
|
---|
168 |
|
---|
169 | part = label_part_next(part);
|
---|
170 | PCUT_ASSERT_NULL(part);
|
---|
171 |
|
---|
172 | label_close(label);
|
---|
173 |
|
---|
174 | test_bd_destroy(bd);
|
---|
175 | }
|
---|
176 |
|
---|
177 | PCUT_TEST(create_destroy_mbr) {
|
---|
178 | label_t *label;
|
---|
179 | label_bd_t lbd;
|
---|
180 | label_info_t linfo;
|
---|
181 | label_part_t *part;
|
---|
182 | test_bd_t *bd = NULL;
|
---|
183 | errno_t rc;
|
---|
184 |
|
---|
185 | rc = test_bd_create(test_block_size, test_nblocks, &bd);
|
---|
186 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
187 |
|
---|
188 | lbd.ops = &label_test_ops;
|
---|
189 | lbd.arg = (void *)bd;
|
---|
190 |
|
---|
191 | rc = label_create(&lbd, lt_mbr, &label);
|
---|
192 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
193 |
|
---|
194 | rc = label_get_info(label, &linfo);
|
---|
195 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
196 |
|
---|
197 | PCUT_ASSERT_INT_EQUALS(lt_mbr, linfo.ltype);
|
---|
198 | PCUT_ASSERT_INT_EQUALS(lf_ext_supp | lf_can_create_pri |
|
---|
199 | lf_can_create_ext, linfo.flags);
|
---|
200 |
|
---|
201 | /* There should be no partitions */
|
---|
202 | part = label_part_first(label);
|
---|
203 | PCUT_ASSERT_NULL(part);
|
---|
204 |
|
---|
205 | /* Close and reopen */
|
---|
206 | label_close(label);
|
---|
207 |
|
---|
208 | rc = label_open(&lbd, &label);
|
---|
209 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
210 |
|
---|
211 | rc = label_get_info(label, &linfo);
|
---|
212 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
213 |
|
---|
214 | /* Everything should still be the same */
|
---|
215 | PCUT_ASSERT_INT_EQUALS(lt_mbr, linfo.ltype);
|
---|
216 | PCUT_ASSERT_INT_EQUALS(lf_ext_supp | lf_can_create_pri |
|
---|
217 | lf_can_create_ext, linfo.flags);
|
---|
218 |
|
---|
219 | rc = label_destroy(label);
|
---|
220 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
221 |
|
---|
222 | /* There should be no label now */
|
---|
223 |
|
---|
224 | rc = label_open(&lbd, &label);
|
---|
225 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
226 |
|
---|
227 | rc = label_get_info(label, &linfo);
|
---|
228 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
229 |
|
---|
230 | PCUT_ASSERT_INT_EQUALS(lt_none, linfo.ltype);
|
---|
231 | PCUT_ASSERT_INT_EQUALS(0, linfo.flags);
|
---|
232 |
|
---|
233 | label_close(label);
|
---|
234 |
|
---|
235 | test_bd_destroy(bd);
|
---|
236 | }
|
---|
237 |
|
---|
238 | PCUT_TEST(create_destroy_gpt) {
|
---|
239 | label_t *label;
|
---|
240 | label_bd_t lbd;
|
---|
241 | label_info_t linfo;
|
---|
242 | label_part_t *part;
|
---|
243 | test_bd_t *bd = NULL;
|
---|
244 | errno_t rc;
|
---|
245 |
|
---|
246 | rc = test_bd_create(test_block_size, test_nblocks, &bd);
|
---|
247 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
248 |
|
---|
249 | lbd.ops = &label_test_ops;
|
---|
250 | lbd.arg = (void *)bd;
|
---|
251 |
|
---|
252 | rc = label_create(&lbd, lt_gpt, &label);
|
---|
253 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
254 |
|
---|
255 | rc = label_get_info(label, &linfo);
|
---|
256 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
257 |
|
---|
258 | PCUT_ASSERT_INT_EQUALS(lt_gpt, linfo.ltype);
|
---|
259 | PCUT_ASSERT_INT_EQUALS(lf_can_create_pri | lf_ptype_uuid, linfo.flags);
|
---|
260 |
|
---|
261 | /* There should be no partitions */
|
---|
262 | part = label_part_first(label);
|
---|
263 | PCUT_ASSERT_NULL(part);
|
---|
264 |
|
---|
265 | /* Close and reopen */
|
---|
266 | label_close(label);
|
---|
267 |
|
---|
268 | rc = label_open(&lbd, &label);
|
---|
269 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
270 |
|
---|
271 | rc = label_get_info(label, &linfo);
|
---|
272 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
273 |
|
---|
274 | /* Everything should still be the same */
|
---|
275 | PCUT_ASSERT_INT_EQUALS(lt_gpt, linfo.ltype);
|
---|
276 | PCUT_ASSERT_INT_EQUALS(lf_can_create_pri | lf_ptype_uuid, linfo.flags);
|
---|
277 |
|
---|
278 | rc = label_destroy(label);
|
---|
279 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
280 |
|
---|
281 | /* There should be no label now */
|
---|
282 |
|
---|
283 | rc = label_open(&lbd, &label);
|
---|
284 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
285 |
|
---|
286 | rc = label_get_info(label, &linfo);
|
---|
287 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
288 |
|
---|
289 | PCUT_ASSERT_INT_EQUALS(lt_none, linfo.ltype);
|
---|
290 | PCUT_ASSERT_INT_EQUALS(0, linfo.flags);
|
---|
291 |
|
---|
292 | label_close(label);
|
---|
293 |
|
---|
294 | test_bd_destroy(bd);
|
---|
295 | }
|
---|
296 |
|
---|
297 | PCUT_TEST(mbr_primary_part) {
|
---|
298 | label_t *label;
|
---|
299 | label_bd_t lbd;
|
---|
300 | label_info_t linfo;
|
---|
301 | label_part_t *part;
|
---|
302 | label_part_spec_t pspec;
|
---|
303 | label_part_info_t pinfo;
|
---|
304 | label_ptype_t ptype;
|
---|
305 | test_bd_t *bd = NULL;
|
---|
306 | errno_t rc;
|
---|
307 |
|
---|
308 | rc = test_bd_create(test_block_size, test_nblocks, &bd);
|
---|
309 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
310 |
|
---|
311 | lbd.ops = &label_test_ops;
|
---|
312 | lbd.arg = (void *)bd;
|
---|
313 |
|
---|
314 | rc = label_create(&lbd, lt_mbr, &label);
|
---|
315 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
316 |
|
---|
317 | rc = label_get_info(label, &linfo);
|
---|
318 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
319 |
|
---|
320 | /* There should be no partitions */
|
---|
321 | part = label_part_first(label);
|
---|
322 | PCUT_ASSERT_NULL(part);
|
---|
323 |
|
---|
324 | rc = label_suggest_ptype(label, lpc_ext4, &ptype);
|
---|
325 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
326 |
|
---|
327 | memset(&pspec, 0, sizeof(pspec));
|
---|
328 | pspec.index = 1;
|
---|
329 | pspec.block0 = linfo.ablock0;
|
---|
330 | pspec.nblocks = linfo.anblocks;
|
---|
331 | pspec.hdr_blocks = 0;
|
---|
332 | pspec.pkind = lpk_primary;
|
---|
333 | pspec.ptype = ptype;
|
---|
334 |
|
---|
335 | rc = label_part_create(label, &pspec, &part);
|
---|
336 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
337 |
|
---|
338 | label_part_get_info(part, &pinfo);
|
---|
339 | PCUT_ASSERT_INT_EQUALS(1, pinfo.index);
|
---|
340 | PCUT_ASSERT_INT_EQUALS(lpk_primary, pinfo.pkind);
|
---|
341 | PCUT_ASSERT_INT_EQUALS(linfo.ablock0, pinfo.block0);
|
---|
342 | PCUT_ASSERT_INT_EQUALS(linfo.anblocks, pinfo.nblocks);
|
---|
343 |
|
---|
344 | /* Close and reopen */
|
---|
345 | label_close(label);
|
---|
346 |
|
---|
347 | rc = label_open(&lbd, &label);
|
---|
348 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
349 |
|
---|
350 | rc = label_get_info(label, &linfo);
|
---|
351 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
352 |
|
---|
353 | PCUT_ASSERT_INT_EQUALS(lt_mbr, linfo.ltype);
|
---|
354 | PCUT_ASSERT_INT_EQUALS(lf_ext_supp | lf_can_create_pri |
|
---|
355 | lf_can_create_ext | lf_can_delete_part, linfo.flags);
|
---|
356 |
|
---|
357 | part = label_part_first(label);
|
---|
358 | PCUT_ASSERT_NOT_NULL(part);
|
---|
359 | PCUT_ASSERT_NULL(label_part_next(part));
|
---|
360 |
|
---|
361 | label_part_get_info(part, &pinfo);
|
---|
362 | PCUT_ASSERT_INT_EQUALS(1, pinfo.index);
|
---|
363 | PCUT_ASSERT_INT_EQUALS(lpk_primary, pinfo.pkind);
|
---|
364 | PCUT_ASSERT_INT_EQUALS(linfo.ablock0, pinfo.block0);
|
---|
365 | PCUT_ASSERT_INT_EQUALS(linfo.anblocks, pinfo.nblocks);
|
---|
366 |
|
---|
367 | /* Destroy the partition */
|
---|
368 | rc = label_part_destroy(part);
|
---|
369 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
370 |
|
---|
371 | /* Close and reopen */
|
---|
372 | label_close(label);
|
---|
373 |
|
---|
374 | rc = label_open(&lbd, &label);
|
---|
375 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
376 |
|
---|
377 | /* There should be no partitions */
|
---|
378 | part = label_part_first(label);
|
---|
379 | PCUT_ASSERT_NULL(part);
|
---|
380 |
|
---|
381 | label_close(label);
|
---|
382 |
|
---|
383 | test_bd_destroy(bd);
|
---|
384 | }
|
---|
385 |
|
---|
386 | PCUT_TEST(mbr_logical_part) {
|
---|
387 | label_t *label;
|
---|
388 | label_bd_t lbd;
|
---|
389 | label_info_t linfo;
|
---|
390 | label_part_t *part, *lpart, *epart;
|
---|
391 | label_part_spec_t pspec;
|
---|
392 | label_ptype_t ptype;
|
---|
393 | label_part_info_t pinfo, lpinfo, epinfo;
|
---|
394 | test_bd_t *bd = NULL;
|
---|
395 | errno_t rc;
|
---|
396 |
|
---|
397 | rc = test_bd_create(test_block_size, test_nblocks, &bd);
|
---|
398 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
399 |
|
---|
400 | lbd.ops = &label_test_ops;
|
---|
401 | lbd.arg = (void *)bd;
|
---|
402 |
|
---|
403 | rc = label_create(&lbd, lt_mbr, &label);
|
---|
404 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
405 |
|
---|
406 | rc = label_get_info(label, &linfo);
|
---|
407 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
408 |
|
---|
409 | /* There should be no partitions */
|
---|
410 | part = label_part_first(label);
|
---|
411 | PCUT_ASSERT_NULL(part);
|
---|
412 |
|
---|
413 | memset(&pspec, 0, sizeof(pspec));
|
---|
414 | pspec.index = 1;
|
---|
415 | pspec.block0 = linfo.ablock0;
|
---|
416 | pspec.nblocks = linfo.anblocks;
|
---|
417 | pspec.hdr_blocks = 0;
|
---|
418 | pspec.pkind = lpk_extended;
|
---|
419 |
|
---|
420 | rc = label_part_create(label, &pspec, &epart);
|
---|
421 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
422 |
|
---|
423 | label_part_get_info(epart, &epinfo);
|
---|
424 | PCUT_ASSERT_INT_EQUALS(1, epinfo.index);
|
---|
425 | PCUT_ASSERT_INT_EQUALS(lpk_extended, epinfo.pkind);
|
---|
426 | PCUT_ASSERT_INT_EQUALS(linfo.ablock0, epinfo.block0);
|
---|
427 | PCUT_ASSERT_INT_EQUALS(linfo.anblocks, epinfo.nblocks);
|
---|
428 |
|
---|
429 | /* Close and reopen */
|
---|
430 | label_close(label);
|
---|
431 |
|
---|
432 | rc = label_open(&lbd, &label);
|
---|
433 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
434 |
|
---|
435 | rc = label_get_info(label, &linfo);
|
---|
436 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
437 |
|
---|
438 | PCUT_ASSERT_INT_EQUALS(lt_mbr, linfo.ltype);
|
---|
439 | PCUT_ASSERT_INT_EQUALS(lf_ext_supp | lf_can_create_pri |
|
---|
440 | lf_can_create_log | lf_can_delete_part, linfo.flags);
|
---|
441 |
|
---|
442 | epart = label_part_first(label);
|
---|
443 | PCUT_ASSERT_NOT_NULL(epart);
|
---|
444 | PCUT_ASSERT_NULL(label_part_next(epart));
|
---|
445 |
|
---|
446 | label_part_get_info(epart, &epinfo);
|
---|
447 | PCUT_ASSERT_INT_EQUALS(1, epinfo.index);
|
---|
448 | PCUT_ASSERT_INT_EQUALS(lpk_extended, epinfo.pkind);
|
---|
449 | PCUT_ASSERT_INT_EQUALS(linfo.ablock0, epinfo.block0);
|
---|
450 | PCUT_ASSERT_INT_EQUALS(linfo.anblocks, epinfo.nblocks);
|
---|
451 |
|
---|
452 | /* Create logical partition */
|
---|
453 | rc = label_suggest_ptype(label, lpc_ext4, &ptype);
|
---|
454 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
455 |
|
---|
456 | memset(&pspec, 0, sizeof(pspec));
|
---|
457 | pspec.index = 0;
|
---|
458 | pspec.block0 = epinfo.block0 + 1;
|
---|
459 | pspec.nblocks = epinfo.nblocks - 1;
|
---|
460 | pspec.hdr_blocks = 1;
|
---|
461 | pspec.pkind = lpk_logical;
|
---|
462 | pspec.ptype = ptype;
|
---|
463 |
|
---|
464 | rc = label_part_create(label, &pspec, &lpart);
|
---|
465 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
466 |
|
---|
467 | label_part_get_info(lpart, &lpinfo);
|
---|
468 | PCUT_ASSERT_INT_EQUALS(5, lpinfo.index);
|
---|
469 | PCUT_ASSERT_INT_EQUALS(lpk_logical, lpinfo.pkind);
|
---|
470 | PCUT_ASSERT_INT_EQUALS(epinfo.block0 + 1, lpinfo.block0);
|
---|
471 | PCUT_ASSERT_INT_EQUALS(epinfo.nblocks - 1, lpinfo.nblocks);
|
---|
472 |
|
---|
473 | /* Close and reopen */
|
---|
474 | label_close(label);
|
---|
475 |
|
---|
476 | rc = label_open(&lbd, &label);
|
---|
477 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
478 |
|
---|
479 | /* Find the extended and the logical partition */
|
---|
480 |
|
---|
481 | epart = NULL;
|
---|
482 | lpart = NULL;
|
---|
483 |
|
---|
484 | part = label_part_first(label);
|
---|
485 | while (part != NULL) {
|
---|
486 | label_part_get_info(part, &pinfo);
|
---|
487 | if (pinfo.pkind == lpk_extended) {
|
---|
488 | epart = part;
|
---|
489 | } else {
|
---|
490 | PCUT_ASSERT_INT_EQUALS(lpk_logical, pinfo.pkind);
|
---|
491 | lpart = part;
|
---|
492 | }
|
---|
493 |
|
---|
494 | part = label_part_next(part);
|
---|
495 | }
|
---|
496 |
|
---|
497 | PCUT_ASSERT_NOT_NULL(epart);
|
---|
498 | PCUT_ASSERT_NOT_NULL(lpart);
|
---|
499 |
|
---|
500 | /* Destroy the logical partition */
|
---|
501 | rc = label_part_destroy(lpart);
|
---|
502 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
503 |
|
---|
504 | /* Destroy the extended partition */
|
---|
505 | rc = label_part_destroy(epart);
|
---|
506 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
507 |
|
---|
508 | /* Close and reopen */
|
---|
509 | label_close(label);
|
---|
510 |
|
---|
511 | rc = label_open(&lbd, &label);
|
---|
512 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
513 |
|
---|
514 | /* There should be no partitions */
|
---|
515 | part = label_part_first(label);
|
---|
516 | PCUT_ASSERT_NULL(part);
|
---|
517 |
|
---|
518 | label_close(label);
|
---|
519 |
|
---|
520 | test_bd_destroy(bd);
|
---|
521 | }
|
---|
522 |
|
---|
523 |
|
---|
524 | PCUT_TEST(gpt_part) {
|
---|
525 | label_t *label;
|
---|
526 | label_bd_t lbd;
|
---|
527 | label_info_t linfo;
|
---|
528 | label_part_t *part;
|
---|
529 | label_part_spec_t pspec;
|
---|
530 | label_part_info_t pinfo;
|
---|
531 | label_ptype_t ptype;
|
---|
532 | test_bd_t *bd = NULL;
|
---|
533 | errno_t rc;
|
---|
534 |
|
---|
535 | rc = test_bd_create(test_block_size, test_nblocks, &bd);
|
---|
536 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
537 |
|
---|
538 | lbd.ops = &label_test_ops;
|
---|
539 | lbd.arg = (void *)bd;
|
---|
540 |
|
---|
541 | rc = label_create(&lbd, lt_gpt, &label);
|
---|
542 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
543 |
|
---|
544 | rc = label_get_info(label, &linfo);
|
---|
545 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
546 |
|
---|
547 | /* There should be no partitions */
|
---|
548 | part = label_part_first(label);
|
---|
549 | PCUT_ASSERT_NULL(part);
|
---|
550 |
|
---|
551 | rc = label_suggest_ptype(label, lpc_ext4, &ptype);
|
---|
552 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
553 |
|
---|
554 | memset(&pspec, 0, sizeof(pspec));
|
---|
555 | pspec.index = 1;
|
---|
556 | pspec.block0 = linfo.ablock0;
|
---|
557 | pspec.nblocks = linfo.anblocks;
|
---|
558 | pspec.hdr_blocks = 0;
|
---|
559 | pspec.pkind = lpk_primary;
|
---|
560 | pspec.ptype = ptype;
|
---|
561 |
|
---|
562 | rc = label_part_create(label, &pspec, &part);
|
---|
563 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
564 |
|
---|
565 | label_part_get_info(part, &pinfo);
|
---|
566 | PCUT_ASSERT_INT_EQUALS(1, pinfo.index);
|
---|
567 | PCUT_ASSERT_INT_EQUALS(lpk_primary, pinfo.pkind);
|
---|
568 | PCUT_ASSERT_INT_EQUALS(linfo.ablock0, pinfo.block0);
|
---|
569 | PCUT_ASSERT_INT_EQUALS(linfo.anblocks, pinfo.nblocks);
|
---|
570 |
|
---|
571 | /* Close and reopen */
|
---|
572 | label_close(label);
|
---|
573 |
|
---|
574 | rc = label_open(&lbd, &label);
|
---|
575 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
576 |
|
---|
577 | rc = label_get_info(label, &linfo);
|
---|
578 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
579 |
|
---|
580 | PCUT_ASSERT_INT_EQUALS(lt_gpt, linfo.ltype);
|
---|
581 | PCUT_ASSERT_INT_EQUALS(lf_can_create_pri | lf_ptype_uuid |
|
---|
582 | lf_can_delete_part, linfo.flags);
|
---|
583 |
|
---|
584 | part = label_part_first(label);
|
---|
585 | PCUT_ASSERT_NOT_NULL(part);
|
---|
586 | PCUT_ASSERT_NULL(label_part_next(part));
|
---|
587 |
|
---|
588 | label_part_get_info(part, &pinfo);
|
---|
589 | PCUT_ASSERT_INT_EQUALS(1, pinfo.index);
|
---|
590 | PCUT_ASSERT_INT_EQUALS(lpk_primary, pinfo.pkind);
|
---|
591 | PCUT_ASSERT_INT_EQUALS(linfo.ablock0, pinfo.block0);
|
---|
592 | PCUT_ASSERT_INT_EQUALS(linfo.anblocks, pinfo.nblocks);
|
---|
593 |
|
---|
594 | /* Destroy the partition */
|
---|
595 | rc = label_part_destroy(part);
|
---|
596 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
597 |
|
---|
598 | /* Close and reopen */
|
---|
599 | label_close(label);
|
---|
600 |
|
---|
601 | rc = label_open(&lbd, &label);
|
---|
602 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
603 |
|
---|
604 | /* There should be no partitions */
|
---|
605 | part = label_part_first(label);
|
---|
606 | PCUT_ASSERT_NULL(part);
|
---|
607 |
|
---|
608 | label_close(label);
|
---|
609 |
|
---|
610 | test_bd_destroy(bd);
|
---|
611 | }
|
---|
612 |
|
---|
613 | PCUT_EXPORT(label);
|
---|