Commit 40a7de7b authored by Alex Brainman's avatar Alex Brainman

misc/cgo/testcshared: simlpify cshared_test.go

Change-Id: Ib35bb7fc9c5b4ccc9b8e1bd16443e0b307be9406
Reviewed-on: https://go-review.googlesource.com/62593Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 57fa1c7c
...@@ -64,8 +64,7 @@ func TestMain(m *testing.M) { ...@@ -64,8 +64,7 @@ func TestMain(m *testing.M) {
libgoname = "libgo." + libSuffix libgoname = "libgo." + libSuffix
ccOut := goEnv("CC") cc = []string{goEnv("CC")}
cc = []string{string(ccOut)}
out := goEnv("GOGCCFLAGS") out := goEnv("GOGCCFLAGS")
quote := '\000' quote := '\000'
...@@ -151,8 +150,8 @@ func goEnv(key string) string { ...@@ -151,8 +150,8 @@ func goEnv(key string) string {
return strings.TrimSpace(string(out)) return strings.TrimSpace(string(out))
} }
func cmdToRun(name string) []string { func cmdToRun(name string) string {
return []string{"./" + name + exeSuffix} return "./" + name + exeSuffix
} }
func adbPush(t *testing.T, filename string) { func adbPush(t *testing.T, filename string) {
...@@ -185,11 +184,10 @@ func adbRun(t *testing.T, env []string, adbargs ...string) string { ...@@ -185,11 +184,10 @@ func adbRun(t *testing.T, env []string, adbargs ...string) string {
if err != nil { if err != nil {
t.Fatalf("adb command failed: %v\n%s\n", err, out) t.Fatalf("adb command failed: %v\n%s\n", err, out)
} }
return strings.Replace(string(out), "\r", "", -1) 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 := exec.Command(args[0], args[1:]...)
cmd.Env = env cmd.Env = env
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
...@@ -198,19 +196,6 @@ func runwithenv(t *testing.T, env []string, args ...string) string { ...@@ -198,19 +196,6 @@ func runwithenv(t *testing.T, env []string, args ...string) string {
} else { } else {
t.Logf("run: %v", args) 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) return string(out)
} }
...@@ -218,16 +203,11 @@ func runExe(t *testing.T, env []string, args ...string) string { ...@@ -218,16 +203,11 @@ func runExe(t *testing.T, env []string, args ...string) string {
if GOOS == "android" { if GOOS == "android" {
return adbRun(t, env, args...) return adbRun(t, env, args...)
} }
return run(t, env, args...)
return runwithenv(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 { func runCC(t *testing.T, args ...string) string {
return runwithenv(t, gopathEnv, args...) return run(t, nil, append(cc, args...)...)
} }
func createHeaders() error { func createHeaders() error {
...@@ -297,16 +277,15 @@ func TestExportedSymbols(t *testing.T) { ...@@ -297,16 +277,15 @@ func TestExportedSymbols(t *testing.T) {
t.Parallel() t.Parallel()
cmd := "testp0" cmd := "testp0"
bin := cmdToRun(cmd)
createHeadersOnce(t) 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) adbPush(t, cmd)
defer os.Remove(cmd) defer os.Remove(cmd)
out := runwithldlibrarypath(t, bin...) out := run(t, append(gopathEnv, "LD_LIBRARY_PATH=."), cmdToRun(cmd))
if strings.TrimSpace(out) != "PASS" { if strings.TrimSpace(out) != "PASS" {
t.Error(out) t.Error(out)
} }
...@@ -317,16 +296,15 @@ func TestExportedSymbolsWithDynamicLoad(t *testing.T) { ...@@ -317,16 +296,15 @@ func TestExportedSymbolsWithDynamicLoad(t *testing.T) {
t.Parallel() t.Parallel()
cmd := "testp1" cmd := "testp1"
bin := cmdToRun(cmd)
createHeadersOnce(t) createHeadersOnce(t)
run(t, append(cc, "-o", cmd, "main1.c", "-ldl")...) runCC(t, "-o", cmd, "main1.c", "-ldl")
adbPush(t, cmd) adbPush(t, cmd)
defer os.Remove(cmd) defer os.Remove(cmd)
out := runExe(t, nil, append(bin, "./"+libgoname)...) out := runExe(t, nil, cmdToRun(cmd), "./"+libgoname)
if strings.TrimSpace(out) != "PASS" { if strings.TrimSpace(out) != "PASS" {
t.Error(out) t.Error(out)
} }
...@@ -338,9 +316,9 @@ func TestUnexportedSymbols(t *testing.T) { ...@@ -338,9 +316,9 @@ func TestUnexportedSymbols(t *testing.T) {
cmd := "testp2" cmd := "testp2"
libname := "libgo2." + libSuffix libname := "libgo2." + libSuffix
bin := cmdToRun(cmd)
rungocmd(t, run(t,
gopathEnv,
"go", "build", "go", "build",
"-buildmode=c-shared", "-buildmode=c-shared",
"-installsuffix", "testcshared", "-installsuffix", "testcshared",
...@@ -353,17 +331,13 @@ func TestUnexportedSymbols(t *testing.T) { ...@@ -353,17 +331,13 @@ func TestUnexportedSymbols(t *testing.T) {
linkFlags = "" linkFlags = ""
} }
run(t, append( runCC(t, "-o", cmd, "main2.c", linkFlags, libname)
cc, "-o", cmd,
"main2.c", linkFlags,
libname,
)...)
adbPush(t, cmd) adbPush(t, cmd)
defer os.Remove(libname) defer os.Remove(libname)
defer os.Remove(cmd) defer os.Remove(cmd)
out := runwithldlibrarypath(t, bin...) out := run(t, append(gopathEnv, "LD_LIBRARY_PATH=."), cmdToRun(cmd))
if strings.TrimSpace(out) != "PASS" { if strings.TrimSpace(out) != "PASS" {
t.Error(out) t.Error(out)
...@@ -379,83 +353,54 @@ func TestMainExportedOnAndroid(t *testing.T) { ...@@ -379,83 +353,54 @@ func TestMainExportedOnAndroid(t *testing.T) {
} }
cmd := "testp3" cmd := "testp3"
bin := cmdToRun(cmd)
createHeadersOnce(t) createHeadersOnce(t)
run(t, append(cc, "-o", cmd, "main3.c", "-ldl")...) runCC(t, "-o", cmd, "main3.c", "-ldl")
adbPush(t, cmd) adbPush(t, cmd)
defer os.Remove(cmd) defer os.Remove(cmd)
out := runExe(t, nil, append(bin, "./"+libgoname)...) out := runExe(t, nil, cmdToRun(cmd), "./"+libgoname)
if strings.TrimSpace(out) != "PASS" { if strings.TrimSpace(out) != "PASS" {
t.Error(out) t.Error(out)
} }
} }
// test4: test signal handlers func testSignalHandlers(t *testing.T, pkgname, cfile, cmd string) {
func TestSignalHandlers(t *testing.T) { libname := pkgname + "." + libSuffix
t.Parallel() run(t,
gopathEnv,
cmd := "testp4"
libname := "libgo4." + libSuffix
bin := cmdToRun(cmd)
rungocmd(t,
"go", "build", "go", "build",
"-buildmode=c-shared", "-buildmode=c-shared",
"-installsuffix", "testcshared", "-installsuffix", "testcshared",
"-o", libname, "libgo4", "-o", libname, pkgname,
) )
adbPush(t, libname) adbPush(t, libname)
run(t, append( runCC(t, "-pthread", "-o", cmd, cfile, "-ldl")
cc, "-pthread", "-o", cmd,
"main4.c", "-ldl",
)...)
adbPush(t, cmd) adbPush(t, cmd)
defer os.Remove(libname) defer os.Remove(libname)
defer os.Remove(cmd) defer os.Remove(cmd)
defer os.Remove("libgo4.h") defer os.Remove(pkgname + ".h")
out := runExe(t, nil, append(bin, "./"+libname)...)
bin := cmdToRun(cmd)
out := runExe(t, nil, bin, "./"+libname)
if strings.TrimSpace(out) != "PASS" { if strings.TrimSpace(out) != "PASS" {
t.Error(run(t, append(bin, libname, "verbose")...)) t.Error(run(t, nil, bin, libname, "verbose"))
} }
} }
// test4: test signal handlers
func TestSignalHandlers(t *testing.T) {
t.Parallel()
testSignalHandlers(t, "libgo4", "main4.c", "testp4")
}
// test5: test signal handlers with os/signal.Notify // test5: test signal handlers with os/signal.Notify
func TestSignalHandlersWithNotify(t *testing.T) { func TestSignalHandlersWithNotify(t *testing.T) {
t.Parallel() t.Parallel()
testSignalHandlers(t, "libgo5", "main5.c", "testp5")
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")...))
}
} }
func TestPIE(t *testing.T) { func TestPIE(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment