Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg : add proc(go version) unit testing #342

Merged
merged 2 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/proc/go_elf/eprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "C"
import "fmt"

//export eprint
func eprint(i C.int) {
fmt.Printf("eCapture unit testing : i = %d\n", uint32(i))
}
21 changes: 21 additions & 0 deletions pkg/proc/go_elf/gccgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

/*
extern void eprint(int i);

void foo(void) {
int i;
for (i=0;i<10;i++) {
eprint(i);
}
}
*/
import "C"

func Foo() {
C.foo()
}

func main() {
Foo()
}
59 changes: 57 additions & 2 deletions pkg/proc/proc_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,73 @@
package proc

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"testing"
)

const ELF_BUILD_BY_CGO = "go_elf"

func TestExtraceGoVersion(t *testing.T) {
path := fmt.Sprintf("/proc/%d/exe", os.Getppid())
ver, err := ExtraceGoVersion(path)
if err != nil {
t.Log(err)
return
}
log.Println(ver)
t.Log(ver)
}

// cd go_elf
// CGO_ENABLED=1 go build .
func TestExtraceGoVersionGccgo(t *testing.T) {
e := os.Chdir("go_elf")
if e != nil {
t.Fatalf("chdir error:%v\n", e)
}

p, e := os.Getwd()
if e != nil {
t.Fatalf("Getwd error:%v", e)
}
t.Logf("pwd:%s", p)

// go build go_elf
pathEnv := os.Getenv("PATH")
t.Logf("env $PATH:%s", pathEnv)

// mkdir directories
goBuildPath := filepath.Join(os.TempDir(), "go-build")
goEnvPath := filepath.Join(os.TempDir(), "go-env")
e = os.MkdirAll(goBuildPath, os.ModePerm)
if e != nil {
t.Fatal(e)
}
e = os.MkdirAll(goEnvPath, os.ModePerm)
if e != nil {
t.Fatal(e)
}

c := exec.Command("go", "build", "-v", ".")

var outb, errb bytes.Buffer
c.Stdout = &outb
c.Stderr = &errb
e = c.Run()
t.Logf("output:%s, errput:%s", outb.String(), errb.String())
if e != nil {
c.Stderr = os.Stderr
t.Fatalf("go build failed:%v", e)
}

p1 := filepath.Join(p, ELF_BUILD_BY_CGO)
ver, err := ExtraceGoVersion(p1)
t.Logf("Extrace GoVersion from CGO ELF :%s", p1)
if err != nil {
t.Fatal(err)
}
t.Logf("version found :%v", ver)
}