From 0ac24abd2dba1944f06b11e0477aded5513a59b2 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Thu, 26 Mar 2015 13:15:35 -0500 Subject: [PATCH] Validate args to thread command --- command/command.go | 3 +++ command/command_test.go | 7 +++++++ proctl/proctl_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/command/command.go b/command/command.go index 684ffcad..bd97e8c6 100644 --- a/command/command.go +++ b/command/command.go @@ -139,6 +139,9 @@ func threads(p *proctl.DebuggedProcess, ars ...string) error { } func thread(p *proctl.DebuggedProcess, ars ...string) error { + if len(ars) == 0 { + return fmt.Errorf("you must specify a thread") + } oldTid := p.CurrentThread.Id tid, err := strconv.Atoi(ars[0]) if err != nil { diff --git a/command/command_test.go b/command/command_test.go index 722ce991..cbbb66b3 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -51,3 +51,10 @@ func TestCommandReplayWithoutPreviousCommand(t *testing.T) { t.Error("Null command not returned", err) } } + +func TestSwitchThread(t *testing.T) { + err := thread(nil, []string{}...) + if err == nil { + t.Fatal("expected error for empty arg slice") + } +} diff --git a/proctl/proctl_test.go b/proctl/proctl_test.go index ef88323a..159a8e31 100644 --- a/proctl/proctl_test.go +++ b/proctl/proctl_test.go @@ -312,3 +312,46 @@ func TestFindReturnAddress(t *testing.T) { } }) } + +func TestSwitchThread(t *testing.T) { + var testfile, _ = filepath.Abs("../_fixtures/testnextprog") + + withTestProcess(testfile, t, func(p *DebuggedProcess) { + // With invalid thread id + err := p.SwitchThread(-1) + if err == nil { + t.Fatal("Expected error for invalid thread id") + } + pc, err := p.FindLocation("main.main") + if err != nil { + t.Fatal(err) + } + _, err = p.Break(pc) + if err != nil { + t.Fatal(err) + } + err = p.Continue() + if err != nil { + t.Fatal(err) + } + var nt int + ct := p.CurrentThread.Id + for tid, _ := range p.Threads { + if tid != ct { + nt = tid + break + } + } + if nt == 0 { + t.Fatal("could not find thread to switch to") + } + // With valid thread id + err = p.SwitchThread(nt) + if err != nil { + t.Fatal(err) + } + if p.CurrentThread.Id != nt { + t.Fatal("Did not switch threads") + } + }) +} -- GitLab