Source file src/cmd/link/internal/ld/go_test.go

     1  // Copyright 2020 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 ld
     6  
     7  import (
     8  	"cmd/internal/objabi"
     9  	"internal/testenv"
    10  	"io/ioutil"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"reflect"
    14  	"runtime"
    15  	"testing"
    16  )
    17  
    18  func TestDedupLibraries(t *testing.T) {
    19  	ctxt := &Link{}
    20  	ctxt.Target.HeadType = objabi.Hlinux
    21  
    22  	libs := []string{"libc.so", "libc.so.6"}
    23  
    24  	got := dedupLibraries(ctxt, libs)
    25  	if !reflect.DeepEqual(got, libs) {
    26  		t.Errorf("dedupLibraries(%v) = %v, want %v", libs, got, libs)
    27  	}
    28  }
    29  
    30  func TestDedupLibrariesOpenBSD(t *testing.T) {
    31  	ctxt := &Link{}
    32  	ctxt.Target.HeadType = objabi.Hopenbsd
    33  
    34  	tests := []struct {
    35  		libs []string
    36  		want []string
    37  	}{
    38  		{
    39  			libs: []string{"libc.so"},
    40  			want: []string{"libc.so"},
    41  		},
    42  		{
    43  			libs: []string{"libc.so", "libc.so.96.1"},
    44  			want: []string{"libc.so.96.1"},
    45  		},
    46  		{
    47  			libs: []string{"libc.so.96.1", "libc.so"},
    48  			want: []string{"libc.so.96.1"},
    49  		},
    50  		{
    51  			libs: []string{"libc.a", "libc.so.96.1"},
    52  			want: []string{"libc.a", "libc.so.96.1"},
    53  		},
    54  		{
    55  			libs: []string{"libpthread.so", "libc.so"},
    56  			want: []string{"libc.so", "libpthread.so"},
    57  		},
    58  		{
    59  			libs: []string{"libpthread.so.26.1", "libpthread.so", "libc.so.96.1", "libc.so"},
    60  			want: []string{"libc.so.96.1", "libpthread.so.26.1"},
    61  		},
    62  		{
    63  			libs: []string{"libpthread.so.26.1", "libpthread.so", "libc.so.96.1", "libc.so", "libfoo.so"},
    64  			want: []string{"libc.so.96.1", "libfoo.so", "libpthread.so.26.1"},
    65  		},
    66  	}
    67  
    68  	for _, test := range tests {
    69  		t.Run("dedup", func(t *testing.T) {
    70  			got := dedupLibraries(ctxt, test.libs)
    71  			if !reflect.DeepEqual(got, test.want) {
    72  				t.Errorf("dedupLibraries(%v) = %v, want %v", test.libs, got, test.want)
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func TestDedupLibrariesOpenBSDLink(t *testing.T) {
    79  	// The behavior we're checking for is of interest only on OpenBSD.
    80  	if runtime.GOOS != "openbsd" {
    81  		t.Skip("test only useful on openbsd")
    82  	}
    83  
    84  	testenv.MustHaveGoBuild(t)
    85  	testenv.MustHaveCGO(t)
    86  	t.Parallel()
    87  
    88  	dir := t.TempDir()
    89  
    90  	// cgo_import_dynamic both the unversioned libraries and pull in the
    91  	// net package to get a cgo package with a versioned library.
    92  	srcFile := filepath.Join(dir, "x.go")
    93  	src := `package main
    94  
    95  import (
    96  	_ "net"
    97  )
    98  
    99  //go:cgo_import_dynamic _ _ "libc.so"
   100  
   101  func main() {}`
   102  	if err := ioutil.WriteFile(srcFile, []byte(src), 0644); err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	exe := filepath.Join(dir, "deduped.exe")
   107  	out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, srcFile).CombinedOutput()
   108  	if err != nil {
   109  		t.Fatalf("build failure: %s\n%s\n", err, string(out))
   110  	}
   111  
   112  	// Result should be runnable.
   113  	if _, err = exec.Command(exe).CombinedOutput(); err != nil {
   114  		t.Fatal(err)
   115  	}
   116  }
   117  

View as plain text