github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/cmd/go/note_test.go (about)

     1  // Copyright 2015 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_test
     6  
     7  import (
     8  	"go/build"
     9  	"runtime"
    10  	"testing"
    11  
    12  	"cmd/go/internal/buildid"
    13  )
    14  
    15  func TestNoteReading(t *testing.T) {
    16  	testNoteReading(t)
    17  }
    18  
    19  func TestNoteReading2K(t *testing.T) {
    20  	if runtime.GOOS == "windows" {
    21  		t.Skipf("2kB is not enough on %s", runtime.GOOS)
    22  	}
    23  	// Set BuildIDReadSize to 2kB to exercise Mach-O parsing more strictly.
    24  	defer func(old int) {
    25  		buildid.BuildIDReadSize = old
    26  	}(buildid.BuildIDReadSize)
    27  	buildid.BuildIDReadSize = 2 * 1024
    28  
    29  	testNoteReading(t)
    30  }
    31  
    32  func testNoteReading(t *testing.T) {
    33  	tg := testgo(t)
    34  	defer tg.cleanup()
    35  	tg.tempFile("hello.go", `package main; func main() { print("hello, world\n") }`)
    36  	const buildID = "TestNoteReading-Build-ID"
    37  	tg.run("build", "-ldflags", "-buildid="+buildID, "-o", tg.path("hello.exe"), tg.path("hello.go"))
    38  	id, err := buildid.ReadBuildIDFromBinary(tg.path("hello.exe"))
    39  	if err != nil {
    40  		t.Fatalf("reading build ID from hello binary: %v", err)
    41  	}
    42  	if id != buildID {
    43  		t.Fatalf("buildID in hello binary = %q, want %q", id, buildID)
    44  	}
    45  
    46  	switch {
    47  	case !build.Default.CgoEnabled:
    48  		t.Skipf("skipping - no cgo, so assuming external linking not available")
    49  	case runtime.GOOS == "linux" && (runtime.GOARCH == "ppc64le" || runtime.GOARCH == "ppc64"):
    50  		t.Skipf("skipping - external linking not supported, golang.org/issue/11184")
    51  	case runtime.GOOS == "openbsd" && runtime.GOARCH == "arm":
    52  		t.Skipf("skipping - external linking not supported, golang.org/issue/10619")
    53  	case runtime.GOOS == "plan9":
    54  		t.Skipf("skipping - external linking not supported")
    55  	}
    56  
    57  	tg.run("build", "-ldflags", "-buildid="+buildID+" -linkmode=external", "-o", tg.path("hello.exe"), tg.path("hello.go"))
    58  	id, err = buildid.ReadBuildIDFromBinary(tg.path("hello.exe"))
    59  	if err != nil {
    60  		t.Fatalf("reading build ID from hello binary (linkmode=external): %v", err)
    61  	}
    62  	if id != buildID {
    63  		t.Fatalf("buildID in hello binary = %q, want %q (linkmode=external)", id, buildID)
    64  	}
    65  }