diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2016-08-16 19:38:26 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2016-08-17 15:59:25 +0200 |
commit | 12f4da11ed1db15f644ab13095483a51d4583022 (patch) | |
tree | fe3a4ef4d77512411ce5cdc16d15942b8009f0fb /src/tests/qemu/init.c | |
parent | e7c1aac62b8ffc3480baeecd036120da4de8f516 (diff) |
qemu: enhancements
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'src/tests/qemu/init.c')
-rw-r--r-- | src/tests/qemu/init.c | 116 |
1 files changed, 94 insertions, 22 deletions
diff --git a/src/tests/qemu/init.c b/src/tests/qemu/init.c index 310f34b..4af220c 100644 --- a/src/tests/qemu/init.c +++ b/src/tests/qemu/init.c @@ -1,8 +1,10 @@ +#define _GNU_SOURCE #include <unistd.h> #include <errno.h> #include <string.h> #include <stdio.h> #include <stdlib.h> +#include <stdbool.h> #include <fcntl.h> #include <sys/wait.h> #include <sys/mount.h> @@ -11,7 +13,9 @@ #include <sys/types.h> #include <sys/io.h> #include <sys/ioctl.h> +#include <sys/utsname.h> #include <linux/random.h> +#include <linux/version.h> __attribute__((noreturn)) static void poweroff(void) { @@ -30,9 +34,16 @@ static void panic(const char *what) #define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m") -int main(int argc, char *argv[]) +static void print_banner(const struct utsname *utsname) +{ + int len = strlen(" WireGuard Test Suite on ") + strlen(utsname->sysname) + strlen(utsname->release); + putchar('\0');putchar('\0');putchar('\0');putchar('\0');putchar('\n'); + printf("\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\x1b[45m\x1b[33m\x1b[1m WireGuard Test Suite on %s %s \x1b[0m\n\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\n", len, "", utsname->sysname, utsname->release, len, ""); +} + +static void seed_rng(void) { - int status, fd1, fd2, i; + int fd1, fd2, i; struct { int entropy_count; int buffer_size; @@ -41,6 +52,23 @@ int main(int argc, char *argv[]) .entropy_count = 128, .buffer_size = 128 }; + pretty_message("[+] Ensuring RNG entropy..."); + fd1 = open("/dev/hwrng", O_RDONLY); + fd2 = open("/dev/urandom", O_WRONLY); + if (fd1 < 0 || fd2 < 0) + panic("open(hwrng,urandom)"); + for (i = 0; i < 4096; ++i) { + if (read(fd1, entropy.buffer, 128) != 128) + panic("read(hwrng)"); + if (ioctl(fd2, RNDADDENTROPY, &entropy) < 0) + panic("ioctl(urandom)"); + } + close(fd1); + close(fd2); +} + +static void mount_filesystems(void) +{ pretty_message("[+] Mounting filesystems..."); mkdir("/dev", 0755); mkdir("/proc", 0755); @@ -62,27 +90,46 @@ int main(int argc, char *argv[]) panic("run symlink"); if (symlink("/proc/self/fd", "/dev/fd")) panic("fd symlink"); +} + +static void enable_logging(void) +{ + int fd; pretty_message("[+] Enabling logging..."); - fd1 = open("/proc/sys/kernel/printk", O_WRONLY); - if (fd1 < 0) + fd = open("/proc/sys/kernel/printk", O_WRONLY); + if (fd < 0) panic("open(printk)"); - if (write(fd1, "9\n", 2) != 2) + if (write(fd, "9\n", 2) != 2) panic("write(printk)"); - close(fd1); - pretty_message("[+] Ensuring RNG entropy..."); - fd1 = open("/dev/hwrng", O_RDONLY); - fd2 = open("/dev/urandom", O_WRONLY); - if (fd1 < 0 || fd2 < 0) - panic("open(hwrng,urandom)"); - for (i = 0; i < 4096; ++i) { - if (read(fd1, entropy.buffer, 128) != 128) - panic("read(hwrng)"); - if (ioctl(fd2, RNDADDENTROPY, &entropy) < 0) - panic("ioctl(urandom)"); + close(fd); +} + +static void kmod_selftests(void) +{ + FILE *file; + char line[2048], *start; + pretty_message("[+] Module self-tests:"); + file = fopen("/proc/kmsg", "r"); + if (!file) + panic("fopen(kmsg)"); + if (fcntl(fileno(file), F_SETFL, O_NONBLOCK) < 0) + panic("fcntl(kmsg, nonblock)"); + while (fgets(line, sizeof(line), file)) { + start = strstr(line, "wireguard: "); + if (!start) + continue; + start += 11; + *strchrnul(start, '\n') = '\0'; + if (strstr(start, "WireGuard loaded.")) + break; + printf(" \x1b[32m* %s\x1b[0m\n", start); } - close(fd1); - close(fd2); + fclose(file); +} +static void launch_tests(void) +{ + int status, fd; pretty_message("[+] Launching tests..."); switch (fork()) { case -1: @@ -97,14 +144,39 @@ int main(int argc, char *argv[]) panic("wait"); if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { pretty_message("[+] Tests successful! :-)"); - fd1 = open("/dev/vport1p1", O_WRONLY); - if (fd1 < 0) + fd = open("/dev/vport1p1", O_WRONLY); + if (fd < 0) panic("open(vport1p1)"); - if (write(fd1, "success\n", 8) != 8) + if (write(fd, "success\n", 8) != 8) panic("write(success)"); - close(fd1); + close(fd); } else puts("\x1b[31m\x1b[1m[-] Tests failed! :-(\x1b[0m"); +} + +static bool linux_4_8_or_higher(const struct utsname *utsname) +{ + unsigned int maj, min, rel; + if (strcmp(utsname->sysname, "Linux")) + return false; + if (sscanf(utsname->release, "%u.%u.%u", &maj, &min, &rel) != 3) + return false; + return KERNEL_VERSION(maj, min, rel) >= KERNEL_VERSION(4, 8, 0); +} + +int main(int argc, char *argv[]) +{ + struct utsname utsname; + if (uname(&utsname) < 0) + panic("uname"); + + print_banner(&utsname); + mount_filesystems(); + kmod_selftests(); + if (!linux_4_8_or_higher(&utsname)) + seed_rng(); + enable_logging(); + launch_tests(); poweroff(); return 1; } |