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
|
package test
import (
"crypto/rand"
"encoding/hex"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"testing"
"github.com/go-delve/delve/pkg/goversion"
)
// EnableRace allows to configure whether the race detector is enabled on target process.
var EnableRace = flag.Bool("racetarget", false, "Enables race detector on inferior process")
var runningWithFixtures bool
var ldFlags string
func init() {
ldFlags = os.Getenv("CGO_LDFLAGS")
}
// Fixture is a test binary.
type Fixture struct {
// Name is the short name of the fixture.
Name string
// Path is the absolute path to the test binary.
Path string
// Source is the absolute path of the test binary source.
Source string
// BuildDir is the directory where the build command was run.
BuildDir string
}
// FixtureKey holds the name and builds flags used for a test fixture.
type fixtureKey struct {
Name string
Flags BuildFlags
}
// Fixtures is a map of fixtureKey{ Fixture.Name, buildFlags } to Fixture.
var fixtures = make(map[fixtureKey]Fixture)
// PathsToRemove is a list of files and directories to remove after running all the tests
var PathsToRemove []string
// FindFixturesDir will search for the directory holding all test fixtures
// beginning with the current directory and searching up 10 directories.
func FindFixturesDir() string {
parent := ".."
fixturesDir := "_fixtures"
for depth := 0; depth < 10; depth++ {
if _, err := os.Stat(fixturesDir); err == nil {
break
}
fixturesDir = filepath.Join(parent, fixturesDir)
}
return fixturesDir
}
// BuildFlags used to build fixture.
type BuildFlags uint32
const (
// LinkStrip enables '-ldflags="-s"'.
LinkStrip BuildFlags = 1 << iota
// EnableCGOOptimization will build CGO code with optimizations.
EnableCGOOptimization
// EnableInlining will build a binary with inline optimizations turned on.
EnableInlining
// EnableOptimization will build a binary with default optimizations.
EnableOptimization
// EnableDWZCompression will enable DWZ compression of DWARF sections.
EnableDWZCompression
BuildModePIE
BuildModePlugin
BuildModeExternalLinker
AllNonOptimized
// LinkDisableDWARF enables '-ldflags="-w"'.
LinkDisableDWARF
)
// TempFile makes a (good enough) random temporary file name
func TempFile(name string) string {
r := make([]byte, 4)
rand.Read(r)
return filepath.Join(os.TempDir(), fmt.Sprintf("%s.%s", name, hex.EncodeToString(r)))
}
// BuildFixture will compile the fixture 'name' using the provided build flags.
func BuildFixture(name string, flags BuildFlags) Fixture {
if !runningWithFixtures {
panic("RunTestsWithFixtures not called")
}
fk := fixtureKey{name, flags}
if f, ok := fixtures[fk]; ok {
return f
}
if flags&EnableCGOOptimization == 0 {
if os.Getenv("CI") == "" || os.Getenv("CGO_CFLAGS") == "" {
os.Setenv("CGO_CFLAGS", "-O0 -g")
}
}
fixturesDir := FindFixturesDir()
dir := fixturesDir
path := filepath.Join(fixturesDir, name+".go")
if name[len(name)-1] == '/' {
dir = filepath.Join(dir, name)
path = ""
name = name[:len(name)-1]
}
tmpfile := TempFile(name)
buildFlags := []string{"build"}
var ver goversion.GoVersion
if ver, _ = goversion.Parse(runtime.Version()); runtime.GOOS == "windows" && ver.Major > 0 && !ver.AfterOrEqual(goversion.GoVersion{Major: 1, Minor: 9, Rev: -1}) {
// Work-around for https://github.com/golang/go/issues/13154
buildFlags = append(buildFlags, "-ldflags=-linkmode internal")
}
ldflagsv := []string{}
if flags&LinkStrip != 0 {
ldflagsv = append(ldflagsv, "-s")
}
if flags&LinkDisableDWARF != 0 {
ldflagsv = append(ldflagsv, "-w")
}
buildFlags = append(buildFlags, "-ldflags="+strings.Join(ldflagsv, " "))
gcflagsv := []string{}
if flags&EnableInlining == 0 {
gcflagsv = append(gcflagsv, "-l")
}
if flags&EnableOptimization == 0 {
gcflagsv = append(gcflagsv, "-N")
}
var gcflags string
if flags&AllNonOptimized != 0 {
gcflags = "-gcflags=all=" + strings.Join(gcflagsv, " ")
} else {
gcflags = "-gcflags=" + strings.Join(gcflagsv, " ")
}
buildFlags = append(buildFlags, gcflags, "-o", tmpfile)
if *EnableRace {
buildFlags = append(buildFlags, "-race")
}
if flags&BuildModePIE != 0 {
buildFlags = append(buildFlags, "-buildmode=pie")
} else {
buildFlags = append(buildFlags, "-buildmode=exe")
}
if flags&BuildModePlugin != 0 {
buildFlags = append(buildFlags, "-buildmode=plugin")
}
if flags&BuildModeExternalLinker != 0 {
buildFlags = append(buildFlags, "-ldflags=-linkmode=external")
}
if ver.IsOldDevel() || ver.AfterOrEqual(goversion.GoVersion{Major: 1, Minor: 11, Rev: -1}) {
if flags&EnableDWZCompression != 0 {
buildFlags = append(buildFlags, "-ldflags=-compressdwarf=false")
}
}
if path != "" {
buildFlags = append(buildFlags, name+".go")
}
cmd := exec.Command("go", buildFlags...)
cmd.Dir = dir
if os.Getenv("CI") != "" {
cmd.Env = os.Environ()
}
// Build the test binary
if out, err := cmd.CombinedOutput(); err != nil {
fmt.Printf("Error compiling %s: %s\n", path, err)
fmt.Printf("%s\n", string(out))
os.Exit(1)
}
if flags&EnableDWZCompression != 0 {
cmd := exec.Command("dwz", tmpfile)
if out, err := cmd.CombinedOutput(); err != nil {
if regexp.MustCompile(`dwz: Section offsets in (.*?) not monotonically increasing`).FindString(string(out)) == "" {
fmt.Printf("Error running dwz on %s: %s\n", tmpfile, err)
fmt.Printf("%s\n", string(out))
os.Exit(1)
}
}
}
source, _ := filepath.Abs(path)
source = filepath.ToSlash(source)
sympath, err := filepath.EvalSymlinks(source)
if err == nil {
source = strings.ReplaceAll(sympath, "\\", "/")
}
absdir, _ := filepath.Abs(dir)
fixture := Fixture{Name: name, Path: tmpfile, Source: source, BuildDir: absdir}
fixtures[fk] = fixture
return fixtures[fk]
}
// RunTestsWithFixtures sets the flag runningWithFixtures to compile fixtures on demand and runs tests with m.Run().
// After the tests are run, it removes the fixtures and paths from PathsToRemove.
func RunTestsWithFixtures(m *testing.M) {
runningWithFixtures = true
defer func() {
runningWithFixtures = false
}()
m.Run()
// Remove the fixtures.
for _, f := range fixtures {
os.Remove(f.Path)
}
for _, p := range PathsToRemove {
fi, err := os.Stat(p)
if err != nil {
panic(err)
}
if fi.IsDir() {
SafeRemoveAll(p)
} else {
os.Remove(p)
}
}
}
var recordingAllowed = map[string]bool{}
var recordingAllowedMu sync.Mutex
// AllowRecording allows the calling test to be used with a recording of the
// fixture.
func AllowRecording(t testing.TB) {
recordingAllowedMu.Lock()
defer recordingAllowedMu.Unlock()
name := t.Name()
t.Logf("enabling recording for %s", name)
recordingAllowed[name] = true
}
// MustHaveRecordingAllowed skips this test if recording is not allowed
//
// Not all the tests can be run with a recording:
// - some fixtures never terminate independently (loopprog,
// testnextnethttp) and can not be recorded
// - some tests assume they can interact with the target process (for
// example TestIssue419, or anything changing the value of a variable),
// which we can't do on with a recording
// - some tests assume that the Pid returned by the process is valid, but
// it won't be at replay time
// - some tests will start the fixture but not never execute a single
// instruction, for some reason rr doesn't like this and will print an
// error if it happens
// - many tests will assume that we can return from a runtime.Breakpoint,
// with a recording this is not possible because when the fixture ran it
// wasn't attached to a debugger and in those circumstances a
// runtime.Breakpoint leads directly to a crash
//
// Some of the tests using runtime.Breakpoint (anything involving variable
// evaluation and TestWorkDir) have been adapted to work with a recording.
func MustHaveRecordingAllowed(t testing.TB) {
recordingAllowedMu.Lock()
defer recordingAllowedMu.Unlock()
name := t.Name()
if !recordingAllowed[name] {
t.Skipf("recording not allowed for %s", name)
}
}
// SafeRemoveAll removes dir and its contents but only as long as dir does
// not contain directories.
func SafeRemoveAll(dir string) {
fis, err := os.ReadDir(dir)
if err != nil {
return
}
for _, fi := range fis {
if fi.IsDir() {
return
}
}
for _, fi := range fis {
if err := os.Remove(filepath.Join(dir, fi.Name())); err != nil {
return
}
}
os.Remove(dir)
}
// MustSupportFunctionCalls skips this test if function calls are
// unsupported on this backend/architecture pair.
func MustSupportFunctionCalls(t *testing.T, testBackend string) {
if !goversion.VersionAfterOrEqual(runtime.Version(), 1, 11) {
t.Skip("this version of Go does not support function calls")
}
if runtime.GOOS == "darwin" && testBackend == "native" {
t.Skip("this backend does not support function calls")
}
if runtime.GOARCH == "386" {
t.Skip(fmt.Errorf("%s does not support FunctionCall for now", runtime.GOARCH))
}
if runtime.GOARCH == "riscv64" {
t.Skip(fmt.Errorf("%s does not support FunctionCall for now", runtime.GOARCH))
}
if runtime.GOARCH == "arm64" {
if !goversion.VersionAfterOrEqual(runtime.Version(), 1, 19) || runtime.GOOS == "windows" {
t.Skip("this version of Go does not support function calls")
}
}
if runtime.GOARCH == "ppc64le" {
if !goversion.VersionAfterOrEqual(runtime.Version(), 1, 22) {
t.Skip("On PPC64LE Building with Go lesser than 1.22 does not support function calls")
}
}
}
// DefaultTestBackend changes the value of testBackend to be the default
// test backend for the OS, if testBackend isn't already set.
func DefaultTestBackend(testBackend *string) {
if *testBackend != "" {
return
}
*testBackend = os.Getenv("PROCTEST")
if *testBackend != "" {
return
}
if runtime.GOOS == "darwin" {
*testBackend = "lldb"
} else {
*testBackend = "native"
}
}
// WithPlugins builds the fixtures in plugins as plugins and returns them.
// The test calling WithPlugins will be skipped if the current combination
// of OS, architecture and version of GO doesn't support plugins or
// debugging plugins.
func WithPlugins(t *testing.T, flags BuildFlags, plugins ...string) []Fixture {
if !goversion.VersionAfterOrEqual(runtime.Version(), 1, 12) {
t.Skip("versions of Go before 1.12 do not include debug information in packages that import plugin (or they do but it's wrong)")
}
if runtime.GOOS != "linux" {
t.Skip("only supported on linux")
}
r := make([]Fixture, len(plugins))
for i := range plugins {
r[i] = BuildFixture(plugins[i], flags|BuildModePlugin)
}
return r
}
var hasCgo = func() bool {
out, err := exec.Command("go", "env", "CGO_ENABLED").CombinedOutput()
if err != nil {
panic(err)
}
if strings.TrimSpace(string(out)) != "1" {
return false
}
_, err1 := exec.LookPath("gcc")
_, err2 := exec.LookPath("clang")
return (err1 == nil) || (err2 == nil)
}()
func MustHaveCgo(t *testing.T) {
if !hasCgo {
t.Skip("Cgo not enabled")
}
}
func RegabiSupported() bool {
// Tracks regabiSupported variable in ParseGOEXPERIMENT internal/buildcfg/exp.go
switch {
case goversion.VersionAfterOrEqual(runtime.Version(), 1, 18):
return runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "ppc64" || runtime.GOARCH == "riscv64"
case goversion.VersionAfterOrEqual(runtime.Version(), 1, 17):
return runtime.GOARCH == "amd64" && (runtime.GOOS == "android" || runtime.GOOS == "linux" || runtime.GOOS == "darwin" || runtime.GOOS == "windows")
default:
return false
}
}
func ProjectRoot() string {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
gopaths := strings.FieldsFunc(os.Getenv("GOPATH"), func(r rune) bool { return r == os.PathListSeparator })
for _, curpath := range gopaths {
// Detects "gopath mode" when GOPATH contains several paths ex. "d:\\dir\\gopath;f:\\dir\\gopath2"
if strings.Contains(wd, curpath) {
return filepath.Join(curpath, "src", "github.com", "go-delve", "delve")
}
}
val, err := exec.Command("go", "list", "-mod=", "-m", "-f", "{{ .Dir }}").Output()
if err != nil {
panic(err) // the Go tool was tested to work earlier
}
return strings.TrimSuffix(string(val), "\n")
}
func GetDlvBinary(t *testing.T) string {
// In case this was set in the environment
// from getDlvBinEBPF lets clear it here, so
// we can ensure we don't get build errors
// depending on the test ordering.
t.Setenv("CGO_LDFLAGS", ldFlags)
var tags []string
if runtime.GOOS == "windows" && runtime.GOARCH == "arm64" {
tags = []string{"-tags=exp.winarm64"}
}
if runtime.GOOS == "linux" && runtime.GOARCH == "ppc64le" {
tags = []string{"-tags=exp.linuxppc64le"}
}
if runtime.GOOS == "linux" && runtime.GOARCH == "riscv64" {
tags = []string{"-tags=exp.linuxriscv64"}
}
return getDlvBinInternal(t, tags...)
}
func GetDlvBinaryEBPF(t *testing.T) string {
return getDlvBinInternal(t, "-tags", "ebpf")
}
func getDlvBinInternal(t *testing.T, goflags ...string) string {
dlvbin := filepath.Join(t.TempDir(), "dlv.exe")
args := append([]string{"build", "-o", dlvbin}, goflags...)
args = append(args, "github.com/go-delve/delve/cmd/dlv")
wd, _ := os.Getwd()
fmt.Printf("at %s %s\n", wd, goflags)
out, err := exec.Command("go", args...).CombinedOutput()
if err != nil {
t.Fatalf("go build -o %v github.com/go-delve/delve/cmd/dlv: %v\n%s", dlvbin, err, string(out))
}
return dlvbin
}
|