git.prognetwork.ru/x0r/utls@v1.3.3/link_test.go (about)

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