1
0
out-of-tree/qemu/qemu-kernel_test.go

302 lines
6.1 KiB
Go
Raw Normal View History

2018-09-18 21:45:21 +00:00
// Copyright 2018 Mikhail Klementev. All rights reserved.
// Use of this source code is governed by a GPLv3 license
// (or later) that can be found in the LICENSE file.
package qemukernel
import (
"crypto/sha512"
"fmt"
"io/ioutil"
"math/rand"
2018-09-19 06:13:28 +00:00
"net"
"os"
"strings"
2018-09-18 21:45:21 +00:00
"testing"
2018-09-22 07:28:08 +00:00
"time"
2018-09-18 21:45:21 +00:00
)
func init() {
rand.Seed(time.Now().UnixNano())
}
2018-09-18 21:45:21 +00:00
func TestQemuSystemNew_InvalidKernelPath(t *testing.T) {
2018-09-22 10:34:43 +00:00
kernel := Kernel{Name: "Invalid", KernelPath: "/invalid/path"}
2018-09-19 06:13:28 +00:00
if _, err := NewQemuSystem(X86_64, kernel, "/bin/sh"); err == nil {
2018-09-18 21:45:21 +00:00
t.Fatal(err)
}
}
func TestQemuSystemNew_InvalidQemuArch(t *testing.T) {
2018-09-22 14:00:49 +00:00
kernel := Kernel{Name: "Valid path", KernelPath: testConfigVmlinuz}
2018-09-19 06:13:28 +00:00
if _, err := NewQemuSystem(unsupported, kernel, "/bin/sh"); err == nil {
t.Fatal(err)
}
}
func TestQemuSystemNew_InvalidQemuDrivePath(t *testing.T) {
2018-09-22 14:00:49 +00:00
kernel := Kernel{Name: "Valid path", KernelPath: testConfigVmlinuz}
2018-09-19 06:13:28 +00:00
if _, err := NewQemuSystem(X86_64, kernel, "/invalid/path"); err == nil {
2018-09-18 21:45:21 +00:00
t.Fatal(err)
}
}
func TestQemuSystemNew(t *testing.T) {
2018-09-22 14:00:49 +00:00
kernel := Kernel{Name: "Valid path", KernelPath: testConfigVmlinuz}
2018-09-19 06:13:28 +00:00
if _, err := NewQemuSystem(X86_64, kernel, "/bin/sh"); err != nil {
2018-09-18 21:45:21 +00:00
t.Fatal(err)
}
}
func TestQemuSystemStart(t *testing.T) {
2018-09-22 10:34:43 +00:00
kernel := Kernel{Name: "Test kernel", KernelPath: testConfigVmlinuz}
2018-09-19 06:13:28 +00:00
qemu, err := NewQemuSystem(X86_64, kernel, "/bin/sh")
2018-09-18 21:45:21 +00:00
if err != nil {
t.Fatal(err)
}
if err = qemu.Start(); err != nil {
t.Fatal(err)
}
qemu.Stop()
}
2018-09-19 06:13:28 +00:00
2018-09-22 12:59:01 +00:00
func TestGetFreeAddrPort(t *testing.T) {
addrPort := getFreeAddrPort()
ln, err := net.Listen("tcp", addrPort)
if err != nil {
t.Fatal(err)
}
ln.Close()
}
2018-09-22 07:28:08 +00:00
func TestQemuSystemStart_Timeout(t *testing.T) {
2018-09-22 12:59:01 +00:00
t.Parallel()
2018-09-22 10:34:43 +00:00
kernel := Kernel{Name: "Test kernel", KernelPath: testConfigVmlinuz}
2018-09-22 07:28:08 +00:00
qemu, err := NewQemuSystem(X86_64, kernel, "/bin/sh")
if err != nil {
t.Fatal(err)
}
qemu.Timeout = time.Second
if err = qemu.Start(); err != nil {
t.Fatal(err)
}
time.Sleep(2 * time.Second)
if !qemu.Died {
t.Fatal("qemu does not died :c")
}
if !qemu.KilledByTimeout {
t.Fatal("qemu died not because of timeout O_o")
}
}
2018-10-06 14:00:31 +00:00
func startTestQemu(t *testing.T, timeout time.Duration) (q *QemuSystem, err error) {
2018-09-22 12:59:01 +00:00
t.Parallel()
2018-09-22 10:34:43 +00:00
kernel := Kernel{
Name: "Test kernel",
KernelPath: testConfigVmlinuz,
InitrdPath: testConfigInitrd,
}
q, err = NewQemuSystem(X86_64, kernel, testConfigRootfs)
if err != nil {
return
}
2018-10-06 14:00:31 +00:00
if timeout != 0 {
q.Timeout = timeout
}
if err = q.Start(); err != nil {
return
}
return
}
func TestQemuSystemCommand(t *testing.T) {
2018-10-06 14:00:31 +00:00
qemu, err := startTestQemu(t, 0)
2018-09-22 10:34:43 +00:00
if err != nil {
t.Fatal(err)
}
defer qemu.Stop()
output, err := qemu.Command("root", "cat /etc/shadow")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "root::") {
t.Fatal("Wrong output from `cat /etc/shadow` by root")
}
output, err = qemu.Command("user", "cat /etc/passwd")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "root:x:0:0:root:/root:/bin/bash") {
t.Fatal("Wrong output from `cat /etc/passwd` by user")
}
output, err = qemu.Command("user", "cat /etc/shadow")
if err == nil { // unsucessful is good because user must not read /etc/shadow
t.Fatal("User have rights for /etc/shadow. WAT?!")
}
}
func TestQemuSystemCopyFile(t *testing.T) {
2018-10-06 14:00:31 +00:00
qemu, err := startTestQemu(t, 0)
if err != nil {
t.Fatal(err)
}
defer qemu.Stop()
localPath := "/bin/sh"
content, err := ioutil.ReadFile(localPath)
if err != nil {
return
}
sha_local := fmt.Sprintf("%x", sha512.Sum512(content))
err = qemu.CopyFile("user", localPath, "/tmp/test")
if err != nil {
t.Fatal(err)
}
sha_remote, err := qemu.Command("user", "sha512sum /tmp/test")
if err != nil {
t.Fatal(err)
}
sha_remote = strings.Split(sha_remote, " ")[0]
if sha_local != sha_remote {
t.Fatal(fmt.Sprintf("Broken file (%s instead of %s)", sha_remote, sha_local))
}
}
func TestQemuSystemCopyAndRun(t *testing.T) {
2018-10-06 14:00:31 +00:00
qemu, err := startTestQemu(t, 0)
if err != nil {
t.Fatal(err)
}
defer qemu.Stop()
randStr := fmt.Sprintf("%d", rand.Int())
content := []byte("#!/bin/sh\n echo -n " + randStr + "\n")
tmpfile, err := ioutil.TempFile("", "executable")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
output, err := qemu.CopyAndRun("user", tmpfile.Name())
if err != nil {
t.Fatal(err)
}
if output != randStr {
t.Fatal("Wrong output from copyied executable (" + output + "," + randStr + ")")
}
}
func TestQemuSystemCopyAndInsmod(t *testing.T) {
2018-10-06 14:00:31 +00:00
qemu, err := startTestQemu(t, 0)
if err != nil {
t.Fatal(err)
}
defer qemu.Stop()
lsmodBefore, err := qemu.Command("root", "lsmod | wc -l")
if err != nil {
t.Fatal(err)
}
_, err = qemu.CopyAndInsmod(testConfigSampleKo)
if err != nil {
t.Fatal(err)
}
lsmodAfter, err := qemu.Command("root", "lsmod | wc -l")
if err != nil {
t.Fatal(err)
}
if lsmodBefore == lsmodAfter {
t.Fatal("insmod returns ok but there is no new kernel modules")
}
}
2018-10-06 14:00:31 +00:00
2018-10-06 14:00:48 +00:00
func TestQemuSystemKernelPanic(t *testing.T) {
qemu, err := startTestQemu(t, time.Minute)
if err != nil {
t.Fatal(err)
}
defer qemu.Stop()
// Enable sysrq
_, err = qemu.Command("root", "echo 1 > /proc/sys/kernel/sysrq")
if err != nil {
t.Fatal(err)
}
// Trigger kernel panic
err = qemu.AsyncCommand("root", "sleep 1s && echo c > /proc/sysrq-trigger")
if err != nil {
t.Fatal(err)
}
// Wait for panic watcher timeout
time.Sleep(5 * time.Second)
if qemu.KilledByTimeout {
t.Fatal("qemu is killed by timeout, not because of panic")
}
if !qemu.Died {
t.Fatal("qemu is not killed after kernel panic")
}
if !qemu.KernelPanic {
t.Fatal("qemu is died but there's no information about panic")
}
}
2018-10-06 14:00:31 +00:00
func TestQemuSystemRun(t *testing.T) {
qemu, err := startTestQemu(t, 0)
if err != nil {
t.Fatal(err)
}
defer qemu.Stop()
for {
_, err := qemu.Command("root", "echo")
if err == nil {
break
}
}
start := time.Now()
err = qemu.AsyncCommand("root", "sleep 10s")
if err != nil {
t.Fatal(err)
}
if time.Since(start) > time.Second {
t.Fatalf("qemu.Run does not async (waited %s)", +time.Since(start))
}
}