forked from goretk/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slow_test.go
497 lines (426 loc) · 12.1 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// 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
// +build slow_test
package gore
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"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 map[string]string
filesMu sync.RWMutex
}
func (f *testFiles) get(os, arch string, pie bool) string {
f.filesMu.Lock()
defer f.filesMu.Unlock()
if pie {
return f.files[os+arch+"-pie"]
}
return f.files[os+arch]
}
func TestMain(m *testing.M) {
fmt.Println("Creating test resources, this can take some time...")
tmpDirs := make([]string, len(dynResources))
fs := make(map[string]string)
for i, r := range dynResources {
fmt.Printf("Building resource file for %s_%s\n", r.os, r.arch)
exe, dir := buildTestResource(testresourcesrc, r.os, r.arch, false)
tmpDirs[i] = dir
fs[r.os+r.arch] = exe
// Build PIE version of the file. Not all host systems, particular macOS, appears to be able
// to compile a PIE build of linux-386. In this case, we skip this combination.
if !(r.arch == "386" && r.os == "linux") {
exe, dir = buildTestResource(testresourcesrc, r.os, r.arch, true)
tmpDirs[i] = dir
fs[r.os+r.arch+"-pie"] = exe
}
}
dynResourceFiles = &testFiles{files: fs}
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) {
for _, test := range dynResources {
t.Run("open_"+test.os+"-"+test.arch, func(t *testing.T) {
assert := assert.New(t)
exe := dynResourceFiles.get(test.os, test.arch, false)
f, err := Open(exe)
assert.NoError(err)
assert.NotNil(f)
assert.NoError(f.Close())
})
t.Run("open_"+test.os+"-"+test.arch+"-pie", func(t *testing.T) {
assert := assert.New(t)
exe := dynResourceFiles.get(test.os, test.arch, true)
if exe == "" {
t.Skip("no PIE available")
}
f, err := Open(exe)
assert.NoError(err)
assert.NotNil(f)
assert.NoError(f.Close())
})
}
}
func TestGetPackages(t *testing.T) {
for _, test := range dynResources {
t.Run("open_"+test.os+"-"+test.arch, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, false)
if exe == "" {
t.Skip("no PIE available")
}
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
std, err := f.GetSTDLib()
assert.NoError(err)
assert.NotEmpty(std, "Should have a list of standard library packages.")
_, err = f.GetGeneratedPackages()
assert.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()
assert.NoError(err)
assert.Empty(ven, "Should not have a list of vendor packages.")
_, err = f.GetUnknown()
assert.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()
assert.NoError(err)
var mainpkg *Package
for _, p := range pkgs {
if p.Name == "main" {
mainpkg = p
break
}
}
mp := false
gd := false
assert.NotNil(mainpkg, "Should include main package")
for _, f := range mainpkg.Functions {
if f.Name == "main" {
mp = true
} else if f.Name == "getData" {
gd = true
} else {
assert.Fail("Unexpected function")
}
}
assert.True(mp, "No main function found")
assert.True(gd, "getData function not found")
})
t.Run("open_"+test.os+"-"+test.arch+"-pie", func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, false)
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
std, err := f.GetSTDLib()
assert.NoError(err)
assert.NotEmpty(std, "Should have a list of standard library packages.")
_, err = f.GetGeneratedPackages()
assert.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()
assert.NoError(err)
assert.Empty(ven, "Should not have a list of vendor packages.")
_, err = f.GetUnknown()
assert.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()
assert.NoError(err)
var mainpkg *Package
for _, p := range pkgs {
if p.Name == "main" {
mainpkg = p
break
}
}
mp := false
gd := false
assert.NotNil(mainpkg, "Should include main package")
for _, f := range mainpkg.Functions {
if f.Name == "main" {
mp = true
} else if f.Name == "getData" {
gd = true
} else {
assert.Fail("Unexpected function")
}
}
assert.True(mp, "No main function found")
assert.True(gd, "getData function not found")
})
}
}
func TestGetTypesFromDynamicBuiltResources(t *testing.T) {
for _, test := range dynResources {
t.Run("open_"+test.os+"-"+test.arch, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, false)
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
typs, err := f.GetTypes()
require.NoError(err)
var stringer *GoType
for _, t := range typs {
if t.PackagePath == "runtime" && t.Name == "runtime.g" {
stringer = t
break
}
}
assert.NotNil(stringer, "the g type from runtime not found")
})
t.Run("open_"+test.os+"-"+test.arch+"-pie", func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, true)
if exe == "" {
t.Skip(("PIE file not available"))
}
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
typs, err := f.GetTypes()
require.NoError(err)
var stringer *GoType
for _, t := range typs {
if t.PackagePath == "runtime" && t.Name == "runtime.g" {
stringer = t
break
}
}
assert.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 created a new version.
if expectedVersion == nil {
expectedVersion = &GoVersion{Name: testVersion}
}
for _, test := range dynResources {
t.Run("parsing_"+test.os+"-"+test.arch, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, false)
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
// Test
version, err := f.GetCompilerVersion()
assert.NoError(err)
assert.Equal(expectedVersion, version)
})
t.Run("parsing_"+test.os+"-"+test.arch+"-pie", func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, true)
if exe == "" {
t.Skip("no PIE available")
}
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
// Test
version, err := f.GetCompilerVersion()
assert.NoError(err)
assert.Equal(expectedVersion, version)
})
}
}
func TestGetBuildID(t *testing.T) {
for _, test := range dynResources {
t.Run("buildID_"+test.os+"-"+test.arch, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, false)
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
assert.Equal(fixedBuildID, f.BuildID, "BuildID extracted doesn't match expected value.")
})
t.Run("buildID_"+test.os+"-"+test.arch+"-pie", func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, true)
if exe == "" {
t.Skip("no PIE available")
}
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
assert.Equal(fixedBuildID, f.BuildID, "BuildID extracted doesn't match expected value.")
})
}
}
func TestSourceInfo(t *testing.T) {
for _, test := range dynResources {
t.Run("buildID_"+test.os+"-"+test.arch, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, false)
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
var testFn *Function
pkgs, err := f.GetPackages()
require.NoError(err)
for _, pkg := range pkgs {
if pkg.Name != "main" {
continue
}
for _, fn := range pkg.Functions {
if fn.Name != "getData" {
continue
}
testFn = fn
break
}
}
require.NotNil(testFn)
file, start, end := f.SourceInfo(testFn)
assert.NotEqual(0, start)
assert.NotEqual(0, end)
assert.NotEqual("", file)
})
t.Run("buildID_"+test.os+"-"+test.arch+"-pie", func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
exe := dynResourceFiles.get(test.os, test.arch, true)
if exe == "" {
t.Skip("no PIE available")
}
f, err := Open(exe)
require.NoError(err)
require.NotNil(f)
defer f.Close()
var testFn *Function
pkgs, err := f.GetPackages()
require.NoError(err)
for _, pkg := range pkgs {
if pkg.Name != "main" {
continue
}
for _, fn := range pkg.Functions {
if fn.Name != "getData" {
continue
}
testFn = fn
break
}
}
require.NotNil(testFn)
file, start, end := f.SourceInfo(testFn)
assert.NotEqual(0, start)
assert.NotEqual(0, end)
assert.NotEqual("", file)
})
}
}
func buildTestResource(body, goos, arch string, pie bool) (string, string) {
goBin, err := exec.LookPath("go")
if err != nil {
panic("No go tool chain found: " + err.Error())
}
tmpdir, err := ioutil.TempDir("", "TestGORE")
if err != nil {
panic(err)
}
src := filepath.Join(tmpdir, "a.go")
err = ioutil.WriteFile(src, []byte(body), 0644)
if err != nil {
panic(err)
}
exe := filepath.Join(tmpdir, "a")
if pie {
exe = exe + "-pie"
}
args := []string{"build", "-o", exe, "-ldflags", "-s -w -buildid=" + fixedBuildID}
if pie {
args = append(args, "-buildmode=pie")
}
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, "PATH="+os.Getenv("PATH"))
out, err := cmd.CombinedOutput()
if err != nil {
panic("building test executable failed: " + string(out))
}
return exe, tmpdir
}
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]
}