github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/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  	if runtime.GOOS == "nacl" {
    59  		t.Skip("skipping on nacl")
    60  	}
    61  
    62  	tmpDir, err := ioutil.TempDir("", "TestNM")
    63  	if err != nil {
    64  		t.Fatal("TempDir failed: ", err)
    65  	}
    66  	defer os.RemoveAll(tmpDir)
    67  
    68  	testnmpath := filepath.Join(tmpDir, "testnm.exe")
    69  	out, err := exec.Command("go", "build", "-o", testnmpath, "cmd/nm").CombinedOutput()
    70  	if err != nil {
    71  		t.Fatalf("go build -o %v cmd/nm: %v\n%s", testnmpath, err, string(out))
    72  	}
    73  
    74  	testfiles := []string{
    75  		"elf/testdata/gcc-386-freebsd-exec",
    76  		"elf/testdata/gcc-amd64-linux-exec",
    77  		"macho/testdata/gcc-386-darwin-exec",
    78  		"macho/testdata/gcc-amd64-darwin-exec",
    79  		"pe/testdata/gcc-amd64-mingw-exec",
    80  		"pe/testdata/gcc-386-mingw-exec",
    81  		"plan9obj/testdata/amd64-plan9-exec",
    82  		"plan9obj/testdata/386-plan9-exec",
    83  	}
    84  	for _, f := range testfiles {
    85  		exepath := filepath.Join(runtime.GOROOT(), "src", "pkg", "debug", f)
    86  		cmd := exec.Command(testnmpath, exepath)
    87  		out, err := cmd.CombinedOutput()
    88  		if err != nil {
    89  			t.Fatalf("go tool nm %v: %v\n%s", exepath, err, string(out))
    90  		}
    91  	}
    92  
    93  	cmd := exec.Command(testnmpath, os.Args[0])
    94  	out, err = cmd.CombinedOutput()
    95  	if err != nil {
    96  		t.Fatalf("go tool nm %v: %v\n%s", os.Args[0], err, string(out))
    97  	}
    98  	checkSymbols(t, out)
    99  }