label_test.c (2339B)
1 /* see LICENSE file for copyright and license details */ 2 /* a table made, written to an image, read back and changed: 3 * cc -Ishared shared/libdisk/label_test.c shared/libdisk/label.c shared/libdisk/gpt.c \ 4 * shared/libutil/diskutil.c shared/libutil/libutil.a -o label_test && ./label_test */ 5 #include "label.h" 6 7 #include <assert.h> 8 #include <fcntl.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <unistd.h> 13 14 #define IMAGE_MIB 64 15 16 static void 17 roundtrip(const char *path, enum label_kind kind) 18 { 19 struct BlockDev dev; 20 struct label a, b; 21 struct label_gap gaps[8]; 22 int esp, root, n; 23 24 assert(blockdev_open(&dev, path, 1) == 0); 25 assert(label_init(&a, &dev, kind) == 0); 26 assert(label_gaps(&a, gaps, 8) == 1); 27 esp = label_add(&a, 0, 16 * 2048, "ef00", "ESP"); 28 root = label_add(&a, 0, 0, "8300", "ROOT"); 29 assert(esp == 0 && root == 1); 30 assert(a.part[esp].start % LABEL_ALIGN == 0 && a.part[root].start == a.part[esp].end + 1); 31 assert(label_gaps(&a, gaps, 8) == 0); 32 /* a gap again where one is deleted, and the next one fills it */ 33 assert(label_delete(&a, esp) == 0); 34 n = label_gaps(&a, gaps, 8); 35 assert(n == 1 && gaps[0].start == a.first + (LABEL_ALIGN - a.first % LABEL_ALIGN) % LABEL_ALIGN); 36 assert(label_add(&a, 0, 8 * 2048, "8200", "SWAP") == 0); 37 assert(label_set_type(&a, root, "8302") == 0); 38 assert(label_write(&dev, &a) == 0); 39 blockdev_close(&dev); 40 41 assert(blockdev_open(&dev, path, 0) == 0); 42 assert(label_read(&dev, &b) == 0); 43 assert(b.kind == kind); 44 assert(b.part[0].used && b.part[1].used && !b.part[2].used); 45 assert(b.part[0].start == a.part[0].start && b.part[0].end == a.part[0].end); 46 assert( 47 !strcmp(b.part[0].type, "8200") 48 && !strcmp(b.part[1].type, kind == LABEL_GPT ? "8302" : "8300") 49 ); 50 if (kind == LABEL_GPT) 51 assert(!strcmp(b.part[0].name, "SWAP") && !strcmp(b.part[1].name, "ROOT")); 52 blockdev_close(&dev); 53 } 54 55 int 56 main(void) 57 { 58 char path[] = "/tmp/label_test.XXXXXX"; 59 int fd = mkstemp(path); 60 61 assert(fd >= 0 && ftruncate(fd, (off_t)IMAGE_MIB << 20) == 0); 62 close(fd); 63 roundtrip(path, LABEL_GPT); 64 roundtrip(path, LABEL_MBR); 65 /* going back to GPT is read as GPT, the MBR one being gone with its table */ 66 roundtrip(path, LABEL_GPT); 67 unlink(path); 68 puts("label_test: ok"); 69 return 0; 70 }