@@ -4630,3 +4630,57 @@ func TestWrongGOOSErrorBeforeLoadError(t *testing.T) {
4630
4630
tg .runFail ("build" , "exclude" )
4631
4631
tg .grepStderr ("unsupported GOOS/GOARCH pair" , "GOOS=windwos go build exclude did not report 'unsupported GOOS/GOARCH pair'" )
4632
4632
}
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:\n got %q; want %q" , out , "hello upx" )
4685
+ }
4686
+ }
0 commit comments