| 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 tls |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"internal/testenv" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"testing" |
| 13 |
) |
| 14 |
|
| 15 |
// Tests that the linker is able to remove references to the Client or Server if unused. |
| 16 |
func TestLinkerGC(t *testing.T) { |
| 17 |
if testing.Short() { |
| 18 |
t.Skip("skipping in short mode") |
| 19 |
} |
| 20 |
t.Parallel() |
| 21 |
|
| 22 |
tests := []struct { |
| 23 |
name string |
| 24 |
program string |
| 25 |
want []string |
| 26 |
bad []string |
| 27 |
}{ |
| 28 |
{ |
| 29 |
name: "empty_import", |
| 30 |
program: `package main |
| 31 |
import _ "crypto/tls" |
| 32 |
func main() {} |
| 33 |
`, |
| 34 |
bad: []string{ |
| 35 |
"tls.(*Conn)", |
| 36 |
"type:crypto/tls.clientHandshakeState", |
| 37 |
"type:crypto/tls.serverHandshakeState", |
| 38 |
}, |
| 39 |
}, |
| 40 |
{ |
| 41 |
name: "client_and_server", |
| 42 |
program: `package main |
| 43 |
import "crypto/tls" |
| 44 |
func main() { |
| 45 |
tls.Dial("", "", nil) |
| 46 |
tls.Server(nil, nil) |
| 47 |
} |
| 48 |
`, |
| 49 |
want: []string{ |
| 50 |
"crypto/tls.(*Conn).clientHandshake", |
| 51 |
"crypto/tls.(*Conn).serverHandshake", |
| 52 |
}, |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "only_client", |
| 56 |
program: `package main |
| 57 |
import "crypto/tls" |
| 58 |
func main() { tls.Dial("", "", nil) } |
| 59 |
`, |
| 60 |
want: []string{ |
| 61 |
"crypto/tls.(*Conn).clientHandshake", |
| 62 |
}, |
| 63 |
bad: []string{ |
| 64 |
"crypto/tls.(*Conn).serverHandshake", |
| 65 |
}, |
| 66 |
}, |
| 67 |
// TODO: add only_server like func main() { tls.Server(nil, nil) } |
| 68 |
// That currently brings in the client via Conn.handleRenegotiation. |
| 69 |
|
| 70 |
} |
| 71 |
tmpDir := t.TempDir() |
| 72 |
goFile := filepath.Join(tmpDir, "x.go") |
| 73 |
exeFile := filepath.Join(tmpDir, "x.exe") |
| 74 |
for _, tt := range tests { |
| 75 |
t.Run(tt.name, func(t *testing.T) { |
| 76 |
if err := os.WriteFile(goFile, []byte(tt.program), 0644); err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
os.Remove(exeFile) |
| 80 |
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", "x.exe", "x.go") |
| 81 |
cmd.Dir = tmpDir |
| 82 |
if out, err := cmd.CombinedOutput(); err != nil { |
| 83 |
t.Fatalf("compile: %v\n%s", err, out) |
| 84 |
} |
| 85 |
|
| 86 |
cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", "x.exe") |
| 87 |
cmd.Dir = tmpDir |
| 88 |
nm, err := testenv.CleanCmdEnv(cmd).CombinedOutput() |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("nm: %v\n%s", err, nm) |
| 91 |
} |
| 92 |
for _, sym := range tt.want { |
| 93 |
if !bytes.Contains(nm, []byte(sym)) { |
| 94 |
t.Errorf("expected symbol %q not found", sym) |
| 95 |
} |
| 96 |
} |
| 97 |
for _, sym := range tt.bad { |
| 98 |
if bytes.Contains(nm, []byte(sym)) { |
| 99 |
t.Errorf("unexpected symbol %q found", sym) |
| 100 |
} |
| 101 |
} |
| 102 |
}) |
| 103 |
} |
| 104 |
} |