gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/gmtls/link_test.go (about) 1 // Copyright (c) 2022 zhaochun 2 // core-gm is licensed under Mulan PSL v2. 3 // You can use this software according to the terms and conditions of the Mulan PSL v2. 4 // You may obtain a copy of Mulan PSL v2 at: 5 // http://license.coscl.org.cn/MulanPSL2 6 // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 7 // See the Mulan PSL v2 for more details. 8 9 /* 10 gmtls是基于`golang/go`的`tls`包实现的国密改造版本。 11 对应版权声明: thrid_licenses/github.com/golang/go/LICENSE 12 */ 13 14 package gmtls 15 16 import ( 17 "bytes" 18 "os" 19 "os/exec" 20 "path/filepath" 21 "testing" 22 23 "gitee.com/ks-custle/core-gm/internal/testenv" 24 ) 25 26 // Tests that the linker is able to remove references to the Client or Server if unused. 27 func TestLinkerGC(t *testing.T) { 28 if testing.Short() { 29 t.Skip("skipping in short mode") 30 } 31 t.Parallel() 32 goBin := testenv.GoToolPath(t) 33 testenv.MustHaveGoBuild(t) 34 35 tests := []struct { 36 name string 37 program string 38 want []string 39 bad []string 40 }{ 41 { 42 name: "empty_import", 43 program: `package main 44 import _ "crypto/tls" 45 func main() {} 46 `, 47 bad: []string{ 48 "tls.(*Conn)", 49 "type.crypto/tls.clientHandshakeState", 50 "type.crypto/tls.serverHandshakeState", 51 }, 52 }, 53 { 54 name: "client_and_server", 55 program: `package main 56 import "crypto/tls" 57 func main() { 58 tls.Dial("", "", nil) 59 tls.Server(nil, nil) 60 } 61 `, 62 want: []string{ 63 "crypto/tls.(*Conn).clientHandshake", 64 "crypto/tls.(*Conn).serverHandshake", 65 }, 66 }, 67 { 68 name: "only_client", 69 program: `package main 70 import "crypto/tls" 71 func main() { tls.Dial("", "", nil) } 72 `, 73 want: []string{ 74 "crypto/tls.(*Conn).clientHandshake", 75 }, 76 bad: []string{ 77 "crypto/tls.(*Conn).serverHandshake", 78 }, 79 }, 80 // TODO: add only_server like func main() { tls.Server(nil, nil) } 81 // That currently brings in the client via Conn.handleRenegotiation. 82 83 } 84 tmpDir := t.TempDir() 85 goFile := filepath.Join(tmpDir, "x.go") 86 exeFile := filepath.Join(tmpDir, "x.exe") 87 for _, tt := range tests { 88 t.Run(tt.name, func(t *testing.T) { 89 if err := os.WriteFile(goFile, []byte(tt.program), 0644); err != nil { 90 t.Fatal(err) 91 } 92 _ = os.Remove(exeFile) 93 cmd := exec.Command(goBin, "build", "-o", "x.exe", "x.go") 94 cmd.Dir = tmpDir 95 if out, err := cmd.CombinedOutput(); err != nil { 96 t.Fatalf("compile: %v, %s", err, out) 97 } 98 99 cmd = exec.Command(goBin, "tool", "nm", "x.exe") 100 cmd.Dir = tmpDir 101 nm, err := cmd.CombinedOutput() 102 if err != nil { 103 t.Fatalf("nm: %v, %s", err, nm) 104 } 105 for _, sym := range tt.want { 106 if !bytes.Contains(nm, []byte(sym)) { 107 t.Errorf("expected symbol %q not found", sym) 108 } 109 } 110 for _, sym := range tt.bad { 111 if bytes.Contains(nm, []byte(sym)) { 112 t.Errorf("unexpected symbol %q found", sym) 113 } 114 } 115 }) 116 } 117 }