forked from goretk/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slow_test.go
415 lines (356 loc) · 9.19 KB
/
slow_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// This file is part of GoRE.
//
// Copyright (C) 2019-2021 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//go:build slow_test
package gore
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var dynResources = []struct {
os string
arch string
}{
{"linux", "386"},
{"linux", "amd64"},
{"windows", "386"},
{"windows", "amd64"},
{"darwin", "amd64"},
}
var dynResourceFiles *testFiles
type testFiles struct {
files sync.Map
}
func (f *testFiles) get(os, arch string, pie, stripped bool) string {
name := os + "-" + arch
if pie {
name += "-pie"
}
if !stripped {
name += "-nostrip"
}
exe, ok := f.files.Load(name)
if !ok {
return ""
}
return exe.(string)
}
// pass nil to check means all combinations
func getMatrix(t *testing.T, checkPie, checkStrip *bool, prefix string, cb func(*testing.T, string)) {
t.Helper()
for _, r := range dynResources {
var pieCases []bool
if checkPie != nil {
pieCases = append(pieCases, *checkPie)
} else {
pieCases = append(pieCases, true, false)
}
for _, pie := range pieCases {
var strippedCases []bool
if checkStrip != nil {
strippedCases = append(strippedCases, *checkStrip)
} else {
strippedCases = append(strippedCases, true, false)
}
for _, stripped := range strippedCases {
exe := dynResourceFiles.get(r.os, r.arch, pie, stripped)
name := r.os + "-" + r.arch
if pie {
name += "-pie"
}
if !stripped {
name += "-nostrip"
}
t.Run(prefix+"-"+name, func(tt *testing.T) {
tt.Parallel()
if exe == "" {
tt.Skip("no executable available")
}
cb(tt, exe)
})
}
}
}
}
func TestMain(m *testing.M) {
fmt.Println("Creating test resources, this can take some time...")
var tmpDirs []string
resultChan := make(chan buildResult)
wg := &sync.WaitGroup{}
dynResourceFiles = &testFiles{files: sync.Map{}}
go func() {
for r := range resultChan {
tmpDirs = append(tmpDirs, r.dir)
name := r.os + "-" + r.arch
if r.pie {
name += "-pie"
}
if !r.strip {
name += "-nostrip"
}
dynResourceFiles.files.Store(name, r.exe)
}
}()
for _, r := range dynResources {
fmt.Printf("Building resource file for %s_%s\n", r.os, r.arch)
for _, pie := range []bool{false, true} {
for _, stripped := range []bool{false, true} {
if pie && r.arch == "386" && r.os == "linux" {
// seems impossible
continue
}
wg.Add(1)
go buildTestResource(testresourcesrc, r.os, r.arch, pie, stripped, wg, resultChan)
}
}
}
wg.Wait()
close(resultChan)
fmt.Println("Launching tests")
code := m.Run()
fmt.Println("Clean up test resources")
for _, d := range tmpDirs {
os.RemoveAll(d)
}
os.Exit(code)
}
func TestOpenAndCloseFile(t *testing.T) {
getMatrix(t, nil, nil, "open", func(t *testing.T, exe string) {
a := assert.New(t)
f, err := Open(exe)
a.NoError(err)
a.NotNil(f)
a.NoError(f.Close())
})
}
func TestGetPackages(t *testing.T) {
getMatrix(t, nil, nil, "getPackages", func(t *testing.T, exe string) {
a := assert.New(t)
r := require.New(t)
f, err := Open(exe)
r.NoError(err)
r.NotNil(f)
defer f.Close()
std, err := f.GetSTDLib()
a.NoError(err)
a.NotEmpty(std, "Should have a list of standard library packages.")
_, err = f.GetGeneratedPackages()
a.NoError(err)
// XXX: This check appears to be unstable. Sometimes files for unknown reason.
// assert.NotEmpty(gen, "Should have a list of generated packages.")
ven, err := f.GetVendors()
a.NoError(err)
a.Empty(ven, "Should not have a list of vendor packages.")
_, err = f.GetUnknown()
a.NoError(err)
// XXX: This check appears to be unstable. Sometimes files for unknown reason.
// assert.Empty(unk, "Should not have a list of unknown packages")
pkgs, err := f.GetPackages()
a.NoError(err)
var mainpkg *Package
for _, p := range pkgs {
if p.Name == "main" {
mainpkg = p
break
}
}
mainPackageFound := false
getDataFuncFound := false
a.NotNil(mainpkg, "Should include main package")
for _, f := range mainpkg.Functions {
if f.Name == "main" {
mainPackageFound = true
} else if f.Name == "getData" {
getDataFuncFound = true
} else {
a.Fail("Unexpected function")
}
}
a.True(mainPackageFound, "No main function found")
a.True(getDataFuncFound, "getData function not found")
})
}
func TestGetTypesFromDynamicBuiltResources(t *testing.T) {
getMatrix(t, nil, nil, "getTypes", func(t *testing.T, exe string) {
a := assert.New(t)
r := require.New(t)
f, err := Open(exe)
r.NoError(err)
r.NotNil(f)
defer f.Close()
typs, err := f.GetTypes()
r.NoError(err)
var stringer *GoType
for _, t := range typs {
if t.PackagePath == "runtime" && t.Name == "runtime.g" {
stringer = t
break
}
}
a.NotNil(stringer, "the g type from runtime not found")
})
}
func TestGetCompilerVersion(t *testing.T) {
testVersion := testCompilerVersion()
expectedVersion := ResolveGoVersion(testVersion)
// If the version could not be resolved, the version is new
// and the library doesn't know about it. Use the version string
// to create a new version.
if expectedVersion == nil {
expectedVersion = &GoVersion{Name: testVersion}
}
getMatrix(t, nil, nil, "compiler-version", func(t *testing.T, exe string) {
a := assert.New(t)
r := require.New(t)
f, err := Open(exe)
r.NoError(err)
r.NotNil(f)
defer f.Close()
// Test
version, err := f.GetCompilerVersion()
a.NoError(err)
a.Equal(expectedVersion, version)
})
}
func TestGetBuildID(t *testing.T) {
getMatrix(t, nil, nil, "buildID", func(t *testing.T, exe string) {
a := assert.New(t)
r := require.New(t)
f, err := Open(exe)
r.NoError(err)
r.NotNil(f)
defer f.Close()
a.Equal(fixedBuildID, f.BuildID, "BuildID extracted doesn't match expected value.")
})
}
func TestSourceInfo(t *testing.T) {
getMatrix(t, nil, nil, "sourceInfo", func(t *testing.T, exe string) {
a := assert.New(t)
r := require.New(t)
f, err := Open(exe)
r.NoError(err)
r.NotNil(f)
defer f.Close()
var testFn *Function
pkgs, err := f.GetPackages()
r.NoError(err)
for _, pkg := range pkgs {
if pkg.Name != "main" {
continue
}
for _, fn := range pkg.Functions {
if fn.Name != "getData" {
continue
}
testFn = fn
break
}
}
r.NotNil(testFn)
file, start, end := f.SourceInfo(testFn)
a.NotEqual(0, start)
a.NotEqual(0, end)
a.NotEqual("", file)
})
}
func TestDwarfString(t *testing.T) {
noStrip := false
getMatrix(t, nil, &noStrip, "dwarfString", func(t *testing.T, exe string) {
r := require.New(t)
f, err := Open(exe)
r.NoError(err)
r.NotNil(f)
defer f.Close()
gover, ok := getBuildVersionFromDwarf(f.fh)
r.True(ok)
r.Equal(gover, runtime.Version())
goroot, ok := getGoRootFromDwarf(f.fh)
r.True(ok)
r.Equal(goroot, runtime.GOROOT())
})
}
type buildResult struct {
exe string
dir string
strip bool
pie bool
os string
arch string
}
func buildTestResource(body, goos, arch string, pie, stripped bool, wg *sync.WaitGroup, result chan buildResult) {
defer wg.Done()
goBin, err := exec.LookPath("go")
if err != nil {
panic("No go tool chain found: " + err.Error())
}
tmpdir, err := os.MkdirTemp("", "gore-test")
if err != nil {
panic(err)
}
src := filepath.Join(tmpdir, "a.go")
err = os.WriteFile(src, []byte(body), 0644)
if err != nil {
panic(err)
}
exe := filepath.Join(tmpdir, "a")
if runtime.GOOS == "windows" {
exe += ".exe"
}
var ldFlags string
if stripped {
ldFlags = "-s -w "
}
ldFlags += "-buildid=" + fixedBuildID
args := []string{"build", "-o", exe, "-ldflags", ldFlags}
if pie {
args = append(args, "-buildmode=pie")
} else {
// Windows use pie by default
args = append(args, "-buildmode=exe")
}
args = append(args, src)
cmd := exec.Command(goBin, args...)
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = tmpdir
}
cmd.Env = append(cmd.Env, "GOCACHE="+tmpdir, "GOARCH="+arch, "GOOS="+goos, "GOPATH="+gopath, "GOTMPDIR="+gopath, "PATH="+os.Getenv("PATH"))
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("building test executable failed: %s\n", string(out))
os.Exit(1)
}
result <- buildResult{exe: exe, dir: tmpdir, strip: stripped, pie: pie, os: goos, arch: arch}
}
func testCompilerVersion() string {
goBin, err := exec.LookPath("go")
if err != nil {
panic("No go tool chain found: " + err.Error())
}
out, err := exec.Command(goBin, "version").CombinedOutput()
if err != nil {
panic("Getting compiler version failed: " + string(out))
}
return strings.Split(string(out), " ")[2]
}