...
1
2
3
4
5 package test
6
7 import (
8 "bytes"
9 "internal/testenv"
10 "os"
11 "path/filepath"
12 "strings"
13 "testing"
14 )
15
16
17
18 func TestScanfRemoval(t *testing.T) {
19 testenv.MustHaveGoBuild(t)
20 t.Parallel()
21
22
23 dir := t.TempDir()
24
25
26 src := filepath.Join(dir, "test.go")
27 f, err := os.Create(src)
28 if err != nil {
29 t.Fatalf("could not create source file: %v", err)
30 }
31 f.Write([]byte(`
32 package main
33 import "fmt"
34 func main() {
35 fmt.Println("hello world")
36 }
37 `))
38 f.Close()
39
40
41 dst := filepath.Join(dir, "test")
42
43
44 cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", dst, src)
45 out, err := cmd.CombinedOutput()
46 if err != nil {
47 t.Fatalf("could not build target: %v\n%s", err, out)
48 }
49
50
51 cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", dst)
52 out, err = cmd.CombinedOutput()
53 if err != nil {
54 t.Fatalf("could not read target: %v", err)
55 }
56 if bytes.Contains(out, []byte("scanInt")) {
57 t.Fatalf("scanf code not removed from helloworld")
58 }
59 }
60
61
62 func TestDashS(t *testing.T) {
63 testenv.MustHaveGoBuild(t)
64 t.Parallel()
65
66
67 dir := t.TempDir()
68
69
70 src := filepath.Join(dir, "test.go")
71 f, err := os.Create(src)
72 if err != nil {
73 t.Fatalf("could not create source file: %v", err)
74 }
75 f.Write([]byte(`
76 package main
77 import "fmt"
78 func main() {
79 fmt.Println("hello world")
80 }
81 `))
82 f.Close()
83
84
85 cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src)
86 out, err := cmd.CombinedOutput()
87 if err != nil {
88 t.Fatalf("could not build target: %v\n%s", err, out)
89 }
90
91 patterns := []string{
92
93
94
95 "\tTEXT\t",
96 "\tFUNCDATA\t",
97 "\tPCDATA\t",
98 }
99 outstr := string(out)
100 for _, p := range patterns {
101 if !strings.Contains(outstr, p) {
102 println(outstr)
103 panic("can't find pattern " + p)
104 }
105 }
106 }
107
View as plain text