@@ -1,7 +1,9 @@
1−{ debug ? false, ... } @ args:
1+{ system ? builtins.currentSystem, debug ? false }:
22
3−import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
3+with import ../lib/testing.nix { inherit system; };
4+with pkgs.lib;
45
6+let
57 testVMConfig = vmName: attrs: { config, pkgs, ... }: let
68 guestAdditions = pkgs.linuxPackages.virtualboxGuestAdditions;
79
@@ -314,138 +316,140 @@ import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
314316 test2.vmScript = dhcpScript;
315317 };
316318
317−in {
318− name = "virtualbox";
319− meta = with pkgs.stdenv.lib.maintainers; {
320− maintainers = [ aszlig wkennington ];
321− };
319+ mkVBoxTest = name: testScript: makeTest {
320+ name = "virtualbox-${name}";
321+
322+ machine = { lib, config, ... }: {
323+ imports = let
324+ mkVMConf = name: val: val.machine // { key = "${name}-config"; };
325+ vmConfigs = mapAttrsToList mkVMConf vboxVMs;
326+ in [ ./common/user-account.nix ./common/x11.nix ] ++ vmConfigs;
327+ virtualisation.memorySize = 2048;
328+ virtualisation.virtualbox.host.enable = true;
329+ users.extraUsers.alice.extraGroups = let
330+ inherit (config.virtualisation.virtualbox.host) enableHardening;
331+ in lib.mkIf enableHardening (lib.singleton "vboxusers");
332+ };
322333
323− machine = { pkgs, lib, config, ... }: {
324− imports = let
325− mkVMConf = name: val: val.machine // { key = "${name}-config"; };
326− vmConfigs = mapAttrsToList mkVMConf vboxVMs;
327− in [ ./common/user-account.nix ./common/x11.nix ] ++ vmConfigs;
328− virtualisation.memorySize = 2048;
329− virtualisation.virtualbox.host.enable = true;
330− users.extraUsers.alice.extraGroups = let
331− inherit (config.virtualisation.virtualbox.host) enableHardening;
332− in lib.mkIf enableHardening (lib.singleton "vboxusers");
333− };
334+ testScript = ''
335+ sub ru ($) {
336+ my $esc = $_[0] =~ s/'/'\\${"'"}'/gr;
337+ return "su - alice -c '$esc'";
338+ }
334339
335− testScript = ''
336− sub ru ($) {
337− my $esc = $_[0] =~ s/'/'\\${"'"}'/gr;
338− return "su - alice -c '$esc'";
339− }
340+ sub vbm {
341+ $machine->succeed(ru("VBoxManage ".$_[0]));
342+ };
340343
341− sub vbm {
342− $machine->succeed(ru("VBoxManage ".$_[0]));
343− };
344+ sub removeUUIDs {
345+ return join("\n", grep { $_ !~ /^UUID:/ } split(/\n/, $_[0]))."\n";
346+ }
344347
345− ${concatStrings (mapAttrsToList (_: getAttr "testSubs") vboxVMs)}
348+ ${concatStrings (mapAttrsToList (_: getAttr "testSubs") vboxVMs)}
346349
347− $machine->waitForX;
350+ $machine->waitForX;
348351
349− ${mkLog "$HOME/.config/VirtualBox/VBoxSVC.log" "HOST-SVC"}
352+ ${mkLog "$HOME/.config/VirtualBox/VBoxSVC.log" "HOST-SVC"}
350353
351− createVM_simple;
354+ ${testScript}
355+ '';
352356
353− subtest "simple-gui", sub {
354− $machine->succeed(ru "VirtualBox &");
355− $machine->waitForWindow(qr/Oracle VM VirtualBox Manager/);
356− $machine->sleep(5);
357− $machine->screenshot("gui_manager_started");
358− $machine->sendKeys("ret");
359− $machine->screenshot("gui_manager_sent_startup");
360− waitForStartup_simple (sub {
361− $machine->sendKeys("ret");
362− });
363− $machine->screenshot("gui_started");
364− waitForVMBoot_simple;
365− $machine->screenshot("gui_booted");
366− shutdownVM_simple;
367− $machine->sleep(5);
368− $machine->screenshot("gui_stopped");
369− $machine->sendKeys("ctrl-q");
370− $machine->sleep(5);
371− $machine->screenshot("gui_manager_stopped");
357+ meta = with pkgs.stdenv.lib.maintainers; {
358+ maintainers = [ aszlig wkennington ];
372359 };
360+ };
373361
374− cleanup_simple;
362+in mapAttrs mkVBoxTest {
363+ simple-gui = ''
364+ createVM_simple;
365+ $machine->succeed(ru "VirtualBox &");
366+ $machine->waitForWindow(qr/Oracle VM VirtualBox Manager/);
367+ $machine->sleep(5);
368+ $machine->screenshot("gui_manager_started");
369+ $machine->sendKeys("ret");
370+ $machine->screenshot("gui_manager_sent_startup");
371+ waitForStartup_simple (sub {
372+ $machine->sendKeys("ret");
373+ });
374+ $machine->screenshot("gui_started");
375+ waitForVMBoot_simple;
376+ $machine->screenshot("gui_booted");
377+ shutdownVM_simple;
378+ $machine->sleep(5);
379+ $machine->screenshot("gui_stopped");
380+ $machine->sendKeys("ctrl-q");
381+ $machine->sleep(5);
382+ $machine->screenshot("gui_manager_stopped");
383+ '';
375384
376− subtest "simple-cli", sub {
377− vbm("startvm simple");
378− waitForStartup_simple;
379− $machine->screenshot("cli_started");
380− waitForVMBoot_simple;
381− $machine->screenshot("cli_booted");
382− shutdownVM_simple;
383− };
385+ simple-cli = ''
386+ createVM_simple;
387+ vbm("startvm simple");
388+ waitForStartup_simple;
389+ $machine->screenshot("cli_started");
390+ waitForVMBoot_simple;
391+ $machine->screenshot("cli_booted");
384392
385− subtest "privilege-escalation", sub {
393+ $machine->nest("Checking for privilege escalation", sub {
386394 $machine->fail("test -e '/root/VirtualBox VMs'");
387395 $machine->fail("test -e '/root/.config/VirtualBox'");
388396 $machine->succeed("test -e '/home/alice/VirtualBox VMs'");
389− };
390−
391− destroyVM_simple;
397+ });
392398
393− sub removeUUIDs {
394− return join("\n", grep { $_ !~ /^UUID:/ } split(/\n/, $_[0]))."\n";
395− }
399+ shutdownVM_simple;
400+ '';
396401
397− subtest "host-usb-permissions", sub {
398− my $userUSB = removeUUIDs vbm("list usbhost");
399− print STDERR $userUSB;
400− my $rootUSB = removeUUIDs $machine->succeed("VBoxManage list usbhost");
401− print STDERR $rootUSB;
402+ host-usb-permissions = ''
403+ my $userUSB = removeUUIDs vbm("list usbhost");
404+ print STDERR $userUSB;
405+ my $rootUSB = removeUUIDs $machine->succeed("VBoxManage list usbhost");
406+ print STDERR $rootUSB;
402407
403− die "USB host devices differ for root and normal user"
404− if $userUSB ne $rootUSB;
405− die "No USB host devices found" if $userUSB =~ /<none>/;
406− };
408+ die "USB host devices differ for root and normal user"
409+ if $userUSB ne $rootUSB;
410+ die "No USB host devices found" if $userUSB =~ /<none>/;
411+ '';
407412
408− subtest "systemd-detect-virt", sub {
409− createVM_detectvirt;
410− vbm("startvm detectvirt");
411− waitForStartup_detectvirt;
412− waitForVMBoot_detectvirt;
413− shutdownVM_detectvirt;
414− my $result = $machine->succeed("cat '$detectvirt_sharepath/result'");
415− chomp $result;
416− destroyVM_detectvirt;
417− die "systemd-detect-virt returned \"$result\" instead of \"oracle\""
418− if $result ne "oracle";
419− };
413+ systemd-detect-virt = ''
414+ createVM_detectvirt;
415+ vbm("startvm detectvirt");
416+ waitForStartup_detectvirt;
417+ waitForVMBoot_detectvirt;
418+ shutdownVM_detectvirt;
419+ my $result = $machine->succeed("cat '$detectvirt_sharepath/result'");
420+ chomp $result;
421+ destroyVM_detectvirt;
422+ die "systemd-detect-virt returned \"$result\" instead of \"oracle\""
423+ if $result ne "oracle";
424+ '';
420425
421− subtest "net-hostonlyif", sub {
422− createVM_test1;
423− createVM_test2;
426+ net-hostonlyif = ''
427+ createVM_test1;
428+ createVM_test2;
424429
425− vbm("startvm test1");
426− waitForStartup_test1;
427− waitForVMBoot_test1;
430+ vbm("startvm test1");
431+ waitForStartup_test1;
432+ waitForVMBoot_test1;
428433
429− vbm("startvm test2");
430− waitForStartup_test2;
431− waitForVMBoot_test2;
434+ vbm("startvm test2");
435+ waitForStartup_test2;
436+ waitForVMBoot_test2;
432437
433− $machine->screenshot("net_booted");
438+ $machine->screenshot("net_booted");
434439
435− my $test1IP = waitForIP_test1 1;
436− my $test2IP = waitForIP_test2 1;
440+ my $test1IP = waitForIP_test1 1;
441+ my $test2IP = waitForIP_test2 1;
437442
438− $machine->succeed("echo '$test2IP' | netcat -c '$test1IP' 1234");
439− $machine->succeed("echo '$test1IP' | netcat -c '$test2IP' 1234");
443+ $machine->succeed("echo '$test2IP' | netcat -c '$test1IP' 1234");
444+ $machine->succeed("echo '$test1IP' | netcat -c '$test2IP' 1234");
440445
441− $machine->waitUntilSucceeds("netcat -c '$test1IP' 5678 >&2");
442− $machine->waitUntilSucceeds("netcat -c '$test2IP' 5678 >&2");
446+ $machine->waitUntilSucceeds("netcat -c '$test1IP' 5678 >&2");
447+ $machine->waitUntilSucceeds("netcat -c '$test2IP' 5678 >&2");
443448
444− shutdownVM_test1;
445− shutdownVM_test2;
449+ shutdownVM_test1;
450+ shutdownVM_test2;
446451
447− destroyVM_test1;
448− destroyVM_test2;
449− };
452+ destroyVM_test1;
453+ destroyVM_test2;
450454 '';
451−}) args
455+}