mirror of https://github.com/golang/go.git
misc/cgo/testcshared: simlpify cshared_test.go
Change-Id: Ib35bb7fc9c5b4ccc9b8e1bd16443e0b307be9406 Reviewed-on: https://go-review.googlesource.com/62593 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
57fa1c7c94
commit
40a7de7b3a
|
|
@ -64,8 +64,7 @@ func TestMain(m *testing.M) {
|
|||
|
||||
libgoname = "libgo." + libSuffix
|
||||
|
||||
ccOut := goEnv("CC")
|
||||
cc = []string{string(ccOut)}
|
||||
cc = []string{goEnv("CC")}
|
||||
|
||||
out := goEnv("GOGCCFLAGS")
|
||||
quote := '\000'
|
||||
|
|
@ -151,8 +150,8 @@ func goEnv(key string) string {
|
|||
return strings.TrimSpace(string(out))
|
||||
}
|
||||
|
||||
func cmdToRun(name string) []string {
|
||||
return []string{"./" + name + exeSuffix}
|
||||
func cmdToRun(name string) string {
|
||||
return "./" + name + exeSuffix
|
||||
}
|
||||
|
||||
func adbPush(t *testing.T, filename string) {
|
||||
|
|
@ -185,11 +184,10 @@ func adbRun(t *testing.T, env []string, adbargs ...string) string {
|
|||
if err != nil {
|
||||
t.Fatalf("adb command failed: %v\n%s\n", err, out)
|
||||
}
|
||||
|
||||
return strings.Replace(string(out), "\r", "", -1)
|
||||
}
|
||||
|
||||
func runwithenv(t *testing.T, env []string, args ...string) string {
|
||||
func run(t *testing.T, env []string, args ...string) string {
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Env = env
|
||||
out, err := cmd.CombinedOutput()
|
||||
|
|
@ -198,19 +196,6 @@ func runwithenv(t *testing.T, env []string, args ...string) string {
|
|||
} else {
|
||||
t.Logf("run: %v", args)
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
||||
|
||||
func run(t *testing.T, args ...string) string {
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("command failed: %v\n%v\n%s\n", args, err, out)
|
||||
} else {
|
||||
t.Logf("run: %v", args)
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
||||
|
||||
|
|
@ -218,16 +203,11 @@ func runExe(t *testing.T, env []string, args ...string) string {
|
|||
if GOOS == "android" {
|
||||
return adbRun(t, env, args...)
|
||||
}
|
||||
|
||||
return runwithenv(t, env, args...)
|
||||
return run(t, env, args...)
|
||||
}
|
||||
|
||||
func runwithldlibrarypath(t *testing.T, args ...string) string {
|
||||
return runExe(t, append(gopathEnv, "LD_LIBRARY_PATH=."), args...)
|
||||
}
|
||||
|
||||
func rungocmd(t *testing.T, args ...string) string {
|
||||
return runwithenv(t, gopathEnv, args...)
|
||||
func runCC(t *testing.T, args ...string) string {
|
||||
return run(t, nil, append(cc, args...)...)
|
||||
}
|
||||
|
||||
func createHeaders() error {
|
||||
|
|
@ -297,16 +277,15 @@ func TestExportedSymbols(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
cmd := "testp0"
|
||||
bin := cmdToRun(cmd)
|
||||
|
||||
createHeadersOnce(t)
|
||||
|
||||
run(t, append(cc, "-I", installdir, "-o", cmd, "main0.c", libgoname)...)
|
||||
runCC(t, "-I", installdir, "-o", cmd, "main0.c", libgoname)
|
||||
adbPush(t, cmd)
|
||||
|
||||
defer os.Remove(cmd)
|
||||
|
||||
out := runwithldlibrarypath(t, bin...)
|
||||
out := run(t, append(gopathEnv, "LD_LIBRARY_PATH=."), cmdToRun(cmd))
|
||||
if strings.TrimSpace(out) != "PASS" {
|
||||
t.Error(out)
|
||||
}
|
||||
|
|
@ -317,16 +296,15 @@ func TestExportedSymbolsWithDynamicLoad(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
cmd := "testp1"
|
||||
bin := cmdToRun(cmd)
|
||||
|
||||
createHeadersOnce(t)
|
||||
|
||||
run(t, append(cc, "-o", cmd, "main1.c", "-ldl")...)
|
||||
runCC(t, "-o", cmd, "main1.c", "-ldl")
|
||||
adbPush(t, cmd)
|
||||
|
||||
defer os.Remove(cmd)
|
||||
|
||||
out := runExe(t, nil, append(bin, "./"+libgoname)...)
|
||||
out := runExe(t, nil, cmdToRun(cmd), "./"+libgoname)
|
||||
if strings.TrimSpace(out) != "PASS" {
|
||||
t.Error(out)
|
||||
}
|
||||
|
|
@ -338,9 +316,9 @@ func TestUnexportedSymbols(t *testing.T) {
|
|||
|
||||
cmd := "testp2"
|
||||
libname := "libgo2." + libSuffix
|
||||
bin := cmdToRun(cmd)
|
||||
|
||||
rungocmd(t,
|
||||
run(t,
|
||||
gopathEnv,
|
||||
"go", "build",
|
||||
"-buildmode=c-shared",
|
||||
"-installsuffix", "testcshared",
|
||||
|
|
@ -353,17 +331,13 @@ func TestUnexportedSymbols(t *testing.T) {
|
|||
linkFlags = ""
|
||||
}
|
||||
|
||||
run(t, append(
|
||||
cc, "-o", cmd,
|
||||
"main2.c", linkFlags,
|
||||
libname,
|
||||
)...)
|
||||
runCC(t, "-o", cmd, "main2.c", linkFlags, libname)
|
||||
adbPush(t, cmd)
|
||||
|
||||
defer os.Remove(libname)
|
||||
defer os.Remove(cmd)
|
||||
|
||||
out := runwithldlibrarypath(t, bin...)
|
||||
out := run(t, append(gopathEnv, "LD_LIBRARY_PATH=."), cmdToRun(cmd))
|
||||
|
||||
if strings.TrimSpace(out) != "PASS" {
|
||||
t.Error(out)
|
||||
|
|
@ -379,83 +353,54 @@ func TestMainExportedOnAndroid(t *testing.T) {
|
|||
}
|
||||
|
||||
cmd := "testp3"
|
||||
bin := cmdToRun(cmd)
|
||||
|
||||
createHeadersOnce(t)
|
||||
|
||||
run(t, append(cc, "-o", cmd, "main3.c", "-ldl")...)
|
||||
runCC(t, "-o", cmd, "main3.c", "-ldl")
|
||||
adbPush(t, cmd)
|
||||
|
||||
defer os.Remove(cmd)
|
||||
|
||||
out := runExe(t, nil, append(bin, "./"+libgoname)...)
|
||||
out := runExe(t, nil, cmdToRun(cmd), "./"+libgoname)
|
||||
if strings.TrimSpace(out) != "PASS" {
|
||||
t.Error(out)
|
||||
}
|
||||
}
|
||||
|
||||
func testSignalHandlers(t *testing.T, pkgname, cfile, cmd string) {
|
||||
libname := pkgname + "." + libSuffix
|
||||
run(t,
|
||||
gopathEnv,
|
||||
"go", "build",
|
||||
"-buildmode=c-shared",
|
||||
"-installsuffix", "testcshared",
|
||||
"-o", libname, pkgname,
|
||||
)
|
||||
adbPush(t, libname)
|
||||
runCC(t, "-pthread", "-o", cmd, cfile, "-ldl")
|
||||
adbPush(t, cmd)
|
||||
|
||||
defer os.Remove(libname)
|
||||
defer os.Remove(cmd)
|
||||
defer os.Remove(pkgname + ".h")
|
||||
|
||||
bin := cmdToRun(cmd)
|
||||
out := runExe(t, nil, bin, "./"+libname)
|
||||
if strings.TrimSpace(out) != "PASS" {
|
||||
t.Error(run(t, nil, bin, libname, "verbose"))
|
||||
}
|
||||
}
|
||||
|
||||
// test4: test signal handlers
|
||||
func TestSignalHandlers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cmd := "testp4"
|
||||
libname := "libgo4." + libSuffix
|
||||
bin := cmdToRun(cmd)
|
||||
|
||||
rungocmd(t,
|
||||
"go", "build",
|
||||
"-buildmode=c-shared",
|
||||
"-installsuffix", "testcshared",
|
||||
"-o", libname, "libgo4",
|
||||
)
|
||||
adbPush(t, libname)
|
||||
run(t, append(
|
||||
cc, "-pthread", "-o", cmd,
|
||||
"main4.c", "-ldl",
|
||||
)...)
|
||||
adbPush(t, cmd)
|
||||
|
||||
defer os.Remove(libname)
|
||||
defer os.Remove(cmd)
|
||||
defer os.Remove("libgo4.h")
|
||||
|
||||
out := runExe(t, nil, append(bin, "./"+libname)...)
|
||||
|
||||
if strings.TrimSpace(out) != "PASS" {
|
||||
t.Error(run(t, append(bin, libname, "verbose")...))
|
||||
}
|
||||
testSignalHandlers(t, "libgo4", "main4.c", "testp4")
|
||||
}
|
||||
|
||||
// test5: test signal handlers with os/signal.Notify
|
||||
func TestSignalHandlersWithNotify(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cmd := "testp5"
|
||||
libname := "libgo5." + libSuffix
|
||||
bin := cmdToRun(cmd)
|
||||
|
||||
rungocmd(t,
|
||||
"go", "build",
|
||||
"-buildmode=c-shared",
|
||||
"-installsuffix", "testcshared",
|
||||
"-o", libname, "libgo5",
|
||||
)
|
||||
adbPush(t, libname)
|
||||
run(t, append(
|
||||
cc, "-pthread", "-o", cmd,
|
||||
"main5.c", "-ldl",
|
||||
)...)
|
||||
adbPush(t, cmd)
|
||||
|
||||
defer os.Remove(libname)
|
||||
defer os.Remove(cmd)
|
||||
defer os.Remove("libgo5.h")
|
||||
|
||||
out := runExe(t, nil, append(bin, "./"+libname)...)
|
||||
|
||||
if strings.TrimSpace(out) != "PASS" {
|
||||
t.Error(run(t, append(bin, libname, "verbose")...))
|
||||
}
|
||||
testSignalHandlers(t, "libgo5", "main5.c", "testp5")
|
||||
}
|
||||
|
||||
func TestPIE(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue