aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

plan_test.c (3416B)


      1 /* see LICENSE file for copyright and license details */
      2 /* a plan made on an image, checked, written, saved and read again:
      3  *   cc -D_GNU_SOURCE -Ishared shared/libdisk/plan_test.c shared/libdisk/plan.c
      4  * shared/libdisk/label.c \
      5  *      shared/libdisk/gpt.c shared/libutil/diskutil.c shared/libutil/hexval.c
      6  * shared/libutil/eprintf.c \ shared/libutil/ealloc.c shared/libutil/strlcpy.c -o plan_test &&
      7  * ./plan_test */
      8 #include "plan.h"
      9 #include <assert.h>
     10 #include <fcntl.h>
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 #include <string.h>
     14 #include <unistd.h>
     15 static int nlines;
     16 static void
     17 line(const char *l, void *a)
     18 {
     19   (void)a;
     20   nlines++;
     21   puts(l);
     22 }
     23 int
     24 main(void)
     25 {
     26   char path[] = "/tmp/plan_test.XXXXXX";
     27   int  fd     = mkstemp(path);
     28   assert(fd >= 0 && ftruncate(fd, (off_t)1 << 40) == 0);
     29   close(fd);
     30   struct plan before, p;
     31   char        why[200];
     32   char       *argv[16];
     33   char        store[256];
     34   assert(plan_read_disk(&before, path) == 0 && before.label.kind == LABEL_NONE);
     35   p = before;
     36   {
     37     struct BlockDev d;
     38     assert(blockdev_open(&d, path, 0) == 0);
     39     assert(label_init(&p.label, &d, LABEL_GPT) == 0);
     40     blockdev_close(&d);
     41   }
     42   assert(plan_check(&p, PLAN_NEED_ROOT, why, sizeof why) < 0);
     43   assert(plan_add(&p, 0, 2048, "ef02", "BIOS", "", "", "") == 0);
     44   assert(plan_add(&p, 0, 64 * 2048, "ef00", "ESP", "vfat", "/boot/efi", "ESP") == 1);
     45   assert(plan_add(&p, 0, 0, "8300", "ROOT", "ext4", "/", "") == 2);
     46   assert(plan_check(&p, PLAN_NEED_ROOT | PLAN_NEED_BOOT, why, sizeof why) == 0);
     47   {
     48     uint64_t end        = p.label.part[2].end;
     49     p.label.part[2].end = p.label.part[2].start + 2047;
     50     assert(plan_check(&p, 0, why, sizeof why) < 0);
     51     p.label.part[2].end = end;
     52   }
     53   assert(plan_changes(&before, &p, line, NULL) >= 3);
     54   char target[PLAN_PATH + 8];
     55   plan_part_path(p.disk, 2, target, sizeof target);
     56   assert(
     57       plan_mkfs_argv(&p, 2, argv, 16, store, sizeof store) == 0 && !strcmp(argv[0], "mkfs.ext4")
     58       && !strcmp(argv[1], target) && !argv[2]
     59   );
     60   assert(plan_write_table(&p) == 0);
     61   FILE *f = tmpfile();
     62   assert(plan_save(&p, f) == 0);
     63   rewind(f);
     64   struct plan q;
     65   assert(plan_load(&q, f, why, sizeof why) == 0);
     66   for (int i = 0; i < 3; i++) {
     67     assert(
     68         q.label.part[i].start == p.label.part[i].start && q.label.part[i].end == p.label.part[i].end
     69     );
     70     assert(!strcmp(q.attr[i].fs, p.attr[i].fs) && !strcmp(q.attr[i].mount, p.attr[i].mount));
     71   }
     72   struct plan r;
     73   assert(plan_read_disk(&r, path) == 0 && r.label.kind == LABEL_GPT && r.label.part[2].used);
     74   p.attr[1].mount[0] = 'x';
     75   assert(plan_check(&p, 0, why, sizeof why) < 0);
     76   puts(why);
     77   plan_set(&p, 1, "vfat", "/", NULL, 1);
     78   assert(plan_check(&p, 0, why, sizeof why) < 0);
     79   puts(why);
     80   {
     81     struct plan g;
     82     char        dev[64];
     83     int         order[8];
     84     assert(plan_guided(&g, path, 128, "ext4") == 0);
     85     assert(plan_role(&g, "esp", dev, sizeof dev) == 1 && g.label.part[1].used);
     86     assert(plan_role(&g, "bios", dev, sizeof dev) == 1 && !strcmp(dev, "1"));
     87     assert(plan_role(&g, "rootfs", dev, sizeof dev) == 1 && !strcmp(dev, "ext4"));
     88     assert(
     89         plan_mount_order(&g, order, 8) == 2 && !strcmp(g.attr[order[0]].mount, "/")
     90         && !strcmp(g.attr[order[1]].mount, "/boot/efi")
     91     );
     92     assert(plan_check(&g, PLAN_NEED_ROOT | PLAN_NEED_BOOT, why, sizeof why) == 0);
     93   }
     94   unlink(path);
     95   puts("plan_test: ok");
     96   return 0;
     97 }