Skip to content

Commit 013ca84

Browse files
ALTreetklauser
authored andcommitted
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 6eaf7bc commit 013ca84

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/cmd/go/go_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4630,3 +4630,57 @@ func TestWrongGOOSErrorBeforeLoadError(t *testing.T) {
46304630
tg.runFail("build", "exclude")
46314631
tg.grepStderr("unsupported GOOS/GOARCH pair", "GOOS=windwos go build exclude did not report 'unsupported GOOS/GOARCH pair'")
46324632
}
4633+
4634+
func TestUpxCompression(t *testing.T) {
4635+
if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
4636+
t.Skipf("skipping upx test on %s/%s", runtime.GOOS, runtime.GOARCH)
4637+
}
4638+
4639+
out, err := exec.Command("upx", "--version").CombinedOutput()
4640+
if err != nil {
4641+
t.Skip("skipping because upx is not available")
4642+
}
4643+
4644+
// upx --version prints `upx <version>` in the first line of output:
4645+
// upx 3.94
4646+
// [...]
4647+
re := regexp.MustCompile(`([[:digit:]]+)\.([[:digit:]]+)`)
4648+
upxVersion := re.FindStringSubmatch(string(out))
4649+
if len(upxVersion) != 3 {
4650+
t.Errorf("bad upx version string: %s", upxVersion)
4651+
}
4652+
4653+
major, err1 := strconv.Atoi(upxVersion[1])
4654+
minor, err2 := strconv.Atoi(upxVersion[2])
4655+
if err1 != nil || err2 != nil {
4656+
t.Errorf("bad upx version string: %s", upxVersion[0])
4657+
}
4658+
4659+
// Anything below 3.94 is known not to work with go binaries
4660+
if (major < 3) || (major == 3 && minor < 94) {
4661+
t.Skipf("skipping because upx version %v.%v is too old", major, minor)
4662+
}
4663+
4664+
tg := testgo(t)
4665+
defer tg.cleanup()
4666+
4667+
tg.tempFile("main.go", `package main; import "fmt"; func main() { fmt.Print("hello upx") }`)
4668+
src := tg.path("main.go")
4669+
obj := tg.path("main")
4670+
tg.run("build", "-o", obj, src)
4671+
4672+
out, err = exec.Command("upx", obj).CombinedOutput()
4673+
if err != nil {
4674+
t.Logf("executing upx\n%s\n", out)
4675+
t.Fatalf("upx failed with %v", err)
4676+
}
4677+
4678+
out, err = exec.Command(obj).CombinedOutput()
4679+
if err != nil {
4680+
t.Logf("%s", out)
4681+
t.Fatalf("running compressed go binary failed with error %s", err)
4682+
}
4683+
if string(out) != "hello upx" {
4684+
t.Fatalf("bad output from compressed go binary:\ngot %q; want %q", out, "hello upx")
4685+
}
4686+
}

0 commit comments

Comments
 (0)