github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/cmd/nm/nm_test.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "bufio" 9 "bytes" 10 "fmt" 11 "io/ioutil" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "runtime" 16 "strings" 17 "testing" 18 ) 19 20 var testData uint32 21 22 func checkSymbols(t *testing.T, nmoutput []byte) { 23 var checkSymbolsFound, testDataFound bool 24 scanner := bufio.NewScanner(bytes.NewBuffer(nmoutput)) 25 for scanner.Scan() { 26 f := strings.Fields(scanner.Text()) 27 if len(f) < 3 { 28 continue 29 } 30 switch f[2] { 31 case "cmd/nm.checkSymbols": 32 checkSymbolsFound = true 33 addr := "0x" + f[0] 34 if addr != fmt.Sprintf("%p", checkSymbols) { 35 t.Errorf("nm shows wrong address %v for checkSymbols (%p)", addr, checkSymbols) 36 } 37 case "cmd/nm.testData": 38 testDataFound = true 39 addr := "0x" + f[0] 40 if addr != fmt.Sprintf("%p", &testData) { 41 t.Errorf("nm shows wrong address %v for testData (%p)", addr, &testData) 42 } 43 } 44 } 45 if err := scanner.Err(); err != nil { 46 t.Errorf("error while reading symbols: %v", err) 47 return 48 } 49 if !checkSymbolsFound { 50 t.Error("nm shows no checkSymbols symbol") 51 } 52 if !testDataFound { 53 t.Error("nm shows no testData symbol") 54 } 55 } 56 57 func TestNM(t *testing.T) { 58 switch runtime.GOOS { 59 case "android", "nacl": 60 t.Skipf("skipping on %s", runtime.GOOS) 61 } 62 63 tmpDir, err := ioutil.TempDir("", "TestNM") 64 if err != nil { 65 t.Fatal("TempDir failed: ", err) 66 } 67 defer os.RemoveAll(tmpDir) 68 69 testnmpath := filepath.Join(tmpDir, "testnm.exe") 70 out, err := exec.Command("go", "build", "-o", testnmpath, "cmd/nm").CombinedOutput() 71 if err != nil { 72 t.Fatalf("go build -o %v cmd/nm: %v\n%s", testnmpath, err, string(out)) 73 } 74 75 testfiles := []string{ 76 "elf/testdata/gcc-386-freebsd-exec", 77 "elf/testdata/gcc-amd64-linux-exec", 78 "macho/testdata/gcc-386-darwin-exec", 79 "macho/testdata/gcc-amd64-darwin-exec", 80 // "pe/testdata/gcc-amd64-mingw-exec", // no symbols! 81 "pe/testdata/gcc-386-mingw-exec", 82 "plan9obj/testdata/amd64-plan9-exec", 83 "plan9obj/testdata/386-plan9-exec", 84 } 85 for _, f := range testfiles { 86 exepath := filepath.Join(runtime.GOROOT(), "src", "debug", f) 87 cmd := exec.Command(testnmpath, exepath) 88 out, err := cmd.CombinedOutput() 89 if err != nil { 90 t.Errorf("go tool nm %v: %v\n%s", exepath, err, string(out)) 91 } 92 } 93 94 cmd := exec.Command(testnmpath, os.Args[0]) 95 out, err = cmd.CombinedOutput() 96 if err != nil { 97 t.Fatalf("go tool nm %v: %v\n%s", os.Args[0], err, string(out)) 98 } 99 checkSymbols(t, out) 100 }