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 (
|
2018-09-19 06:13:28 +00:00
|
|
|
"net"
|
2018-09-19 07:00:12 +00:00
|
|
|
"strings"
|
2018-09-18 21:45:21 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQemuSystemNew_InvalidKernelPath(t *testing.T) {
|
|
|
|
kernel := Kernel{Name: "Invalid", Path: "/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) {
|
|
|
|
// FIXME put kernel image to path not just "any valid path"
|
|
|
|
kernel := Kernel{Name: "Valid path", Path: "/bin/sh"}
|
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) {
|
|
|
|
// FIXME put kernel image to path not just "any valid path"
|
|
|
|
kernel := Kernel{Name: "Valid path", Path: "/bin/sh"}
|
|
|
|
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) {
|
|
|
|
// FIXME put kernel image to path not just "any valid path"
|
|
|
|
kernel := Kernel{Name: "Valid path", Path: "/bin/sh"}
|
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) {
|
|
|
|
// TODO check kernel path on other distros than gentoo
|
|
|
|
kernel := Kernel{Name: "Host kernel", Path: "/boot/vmlinuz-4.18.8"}
|
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
|
|
|
|
|
|
|
func TestGetFreeAddrPort(t *testing.T) {
|
|
|
|
addrPort := getFreeAddrPort()
|
|
|
|
ln, err := net.Listen("tcp", addrPort)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ln.Close()
|
|
|
|
}
|
2018-09-19 07:00:12 +00:00
|
|
|
|
2018-09-20 23:45:46 +00:00
|
|
|
func startTestQemu() (q *QemuSystem, err error) {
|
2018-09-19 07:00:12 +00:00
|
|
|
// FIXME hardcoded kernel path
|
|
|
|
kernel := Kernel{Name: "Host kernel", Path: "/boot/vmlinuz-4.18.8"}
|
|
|
|
// FIXME hardcoded qcow2 path
|
2018-09-20 23:45:46 +00:00
|
|
|
q, err = NewQemuSystem(X86_64, kernel, "/home/user/qemu/sid.img")
|
2018-09-19 07:00:12 +00:00
|
|
|
if err != nil {
|
2018-09-20 23:45:46 +00:00
|
|
|
return
|
2018-09-19 07:00:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:45:46 +00:00
|
|
|
if err = q.Start(); err != nil {
|
|
|
|
return
|
2018-09-19 07:00:12 +00:00
|
|
|
}
|
2018-09-20 23:45:46 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQemuSystemCommand(t *testing.T) {
|
|
|
|
qemu, err := startTestQemu()
|
2018-09-19 07:00:12 +00:00
|
|
|
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?!")
|
|
|
|
}
|
|
|
|
}
|