Factor out test helpers
This commit is contained in:
parent
1b74a18814
commit
1f171beae0
49
_helper/helper.go
Normal file
49
_helper/helper.go
Normal file
@ -0,0 +1,49 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/derekparker/dbg/proctl"
|
||||
)
|
||||
|
||||
type testfunc func(p *proctl.DebuggedProcess)
|
||||
|
||||
func GetRegisters(p *proctl.DebuggedProcess, t *testing.T) *syscall.PtraceRegs {
|
||||
regs, err := p.Registers()
|
||||
if err != nil {
|
||||
t.Fatal("Registers():", err)
|
||||
}
|
||||
|
||||
return regs
|
||||
}
|
||||
|
||||
func WithTestProcess(name string, t *testing.T, fn testfunc) {
|
||||
runtime.LockOSThread()
|
||||
cmd, err := startTestProcess(name)
|
||||
if err != nil {
|
||||
t.Fatal("Starting test process:", err)
|
||||
}
|
||||
|
||||
pid := cmd.Process.Pid
|
||||
p, err := proctl.NewDebugProcess(pid)
|
||||
if err != nil {
|
||||
t.Fatal("NewDebugProcess():", err)
|
||||
}
|
||||
defer cmd.Process.Kill()
|
||||
|
||||
fn(p)
|
||||
}
|
||||
|
||||
func startTestProcess(name string) (*exec.Cmd, error) {
|
||||
cmd := exec.Command("../_fixtures/" + name)
|
||||
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cmd, nil
|
||||
}
|
@ -1,14 +1,13 @@
|
||||
package proctl
|
||||
package proctl_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testfunc func(p *DebuggedProcess)
|
||||
"github.com/derekparker/dbg/_helper"
|
||||
"github.com/derekparker/dbg/proctl"
|
||||
)
|
||||
|
||||
func dataAtAddr(pid int, addr uint64) ([]byte, error) {
|
||||
data := make([]byte, 1)
|
||||
@ -20,45 +19,8 @@ func dataAtAddr(pid int, addr uint64) ([]byte, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func getRegisters(p *DebuggedProcess, t *testing.T) *syscall.PtraceRegs {
|
||||
regs, err := p.Registers()
|
||||
if err != nil {
|
||||
t.Fatal("Registers():", err)
|
||||
}
|
||||
|
||||
return regs
|
||||
}
|
||||
|
||||
func withTestProcess(name string, t *testing.T, fn testfunc) {
|
||||
runtime.LockOSThread()
|
||||
cmd, err := startTestProcess(name)
|
||||
if err != nil {
|
||||
t.Fatal("Starting test process:", err)
|
||||
}
|
||||
|
||||
pid := cmd.Process.Pid
|
||||
p, err := NewDebugProcess(pid)
|
||||
if err != nil {
|
||||
t.Fatal("NewDebugProcess():", err)
|
||||
}
|
||||
defer cmd.Process.Kill()
|
||||
|
||||
fn(p)
|
||||
}
|
||||
|
||||
func startTestProcess(name string) (*exec.Cmd, error) {
|
||||
cmd := exec.Command("../_fixtures/" + name)
|
||||
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func TestAttachProcess(t *testing.T) {
|
||||
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
||||
helper.WithTestProcess("testprog", t, func(p *proctl.DebuggedProcess) {
|
||||
if !p.ProcessState.Sys().(syscall.WaitStatus).Stopped() {
|
||||
t.Errorf("Process was not stopped correctly")
|
||||
}
|
||||
@ -66,12 +28,12 @@ func TestAttachProcess(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStep(t *testing.T) {
|
||||
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
||||
helper.WithTestProcess("testprog", t, func(p *proctl.DebuggedProcess) {
|
||||
if p.ProcessState.Exited() {
|
||||
t.Fatal("Process already exited")
|
||||
}
|
||||
|
||||
regs := getRegisters(p, t)
|
||||
regs := helper.GetRegisters(p, t)
|
||||
rip := regs.PC()
|
||||
|
||||
err := p.Step()
|
||||
@ -79,7 +41,7 @@ func TestStep(t *testing.T) {
|
||||
t.Fatal("Step():", err)
|
||||
}
|
||||
|
||||
regs = getRegisters(p, t)
|
||||
regs = helper.GetRegisters(p, t)
|
||||
|
||||
if rip >= regs.PC() {
|
||||
t.Errorf("Expected %#v to be greater than %#v", regs.PC(), rip)
|
||||
@ -88,7 +50,7 @@ func TestStep(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContinue(t *testing.T) {
|
||||
withTestProcess("continuetestprog", t, func(p *DebuggedProcess) {
|
||||
helper.WithTestProcess("continuetestprog", t, func(p *proctl.DebuggedProcess) {
|
||||
if p.ProcessState.Exited() {
|
||||
t.Fatal("Process already exited")
|
||||
}
|
||||
@ -105,7 +67,7 @@ func TestContinue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBreakPoint(t *testing.T) {
|
||||
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
||||
helper.WithTestProcess("testprog", t, func(p *proctl.DebuggedProcess) {
|
||||
sleepytimefunc := p.GoSymTable.LookupFunc("main.sleepytime")
|
||||
sleepyaddr := sleepytimefunc.Entry
|
||||
|
||||
@ -120,7 +82,7 @@ func TestBreakPoint(t *testing.T) {
|
||||
t.Fatal("Continue():", err)
|
||||
}
|
||||
|
||||
regs := getRegisters(p, t)
|
||||
regs := helper.GetRegisters(p, t)
|
||||
|
||||
pc := regs.PC()
|
||||
if pc != breakpc {
|
||||
@ -132,7 +94,7 @@ func TestBreakPoint(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
regs = getRegisters(p, t)
|
||||
regs = helper.GetRegisters(p, t)
|
||||
|
||||
pc = regs.PC()
|
||||
if pc == breakpc {
|
||||
@ -142,7 +104,7 @@ func TestBreakPoint(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBreakPointWithNonExistantFunction(t *testing.T) {
|
||||
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
||||
helper.WithTestProcess("testprog", t, func(p *proctl.DebuggedProcess) {
|
||||
_, err := p.Break(uintptr(0))
|
||||
if err == nil {
|
||||
t.Fatal("Should not be able to break at non existant function")
|
||||
@ -151,7 +113,7 @@ func TestBreakPointWithNonExistantFunction(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClearBreakPoint(t *testing.T) {
|
||||
withTestProcess("testprog", t, func(p *DebuggedProcess) {
|
||||
helper.WithTestProcess("testprog", t, func(p *proctl.DebuggedProcess) {
|
||||
fn := p.GoSymTable.LookupFunc("main.sleepytime")
|
||||
bp, err := p.Break(uintptr(fn.Entry))
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user