#!/usr/bin/perl -T

BEGIN {
    require "./debian/tests/utils/mock.pm";
    Cryptsetup::Mock::->import();
}

my $POWERCYCLE_COUNT = $ARGV[0];

unlock_disk("topsecret");

if ($POWERCYCLE_COUNT == 0) {
    login_nopassword("root");

    # make sure the root FS and swap are help by dm-crypt devices
    assert_command(q{cryptsetup luksOpen --test-passphrase /dev/vda3 <<<topsecret});
    my $out = shell_command(q{lsblk -in -oNAME,TYPE,MOUNTPOINT /dev/vda3});
    die unless $out =~ m#^`-vda3_crypt\s+crypt\s*$#m;
    die unless $out =~ m#^\s{2}[`|]-cryptvg-root\s+lvm\s+/\s*$#m;
    die unless $out =~ m#^\s{2}[`|]-cryptvg-swap\s+lvm\s+\[SWAP\]\s*$#m;

    # create a stamp in memory, hibernate (suspend on disk) and thaw
    shell_command(q{echo hello >/dev/shm/foo.stamp});
    hibernate();
}
else {
    # no need to relogin, we get the shell as we left it
    assert_command(q{grep -Fx hello </dev/shm/foo.stamp});
    poweroff();

    # XXX would be nice to test cryptsetup-suspend here, but it's not
    # trivial to do since it doesn't use the serial console but VT8 for
    # the prompt, which makes sense for a graphical environment but
    # that's not a channel we can easily mock
}
