From: Alberto Donizetti Date: Mon, 9 Oct 2017 07:54:35 +0000 (+0200) Subject: cmd/go: add a test to ensure upx works on go binaries X-Git-Tag: go1.10beta1~560 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=013ca842aa1414e3f6c6448d4269c22ce690087e;p=gostls13.git cmd/go: add a test to ensure upx works on go binaries On linux/amd64, for now. Updates #16706 Change-Id: Ib8c89b6edc73fb88042c06873ff815d387491504 Reviewed-on: https://go-review.googlesource.com/69117 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index b255d7630e..a51d58a968 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -4630,3 +4630,57 @@ func TestWrongGOOSErrorBeforeLoadError(t *testing.T) { tg.runFail("build", "exclude") tg.grepStderr("unsupported GOOS/GOARCH pair", "GOOS=windwos go build exclude did not report 'unsupported GOOS/GOARCH pair'") } + +func TestUpxCompression(t *testing.T) { + if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" { + t.Skipf("skipping upx test on %s/%s", runtime.GOOS, runtime.GOARCH) + } + + out, err := exec.Command("upx", "--version").CombinedOutput() + if err != nil { + t.Skip("skipping because upx is not available") + } + + // upx --version prints `upx ` in the first line of output: + // upx 3.94 + // [...] + re := regexp.MustCompile(`([[:digit:]]+)\.([[:digit:]]+)`) + upxVersion := re.FindStringSubmatch(string(out)) + if len(upxVersion) != 3 { + t.Errorf("bad upx version string: %s", upxVersion) + } + + major, err1 := strconv.Atoi(upxVersion[1]) + minor, err2 := strconv.Atoi(upxVersion[2]) + if err1 != nil || err2 != nil { + t.Errorf("bad upx version string: %s", upxVersion[0]) + } + + // Anything below 3.94 is known not to work with go binaries + if (major < 3) || (major == 3 && minor < 94) { + t.Skipf("skipping because upx version %v.%v is too old", major, minor) + } + + tg := testgo(t) + defer tg.cleanup() + + tg.tempFile("main.go", `package main; import "fmt"; func main() { fmt.Print("hello upx") }`) + src := tg.path("main.go") + obj := tg.path("main") + tg.run("build", "-o", obj, src) + + out, err = exec.Command("upx", obj).CombinedOutput() + if err != nil { + t.Logf("executing upx\n%s\n", out) + t.Fatalf("upx failed with %v", err) + } + + out, err = exec.Command(obj).CombinedOutput() + if err != nil { + t.Logf("%s", out) + t.Fatalf("running compressed go binary failed with error %s", err) + } + if string(out) != "hello upx" { + t.Fatalf("bad output from compressed go binary:\ngot %q; want %q", out, "hello upx") + } +}