blob: 39f0981c4135b75040b9c2c1f32f0e74cffec727 [file] [log] [blame]
Rebecca Stambler92778472021-01-05 23:05:35 -05001// Copyright 2016 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
Alan Donovan5e7fa1c2016-10-24 17:33:01 -04005package gccgoexportdata_test
6
7import (
8 "go/types"
9 "os"
10 "testing"
11
12 "golang.org/x/tools/go/gccgoexportdata"
13)
14
15// Test ensures this package can read gccgo export data from the
Alan Donovan5061f922016-11-09 12:56:38 -050016// .go_export from a standalone ELF file or such a file in an archive
17// library.
18//
19// The testdata/{short,long}.a ELF archive files were produced by:
20//
Russ Coxd5f48fc2022-04-11 23:03:04 -040021// $ echo 'package foo; func F()' > foo.go
22// $ gccgo -c -fgo-pkgpath blah foo.go
23// $ objcopy -j .go_export foo.o foo.gox
24// $ ar q short.a foo.gox
25// $ objcopy -j .go_export foo.o name-longer-than-16-bytes.gox
26// $ ar q long.a name-longer-than-16-bytes.gox
Alan Donovan5061f922016-11-09 12:56:38 -050027//
28// The file long.a contains an archive string table.
29//
30// The errors.gox file (an ELF object file) comes from the toolchain's
31// standard library.
Alan Donovan5e7fa1c2016-10-24 17:33:01 -040032func Test(t *testing.T) {
Alan Donovan5061f922016-11-09 12:56:38 -050033 for _, test := range []struct {
34 filename, path, member, wantType string
35 }{
36 {"testdata/errors.gox", "errors", "New", "func(text string) error"},
37 {"testdata/short.a", "short", "F", "func()"},
38 {"testdata/long.a", "long", "F", "func()"},
39 } {
40 t.Logf("filename = %s", test.filename)
41 f, err := os.Open(test.filename)
42 if err != nil {
43 t.Error(err)
44 continue
45 }
46 defer f.Close()
47 r, err := gccgoexportdata.NewReader(f)
48 if err != nil {
49 t.Error(err)
50 continue
51 }
Alan Donovan5e7fa1c2016-10-24 17:33:01 -040052
Alan Donovan5061f922016-11-09 12:56:38 -050053 imports := make(map[string]*types.Package)
54 pkg, err := gccgoexportdata.Read(r, nil, imports, test.path)
55 if err != nil {
56 t.Error(err)
57 continue
58 }
Alan Donovan5e7fa1c2016-10-24 17:33:01 -040059
Alan Donovan5061f922016-11-09 12:56:38 -050060 // Check type of designated package member.
61 obj := pkg.Scope().Lookup(test.member)
62 if obj == nil {
63 t.Errorf("%s.%s not found", test.path, test.member)
64 continue
65 }
66 if obj.Type().String() != test.wantType {
67 t.Errorf("%s.%s.Type = %s, want %s",
68 test.path, test.member, obj.Type(), test.wantType)
69 }
Alan Donovan5e7fa1c2016-10-24 17:33:01 -040070 }
71}