1
2
3
4
5 package archive
6
7 import (
8 "bytes"
9 "debug/elf"
10 "debug/macho"
11 "debug/pe"
12 "fmt"
13 "internal/testenv"
14 "internal/xcoff"
15 "io"
16 "os"
17 "path/filepath"
18 "runtime"
19 "strings"
20 "sync"
21 "testing"
22 "unicode/utf8"
23 )
24
25 var buildDir string
26
27 func TestMain(m *testing.M) {
28 if !testenv.HasGoBuild() {
29 return
30 }
31
32 exit := m.Run()
33
34 if buildDir != "" {
35 os.RemoveAll(buildDir)
36 }
37 os.Exit(exit)
38 }
39
40 func copyDir(dst, src string) error {
41 err := os.MkdirAll(dst, 0777)
42 if err != nil {
43 return err
44 }
45 entries, err := os.ReadDir(src)
46 if err != nil {
47 return err
48 }
49 for _, entry := range entries {
50 err = copyFile(filepath.Join(dst, entry.Name()), filepath.Join(src, entry.Name()))
51 if err != nil {
52 return err
53 }
54 }
55 return nil
56 }
57
58 func copyFile(dst, src string) (err error) {
59 var s, d *os.File
60 s, err = os.Open(src)
61 if err != nil {
62 return err
63 }
64 defer s.Close()
65 d, err = os.Create(dst)
66 if err != nil {
67 return err
68 }
69 defer func() {
70 e := d.Close()
71 if err == nil {
72 err = e
73 }
74 }()
75 _, err = io.Copy(d, s)
76 if err != nil {
77 return err
78 }
79 return nil
80 }
81
82 var (
83 buildOnce sync.Once
84 builtGoobjs goobjPaths
85 buildErr error
86 )
87
88 type goobjPaths struct {
89 go1obj string
90 go2obj string
91 goarchive string
92 cgoarchive string
93 }
94
95 func buildGoobj(t *testing.T) goobjPaths {
96 buildOnce.Do(func() {
97 buildErr = func() (err error) {
98 buildDir, err = os.MkdirTemp("", "TestGoobj")
99 if err != nil {
100 return err
101 }
102
103 go1obj := filepath.Join(buildDir, "go1.o")
104 go2obj := filepath.Join(buildDir, "go2.o")
105 goarchive := filepath.Join(buildDir, "go.a")
106 cgoarchive := ""
107
108 gotool, err := testenv.GoTool()
109 if err != nil {
110 return err
111 }
112
113 go1src := filepath.Join("testdata", "go1.go")
114 go2src := filepath.Join("testdata", "go2.go")
115
116 importcfgfile := filepath.Join(buildDir, "importcfg")
117 testenv.WriteImportcfg(t, importcfgfile, nil, go1src, go2src)
118
119 out, err := testenv.Command(t, gotool, "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", go1obj, go1src).CombinedOutput()
120 if err != nil {
121 return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out)
122 }
123 out, err = testenv.Command(t, gotool, "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", go2obj, go2src).CombinedOutput()
124 if err != nil {
125 return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out)
126 }
127 out, err = testenv.Command(t, gotool, "tool", "pack", "c", goarchive, go1obj, go2obj).CombinedOutput()
128 if err != nil {
129 return fmt.Errorf("go tool pack c %s %s %s: %v\n%s", goarchive, go1obj, go2obj, err, out)
130 }
131
132 if testenv.HasCGO() {
133 cgoarchive = filepath.Join(buildDir, "mycgo.a")
134 gopath := filepath.Join(buildDir, "gopath")
135 err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo"))
136 if err == nil {
137 err = os.WriteFile(filepath.Join(gopath, "src", "mycgo", "go.mod"), []byte("module mycgo\n"), 0666)
138 }
139 if err != nil {
140 return err
141 }
142 cmd := testenv.Command(t, gotool, "build", "-buildmode=archive", "-o", cgoarchive, "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo")
143 cmd.Dir = filepath.Join(gopath, "src", "mycgo")
144 cmd.Env = append(os.Environ(), "GOPATH="+gopath)
145 out, err = cmd.CombinedOutput()
146 if err != nil {
147 return fmt.Errorf("go install mycgo: %v\n%s", err, out)
148 }
149 }
150
151 builtGoobjs = goobjPaths{
152 go1obj: go1obj,
153 go2obj: go2obj,
154 goarchive: goarchive,
155 cgoarchive: cgoarchive,
156 }
157 return nil
158 }()
159 })
160
161 if buildErr != nil {
162 t.Helper()
163 t.Fatal(buildErr)
164 }
165 return builtGoobjs
166 }
167
168 func TestParseGoobj(t *testing.T) {
169 path := buildGoobj(t).go1obj
170
171 f, err := os.Open(path)
172 if err != nil {
173 t.Fatal(err)
174 }
175 defer f.Close()
176
177 a, err := Parse(f, false)
178 if err != nil {
179 t.Fatal(err)
180 }
181 if len(a.Entries) != 2 {
182 t.Errorf("expect 2 entry, found %d", len(a.Entries))
183 }
184 for _, e := range a.Entries {
185 if e.Type == EntryPkgDef {
186 continue
187 }
188 if e.Type != EntryGoObj {
189 t.Errorf("wrong type of object: want EntryGoObj, got %v", e.Type)
190 }
191 if !bytes.Contains(e.Obj.TextHeader, []byte(runtime.GOARCH)) {
192 t.Errorf("text header does not contain GOARCH %s: %q", runtime.GOARCH, e.Obj.TextHeader)
193 }
194 }
195 }
196
197 func TestParseArchive(t *testing.T) {
198 path := buildGoobj(t).goarchive
199
200 f, err := os.Open(path)
201 if err != nil {
202 t.Fatal(err)
203 }
204 defer f.Close()
205
206 a, err := Parse(f, false)
207 if err != nil {
208 t.Fatal(err)
209 }
210 if len(a.Entries) != 3 {
211 t.Errorf("expect 3 entry, found %d", len(a.Entries))
212 }
213 var found1 bool
214 var found2 bool
215 for _, e := range a.Entries {
216 if e.Type == EntryPkgDef {
217 continue
218 }
219 if e.Type != EntryGoObj {
220 t.Errorf("wrong type of object: want EntryGoObj, got %v", e.Type)
221 }
222 if !bytes.Contains(e.Obj.TextHeader, []byte(runtime.GOARCH)) {
223 t.Errorf("text header does not contain GOARCH %s: %q", runtime.GOARCH, e.Obj.TextHeader)
224 }
225 if e.Name == "go1.o" {
226 found1 = true
227 }
228 if e.Name == "go2.o" {
229 found2 = true
230 }
231 }
232 if !found1 {
233 t.Errorf(`object "go1.o" not found`)
234 }
235 if !found2 {
236 t.Errorf(`object "go2.o" not found`)
237 }
238 }
239
240 func TestParseCGOArchive(t *testing.T) {
241 testenv.MustHaveCGO(t)
242
243 path := buildGoobj(t).cgoarchive
244
245 f, err := os.Open(path)
246 if err != nil {
247 t.Fatal(err)
248 }
249 defer f.Close()
250
251 a, err := Parse(f, false)
252 if err != nil {
253 t.Fatal(err)
254 }
255
256 c1 := "c1"
257 c2 := "c2"
258 switch runtime.GOOS {
259 case "darwin", "ios":
260 c1 = "_" + c1
261 c2 = "_" + c2
262 case "windows":
263 if runtime.GOARCH == "386" {
264 c1 = "_" + c1
265 c2 = "_" + c2
266 }
267 case "aix":
268 c1 = "." + c1
269 c2 = "." + c2
270 }
271
272 var foundgo, found1, found2 bool
273
274 for _, e := range a.Entries {
275 switch e.Type {
276 default:
277 t.Errorf("unknown object type")
278 case EntryPkgDef:
279 continue
280 case EntryGoObj:
281 foundgo = true
282 if !bytes.Contains(e.Obj.TextHeader, []byte(runtime.GOARCH)) {
283 t.Errorf("text header does not contain GOARCH %s: %q", runtime.GOARCH, e.Obj.TextHeader)
284 }
285 continue
286 case EntryNativeObj:
287 }
288
289 obj := io.NewSectionReader(f, e.Offset, e.Size)
290 switch runtime.GOOS {
291 case "darwin", "ios":
292 mf, err := macho.NewFile(obj)
293 if err != nil {
294 t.Fatal(err)
295 }
296 if mf.Symtab == nil {
297 continue
298 }
299 for _, s := range mf.Symtab.Syms {
300 switch s.Name {
301 case c1:
302 found1 = true
303 case c2:
304 found2 = true
305 }
306 }
307 case "windows":
308 pf, err := pe.NewFile(obj)
309 if err != nil {
310 t.Fatal(err)
311 }
312 for _, s := range pf.Symbols {
313 switch s.Name {
314 case c1:
315 found1 = true
316 case c2:
317 found2 = true
318 }
319 }
320 case "aix":
321 xf, err := xcoff.NewFile(obj)
322 if err != nil {
323 t.Fatal(err)
324 }
325 for _, s := range xf.Symbols {
326 switch s.Name {
327 case c1:
328 found1 = true
329 case c2:
330 found2 = true
331 }
332 }
333 default:
334 ef, err := elf.NewFile(obj)
335 if err != nil {
336 t.Fatal(err)
337 }
338 syms, err := ef.Symbols()
339 if err != nil {
340 t.Fatal(err)
341 }
342 for _, s := range syms {
343 switch s.Name {
344 case c1:
345 found1 = true
346 case c2:
347 found2 = true
348 }
349 }
350 }
351 }
352
353 if !foundgo {
354 t.Errorf(`go object not found`)
355 }
356 if !found1 {
357 t.Errorf(`symbol %q not found`, c1)
358 }
359 if !found2 {
360 t.Errorf(`symbol %q not found`, c2)
361 }
362 }
363
364 func TestExactly16Bytes(t *testing.T) {
365 var tests = []string{
366 "",
367 "a",
368 "日本語",
369 "1234567890123456",
370 "12345678901234567890",
371 "1234567890123本語4567890",
372 "12345678901234日本語567890",
373 "123456789012345日本語67890",
374 "1234567890123456日本語7890",
375 "1234567890123456日本語7日本語890",
376 "12345678901234567890.syso",
377 "1234567890日本語.syso",
378 }
379 for _, str := range tests {
380 got := exactly16Bytes(str)
381 if len(got) != 16 {
382 t.Errorf("exactly16Bytes(%q) is %q, length %d", str, got, len(got))
383 }
384
385 for _, c := range got {
386 if c == utf8.RuneError {
387 t.Errorf("exactly16Bytes(%q) is %q, has partial rune", str, got)
388 }
389 }
390 if strings.HasSuffix(str, ".syso") && !strings.HasSuffix(strings.TrimRight(got, " "), ".syso") {
391 t.Errorf("exactly16Bytes(%q) is %q, did not preserve .syso suffix", str, got)
392 }
393 }
394 }
395
View as plain text