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