How to use fatalf method of main Package

Best Syzkaller code snippet using main.fatalf

integration2_test.go

Source:integration2_test.go Github

copy

Full Screen

1package service_test2import (3 "flag"4 "fmt"5 "math/rand"6 "net"7 "os"8 "path/filepath"9 "runtime"10 "strconv"11 "strings"12 "testing"13 "time"14 protest "github.com/derekparker/delve/pkg/proc/test"15 "github.com/derekparker/delve/pkg/goversion"16 "github.com/derekparker/delve/service"17 "github.com/derekparker/delve/service/api"18 "github.com/derekparker/delve/service/rpc2"19 "github.com/derekparker/delve/service/rpccommon"20)21var normalLoadConfig = api.LoadConfig{true, 1, 64, 64, -1}22var testBackend string23func TestMain(m *testing.M) {24 flag.StringVar(&testBackend, "backend", "", "selects backend")25 flag.Parse()26 if testBackend == "" {27 testBackend = os.Getenv("PROCTEST")28 if testBackend == "" {29 testBackend = "native"30 }31 }32 os.Exit(protest.RunTestsWithFixtures(m))33}34func withTestClient2(name string, t *testing.T, fn func(c service.Client)) {35 if testBackend == "rr" {36 protest.MustHaveRecordingAllowed(t)37 }38 listener, err := net.Listen("tcp", "localhost:0")39 if err != nil {40 t.Fatalf("couldn't start listener: %s\n", err)41 }42 defer listener.Close()43 server := rpccommon.NewServer(&service.Config{44 Listener: listener,45 ProcessArgs: []string{protest.BuildFixture(name, 0).Path},46 Backend: testBackend,47 }, false)48 if err := server.Run(); err != nil {49 t.Fatal(err)50 }51 client := rpc2.NewClient(listener.Addr().String())52 defer func() {53 dir, _ := client.TraceDirectory()54 client.Detach(true)55 if dir != "" {56 protest.SafeRemoveAll(dir)57 }58 }()59 fn(client)60}61func TestRunWithInvalidPath(t *testing.T) {62 if testBackend == "rr" {63 // This test won't work because rr returns an error, after recording, when64 // the recording failed but also when the recording succeeded but the65 // inferior returned an error. Therefore we have to ignore errors from rr.66 return67 }68 listener, err := net.Listen("tcp", "localhost:0")69 if err != nil {70 t.Fatalf("couldn't start listener: %s\n", err)71 }72 defer listener.Close()73 server := rpccommon.NewServer(&service.Config{74 Listener: listener,75 ProcessArgs: []string{"invalid_path"},76 APIVersion: 2,77 Backend: testBackend,78 }, false)79 if err := server.Run(); err == nil {80 t.Fatal("Expected Run to return error for invalid program path")81 }82}83func TestRestart_afterExit(t *testing.T) {84 withTestClient2("continuetestprog", t, func(c service.Client) {85 origPid := c.ProcessPid()86 state := <-c.Continue()87 if !state.Exited {88 t.Fatal("expected initial process to have exited")89 }90 if _, err := c.Restart(); err != nil {91 t.Fatal(err)92 }93 if c.ProcessPid() == origPid {94 t.Fatal("did not spawn new process, has same PID")95 }96 state = <-c.Continue()97 if !state.Exited {98 t.Fatalf("expected restarted process to have exited %v", state)99 }100 })101}102func TestRestart_breakpointPreservation(t *testing.T) {103 protest.AllowRecording(t)104 withTestClient2("continuetestprog", t, func(c service.Client) {105 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1, Name: "firstbreakpoint", Tracepoint: true})106 assertNoError(err, t, "CreateBreakpoint()")107 stateCh := c.Continue()108 state := <-stateCh109 if state.CurrentThread.Breakpoint.Name != "firstbreakpoint" || !state.CurrentThread.Breakpoint.Tracepoint {110 t.Fatalf("Wrong breakpoint: %#v\n", state.CurrentThread.Breakpoint)111 }112 state = <-stateCh113 if !state.Exited {114 t.Fatal("Did not exit after first tracepoint")115 }116 t.Log("Restart")117 c.Restart()118 stateCh = c.Continue()119 state = <-stateCh120 if state.CurrentThread.Breakpoint.Name != "firstbreakpoint" || !state.CurrentThread.Breakpoint.Tracepoint {121 t.Fatalf("Wrong breakpoint (after restart): %#v\n", state.CurrentThread.Breakpoint)122 }123 state = <-stateCh124 if !state.Exited {125 t.Fatal("Did not exit after first tracepoint (after restart)")126 }127 })128}129func TestRestart_duringStop(t *testing.T) {130 withTestClient2("continuetestprog", t, func(c service.Client) {131 origPid := c.ProcessPid()132 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1})133 if err != nil {134 t.Fatal(err)135 }136 state := <-c.Continue()137 if state.CurrentThread.Breakpoint == nil {138 t.Fatal("did not hit breakpoint")139 }140 if _, err := c.Restart(); err != nil {141 t.Fatal(err)142 }143 if c.ProcessPid() == origPid {144 t.Fatal("did not spawn new process, has same PID")145 }146 bps, err := c.ListBreakpoints()147 if err != nil {148 t.Fatal(err)149 }150 if len(bps) == 0 {151 t.Fatal("breakpoints not preserved")152 }153 })154}155func TestRestart_attachPid(t *testing.T) {156 // Assert it does not work and returns error.157 // We cannot restart a process we did not spawn.158 server := rpccommon.NewServer(&service.Config{159 Listener: nil,160 AttachPid: 999,161 APIVersion: 2,162 Backend: testBackend,163 }, false)164 if err := server.Restart(); err == nil {165 t.Fatal("expected error on restart after attaching to pid but got none")166 }167}168func TestClientServer_exit(t *testing.T) {169 protest.AllowRecording(t)170 withTestClient2("continuetestprog", t, func(c service.Client) {171 state, err := c.GetState()172 if err != nil {173 t.Fatalf("Unexpected error: %v", err)174 }175 if e, a := false, state.Exited; e != a {176 t.Fatalf("Expected exited %v, got %v", e, a)177 }178 state = <-c.Continue()179 if state.Err == nil {180 t.Fatalf("Error expected after continue")181 }182 if !state.Exited {183 t.Fatalf("Expected exit after continue: %v", state)184 }185 _, err = c.GetState()186 if err == nil {187 t.Fatal("Expected error on querying state from exited process")188 }189 })190}191func TestClientServer_step(t *testing.T) {192 protest.AllowRecording(t)193 withTestClient2("testprog", t, func(c service.Client) {194 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: -1})195 if err != nil {196 t.Fatalf("Unexpected error: %v", err)197 }198 stateBefore := <-c.Continue()199 if stateBefore.Err != nil {200 t.Fatalf("Unexpected error: %v", stateBefore.Err)201 }202 stateAfter, err := c.Step()203 if err != nil {204 t.Fatalf("Unexpected error: %v", err)205 }206 if before, after := stateBefore.CurrentThread.PC, stateAfter.CurrentThread.PC; before >= after {207 t.Errorf("Expected %#v to be greater than %#v", before, after)208 }209 })210}211func TestClientServer_stepout(t *testing.T) {212 protest.AllowRecording(t)213 withTestClient2("testnextprog", t, func(c service.Client) {214 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: -1})215 assertNoError(err, t, "CreateBreakpoint()")216 stateBefore := <-c.Continue()217 assertNoError(stateBefore.Err, t, "Continue()")218 if stateBefore.CurrentThread.Line != 13 {219 t.Fatalf("wrong line number %s:%d, expected %d", stateBefore.CurrentThread.File, stateBefore.CurrentThread.Line, 13)220 }221 stateAfter, err := c.StepOut()222 assertNoError(err, t, "StepOut()")223 if stateAfter.CurrentThread.Line != 35 {224 t.Fatalf("wrong line number %s:%d, expected %d", stateAfter.CurrentThread.File, stateAfter.CurrentThread.Line, 13)225 }226 })227}228func testnext2(testcases []nextTest, initialLocation string, t *testing.T) {229 protest.AllowRecording(t)230 withTestClient2("testnextprog", t, func(c service.Client) {231 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: initialLocation, Line: -1})232 if err != nil {233 t.Fatalf("Unexpected error: %v", err)234 }235 state := <-c.Continue()236 if state.Err != nil {237 t.Fatalf("Unexpected error: %v", state.Err)238 }239 _, err = c.ClearBreakpoint(bp.ID)240 if err != nil {241 t.Fatalf("Unexpected error: %v", err)242 }243 for _, tc := range testcases {244 if state.CurrentThread.Line != tc.begin {245 t.Fatalf("Program not stopped at correct spot expected %d was %d", tc.begin, state.CurrentThread.Line)246 }247 t.Logf("Next for scenario %#v", tc)248 state, err = c.Next()249 if err != nil {250 t.Fatalf("Unexpected error: %v", err)251 }252 if state.CurrentThread.Line != tc.end {253 t.Fatalf("Program did not continue to correct next location expected %d was %d", tc.end, state.CurrentThread.Line)254 }255 }256 })257}258func TestNextGeneral(t *testing.T) {259 var testcases []nextTest260 ver, _ := goversion.Parse(runtime.Version())261 if ver.Major < 0 || ver.AfterOrEqual(goversion.GoVersion{1, 7, -1, 0, 0, ""}) {262 testcases = []nextTest{263 {17, 19},264 {19, 20},265 {20, 23},266 {23, 24},267 {24, 26},268 {26, 31},269 {31, 23},270 {23, 24},271 {24, 26},272 {26, 31},273 {31, 23},274 {23, 24},275 {24, 26},276 {26, 27},277 {27, 28},278 {28, 34},279 }280 } else {281 testcases = []nextTest{282 {17, 19},283 {19, 20},284 {20, 23},285 {23, 24},286 {24, 26},287 {26, 31},288 {31, 23},289 {23, 24},290 {24, 26},291 {26, 31},292 {31, 23},293 {23, 24},294 {24, 26},295 {26, 27},296 {27, 34},297 }298 }299 testnext2(testcases, "main.testnext", t)300}301func TestNextFunctionReturn(t *testing.T) {302 testcases := []nextTest{303 {13, 14},304 {14, 15},305 {15, 35},306 }307 testnext2(testcases, "main.helloworld", t)308}309func TestClientServer_breakpointInMainThread(t *testing.T) {310 protest.AllowRecording(t)311 withTestClient2("testprog", t, func(c service.Client) {312 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: 1})313 if err != nil {314 t.Fatalf("Unexpected error: %v", err)315 }316 state := <-c.Continue()317 if err != nil {318 t.Fatalf("Unexpected error: %v, state: %#v", err, state)319 }320 pc := state.CurrentThread.PC321 if pc-1 != bp.Addr && pc != bp.Addr {322 f, l := state.CurrentThread.File, state.CurrentThread.Line323 t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)324 }325 })326}327func TestClientServer_breakpointInSeparateGoroutine(t *testing.T) {328 protest.AllowRecording(t)329 withTestClient2("testthreads", t, func(c service.Client) {330 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.anotherthread", Line: 1})331 if err != nil {332 t.Fatalf("Unexpected error: %v", err)333 }334 state := <-c.Continue()335 if state.Err != nil {336 t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)337 }338 f, l := state.CurrentThread.File, state.CurrentThread.Line339 if f != "testthreads.go" && l != 9 {340 t.Fatal("Program did not hit breakpoint")341 }342 })343}344func TestClientServer_breakAtNonexistentPoint(t *testing.T) {345 withTestClient2("testprog", t, func(c service.Client) {346 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "nowhere", Line: 1})347 if err == nil {348 t.Fatal("Should not be able to break at non existent function")349 }350 })351}352func TestClientServer_clearBreakpoint(t *testing.T) {353 withTestClient2("testprog", t, func(c service.Client) {354 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sleepytime", Line: 1})355 if err != nil {356 t.Fatalf("Unexpected error: %v", err)357 }358 if e, a := 1, countBreakpoints(t, c); e != a {359 t.Fatalf("Expected breakpoint count %d, got %d", e, a)360 }361 deleted, err := c.ClearBreakpoint(bp.ID)362 if err != nil {363 t.Fatalf("Unexpected error: %v", err)364 }365 if deleted.ID != bp.ID {366 t.Fatalf("Expected deleted breakpoint ID %v, got %v", bp.ID, deleted.ID)367 }368 if e, a := 0, countBreakpoints(t, c); e != a {369 t.Fatalf("Expected breakpoint count %d, got %d", e, a)370 }371 })372}373func TestClientServer_switchThread(t *testing.T) {374 protest.AllowRecording(t)375 withTestClient2("testnextprog", t, func(c service.Client) {376 // With invalid thread id377 _, err := c.SwitchThread(-1)378 if err == nil {379 t.Fatal("Expected error for invalid thread id")380 }381 _, err = c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1})382 if err != nil {383 t.Fatalf("Unexpected error: %v", err)384 }385 state := <-c.Continue()386 if state.Err != nil {387 t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)388 }389 var nt int390 ct := state.CurrentThread.ID391 threads, err := c.ListThreads()392 if err != nil {393 t.Fatalf("Unexpected error: %v", err)394 }395 for _, th := range threads {396 if th.ID != ct {397 nt = th.ID398 break399 }400 }401 if nt == 0 {402 t.Fatal("could not find thread to switch to")403 }404 // With valid thread id405 state, err = c.SwitchThread(nt)406 if err != nil {407 t.Fatal(err)408 }409 if state.CurrentThread.ID != nt {410 t.Fatal("Did not switch threads")411 }412 })413}414func TestClientServer_infoLocals(t *testing.T) {415 protest.AllowRecording(t)416 withTestClient2("testnextprog", t, func(c service.Client) {417 fp := testProgPath(t, "testnextprog")418 _, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 23})419 if err != nil {420 t.Fatalf("Unexpected error: %v", err)421 }422 state := <-c.Continue()423 if state.Err != nil {424 t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)425 }426 locals, err := c.ListLocalVariables(api.EvalScope{-1, 0}, normalLoadConfig)427 if err != nil {428 t.Fatalf("Unexpected error: %v", err)429 }430 if len(locals) != 3 {431 t.Fatalf("Expected 3 locals, got %d %#v", len(locals), locals)432 }433 })434}435func TestClientServer_infoArgs(t *testing.T) {436 protest.AllowRecording(t)437 withTestClient2("testnextprog", t, func(c service.Client) {438 fp := testProgPath(t, "testnextprog")439 _, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 47})440 if err != nil {441 t.Fatalf("Unexpected error: %v", err)442 }443 state := <-c.Continue()444 if state.Err != nil {445 t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)446 }447 regs, err := c.ListRegisters(0, false)448 if err != nil {449 t.Fatalf("Unexpected error: %v", err)450 }451 if len(regs) == 0 {452 t.Fatal("Expected string showing registers values, got empty string")453 }454 locals, err := c.ListFunctionArgs(api.EvalScope{-1, 0}, normalLoadConfig)455 if err != nil {456 t.Fatalf("Unexpected error: %v", err)457 }458 if len(locals) != 2 {459 t.Fatalf("Expected 2 function args, got %d %#v", len(locals), locals)460 }461 })462}463func TestClientServer_traceContinue(t *testing.T) {464 protest.AllowRecording(t)465 withTestClient2("integrationprog", t, func(c service.Client) {466 fp := testProgPath(t, "integrationprog")467 _, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 15, Tracepoint: true, Goroutine: true, Stacktrace: 5, Variables: []string{"i"}})468 if err != nil {469 t.Fatalf("Unexpected error: %v\n", err)470 }471 count := 0472 contChan := c.Continue()473 for state := range contChan {474 if state.CurrentThread != nil && state.CurrentThread.Breakpoint != nil {475 count++476 t.Logf("%v", state)477 bpi := state.CurrentThread.BreakpointInfo478 if bpi.Goroutine == nil {479 t.Fatalf("No goroutine information")480 }481 if len(bpi.Stacktrace) <= 0 {482 t.Fatalf("No stacktrace\n")483 }484 if len(bpi.Variables) != 1 {485 t.Fatalf("Wrong number of variables returned: %d", len(bpi.Variables))486 }487 if bpi.Variables[0].Name != "i" {488 t.Fatalf("Wrong variable returned %s", bpi.Variables[0].Name)489 }490 t.Logf("Variable i is %v", bpi.Variables[0])491 n, err := strconv.Atoi(bpi.Variables[0].Value)492 if err != nil || n != count-1 {493 t.Fatalf("Wrong variable value %q (%v %d)", bpi.Variables[0].Value, err, count)494 }495 }496 if state.Exited {497 continue498 }499 t.Logf("%v", state)500 if state.Err != nil {501 t.Fatalf("Unexpected error during continue: %v\n", state.Err)502 }503 }504 if count != 3 {505 t.Fatalf("Wrong number of continues hit: %d\n", count)506 }507 })508}509func TestClientServer_traceContinue2(t *testing.T) {510 protest.AllowRecording(t)511 withTestClient2("integrationprog", t, func(c service.Client) {512 bp1, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1, Tracepoint: true})513 if err != nil {514 t.Fatalf("Unexpected error: %v\n", err)515 }516 bp2, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: 1, Tracepoint: true})517 if err != nil {518 t.Fatalf("Unexpected error: %v\n", err)519 }520 countMain := 0521 countSayhi := 0522 contChan := c.Continue()523 for state := range contChan {524 if state.CurrentThread != nil && state.CurrentThread.Breakpoint != nil {525 switch state.CurrentThread.Breakpoint.ID {526 case bp1.ID:527 countMain++528 case bp2.ID:529 countSayhi++530 }531 t.Logf("%v", state)532 }533 if state.Exited {534 continue535 }536 if state.Err != nil {537 t.Fatalf("Unexpected error during continue: %v\n", state.Err)538 }539 }540 if countMain != 1 {541 t.Fatalf("Wrong number of continues (main.main) hit: %d\n", countMain)542 }543 if countSayhi != 3 {544 t.Fatalf("Wrong number of continues (main.sayhi) hit: %d\n", countSayhi)545 }546 })547}548func TestClientServer_FindLocations(t *testing.T) {549 withTestClient2("locationsprog", t, func(c service.Client) {550 someFunctionCallAddr := findLocationHelper(t, c, "locationsprog.go:26", false, 1, 0)[0]551 someFunctionLine1 := findLocationHelper(t, c, "locationsprog.go:27", false, 1, 0)[0]552 findLocationHelper(t, c, "anotherFunction:1", false, 1, someFunctionLine1)553 findLocationHelper(t, c, "main.anotherFunction:1", false, 1, someFunctionLine1)554 findLocationHelper(t, c, "anotherFunction", false, 1, someFunctionCallAddr)555 findLocationHelper(t, c, "main.anotherFunction", false, 1, someFunctionCallAddr)556 findLocationHelper(t, c, fmt.Sprintf("*0x%x", someFunctionCallAddr), false, 1, someFunctionCallAddr)557 findLocationHelper(t, c, "sprog.go:26", true, 0, 0)558 findLocationHelper(t, c, "String", true, 0, 0)559 findLocationHelper(t, c, "main.String", true, 0, 0)560 someTypeStringFuncAddr := findLocationHelper(t, c, "locationsprog.go:14", false, 1, 0)[0]561 otherTypeStringFuncAddr := findLocationHelper(t, c, "locationsprog.go:18", false, 1, 0)[0]562 findLocationHelper(t, c, "SomeType.String", false, 1, someTypeStringFuncAddr)563 findLocationHelper(t, c, "(*SomeType).String", false, 1, someTypeStringFuncAddr)564 findLocationHelper(t, c, "main.SomeType.String", false, 1, someTypeStringFuncAddr)565 findLocationHelper(t, c, "main.(*SomeType).String", false, 1, someTypeStringFuncAddr)566 // Issue #275567 readfile := findLocationHelper(t, c, "io/ioutil.ReadFile", false, 1, 0)[0]568 // Issue #296569 findLocationHelper(t, c, "/io/ioutil.ReadFile", false, 1, readfile)570 findLocationHelper(t, c, "ioutil.ReadFile", false, 1, readfile)571 stringAddrs := findLocationHelper(t, c, "/^main.*Type.*String$/", false, 2, 0)572 if otherTypeStringFuncAddr != stringAddrs[0] && otherTypeStringFuncAddr != stringAddrs[1] {573 t.Fatalf("Wrong locations returned for \"/.*Type.*String/\", got: %v expected: %v and %v\n", stringAddrs, someTypeStringFuncAddr, otherTypeStringFuncAddr)574 }575 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 4, Tracepoint: false})576 if err != nil {577 t.Fatalf("CreateBreakpoint(): %v\n", err)578 }579 <-c.Continue()580 locationsprog35Addr := findLocationHelper(t, c, "locationsprog.go:35", false, 1, 0)[0]581 findLocationHelper(t, c, fmt.Sprintf("%s:35", testProgPath(t, "locationsprog")), false, 1, locationsprog35Addr)582 findLocationHelper(t, c, "+1", false, 1, locationsprog35Addr)583 findLocationHelper(t, c, "35", false, 1, locationsprog35Addr)584 findLocationHelper(t, c, "-1", false, 1, findLocationHelper(t, c, "locationsprog.go:33", false, 1, 0)[0])585 findLocationHelper(t, c, `*amap["k"]`, false, 1, findLocationHelper(t, c, `amap["k"]`, false, 1, 0)[0])586 })587 withTestClient2("testnextdefer", t, func(c service.Client) {588 firstMainLine := findLocationHelper(t, c, "testnextdefer.go:5", false, 1, 0)[0]589 findLocationHelper(t, c, "main.main", false, 1, firstMainLine)590 })591 withTestClient2("stacktraceprog", t, func(c service.Client) {592 stacktracemeAddr := findLocationHelper(t, c, "stacktraceprog.go:4", false, 1, 0)[0]593 findLocationHelper(t, c, "main.stacktraceme", false, 1, stacktracemeAddr)594 })595 withTestClient2("locationsUpperCase", t, func(c service.Client) {596 // Upper case597 findLocationHelper(t, c, "locationsUpperCase.go:6", false, 1, 0)598 // Fully qualified path599 path := protest.Fixtures[protest.FixtureKey{"locationsUpperCase", 0}].Source600 findLocationHelper(t, c, path+":6", false, 1, 0)601 bp, err := c.CreateBreakpoint(&api.Breakpoint{File: path, Line: 6})602 if err != nil {603 t.Fatalf("Could not set breakpoint in %s: %v\n", path, err)604 }605 c.ClearBreakpoint(bp.ID)606 // Allow `/` or `\` on Windows607 if runtime.GOOS == "windows" {608 findLocationHelper(t, c, filepath.FromSlash(path)+":6", false, 1, 0)609 bp, err = c.CreateBreakpoint(&api.Breakpoint{File: filepath.FromSlash(path), Line: 6})610 if err != nil {611 t.Fatalf("Could not set breakpoint in %s: %v\n", filepath.FromSlash(path), err)612 }613 c.ClearBreakpoint(bp.ID)614 }615 // Case-insensitive on Windows, case-sensitive otherwise616 shouldWrongCaseBeError := true617 numExpectedMatches := 0618 if runtime.GOOS == "windows" {619 shouldWrongCaseBeError = false620 numExpectedMatches = 1621 }622 findLocationHelper(t, c, strings.ToLower(path)+":6", shouldWrongCaseBeError, numExpectedMatches, 0)623 bp, err = c.CreateBreakpoint(&api.Breakpoint{File: strings.ToLower(path), Line: 6})624 if (err == nil) == shouldWrongCaseBeError {625 t.Fatalf("Could not set breakpoint in %s: %v\n", strings.ToLower(path), err)626 }627 c.ClearBreakpoint(bp.ID)628 })629}630func TestClientServer_FindLocationsAddr(t *testing.T) {631 withTestClient2("locationsprog2", t, func(c service.Client) {632 <-c.Continue()633 afunction := findLocationHelper(t, c, "main.afunction", false, 1, 0)[0]634 anonfunc := findLocationHelper(t, c, "main.main.func1", false, 1, 0)[0]635 findLocationHelper(t, c, "*fn1", false, 1, afunction)636 findLocationHelper(t, c, "*fn3", false, 1, anonfunc)637 })638}639func TestClientServer_FindLocationsExactMatch(t *testing.T) {640 // if an expression matches multiple functions but one of them is an exact641 // match it should be used anyway.642 // In this example "math/rand.Intn" would normally match "math/rand.Intn"643 // and "math/rand.(*Rand).Intn" but since the first match is exact it644 // should be prioritized.645 withTestClient2("locationsprog3", t, func(c service.Client) {646 <-c.Continue()647 findLocationHelper(t, c, "math/rand.Intn", false, 1, 0)648 })649}650func TestClientServer_EvalVariable(t *testing.T) {651 withTestClient2("testvariables", t, func(c service.Client) {652 state := <-c.Continue()653 if state.Err != nil {654 t.Fatalf("Continue(): %v\n", state.Err)655 }656 var1, err := c.EvalVariable(api.EvalScope{-1, 0}, "a1", normalLoadConfig)657 assertNoError(err, t, "EvalVariable")658 t.Logf("var1: %s", var1.SinglelineString())659 if var1.Value != "foofoofoofoofoofoo" {660 t.Fatalf("Wrong variable value: %s", var1.Value)661 }662 })663}664func TestClientServer_SetVariable(t *testing.T) {665 withTestClient2("testvariables", t, func(c service.Client) {666 state := <-c.Continue()667 if state.Err != nil {668 t.Fatalf("Continue(): %v\n", state.Err)669 }670 assertNoError(c.SetVariable(api.EvalScope{-1, 0}, "a2", "8"), t, "SetVariable()")671 a2, err := c.EvalVariable(api.EvalScope{-1, 0}, "a2", normalLoadConfig)672 if err != nil {673 t.Fatalf("Could not evaluate variable: %v", err)674 }675 t.Logf("a2: %v", a2)676 n, err := strconv.Atoi(a2.Value)677 if err != nil && n != 8 {678 t.Fatalf("Wrong variable value: %v", a2)679 }680 })681}682func TestClientServer_FullStacktrace(t *testing.T) {683 protest.AllowRecording(t)684 withTestClient2("goroutinestackprog", t, func(c service.Client) {685 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.stacktraceme", Line: -1})686 assertNoError(err, t, "CreateBreakpoint()")687 state := <-c.Continue()688 if state.Err != nil {689 t.Fatalf("Continue(): %v\n", state.Err)690 }691 gs, err := c.ListGoroutines()692 assertNoError(err, t, "GoroutinesInfo()")693 found := make([]bool, 10)694 for _, g := range gs {695 frames, err := c.Stacktrace(g.ID, 10, &normalLoadConfig)696 assertNoError(err, t, fmt.Sprintf("Stacktrace(%d)", g.ID))697 for i, frame := range frames {698 if frame.Function == nil {699 continue700 }701 if frame.Function.Name != "main.agoroutine" {702 continue703 }704 t.Logf("frame %d: %v", i, frame)705 for _, arg := range frame.Arguments {706 if arg.Name != "i" {707 continue708 }709 t.Logf("frame %v, variable i is %v\n", frame, arg)710 argn, err := strconv.Atoi(arg.Value)711 if err == nil {712 found[argn] = true713 }714 }715 }716 }717 for i := range found {718 if !found[i] {719 t.Fatalf("Goroutine %d not found", i)720 }721 }722 state = <-c.Continue()723 if state.Err != nil {724 t.Fatalf("Continue(): %v\n", state.Err)725 }726 frames, err := c.Stacktrace(-1, 10, &normalLoadConfig)727 assertNoError(err, t, "Stacktrace")728 cur := 3729 for i, frame := range frames {730 if i == 0 {731 continue732 }733 t.Logf("frame %d: %v", i, frame)734 v := frame.Var("n")735 if v == nil {736 t.Fatalf("Could not find value of variable n in frame %d", i)737 }738 vn, err := strconv.Atoi(v.Value)739 if err != nil || vn != cur {740 t.Fatalf("Expected value %d got %d (error: %v)", cur, vn, err)741 }742 cur--743 if cur < 0 {744 break745 }746 }747 })748}749func TestIssue355(t *testing.T) {750 // After the target process has terminated should return an error but not crash751 protest.AllowRecording(t)752 withTestClient2("continuetestprog", t, func(c service.Client) {753 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: -1})754 assertNoError(err, t, "CreateBreakpoint()")755 ch := c.Continue()756 state := <-ch757 tid := state.CurrentThread.ID758 gid := state.SelectedGoroutine.ID759 assertNoError(state.Err, t, "First Continue()")760 ch = c.Continue()761 state = <-ch762 if !state.Exited {763 t.Fatalf("Target did not terminate after second continue")764 }765 ch = c.Continue()766 state = <-ch767 assertError(state.Err, t, "Continue()")768 _, err = c.Next()769 assertError(err, t, "Next()")770 _, err = c.Step()771 assertError(err, t, "Step()")772 _, err = c.StepInstruction()773 assertError(err, t, "StepInstruction()")774 _, err = c.SwitchThread(tid)775 assertError(err, t, "SwitchThread()")776 _, err = c.SwitchGoroutine(gid)777 assertError(err, t, "SwitchGoroutine()")778 _, err = c.Halt()779 assertError(err, t, "Halt()")780 _, err = c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: -1})781 if testBackend != "rr" {782 assertError(err, t, "CreateBreakpoint()")783 }784 _, err = c.ClearBreakpoint(bp.ID)785 if testBackend != "rr" {786 assertError(err, t, "ClearBreakpoint()")787 }788 _, err = c.ListThreads()789 assertError(err, t, "ListThreads()")790 _, err = c.GetThread(tid)791 assertError(err, t, "GetThread()")792 assertError(c.SetVariable(api.EvalScope{gid, 0}, "a", "10"), t, "SetVariable()")793 _, err = c.ListLocalVariables(api.EvalScope{gid, 0}, normalLoadConfig)794 assertError(err, t, "ListLocalVariables()")795 _, err = c.ListFunctionArgs(api.EvalScope{gid, 0}, normalLoadConfig)796 assertError(err, t, "ListFunctionArgs()")797 _, err = c.ListRegisters(0, false)798 assertError(err, t, "ListRegisters()")799 _, err = c.ListGoroutines()800 assertError(err, t, "ListGoroutines()")801 _, err = c.Stacktrace(gid, 10, &normalLoadConfig)802 assertError(err, t, "Stacktrace()")803 _, err = c.FindLocation(api.EvalScope{gid, 0}, "+1")804 assertError(err, t, "FindLocation()")805 _, err = c.DisassemblePC(api.EvalScope{-1, 0}, 0x40100, api.IntelFlavour)806 assertError(err, t, "DisassemblePC()")807 })808}809func TestDisasm(t *testing.T) {810 // Tests that disassembling by PC, range, and current PC all yeld similar results811 // Tests that disassembly by current PC will return a disassembly containing the instruction at PC812 // Tests that stepping on a calculated CALL instruction will yield a disassembly that contains the813 // effective destination of the CALL instruction814 withTestClient2("locationsprog2", t, func(c service.Client) {815 ch := c.Continue()816 state := <-ch817 assertNoError(state.Err, t, "Continue()")818 locs, err := c.FindLocation(api.EvalScope{-1, 0}, "main.main")819 assertNoError(err, t, "FindLocation()")820 if len(locs) != 1 {821 t.Fatalf("wrong number of locations for main.main: %d", len(locs))822 }823 d1, err := c.DisassemblePC(api.EvalScope{-1, 0}, locs[0].PC, api.IntelFlavour)824 assertNoError(err, t, "DisassemblePC()")825 if len(d1) < 2 {826 t.Fatalf("wrong size of disassembly: %d", len(d1))827 }828 pcstart := d1[0].Loc.PC829 pcend := d1[len(d1)-1].Loc.PC + uint64(len(d1[len(d1)-1].Bytes))830 d2, err := c.DisassembleRange(api.EvalScope{-1, 0}, pcstart, pcend, api.IntelFlavour)831 assertNoError(err, t, "DisassembleRange()")832 if len(d1) != len(d2) {833 t.Logf("d1: %v", d1)834 t.Logf("d2: %v", d2)835 t.Fatal("mismatched length between disassemble pc and disassemble range")836 }837 d3, err := c.DisassemblePC(api.EvalScope{-1, 0}, state.CurrentThread.PC, api.IntelFlavour)838 assertNoError(err, t, "DisassemblePC() - second call")839 if len(d1) != len(d3) {840 t.Logf("d1: %v", d1)841 t.Logf("d3: %v", d3)842 t.Fatal("mismatched length between the two calls of disassemble pc")843 }844 // look for static call to afunction() on line 29845 found := false846 for i := range d3 {847 if d3[i].Loc.Line == 29 && strings.HasPrefix(d3[i].Text, "call") && d3[i].DestLoc != nil && d3[i].DestLoc.Function != nil && d3[i].DestLoc.Function.Name == "main.afunction" {848 found = true849 break850 }851 }852 if !found {853 t.Fatal("Could not find call to main.afunction on line 29")854 }855 haspc := false856 for i := range d3 {857 if d3[i].AtPC {858 haspc = true859 break860 }861 }862 if !haspc {863 t.Logf("d3: %v", d3)864 t.Fatal("PC instruction not found")865 }866 startinstr := getCurinstr(d3)867 count := 0868 for {869 if count > 20 {870 t.Fatal("too many step instructions executed without finding a call instruction")871 }872 state, err := c.StepInstruction()873 assertNoError(err, t, fmt.Sprintf("StepInstruction() %d", count))874 d3, err = c.DisassemblePC(api.EvalScope{-1, 0}, state.CurrentThread.PC, api.IntelFlavour)875 assertNoError(err, t, fmt.Sprintf("StepInstruction() %d", count))876 curinstr := getCurinstr(d3)877 if curinstr == nil {878 t.Fatalf("Could not find current instruction %d", count)879 }880 if curinstr.Loc.Line != startinstr.Loc.Line {881 t.Fatal("Calling StepInstruction() repeatedly did not find the call instruction")882 }883 if strings.HasPrefix(curinstr.Text, "call") {884 t.Logf("call: %v", curinstr)885 if curinstr.DestLoc == nil || curinstr.DestLoc.Function == nil {886 t.Fatalf("Call instruction does not have destination: %v", curinstr)887 }888 if curinstr.DestLoc.Function.Name != "main.afunction" {889 t.Fatalf("Call instruction destination not main.afunction: %v", curinstr)890 }891 break892 }893 count++894 }895 })896}897func TestNegativeStackDepthBug(t *testing.T) {898 // After the target process has terminated should return an error but not crash899 protest.AllowRecording(t)900 withTestClient2("continuetestprog", t, func(c service.Client) {901 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: -1})902 assertNoError(err, t, "CreateBreakpoint()")903 ch := c.Continue()904 state := <-ch905 assertNoError(state.Err, t, "Continue()")906 _, err = c.Stacktrace(-1, -2, &normalLoadConfig)907 assertError(err, t, "Stacktrace()")908 })909}910func TestClientServer_CondBreakpoint(t *testing.T) {911 protest.AllowRecording(t)912 withTestClient2("parallel_next", t, func(c service.Client) {913 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: 1})914 assertNoError(err, t, "CreateBreakpoint()")915 bp.Cond = "n == 7"916 assertNoError(c.AmendBreakpoint(bp), t, "AmendBreakpoint() 1")917 bp, err = c.GetBreakpoint(bp.ID)918 assertNoError(err, t, "GetBreakpoint() 1")919 bp.Variables = append(bp.Variables, "n")920 assertNoError(c.AmendBreakpoint(bp), t, "AmendBreakpoint() 2")921 bp, err = c.GetBreakpoint(bp.ID)922 assertNoError(err, t, "GetBreakpoint() 2")923 if bp.Cond == "" {924 t.Fatalf("No condition set on breakpoint %#v", bp)925 }926 if len(bp.Variables) != 1 {927 t.Fatalf("Wrong number of expressions to evaluate on breakpoint %#v", bp)928 }929 state := <-c.Continue()930 assertNoError(state.Err, t, "Continue()")931 nvar, err := c.EvalVariable(api.EvalScope{-1, 0}, "n", normalLoadConfig)932 assertNoError(err, t, "EvalVariable()")933 if nvar.SinglelineString() != "7" {934 t.Fatalf("Stopped on wrong goroutine %s\n", nvar.Value)935 }936 })937}938func TestSkipPrologue(t *testing.T) {939 withTestClient2("locationsprog2", t, func(c service.Client) {940 <-c.Continue()941 afunction := findLocationHelper(t, c, "main.afunction", false, 1, 0)[0]942 findLocationHelper(t, c, "*fn1", false, 1, afunction)943 findLocationHelper(t, c, "locationsprog2.go:8", false, 1, afunction)944 afunction0 := findLocationHelper(t, c, "main.afunction:0", false, 1, 0)[0]945 if afunction == afunction0 {946 t.Fatal("Skip prologue failed")947 }948 })949}950func TestSkipPrologue2(t *testing.T) {951 withTestClient2("callme", t, func(c service.Client) {952 callme := findLocationHelper(t, c, "main.callme", false, 1, 0)[0]953 callmeZ := findLocationHelper(t, c, "main.callme:0", false, 1, 0)[0]954 findLocationHelper(t, c, "callme.go:5", false, 1, callme)955 if callme == callmeZ {956 t.Fatal("Skip prologue failed")957 }958 callme2 := findLocationHelper(t, c, "main.callme2", false, 1, 0)[0]959 callme2Z := findLocationHelper(t, c, "main.callme2:0", false, 1, 0)[0]960 findLocationHelper(t, c, "callme.go:12", false, 1, callme2)961 if callme2 == callme2Z {962 t.Fatal("Skip prologue failed")963 }964 callme3 := findLocationHelper(t, c, "main.callme3", false, 1, 0)[0]965 callme3Z := findLocationHelper(t, c, "main.callme3:0", false, 1, 0)[0]966 ver, _ := goversion.Parse(runtime.Version())967 if ver.Major < 0 || ver.AfterOrEqual(goversion.GoVer18Beta) {968 findLocationHelper(t, c, "callme.go:19", false, 1, callme3)969 } else {970 // callme3 does not have local variables therefore the first line of the971 // function is immediately after the prologue972 // This is only true before 1.8 where frame pointer chaining introduced a973 // bit of prologue even for functions without local variables974 findLocationHelper(t, c, "callme.go:19", false, 1, callme3Z)975 }976 if callme3 == callme3Z {977 t.Fatal("Skip prologue failed")978 }979 })980}981func TestIssue419(t *testing.T) {982 // Calling service/rpc.(*Client).Halt could cause a crash because both Halt and Continue simultaneously983 // try to read 'runtime.g' and debug/dwarf.Data.Type is not thread safe984 withTestClient2("issue419", t, func(c service.Client) {985 go func() {986 rand.Seed(time.Now().Unix())987 d := time.Duration(rand.Intn(4) + 1)988 time.Sleep(d * time.Second)989 _, err := c.Halt()990 assertNoError(err, t, "RequestManualStop()")991 }()992 statech := c.Continue()993 state := <-statech994 assertNoError(state.Err, t, "Continue()")995 })996}997func TestTypesCommand(t *testing.T) {998 protest.AllowRecording(t)999 withTestClient2("testvariables2", t, func(c service.Client) {1000 state := <-c.Continue()1001 assertNoError(state.Err, t, "Continue()")1002 types, err := c.ListTypes("")1003 assertNoError(err, t, "ListTypes()")1004 found := false1005 for i := range types {1006 if types[i] == "main.astruct" {1007 found = true1008 break1009 }1010 }1011 if !found {1012 t.Fatal("Type astruct not found in ListTypes output")1013 }1014 types, err = c.ListTypes("^main.astruct$")1015 assertNoError(err, t, "ListTypes(\"main.astruct\")")1016 if len(types) != 1 {1017 t.Fatalf("ListTypes(\"^main.astruct$\") did not filter properly, expected 1 got %d: %v", len(types), types)1018 }1019 })1020}1021func TestIssue406(t *testing.T) {1022 protest.AllowRecording(t)1023 withTestClient2("issue406", t, func(c service.Client) {1024 locs, err := c.FindLocation(api.EvalScope{-1, 0}, "issue406.go:146")1025 assertNoError(err, t, "FindLocation()")1026 _, err = c.CreateBreakpoint(&api.Breakpoint{Addr: locs[0].PC})1027 assertNoError(err, t, "CreateBreakpoint()")1028 ch := c.Continue()1029 state := <-ch1030 assertNoError(state.Err, t, "Continue()")1031 v, err := c.EvalVariable(api.EvalScope{-1, 0}, "cfgtree", normalLoadConfig)1032 assertNoError(err, t, "EvalVariable()")1033 vs := v.MultilineString("")1034 t.Logf("cfgtree formats to: %s\n", vs)1035 })1036}1037func TestEvalExprName(t *testing.T) {1038 withTestClient2("testvariables2", t, func(c service.Client) {1039 state := <-c.Continue()1040 assertNoError(state.Err, t, "Continue()")1041 var1, err := c.EvalVariable(api.EvalScope{-1, 0}, "i1+1", normalLoadConfig)1042 assertNoError(err, t, "EvalVariable")1043 const name = "i1+1"1044 t.Logf("i1+1 → %#v", var1)1045 if var1.Name != name {1046 t.Fatalf("Wrong variable name %q, expected %q", var1.Name, name)1047 }1048 })1049}1050func TestClientServer_Issue528(t *testing.T) {1051 // FindLocation with Receiver.MethodName syntax does not work1052 // on remote package names due to a bug in debug/gosym that1053 // Was fixed in go 1.7 // Commit that fixes the issue in go:1054 // f744717d1924340b8f5e5a385e99078693ad90971055 ver, _ := goversion.Parse(runtime.Version())1056 if ver.Major > 0 && !ver.AfterOrEqual(goversion.GoVersion{1, 7, -1, 0, 0, ""}) {1057 t.Log("Test skipped")1058 return1059 }1060 withTestClient2("issue528", t, func(c service.Client) {1061 findLocationHelper(t, c, "State.Close", false, 1, 0)1062 })1063}1064func TestClientServer_FpRegisters(t *testing.T) {1065 regtests := []struct{ name, value string }{1066 {"ST(0)", "0x3fffe666660000000000"},1067 {"ST(1)", "0x3fffd9999a0000000000"},1068 {"ST(2)", "0x3fffcccccd0000000000"},1069 {"ST(3)", "0x3fffc000000000000000"},1070 {"ST(4)", "0x3fffb333333333333000"},1071 {"ST(5)", "0x3fffa666666666666800"},1072 {"ST(6)", "0x3fff9999999999999800"},1073 {"ST(7)", "0x3fff8cccccccccccd000"},1074 {"XMM0", "0x3ff33333333333333ff199999999999a v2_int={ 3ff199999999999a 3ff3333333333333 } v4_int={ 9999999a 3ff19999 33333333 3ff33333 } v8_int={ 999a 9999 9999 3ff1 3333 3333 3333 3ff3 } v16_int={ 9a 99 99 99 99 99 f1 3f 33 33 33 33 33 33 f3 3f }"},1075 {"XMM1", "0x3ff66666666666663ff4cccccccccccd"},1076 {"XMM2", "0x3fe666663fd9999a3fcccccd3fc00000"},1077 {"XMM3", "0x3ff199999999999a3ff3333333333333"},1078 {"XMM4", "0x3ff4cccccccccccd3ff6666666666666"},1079 {"XMM5", "0x3fcccccd3fc000003fe666663fd9999a"},1080 {"XMM6", "0x4004cccccccccccc4003333333333334"},1081 {"XMM7", "0x40026666666666664002666666666666"},1082 {"XMM8", "0x4059999a404ccccd4059999a404ccccd"},1083 }1084 protest.AllowRecording(t)1085 withTestClient2("fputest/", t, func(c service.Client) {1086 <-c.Continue()1087 regs, err := c.ListRegisters(0, true)1088 assertNoError(err, t, "ListRegisters()")1089 t.Logf("%s", regs.String())1090 for _, regtest := range regtests {1091 found := false1092 for _, reg := range regs {1093 if reg.Name == regtest.name {1094 found = true1095 if !strings.HasPrefix(reg.Value, regtest.value) {1096 t.Fatalf("register %s expected %q got %q", reg.Name, regtest.value, reg.Value)1097 }1098 }1099 }1100 if !found {1101 t.Fatalf("register %s not found: %v", regtest.name, regs)1102 }1103 }1104 })1105}1106func TestClientServer_RestartBreakpointPosition(t *testing.T) {1107 protest.AllowRecording(t)1108 withTestClient2("locationsprog2", t, func(c service.Client) {1109 bpBefore, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.afunction", Line: -1, Tracepoint: true, Name: "this"})1110 addrBefore := bpBefore.Addr1111 t.Logf("%x\n", bpBefore.Addr)1112 assertNoError(err, t, "CreateBreakpoint")1113 stateCh := c.Continue()1114 for range stateCh {1115 }1116 _, err = c.Halt()1117 assertNoError(err, t, "Halt")1118 _, err = c.Restart()1119 assertNoError(err, t, "Restart")1120 bps, err := c.ListBreakpoints()1121 assertNoError(err, t, "ListBreakpoints")1122 for _, bp := range bps {1123 if bp.Name == bpBefore.Name {1124 if bp.Addr != addrBefore {1125 t.Fatalf("Address changed after restart: %x %x", bp.Addr, addrBefore)1126 }1127 t.Logf("%x %x\n", bp.Addr, addrBefore)1128 }1129 }1130 })1131}1132func TestClientServer_SelectedGoroutineLoc(t *testing.T) {1133 // CurrentLocation of SelectedGoroutine should reflect what's happening on1134 // the thread running the goroutine, not the position the goroutine was in1135 // the last time it was parked.1136 protest.AllowRecording(t)1137 withTestClient2("testprog", t, func(c service.Client) {1138 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: -11})1139 assertNoError(err, t, "CreateBreakpoint")1140 s := <-c.Continue()1141 assertNoError(s.Err, t, "Continue")1142 gloc := s.SelectedGoroutine.CurrentLoc1143 if gloc.PC != s.CurrentThread.PC {1144 t.Errorf("mismatched PC %#x %#x", gloc.PC, s.CurrentThread.PC)1145 }1146 if gloc.File != s.CurrentThread.File || gloc.Line != s.CurrentThread.Line {1147 t.Errorf("mismatched file:lineno: %s:%d %s:%d", gloc.File, gloc.Line, s.CurrentThread.File, s.CurrentThread.Line)1148 }1149 })1150}1151func TestClientServer_ReverseContinue(t *testing.T) {1152 protest.AllowRecording(t)1153 if testBackend != "rr" {1154 t.Skip("backend is not rr")1155 }1156 withTestClient2("continuetestprog", t, func(c service.Client) {1157 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: -1})1158 assertNoError(err, t, "CreateBreakpoint(main.main)")1159 _, err = c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: -1})1160 assertNoError(err, t, "CreateBreakpoint(main.sayhi)")1161 state := <-c.Continue()1162 assertNoError(state.Err, t, "first continue")1163 mainPC := state.CurrentThread.PC1164 t.Logf("after first continue %#x", mainPC)1165 state = <-c.Continue()1166 assertNoError(state.Err, t, "second continue")1167 sayhiPC := state.CurrentThread.PC1168 t.Logf("after second continue %#x", sayhiPC)1169 if mainPC == sayhiPC {1170 t.Fatalf("expected different PC after second PC (%#x)", mainPC)1171 }1172 state = <-c.Rewind()1173 assertNoError(state.Err, t, "rewind")1174 if mainPC != state.CurrentThread.PC {1175 t.Fatalf("Expected rewind to go back to the first breakpoint: %#x", state.CurrentThread.PC)1176 }1177 })1178}1179func TestClientServer_collectBreakpointInfoOnNext(t *testing.T) {1180 protest.AllowRecording(t)1181 withTestClient2("testnextprog", t, func(c service.Client) {1182 _, err := c.CreateBreakpoint(&api.Breakpoint{1183 Addr: findLocationHelper(t, c, "testnextprog.go:23", false, 1, 0)[0],1184 Variables: []string{"j"},1185 LoadLocals: &normalLoadConfig})1186 assertNoError(err, t, "CreateBreakpoint()")1187 _, err = c.CreateBreakpoint(&api.Breakpoint{1188 Addr: findLocationHelper(t, c, "testnextprog.go:24", false, 1, 0)[0],1189 Variables: []string{"j"},1190 LoadLocals: &normalLoadConfig})1191 assertNoError(err, t, "CreateBreakpoint()")1192 stateBefore := <-c.Continue()1193 assertNoError(stateBefore.Err, t, "Continue()")1194 if stateBefore.CurrentThread.Line != 23 {1195 t.Fatalf("wrong line number %s:%d, expected %d", stateBefore.CurrentThread.File, stateBefore.CurrentThread.Line, 23)1196 }1197 if bi := stateBefore.CurrentThread.BreakpointInfo; bi == nil || len(bi.Variables) != 1 {1198 t.Fatalf("bad breakpoint info %v", bi)1199 }1200 stateAfter, err := c.Next()1201 assertNoError(err, t, "Next()")1202 if stateAfter.CurrentThread.Line != 24 {1203 t.Fatalf("wrong line number %s:%d, expected %d", stateAfter.CurrentThread.File, stateAfter.CurrentThread.Line, 24)1204 }1205 if bi := stateAfter.CurrentThread.BreakpointInfo; bi == nil || len(bi.Variables) != 1 {1206 t.Fatalf("bad breakpoint info %v", bi)1207 }1208 })1209}1210func TestClientServer_collectBreakpointInfoError(t *testing.T) {1211 protest.AllowRecording(t)1212 withTestClient2("testnextprog", t, func(c service.Client) {1213 _, err := c.CreateBreakpoint(&api.Breakpoint{1214 Addr: findLocationHelper(t, c, "testnextprog.go:23", false, 1, 0)[0],1215 Variables: []string{"nonexistentvariable", "j"},1216 LoadLocals: &normalLoadConfig})1217 assertNoError(err, t, "CreateBreakpoint()")1218 state := <-c.Continue()1219 assertNoError(state.Err, t, "Continue()")1220 })1221}1222func TestClientServerConsistentExit(t *testing.T) {1223 // This test is useful because it ensures that Next and Continue operations both1224 // exit with the same exit status and details when the target application terminates.1225 // Other program execution API calls should also behave in the same way.1226 // An error should be present in state.Err.1227 withTestClient2("pr1055", t, func(c service.Client) {1228 fp := testProgPath(t, "pr1055")1229 _, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 12})1230 if err != nil {1231 t.Fatalf("Unexpected error: %v", err)1232 }1233 state := <-c.Continue()1234 if state.Err != nil {1235 t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)1236 }1237 state, err = c.Next()1238 if err != nil {1239 t.Fatalf("Unexpected error: %v", err)1240 }1241 if !state.Exited {1242 t.Fatal("Process state is not exited")1243 }1244 if state.ExitStatus != 2 {1245 t.Fatalf("Process exit status is not 2, got: %v", state.ExitStatus)1246 }1247 })1248}...

Full Screen

Full Screen

integration1_test.go

Source:integration1_test.go Github

copy

Full Screen

1package service_test2import (3 "fmt"4 "math/rand"5 "net"6 "path/filepath"7 "runtime"8 "strconv"9 "strings"10 "testing"11 "time"12 protest "github.com/derekparker/delve/pkg/proc/test"13 "github.com/derekparker/delve/pkg/goversion"14 "github.com/derekparker/delve/service"15 "github.com/derekparker/delve/service/api"16 "github.com/derekparker/delve/service/rpc1"17 "github.com/derekparker/delve/service/rpccommon"18)19func withTestClient1(name string, t *testing.T, fn func(c *rpc1.RPCClient)) {20 if testBackend == "rr" {21 protest.MustHaveRecordingAllowed(t)22 }23 listener, err := net.Listen("tcp", "localhost:0")24 if err != nil {25 t.Fatalf("couldn't start listener: %s\n", err)26 }27 defer listener.Close()28 server := rpccommon.NewServer(&service.Config{29 Listener: listener,30 ProcessArgs: []string{protest.BuildFixture(name, 0).Path},31 Backend: testBackend,32 }, false)33 if err := server.Run(); err != nil {34 t.Fatal(err)35 }36 client := rpc1.NewClient(listener.Addr().String())37 defer func() {38 client.Detach(true)39 }()40 fn(client)41}42func Test1RunWithInvalidPath(t *testing.T) {43 if testBackend == "rr" {44 // This test won't work because rr returns an error, after recording, when45 // the recording failed but also when the recording succeeded but the46 // inferior returned an error. Therefore we have to ignore errors from rr.47 return48 }49 listener, err := net.Listen("tcp", "localhost:0")50 if err != nil {51 t.Fatalf("couldn't start listener: %s\n", err)52 }53 defer listener.Close()54 server := rpccommon.NewServer(&service.Config{55 Listener: listener,56 ProcessArgs: []string{"invalid_path"},57 Backend: testBackend,58 }, false)59 if err := server.Run(); err == nil {60 t.Fatal("Expected Run to return error for invalid program path")61 }62}63func Test1Restart_afterExit(t *testing.T) {64 withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {65 origPid := c.ProcessPid()66 state := <-c.Continue()67 if !state.Exited {68 t.Fatal("expected initial process to have exited")69 }70 if err := c.Restart(); err != nil {71 t.Fatal(err)72 }73 if c.ProcessPid() == origPid {74 t.Fatal("did not spawn new process, has same PID")75 }76 state = <-c.Continue()77 if !state.Exited {78 t.Fatalf("expected restarted process to have exited %v", state)79 }80 })81}82func Test1Restart_breakpointPreservation(t *testing.T) {83 withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {84 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1, Name: "firstbreakpoint", Tracepoint: true})85 assertNoError(err, t, "CreateBreakpoint()")86 stateCh := c.Continue()87 state := <-stateCh88 if state.CurrentThread.Breakpoint.Name != "firstbreakpoint" || !state.CurrentThread.Breakpoint.Tracepoint {89 t.Fatalf("Wrong breakpoint: %#v\n", state.CurrentThread.Breakpoint)90 }91 state = <-stateCh92 if !state.Exited {93 t.Fatal("Did not exit after first tracepoint")94 }95 t.Log("Restart")96 c.Restart()97 stateCh = c.Continue()98 state = <-stateCh99 if state.CurrentThread.Breakpoint.Name != "firstbreakpoint" || !state.CurrentThread.Breakpoint.Tracepoint {100 t.Fatalf("Wrong breakpoint (after restart): %#v\n", state.CurrentThread.Breakpoint)101 }102 state = <-stateCh103 if !state.Exited {104 t.Fatal("Did not exit after first tracepoint (after restart)")105 }106 })107}108func Test1Restart_duringStop(t *testing.T) {109 withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {110 origPid := c.ProcessPid()111 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1})112 if err != nil {113 t.Fatal(err)114 }115 state := <-c.Continue()116 if state.CurrentThread.Breakpoint == nil {117 t.Fatal("did not hit breakpoint")118 }119 if err := c.Restart(); err != nil {120 t.Fatal(err)121 }122 if c.ProcessPid() == origPid {123 t.Fatal("did not spawn new process, has same PID")124 }125 bps, err := c.ListBreakpoints()126 if err != nil {127 t.Fatal(err)128 }129 if len(bps) == 0 {130 t.Fatal("breakpoints not preserved")131 }132 })133}134func Test1Restart_attachPid(t *testing.T) {135 // Assert it does not work and returns error.136 // We cannot restart a process we did not spawn.137 server := rpccommon.NewServer(&service.Config{138 Listener: nil,139 AttachPid: 999,140 Backend: testBackend,141 }, false)142 if err := server.Restart(); err == nil {143 t.Fatal("expected error on restart after attaching to pid but got none")144 }145}146func Test1ClientServer_exit(t *testing.T) {147 withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {148 state, err := c.GetState()149 if err != nil {150 t.Fatalf("Unexpected error: %v", err)151 }152 if e, a := false, state.Exited; e != a {153 t.Fatalf("Expected exited %v, got %v", e, a)154 }155 state = <-c.Continue()156 if state.Err == nil {157 t.Fatalf("Error expected after continue")158 }159 if !state.Exited {160 t.Fatalf("Expected exit after continue: %v", state)161 }162 _, err = c.GetState()163 if err == nil {164 t.Fatal("Expected error on querying state from exited process")165 }166 })167}168func Test1ClientServer_step(t *testing.T) {169 withTestClient1("testprog", t, func(c *rpc1.RPCClient) {170 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: -1})171 if err != nil {172 t.Fatalf("Unexpected error: %v", err)173 }174 stateBefore := <-c.Continue()175 if stateBefore.Err != nil {176 t.Fatalf("Unexpected error: %v", stateBefore.Err)177 }178 stateAfter, err := c.Step()179 if err != nil {180 t.Fatalf("Unexpected error: %v", err)181 }182 if before, after := stateBefore.CurrentThread.PC, stateAfter.CurrentThread.PC; before >= after {183 t.Errorf("Expected %#v to be greater than %#v", before, after)184 }185 })186}187func testnext(testcases []nextTest, initialLocation string, t *testing.T) {188 withTestClient1("testnextprog", t, func(c *rpc1.RPCClient) {189 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: initialLocation, Line: -1})190 if err != nil {191 t.Fatalf("Unexpected error: %v", err)192 }193 state := <-c.Continue()194 if state.Err != nil {195 t.Fatalf("Unexpected error: %v", state.Err)196 }197 _, err = c.ClearBreakpoint(bp.ID)198 if err != nil {199 t.Fatalf("Unexpected error: %v", err)200 }201 for _, tc := range testcases {202 if state.CurrentThread.Line != tc.begin {203 t.Fatalf("Program not stopped at correct spot expected %d was %d", tc.begin, state.CurrentThread.Line)204 }205 t.Logf("Next for scenario %#v", tc)206 state, err = c.Next()207 if err != nil {208 t.Fatalf("Unexpected error: %v", err)209 }210 if state.CurrentThread.Line != tc.end {211 t.Fatalf("Program did not continue to correct next location expected %d was %d", tc.end, state.CurrentThread.Line)212 }213 }214 })215}216func Test1NextGeneral(t *testing.T) {217 var testcases []nextTest218 ver, _ := goversion.Parse(runtime.Version())219 if ver.Major < 0 || ver.AfterOrEqual(goversion.GoVersion{1, 7, -1, 0, 0, ""}) {220 testcases = []nextTest{221 {17, 19},222 {19, 20},223 {20, 23},224 {23, 24},225 {24, 26},226 {26, 31},227 {31, 23},228 {23, 24},229 {24, 26},230 {26, 31},231 {31, 23},232 {23, 24},233 {24, 26},234 {26, 27},235 {27, 28},236 {28, 34},237 }238 } else {239 testcases = []nextTest{240 {17, 19},241 {19, 20},242 {20, 23},243 {23, 24},244 {24, 26},245 {26, 31},246 {31, 23},247 {23, 24},248 {24, 26},249 {26, 31},250 {31, 23},251 {23, 24},252 {24, 26},253 {26, 27},254 {27, 34},255 }256 }257 testnext(testcases, "main.testnext", t)258}259func Test1NextFunctionReturn(t *testing.T) {260 testcases := []nextTest{261 {13, 14},262 {14, 15},263 {15, 35},264 }265 testnext(testcases, "main.helloworld", t)266}267func Test1ClientServer_breakpointInMainThread(t *testing.T) {268 withTestClient1("testprog", t, func(c *rpc1.RPCClient) {269 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: 1})270 if err != nil {271 t.Fatalf("Unexpected error: %v", err)272 }273 state := <-c.Continue()274 if err != nil {275 t.Fatalf("Unexpected error: %v, state: %#v", err, state)276 }277 pc := state.CurrentThread.PC278 if pc-1 != bp.Addr && pc != bp.Addr {279 f, l := state.CurrentThread.File, state.CurrentThread.Line280 t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)281 }282 })283}284func Test1ClientServer_breakpointInSeparateGoroutine(t *testing.T) {285 withTestClient1("testthreads", t, func(c *rpc1.RPCClient) {286 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.anotherthread", Line: 1})287 if err != nil {288 t.Fatalf("Unexpected error: %v", err)289 }290 state := <-c.Continue()291 if state.Err != nil {292 t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)293 }294 f, l := state.CurrentThread.File, state.CurrentThread.Line295 if f != "testthreads.go" && l != 9 {296 t.Fatal("Program did not hit breakpoint")297 }298 })299}300func Test1ClientServer_breakAtNonexistentPoint(t *testing.T) {301 withTestClient1("testprog", t, func(c *rpc1.RPCClient) {302 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "nowhere", Line: 1})303 if err == nil {304 t.Fatal("Should not be able to break at non existent function")305 }306 })307}308func Test1ClientServer_clearBreakpoint(t *testing.T) {309 withTestClient1("testprog", t, func(c *rpc1.RPCClient) {310 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sleepytime", Line: 1})311 if err != nil {312 t.Fatalf("Unexpected error: %v", err)313 }314 if e, a := 1, countBreakpoints(t, c); e != a {315 t.Fatalf("Expected breakpoint count %d, got %d", e, a)316 }317 deleted, err := c.ClearBreakpoint(bp.ID)318 if err != nil {319 t.Fatalf("Unexpected error: %v", err)320 }321 if deleted.ID != bp.ID {322 t.Fatalf("Expected deleted breakpoint ID %v, got %v", bp.ID, deleted.ID)323 }324 if e, a := 0, countBreakpoints(t, c); e != a {325 t.Fatalf("Expected breakpoint count %d, got %d", e, a)326 }327 })328}329func Test1ClientServer_switchThread(t *testing.T) {330 withTestClient1("testnextprog", t, func(c *rpc1.RPCClient) {331 // With invalid thread id332 _, err := c.SwitchThread(-1)333 if err == nil {334 t.Fatal("Expected error for invalid thread id")335 }336 _, err = c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1})337 if err != nil {338 t.Fatalf("Unexpected error: %v", err)339 }340 state := <-c.Continue()341 if state.Err != nil {342 t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)343 }344 var nt int345 ct := state.CurrentThread.ID346 threads, err := c.ListThreads()347 if err != nil {348 t.Fatalf("Unexpected error: %v", err)349 }350 for _, th := range threads {351 if th.ID != ct {352 nt = th.ID353 break354 }355 }356 if nt == 0 {357 t.Fatal("could not find thread to switch to")358 }359 // With valid thread id360 state, err = c.SwitchThread(nt)361 if err != nil {362 t.Fatal(err)363 }364 if state.CurrentThread.ID != nt {365 t.Fatal("Did not switch threads")366 }367 })368}369func Test1ClientServer_infoLocals(t *testing.T) {370 withTestClient1("testnextprog", t, func(c *rpc1.RPCClient) {371 fp := testProgPath(t, "testnextprog")372 _, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 23})373 if err != nil {374 t.Fatalf("Unexpected error: %v", err)375 }376 state := <-c.Continue()377 if state.Err != nil {378 t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)379 }380 locals, err := c.ListLocalVariables(api.EvalScope{-1, 0})381 if err != nil {382 t.Fatalf("Unexpected error: %v", err)383 }384 if len(locals) != 3 {385 t.Fatalf("Expected 3 locals, got %d %#v", len(locals), locals)386 }387 })388}389func Test1ClientServer_infoArgs(t *testing.T) {390 withTestClient1("testnextprog", t, func(c *rpc1.RPCClient) {391 fp := testProgPath(t, "testnextprog")392 _, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 47})393 if err != nil {394 t.Fatalf("Unexpected error: %v", err)395 }396 state := <-c.Continue()397 if state.Err != nil {398 t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)399 }400 regs, err := c.ListRegisters()401 if err != nil {402 t.Fatalf("Unexpected error: %v", err)403 }404 if regs == "" {405 t.Fatal("Expected string showing registers values, got empty string")406 }407 locals, err := c.ListFunctionArgs(api.EvalScope{-1, 0})408 if err != nil {409 t.Fatalf("Unexpected error: %v", err)410 }411 if len(locals) != 2 {412 t.Fatalf("Expected 2 function args, got %d %#v", len(locals), locals)413 }414 })415}416func Test1ClientServer_traceContinue(t *testing.T) {417 withTestClient1("integrationprog", t, func(c *rpc1.RPCClient) {418 fp := testProgPath(t, "integrationprog")419 _, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 15, Tracepoint: true, Goroutine: true, Stacktrace: 5, Variables: []string{"i"}})420 if err != nil {421 t.Fatalf("Unexpected error: %v\n", err)422 }423 count := 0424 contChan := c.Continue()425 for state := range contChan {426 if state.CurrentThread != nil && state.CurrentThread.Breakpoint != nil {427 count++428 t.Logf("%v", state)429 bpi := state.CurrentThread.BreakpointInfo430 if bpi.Goroutine == nil {431 t.Fatalf("No goroutine information")432 }433 if len(bpi.Stacktrace) <= 0 {434 t.Fatalf("No stacktrace\n")435 }436 if len(bpi.Variables) != 1 {437 t.Fatalf("Wrong number of variables returned: %d", len(bpi.Variables))438 }439 if bpi.Variables[0].Name != "i" {440 t.Fatalf("Wrong variable returned %s", bpi.Variables[0].Name)441 }442 t.Logf("Variable i is %v", bpi.Variables[0])443 n, err := strconv.Atoi(bpi.Variables[0].Value)444 if err != nil || n != count-1 {445 t.Fatalf("Wrong variable value %q (%v %d)", bpi.Variables[0].Value, err, count)446 }447 }448 if state.Exited {449 continue450 }451 t.Logf("%v", state)452 if state.Err != nil {453 t.Fatalf("Unexpected error during continue: %v\n", state.Err)454 }455 }456 if count != 3 {457 t.Fatalf("Wrong number of continues hit: %d\n", count)458 }459 })460}461func Test1ClientServer_traceContinue2(t *testing.T) {462 withTestClient1("integrationprog", t, func(c *rpc1.RPCClient) {463 bp1, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1, Tracepoint: true})464 if err != nil {465 t.Fatalf("Unexpected error: %v\n", err)466 }467 bp2, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: 1, Tracepoint: true})468 if err != nil {469 t.Fatalf("Unexpected error: %v\n", err)470 }471 countMain := 0472 countSayhi := 0473 contChan := c.Continue()474 for state := range contChan {475 if state.CurrentThread != nil && state.CurrentThread.Breakpoint != nil {476 switch state.CurrentThread.Breakpoint.ID {477 case bp1.ID:478 countMain++479 case bp2.ID:480 countSayhi++481 }482 t.Logf("%v", state)483 }484 if state.Exited {485 continue486 }487 if state.Err != nil {488 t.Fatalf("Unexpected error during continue: %v\n", state.Err)489 }490 }491 if countMain != 1 {492 t.Fatalf("Wrong number of continues (main.main) hit: %d\n", countMain)493 }494 if countSayhi != 3 {495 t.Fatalf("Wrong number of continues (main.sayhi) hit: %d\n", countSayhi)496 }497 })498}499func Test1ClientServer_FindLocations(t *testing.T) {500 withTestClient1("locationsprog", t, func(c *rpc1.RPCClient) {501 someFunctionCallAddr := findLocationHelper(t, c, "locationsprog.go:26", false, 1, 0)[0]502 someFunctionLine1 := findLocationHelper(t, c, "locationsprog.go:27", false, 1, 0)[0]503 findLocationHelper(t, c, "anotherFunction:1", false, 1, someFunctionLine1)504 findLocationHelper(t, c, "main.anotherFunction:1", false, 1, someFunctionLine1)505 findLocationHelper(t, c, "anotherFunction", false, 1, someFunctionCallAddr)506 findLocationHelper(t, c, "main.anotherFunction", false, 1, someFunctionCallAddr)507 findLocationHelper(t, c, fmt.Sprintf("*0x%x", someFunctionCallAddr), false, 1, someFunctionCallAddr)508 findLocationHelper(t, c, "sprog.go:26", true, 0, 0)509 findLocationHelper(t, c, "String", true, 0, 0)510 findLocationHelper(t, c, "main.String", true, 0, 0)511 someTypeStringFuncAddr := findLocationHelper(t, c, "locationsprog.go:14", false, 1, 0)[0]512 otherTypeStringFuncAddr := findLocationHelper(t, c, "locationsprog.go:18", false, 1, 0)[0]513 findLocationHelper(t, c, "SomeType.String", false, 1, someTypeStringFuncAddr)514 findLocationHelper(t, c, "(*SomeType).String", false, 1, someTypeStringFuncAddr)515 findLocationHelper(t, c, "main.SomeType.String", false, 1, someTypeStringFuncAddr)516 findLocationHelper(t, c, "main.(*SomeType).String", false, 1, someTypeStringFuncAddr)517 // Issue #275518 readfile := findLocationHelper(t, c, "io/ioutil.ReadFile", false, 1, 0)[0]519 // Issue #296520 findLocationHelper(t, c, "/io/ioutil.ReadFile", false, 1, readfile)521 findLocationHelper(t, c, "ioutil.ReadFile", false, 1, readfile)522 stringAddrs := findLocationHelper(t, c, "/^main.*Type.*String$/", false, 2, 0)523 if otherTypeStringFuncAddr != stringAddrs[0] && otherTypeStringFuncAddr != stringAddrs[1] {524 t.Fatalf("Wrong locations returned for \"/.*Type.*String/\", got: %v expected: %v and %v\n", stringAddrs, someTypeStringFuncAddr, otherTypeStringFuncAddr)525 }526 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 4, Tracepoint: false})527 if err != nil {528 t.Fatalf("CreateBreakpoint(): %v\n", err)529 }530 <-c.Continue()531 locationsprog35Addr := findLocationHelper(t, c, "locationsprog.go:35", false, 1, 0)[0]532 findLocationHelper(t, c, fmt.Sprintf("%s:35", testProgPath(t, "locationsprog")), false, 1, locationsprog35Addr)533 findLocationHelper(t, c, "+1", false, 1, locationsprog35Addr)534 findLocationHelper(t, c, "35", false, 1, locationsprog35Addr)535 findLocationHelper(t, c, "-1", false, 1, findLocationHelper(t, c, "locationsprog.go:33", false, 1, 0)[0])536 })537 withTestClient1("testnextdefer", t, func(c *rpc1.RPCClient) {538 firstMainLine := findLocationHelper(t, c, "testnextdefer.go:5", false, 1, 0)[0]539 findLocationHelper(t, c, "main.main", false, 1, firstMainLine)540 })541 withTestClient1("stacktraceprog", t, func(c *rpc1.RPCClient) {542 stacktracemeAddr := findLocationHelper(t, c, "stacktraceprog.go:4", false, 1, 0)[0]543 findLocationHelper(t, c, "main.stacktraceme", false, 1, stacktracemeAddr)544 })545 withTestClient1("locationsUpperCase", t, func(c *rpc1.RPCClient) {546 // Upper case547 findLocationHelper(t, c, "locationsUpperCase.go:6", false, 1, 0)548 // Fully qualified path549 path := protest.Fixtures[protest.FixtureKey{"locationsUpperCase", 0}].Source550 findLocationHelper(t, c, path+":6", false, 1, 0)551 bp, err := c.CreateBreakpoint(&api.Breakpoint{File: path, Line: 6})552 if err != nil {553 t.Fatalf("Could not set breakpoint in %s: %v\n", path, err)554 }555 c.ClearBreakpoint(bp.ID)556 // Allow `/` or `\` on Windows557 if runtime.GOOS == "windows" {558 findLocationHelper(t, c, filepath.FromSlash(path)+":6", false, 1, 0)559 bp, err = c.CreateBreakpoint(&api.Breakpoint{File: filepath.FromSlash(path), Line: 6})560 if err != nil {561 t.Fatalf("Could not set breakpoint in %s: %v\n", filepath.FromSlash(path), err)562 }563 c.ClearBreakpoint(bp.ID)564 }565 // Case-insensitive on Windows, case-sensitive otherwise566 shouldWrongCaseBeError := true567 numExpectedMatches := 0568 if runtime.GOOS == "windows" {569 shouldWrongCaseBeError = false570 numExpectedMatches = 1571 }572 findLocationHelper(t, c, strings.ToLower(path)+":6", shouldWrongCaseBeError, numExpectedMatches, 0)573 bp, err = c.CreateBreakpoint(&api.Breakpoint{File: strings.ToLower(path), Line: 6})574 if (err == nil) == shouldWrongCaseBeError {575 t.Fatalf("Could not set breakpoint in %s: %v\n", strings.ToLower(path), err)576 }577 c.ClearBreakpoint(bp.ID)578 })579}580func Test1ClientServer_FindLocationsAddr(t *testing.T) {581 withTestClient1("locationsprog2", t, func(c *rpc1.RPCClient) {582 <-c.Continue()583 afunction := findLocationHelper(t, c, "main.afunction", false, 1, 0)[0]584 anonfunc := findLocationHelper(t, c, "main.main.func1", false, 1, 0)[0]585 findLocationHelper(t, c, "*fn1", false, 1, afunction)586 findLocationHelper(t, c, "*fn3", false, 1, anonfunc)587 })588}589func Test1ClientServer_EvalVariable(t *testing.T) {590 withTestClient1("testvariables", t, func(c *rpc1.RPCClient) {591 state := <-c.Continue()592 if state.Err != nil {593 t.Fatalf("Continue(): %v\n", state.Err)594 }595 var1, err := c.EvalVariable(api.EvalScope{-1, 0}, "a1")596 assertNoError(err, t, "EvalVariable")597 t.Logf("var1: %s", var1.SinglelineString())598 if var1.Value != "foofoofoofoofoofoo" {599 t.Fatalf("Wrong variable value: %s", var1.Value)600 }601 })602}603func Test1ClientServer_SetVariable(t *testing.T) {604 withTestClient1("testvariables", t, func(c *rpc1.RPCClient) {605 state := <-c.Continue()606 if state.Err != nil {607 t.Fatalf("Continue(): %v\n", state.Err)608 }609 assertNoError(c.SetVariable(api.EvalScope{-1, 0}, "a2", "8"), t, "SetVariable()")610 a2, err := c.EvalVariable(api.EvalScope{-1, 0}, "a2")611 if err != nil {612 t.Fatalf("Could not evaluate variable: %v", err)613 }614 t.Logf("a2: %v", a2)615 n, err := strconv.Atoi(a2.Value)616 if err != nil && n != 8 {617 t.Fatalf("Wrong variable value: %v", a2)618 }619 })620}621func Test1ClientServer_FullStacktrace(t *testing.T) {622 withTestClient1("goroutinestackprog", t, func(c *rpc1.RPCClient) {623 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.stacktraceme", Line: -1})624 assertNoError(err, t, "CreateBreakpoint()")625 state := <-c.Continue()626 if state.Err != nil {627 t.Fatalf("Continue(): %v\n", state.Err)628 }629 gs, err := c.ListGoroutines()630 assertNoError(err, t, "GoroutinesInfo()")631 found := make([]bool, 10)632 for _, g := range gs {633 frames, err := c.Stacktrace(g.ID, 10, true)634 assertNoError(err, t, fmt.Sprintf("Stacktrace(%d)", g.ID))635 for i, frame := range frames {636 if frame.Function == nil {637 continue638 }639 if frame.Function.Name != "main.agoroutine" {640 continue641 }642 t.Logf("frame %d: %v", i, frame)643 for _, arg := range frame.Arguments {644 if arg.Name != "i" {645 continue646 }647 t.Logf("frame %d, variable i is %v\n", i, arg)648 argn, err := strconv.Atoi(arg.Value)649 if err == nil {650 found[argn] = true651 }652 }653 }654 }655 for i := range found {656 if !found[i] {657 t.Fatalf("Goroutine %d not found", i)658 }659 }660 state = <-c.Continue()661 if state.Err != nil {662 t.Fatalf("Continue(): %v\n", state.Err)663 }664 frames, err := c.Stacktrace(-1, 10, true)665 assertNoError(err, t, "Stacktrace")666 cur := 3667 for i, frame := range frames {668 if i == 0 {669 continue670 }671 t.Logf("frame %d: %v", i, frame)672 v := frame.Var("n")673 if v == nil {674 t.Fatalf("Could not find value of variable n in frame %d", i)675 }676 vn, err := strconv.Atoi(v.Value)677 if err != nil || vn != cur {678 t.Fatalf("Expected value %d got %d (error: %v)", cur, vn, err)679 }680 cur--681 if cur < 0 {682 break683 }684 }685 })686}687func Test1Issue355(t *testing.T) {688 // After the target process has terminated should return an error but not crash689 withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {690 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: -1})691 assertNoError(err, t, "CreateBreakpoint()")692 ch := c.Continue()693 state := <-ch694 tid := state.CurrentThread.ID695 gid := state.SelectedGoroutine.ID696 assertNoError(state.Err, t, "First Continue()")697 ch = c.Continue()698 state = <-ch699 if !state.Exited {700 t.Fatalf("Target did not terminate after second continue")701 }702 ch = c.Continue()703 state = <-ch704 assertError(state.Err, t, "Continue()")705 _, err = c.Next()706 assertError(err, t, "Next()")707 _, err = c.Step()708 assertError(err, t, "Step()")709 _, err = c.StepInstruction()710 assertError(err, t, "StepInstruction()")711 _, err = c.SwitchThread(tid)712 assertError(err, t, "SwitchThread()")713 _, err = c.SwitchGoroutine(gid)714 assertError(err, t, "SwitchGoroutine()")715 _, err = c.Halt()716 assertError(err, t, "Halt()")717 _, err = c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: -1})718 assertError(err, t, "CreateBreakpoint()")719 _, err = c.ClearBreakpoint(bp.ID)720 assertError(err, t, "ClearBreakpoint()")721 _, err = c.ListThreads()722 assertError(err, t, "ListThreads()")723 _, err = c.GetThread(tid)724 assertError(err, t, "GetThread()")725 assertError(c.SetVariable(api.EvalScope{gid, 0}, "a", "10"), t, "SetVariable()")726 _, err = c.ListLocalVariables(api.EvalScope{gid, 0})727 assertError(err, t, "ListLocalVariables()")728 _, err = c.ListFunctionArgs(api.EvalScope{gid, 0})729 assertError(err, t, "ListFunctionArgs()")730 _, err = c.ListRegisters()731 assertError(err, t, "ListRegisters()")732 _, err = c.ListGoroutines()733 assertError(err, t, "ListGoroutines()")734 _, err = c.Stacktrace(gid, 10, false)735 assertError(err, t, "Stacktrace()")736 _, err = c.FindLocation(api.EvalScope{gid, 0}, "+1")737 assertError(err, t, "FindLocation()")738 _, err = c.DisassemblePC(api.EvalScope{-1, 0}, 0x40100, api.IntelFlavour)739 assertError(err, t, "DisassemblePC()")740 })741}742func Test1Disasm(t *testing.T) {743 // Tests that disassembling by PC, range, and current PC all yeld similar results744 // Tests that disassembly by current PC will return a disassembly containing the instruction at PC745 // Tests that stepping on a calculated CALL instruction will yield a disassembly that contains the746 // effective destination of the CALL instruction747 withTestClient1("locationsprog2", t, func(c *rpc1.RPCClient) {748 ch := c.Continue()749 state := <-ch750 assertNoError(state.Err, t, "Continue()")751 locs, err := c.FindLocation(api.EvalScope{-1, 0}, "main.main")752 assertNoError(err, t, "FindLocation()")753 if len(locs) != 1 {754 t.Fatalf("wrong number of locations for main.main: %d", len(locs))755 }756 d1, err := c.DisassemblePC(api.EvalScope{-1, 0}, locs[0].PC, api.IntelFlavour)757 assertNoError(err, t, "DisassemblePC()")758 if len(d1) < 2 {759 t.Fatalf("wrong size of disassembly: %d", len(d1))760 }761 pcstart := d1[0].Loc.PC762 pcend := d1[len(d1)-1].Loc.PC + uint64(len(d1[len(d1)-1].Bytes))763 d2, err := c.DisassembleRange(api.EvalScope{-1, 0}, pcstart, pcend, api.IntelFlavour)764 assertNoError(err, t, "DisassembleRange()")765 if len(d1) != len(d2) {766 t.Logf("d1: %v", d1)767 t.Logf("d2: %v", d2)768 t.Fatal("mismatched length between disassemble pc and disassemble range")769 }770 d3, err := c.DisassemblePC(api.EvalScope{-1, 0}, state.CurrentThread.PC, api.IntelFlavour)771 assertNoError(err, t, "DisassemblePC() - second call")772 if len(d1) != len(d3) {773 t.Logf("d1: %v", d1)774 t.Logf("d3: %v", d3)775 t.Fatal("mismatched length between the two calls of disassemble pc")776 }777 // look for static call to afunction() on line 29778 found := false779 for i := range d3 {780 if d3[i].Loc.Line == 29 && strings.HasPrefix(d3[i].Text, "call") && d3[i].DestLoc != nil && d3[i].DestLoc.Function != nil && d3[i].DestLoc.Function.Name == "main.afunction" {781 found = true782 break783 }784 }785 if !found {786 t.Fatal("Could not find call to main.afunction on line 29")787 }788 haspc := false789 for i := range d3 {790 if d3[i].AtPC {791 haspc = true792 break793 }794 }795 if !haspc {796 t.Logf("d3: %v", d3)797 t.Fatal("PC instruction not found")798 }799 startinstr := getCurinstr(d3)800 count := 0801 for {802 if count > 20 {803 t.Fatal("too many step instructions executed without finding a call instruction")804 }805 state, err := c.StepInstruction()806 assertNoError(err, t, fmt.Sprintf("StepInstruction() %d", count))807 d3, err = c.DisassemblePC(api.EvalScope{-1, 0}, state.CurrentThread.PC, api.IntelFlavour)808 assertNoError(err, t, fmt.Sprintf("StepInstruction() %d", count))809 curinstr := getCurinstr(d3)810 if curinstr == nil {811 t.Fatalf("Could not find current instruction %d", count)812 }813 if curinstr.Loc.Line != startinstr.Loc.Line {814 t.Fatal("Calling StepInstruction() repeatedly did not find the call instruction")815 }816 if strings.HasPrefix(curinstr.Text, "call") {817 t.Logf("call: %v", curinstr)818 if curinstr.DestLoc == nil || curinstr.DestLoc.Function == nil {819 t.Fatalf("Call instruction does not have destination: %v", curinstr)820 }821 if curinstr.DestLoc.Function.Name != "main.afunction" {822 t.Fatalf("Call instruction destination not main.afunction: %v", curinstr)823 }824 break825 }826 count++827 }828 })829}830func Test1NegativeStackDepthBug(t *testing.T) {831 // After the target process has terminated should return an error but not crash832 withTestClient1("continuetestprog", t, func(c *rpc1.RPCClient) {833 _, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: -1})834 assertNoError(err, t, "CreateBreakpoint()")835 ch := c.Continue()836 state := <-ch837 assertNoError(state.Err, t, "Continue()")838 _, err = c.Stacktrace(-1, -2, true)839 assertError(err, t, "Stacktrace()")840 })841}842func Test1ClientServer_CondBreakpoint(t *testing.T) {843 withTestClient1("parallel_next", t, func(c *rpc1.RPCClient) {844 bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: 1})845 assertNoError(err, t, "CreateBreakpoint()")846 bp.Cond = "n == 7"847 assertNoError(c.AmendBreakpoint(bp), t, "AmendBreakpoint() 1")848 bp, err = c.GetBreakpoint(bp.ID)849 assertNoError(err, t, "GetBreakpoint() 1")850 bp.Variables = append(bp.Variables, "n")851 assertNoError(c.AmendBreakpoint(bp), t, "AmendBreakpoint() 2")852 bp, err = c.GetBreakpoint(bp.ID)853 assertNoError(err, t, "GetBreakpoint() 2")854 if bp.Cond == "" {855 t.Fatalf("No condition set on breakpoint %#v", bp)856 }857 if len(bp.Variables) != 1 {858 t.Fatalf("Wrong number of expressions to evaluate on breakpoint %#v", bp)859 }860 state := <-c.Continue()861 assertNoError(state.Err, t, "Continue()")862 nvar, err := c.EvalVariable(api.EvalScope{-1, 0}, "n")863 assertNoError(err, t, "EvalVariable()")864 if nvar.SinglelineString() != "7" {865 t.Fatalf("Stopped on wrong goroutine %s\n", nvar.Value)866 }867 })868}869func Test1SkipPrologue(t *testing.T) {870 withTestClient1("locationsprog2", t, func(c *rpc1.RPCClient) {871 <-c.Continue()872 afunction := findLocationHelper(t, c, "main.afunction", false, 1, 0)[0]873 findLocationHelper(t, c, "*fn1", false, 1, afunction)874 findLocationHelper(t, c, "locationsprog2.go:8", false, 1, afunction)875 afunction0 := findLocationHelper(t, c, "main.afunction:0", false, 1, 0)[0]876 if afunction == afunction0 {877 t.Fatal("Skip prologue failed")878 }879 })880}881func Test1SkipPrologue2(t *testing.T) {882 withTestClient1("callme", t, func(c *rpc1.RPCClient) {883 callme := findLocationHelper(t, c, "main.callme", false, 1, 0)[0]884 callmeZ := findLocationHelper(t, c, "main.callme:0", false, 1, 0)[0]885 findLocationHelper(t, c, "callme.go:5", false, 1, callme)886 if callme == callmeZ {887 t.Fatal("Skip prologue failed")888 }889 callme2 := findLocationHelper(t, c, "main.callme2", false, 1, 0)[0]890 callme2Z := findLocationHelper(t, c, "main.callme2:0", false, 1, 0)[0]891 findLocationHelper(t, c, "callme.go:12", false, 1, callme2)892 if callme2 == callme2Z {893 t.Fatal("Skip prologue failed")894 }895 callme3 := findLocationHelper(t, c, "main.callme3", false, 1, 0)[0]896 callme3Z := findLocationHelper(t, c, "main.callme3:0", false, 1, 0)[0]897 ver, _ := goversion.Parse(runtime.Version())898 if ver.Major < 0 || ver.AfterOrEqual(goversion.GoVer18Beta) {899 findLocationHelper(t, c, "callme.go:19", false, 1, callme3)900 } else {901 // callme3 does not have local variables therefore the first line of the902 // function is immediately after the prologue903 // This is only true before 1.8 where frame pointer chaining introduced a904 // bit of prologue even for functions without local variables905 findLocationHelper(t, c, "callme.go:19", false, 1, callme3Z)906 }907 if callme3 == callme3Z {908 t.Fatal("Skip prologue failed")909 }910 })911}912func Test1Issue419(t *testing.T) {913 // Calling service/rpc.(*Client).Halt could cause a crash because both Halt and Continue simultaneously914 // try to read 'runtime.g' and debug/dwarf.Data.Type is not thread safe915 withTestClient1("issue419", t, func(c *rpc1.RPCClient) {916 go func() {917 rand.Seed(time.Now().Unix())918 d := time.Duration(rand.Intn(4) + 1)919 time.Sleep(d * time.Second)920 _, err := c.Halt()921 assertNoError(err, t, "RequestManualStop()")922 }()923 statech := c.Continue()924 state := <-statech925 assertNoError(state.Err, t, "Continue()")926 })927}928func Test1TypesCommand(t *testing.T) {929 withTestClient1("testvariables2", t, func(c *rpc1.RPCClient) {930 state := <-c.Continue()931 assertNoError(state.Err, t, "Continue()")932 types, err := c.ListTypes("")933 assertNoError(err, t, "ListTypes()")934 found := false935 for i := range types {936 if types[i] == "main.astruct" {937 found = true938 break939 }940 }941 if !found {942 t.Fatal("Type astruct not found in ListTypes output")943 }944 types, err = c.ListTypes("^main.astruct$")945 assertNoError(err, t, "ListTypes(\"main.astruct\")")946 if len(types) != 1 {947 t.Fatalf("ListTypes(\"^main.astruct$\") did not filter properly, expected 1 got %d: %v", len(types), types)948 }949 })950}951func Test1Issue406(t *testing.T) {952 withTestClient1("issue406", t, func(c *rpc1.RPCClient) {953 locs, err := c.FindLocation(api.EvalScope{-1, 0}, "issue406.go:146")954 assertNoError(err, t, "FindLocation()")955 _, err = c.CreateBreakpoint(&api.Breakpoint{Addr: locs[0].PC})956 assertNoError(err, t, "CreateBreakpoint()")957 ch := c.Continue()958 state := <-ch959 assertNoError(state.Err, t, "Continue()")960 v, err := c.EvalVariable(api.EvalScope{-1, 0}, "cfgtree")961 assertNoError(err, t, "EvalVariable()")962 vs := v.MultilineString("")963 t.Logf("cfgtree formats to: %s\n", vs)964 })965}...

Full Screen

Full Screen

config_test.go

Source:config_test.go Github

copy

Full Screen

1package main_test2import (3 "bytes"4 "reflect"5 "strings"6 "testing"7 "time"8 "github.com/BurntSushi/toml"9 main "github.com/influxdb/influxdb/cmd/influxd"10)11// Testing configuration file.12const testFile = `13# Welcome to the InfluxDB configuration file.14# If hostname (on the OS) doesn't return a name that can be resolved by the other15# systems in the cluster, you'll have to set the hostname to an IP or something16# that can be resolved here.17hostname = "myserver.com"18port = 808619# Control authentication20[authentication]21enabled = true22[logging]23write-tracing = true24raft-tracing = true25[monitoring]26enabled = true27write-interval = "1m"28# Configure the admin server29[admin]30enabled = true31port = 808332# Controls certain parameters that only take effect until an initial successful33# start-up has occurred.34[initialization]35join-urls = "http://127.0.0.1:8086"36# Configure the http api37[api]38bind-address = "10.1.2.3"39ssl-port = 8087 # Ssl support is enabled if you set a port and cert40ssl-cert = "../cert.pem"41# connections will timeout after this amount of time. Ensures that clients that misbehave42# and keep alive connections they don't use won't end up connection a million times.43# However, if a request is taking longer than this to complete, could be a problem.44read-timeout = "5s"45[input_plugins]46 [input_plugins.udp]47 enabled = true48 port = 444449 database = "test"50# Configure the Graphite servers51[[graphite]]52protocol = "TCP"53enabled = true54bind-address = "192.168.0.1"55port = 200356database = "graphite_tcp" # store graphite data in this database57name-position = "last"58name-separator = "-"59[[graphite]]60protocol = "udP"61enabled = true62bind-address = "192.168.0.2"63port = 200564# Configure collectd server65[collectd]66enabled = true67bind-address = "192.168.0.3"68port = 2582769database = "collectd_database"70typesdb = "foo-db-type"71# Configure OpenTSDB server72[opentsdb]73enabled = true74address = "192.168.0.3"75port = 424276database = "opentsdb_database"77retention-policy = "raw"78# Broker configuration79[broker]80# The broker port should be open between all servers in a cluster.81# However, this port shouldn't be accessible from the internet.82enabled = false83# Where the broker logs are stored. The user running InfluxDB will need read/write access.84dir = "/tmp/influxdb/development/broker"85# Raft distributed consensus86[raft]87apply-interval = "10ms"88election-timeout = "1s"89[data]90dir = "/tmp/influxdb/development/db"91retention-auto-create = false92retention-check-enabled = true93retention-check-period = "5m"94enabled = false95[continuous_queries]96disabled = true97[snapshot]98enabled = true99`100// Ensure that megabyte sizes can be parsed.101func TestSize_UnmarshalText_MB(t *testing.T) {102 var s main.Size103 if err := s.UnmarshalText([]byte("200m")); err != nil {104 t.Fatalf("unexpected error: %s", err)105 } else if s != 200*(1<<20) {106 t.Fatalf("unexpected size: %d", s)107 }108}109// Ensure that gigabyte sizes can be parsed.110func TestSize_UnmarshalText_GB(t *testing.T) {111 if typ := reflect.TypeOf(0); typ.Size() != 8 {112 t.Skip("large gigabyte parsing on 64-bit arch only")113 }114 var s main.Size115 if err := s.UnmarshalText([]byte("10g")); err != nil {116 t.Fatalf("unexpected error: %s", err)117 } else if s != 10*(1<<30) {118 t.Fatalf("unexpected size: %d", s)119 }120}121// Ensure that a TOML configuration file can be parsed into a Config.122func TestParseConfig(t *testing.T) {123 c, err := main.ParseConfig(testFile)124 if err != nil {125 t.Fatalf("unexpected error: %s", err)126 } else if c.Hostname != "myserver.com" {127 t.Fatalf("hostname mismatch: %v", c.Hostname)128 }129 if exp := 8086; c.Port != exp {130 t.Fatalf("port mismatch. got %v, exp %v", c.Port, exp)131 }132 if c.Initialization.JoinURLs != "http://127.0.0.1:8086" {133 t.Fatalf("JoinURLs mistmatch: %v", c.Initialization.JoinURLs)134 }135 if !c.Authentication.Enabled {136 t.Fatalf("authentication enabled mismatch: %v", c.Authentication.Enabled)137 }138 if exp := "10.1.2.3"; c.HTTPAPI.BindAddress != exp {139 t.Fatalf("http api bind-address mismatch: got %v, exp %v", c.HTTPAPI.BindAddress, exp)140 }141 if c.UDP.Enabled {142 t.Fatalf("udp enabled mismatch: %v", c.UDP.Enabled)143 }144 if c.Admin.Enabled != true {145 t.Fatalf("admin enabled mismatch: %v", c.Admin.Enabled)146 }147 if c.Admin.Port != 8083 {148 t.Fatalf("admin port mismatch: %v", c.Admin.Port)149 }150 if c.ContinuousQuery.Disabled != true {151 t.Fatalf("continuous query disable mismatch: %v", c.ContinuousQuery.Disabled)152 }153 if len(c.Graphites) != 2 {154 t.Fatalf("graphites mismatch. expected %v, got: %v", 2, len(c.Graphites))155 }156 tcpGraphite := c.Graphites[0]157 switch {158 case tcpGraphite.Enabled != true:159 t.Fatalf("graphite tcp enabled mismatch: expected: %v, got %v", true, tcpGraphite.Enabled)160 case tcpGraphite.BindAddress != "192.168.0.1":161 t.Fatalf("graphite tcp address mismatch: expected %v, got %v", "192.168.0.1", tcpGraphite.BindAddress)162 case tcpGraphite.Port != 2003:163 t.Fatalf("graphite tcp port mismatch: expected %v, got %v", 2003, tcpGraphite.Port)164 case tcpGraphite.Database != "graphite_tcp":165 t.Fatalf("graphite tcp database mismatch: expected %v, got %v", "graphite_tcp", tcpGraphite.Database)166 case strings.ToLower(tcpGraphite.Protocol) != "tcp":167 t.Fatalf("graphite tcp protocol mismatch: expected %v, got %v", "tcp", strings.ToLower(tcpGraphite.Protocol))168 case tcpGraphite.LastEnabled() != true:169 t.Fatalf("graphite tcp name-position mismatch: expected %v, got %v", "last", tcpGraphite.NamePosition)170 case tcpGraphite.NameSeparatorString() != "-":171 t.Fatalf("graphite tcp name-separator mismatch: expected %v, got %v", "-", tcpGraphite.NameSeparatorString())172 }173 udpGraphite := c.Graphites[1]174 switch {175 case udpGraphite.Enabled != true:176 t.Fatalf("graphite udp enabled mismatch: expected: %v, got %v", true, udpGraphite.Enabled)177 case udpGraphite.BindAddress != "192.168.0.2":178 t.Fatalf("graphite udp address mismatch: expected %v, got %v", "192.168.0.2", udpGraphite.BindAddress)179 case udpGraphite.Port != 2005:180 t.Fatalf("graphite udp port mismatch: expected %v, got %v", 2005, udpGraphite.Port)181 case udpGraphite.DatabaseString() != "graphite":182 t.Fatalf("graphite database mismatch: expected %v, got %v", "graphite", udpGraphite.Database)183 case strings.ToLower(udpGraphite.Protocol) != "udp":184 t.Fatalf("graphite udp protocol mismatch: expected %v, got %v", "udp", strings.ToLower(udpGraphite.Protocol))185 }186 switch {187 case c.Collectd.Enabled != true:188 t.Errorf("collectd enabled mismatch: expected: %v, got %v", true, c.Collectd.Enabled)189 case c.Collectd.BindAddress != "192.168.0.3":190 t.Errorf("collectd address mismatch: expected %v, got %v", "192.168.0.3", c.Collectd.BindAddress)191 case c.Collectd.Port != 25827:192 t.Errorf("collectd port mismatch: expected %v, got %v", 2005, c.Collectd.Port)193 case c.Collectd.Database != "collectd_database":194 t.Errorf("collectdabase mismatch: expected %v, got %v", "collectd_database", c.Collectd.Database)195 case c.Collectd.TypesDB != "foo-db-type":196 t.Errorf("collectd typesdb mismatch: expected %v, got %v", "foo-db-type", c.Collectd.TypesDB)197 }198 switch {199 case c.OpenTSDB.Enabled != true:200 t.Errorf("opentsdb enabled mismatch: expected: %v, got %v", true, c.OpenTSDB.Enabled)201 case c.OpenTSDB.ListenAddress() != "192.168.0.3:4242":202 t.Errorf("opentsdb listen address mismatch: expected %v, got %v", "192.168.0.3:4242", c.OpenTSDB.ListenAddress())203 case c.OpenTSDB.DatabaseString() != "opentsdb_database":204 t.Errorf("opentsdb database mismatch: expected %v, got %v", "opentsdb_database", c.OpenTSDB.DatabaseString())205 case c.OpenTSDB.RetentionPolicy != "raw":206 t.Errorf("collectd retention-policy mismatch: expected %v, got %v", "foo-db-type", c.OpenTSDB.RetentionPolicy)207 }208 if c.Broker.Dir != "/tmp/influxdb/development/broker" {209 t.Fatalf("broker dir mismatch: %v", c.Broker.Dir)210 }211 if c.Broker.Enabled != false {212 t.Fatalf("broker disabled mismatch: %v, got: %v", false, c.Broker.Enabled)213 }214 if c.Raft.ApplyInterval != main.Duration(10*time.Millisecond) {215 t.Fatalf("Raft apply interval mismatch: %v, got %v", 10*time.Millisecond, c.Raft.ApplyInterval)216 }217 if c.Data.Dir != "/tmp/influxdb/development/db" {218 t.Fatalf("data dir mismatch: %v", c.Data.Dir)219 }220 if c.Data.RetentionCheckEnabled != true {221 t.Fatalf("Retention check enabled mismatch: %v", c.Data.RetentionCheckEnabled)222 }223 if c.Data.RetentionCheckPeriod != main.Duration(5*time.Minute) {224 t.Fatalf("Retention check period mismatch: %v", c.Data.RetentionCheckPeriod)225 }226 if c.Data.Enabled != false {227 t.Fatalf("data disabled mismatch: %v, got: %v", false, c.Data.Enabled)228 }229 if c.Monitoring.WriteInterval.String() != "1m0s" {230 t.Fatalf("Monitoring.WriteInterval mismatch: %v", c.Monitoring.WriteInterval)231 }232 if !c.Snapshot.Enabled {233 t.Fatalf("snapshot enabled mismatch: %v, got %v", true, c.Snapshot.Enabled)234 }235 // TODO: UDP Servers testing.236 /*237 c.Assert(config.UdpServers, HasLen, 1)238 c.Assert(config.UdpServers[0].Enabled, Equals, true)239 c.Assert(config.UdpServers[0].Port, Equals, 4444)240 c.Assert(config.UdpServers[0].Database, Equals, "test")241 */242}243func TestEncodeConfig(t *testing.T) {244 c := main.Config{}245 c.Monitoring.WriteInterval = main.Duration(time.Minute)246 buf := new(bytes.Buffer)247 if err := toml.NewEncoder(buf).Encode(&c); err != nil {248 t.Fatal("Failed to encode: ", err)249 }250 got, search := buf.String(), `write-interval = "1m0s"`251 if !strings.Contains(got, search) {252 t.Fatalf("Encoding config failed.\nfailed to find %s in:\n%s\n", search, got)253 }254}255func TestCollectd_ConnectionString(t *testing.T) {256 var tests = []struct {257 name string258 defaultBindAddr string259 connectionString string260 config main.Collectd261 }{262 {263 name: "No address or port provided from config",264 defaultBindAddr: "192.168.0.1",265 connectionString: "192.168.0.1:25826",266 config: main.Collectd{},267 },268 {269 name: "address provided, no port provided from config",270 defaultBindAddr: "192.168.0.1",271 connectionString: "192.168.0.2:25826",272 config: main.Collectd{BindAddress: "192.168.0.2"},273 },274 {275 name: "no address provided, port provided from config",276 defaultBindAddr: "192.168.0.1",277 connectionString: "192.168.0.1:25827",278 config: main.Collectd{Port: 25827},279 },280 {281 name: "both address and port provided from config",282 defaultBindAddr: "192.168.0.1",283 connectionString: "192.168.0.2:25827",284 config: main.Collectd{BindAddress: "192.168.0.2", Port: 25827},285 },286 }287 for _, test := range tests {288 t.Logf("test: %q", test.name)289 s := test.config.ConnectionString(test.defaultBindAddr)290 if s != test.connectionString {291 t.Errorf("connection string mismatch, expected: %q, got: %q", test.connectionString, s)292 }293 }294}...

Full Screen

Full Screen

fatalf

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4}5import (6func main() {7 fmt.Println("Hello, playground")8}9import (10func main() {11 fmt.Println("Hello, playground")12}13import (14func main() {15 fmt.Println("Hello, playground")16}17import (18func main() {19 fmt.Println("Hello, playground")20}21import (22func main() {23 fmt.Println("Hello, playground")24}25import (26func main() {27 fmt.Println("Hello, playground")28}29import (30func main() {31 fmt.Println("Hello, playground")32}33import (34func main() {35 fmt.Println("Hello, playground")36}37import (38func main() {39 fmt.Println("Hello, playground")40}41import (42func main() {43 fmt.Println("Hello, playground")44}45import (46func main() {47 fmt.Println("Hello, playground")48}49import (50func main() {51 fmt.Println("Hello, playground")52}

Full Screen

Full Screen

fatalf

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Printf("Value of c : %d4}5main.main()6import "fmt"7func main() {8 defer func() {9 if r := recover(); r != nil {10 fmt.Println("Recovered from panic")11 }12 }()13 fmt.Printf("Value of c : %d14}15import "fmt"16func main() {17 defer func() {18 if r := recover(); r != nil {19 fmt.Println("Recovered from panic")20 }21 }()22 fmt.Printf("Value of c : %d23 panic("This is a custom panic")24}25main.main()

Full Screen

Full Screen

fatalf

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Printf("Hello World")4 fmt.Print("Hello World")5 fmt.Println("Hello World")6}7import "fmt"8func main() {9 fmt.Println("value of a is:", a)10}11import "fmt"12func main() {13 fmt.Println("value of a is:", a)14}15import "fmt"

Full Screen

Full Screen

fatalf

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4func init() {5 fmt.Printf("init6}7import (8func main() {9}10func init() {11 fmt.Printf("init12}13func init() {14 fmt.Printf("init215}16import (17func main() {18}19func init() {20 fmt.Printf("init21}22func init() {23 fmt.Printf("init224}25func init() {26 fmt.Printf("init327}28import (29func main() {30}31func init() {32 fmt.Printf("init33}34func init() {35 fmt.Printf("init236}37func init() {38 fmt.Printf("init339}40func init() {41 fmt.Printf("init442}43import (44func main() {45}46func init() {47 fmt.Printf("init48}49func init() {50 fmt.Printf("init251}52func init() {53 fmt.Printf("init354}55func init() {56 fmt.Printf("init457}58func init() {59 fmt.Printf("init560}61import (62func main() {63}64func init() {

Full Screen

Full Screen

fatalf

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 log.Fatalln("This is a fatal message")4 fmt.Println("This is a message after fatal")5}6import (7func main() {8 log.Panicln("This is a panic message")9 fmt.Println("This is a message after panic")10}11log.Panicln(0xc0000a3f30, 0x1, 0x1)12main.main()13log.Panicln(0xc0000a3f30, 0x1, 0x1)14main.main()15import (16func main() {17 log.Panicln("This is a panic message")18 fmt.Println("This is a message

Full Screen

Full Screen

fatalf

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 log.Fatalf("This is an error message")4 fmt.Println("This line will not be executed")5}6import (7func main() {8 log.Fatalln("This is an error message")9 fmt.Println("This line will not be executed")10}11import (12func main() {13 log.Panic("This is an error message")14 fmt.Println("This line will not be executed")15}16import (17func main() {18 log.Panicf("This is an error message")

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful