add tests for dlfcn (#96)
This commit adds two tests. One comes from an example which wasn't really an example of using purego but more of a test to make sure fakecgo was functioning properly. Now it's an actual test. The other is a simple test that calls into libc
- commit
- cb589c51828bd47bef6f24344747252ea3e5fdc7
- author
- TotallyGamerJet · · 2023-02-12 20:02 UTC
- committer
- GitHub · · 2023-02-12 20:02 UTC
- parent
- e9c3434
- signature
- GPG Unverified
4AEE18F83AFDEB23The key is unknown
2 files · +45 -7
A dlfcn_test.go +39 -0
| @@ -0,0 +1,39 @@ | |||
| 1 | + | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | + | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors | |
| 3 | + | ||
| 4 | + | //go:build darwin || linux | |
| 5 | + | ||
| 6 | + | package purego_test | |
| 7 | + | ||
| 8 | + | import ( | |
| 9 | + | "fmt" | |
| 10 | + | "runtime" | |
| 11 | + | "testing" | |
| 12 | + | ||
| 13 | + | "github.com/ebitengine/purego" | |
| 14 | + | ) | |
| 15 | + | ||
| 16 | + | func getSystemLibrary() (string, error) { | |
| 17 | + | switch runtime.GOOS { | |
| 18 | + | case "darwin": | |
| 19 | + | return "/usr/lib/libSystem.B.dylib", nil | |
| 20 | + | case "linux": | |
| 21 | + | return "libc.so.6", nil | |
| 22 | + | default: | |
| 23 | + | return "", fmt.Errorf("GOOS=%s is not supported", runtime.GOOS) | |
| 24 | + | } | |
| 25 | + | } | |
| 26 | + | ||
| 27 | + | func TestRegisterFunc(t *testing.T) { | |
| 28 | + | library, err := getSystemLibrary() | |
| 29 | + | if err != nil { | |
| 30 | + | t.Errorf("couldn't get system library: %s", err) | |
| 31 | + | } | |
| 32 | + | libc := purego.Dlopen(library, purego.RTLD_NOW|purego.RTLD_GLOBAL) | |
| 33 | + | if err := purego.Dlerror(); err != "" { | |
| 34 | + | t.Errorf("failed to dlopen: %s", err) | |
| 35 | + | } | |
| 36 | + | var puts func(string) | |
| 37 | + | purego.RegisterLibFunc(&puts, libc, "puts") | |
| 38 | + | puts("Calling C from from Go without Cgo!") | |
| 39 | + | } | |
| @@ -0,0 +1,39 @@ | |||||
| 1 | + | // SPDX-License-Identifier: Apache-2.0 | |||
| 2 | + | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors | |||
| 3 | + | ||||
| 4 | + | //go:build darwin || linux | |||
| 5 | + | ||||
| 6 | + | package purego_test | |||
| 7 | + | ||||
| 8 | + | import ( | |||
| 9 | + | "fmt" | |||
| 10 | + | "runtime" | |||
| 11 | + | "testing" | |||
| 12 | + | ||||
| 13 | + | "github.com/ebitengine/purego" | |||
| 14 | + | ) | |||
| 15 | + | ||||
| 16 | + | func getSystemLibrary() (string, error) { | |||
| 17 | + | switch runtime.GOOS { | |||
| 18 | + | case "darwin": | |||
| 19 | + | return "/usr/lib/libSystem.B.dylib", nil | |||
| 20 | + | case "linux": | |||
| 21 | + | return "libc.so.6", nil | |||
| 22 | + | default: | |||
| 23 | + | return "", fmt.Errorf("GOOS=%s is not supported", runtime.GOOS) | |||
| 24 | + | } | |||
| 25 | + | } | |||
| 26 | + | ||||
| 27 | + | func TestRegisterFunc(t *testing.T) { | |||
| 28 | + | library, err := getSystemLibrary() | |||
| 29 | + | if err != nil { | |||
| 30 | + | t.Errorf("couldn't get system library: %s", err) | |||
| 31 | + | } | |||
| 32 | + | libc := purego.Dlopen(library, purego.RTLD_NOW|purego.RTLD_GLOBAL) | |||
| 33 | + | if err := purego.Dlerror(); err != "" { | |||
| 34 | + | t.Errorf("failed to dlopen: %s", err) | |||
| 35 | + | } | |||
| 36 | + | var puts func(string) | |||
| 37 | + | purego.RegisterLibFunc(&puts, libc, "puts") | |||
| 38 | + | puts("Calling C from from Go without Cgo!") | |||
| 39 | + | } | |||
R example/main.go → syscall_test.go +6 -7 renamed, 58% similar
| @@ -1,24 +1,23 @@ | |||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | - | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 2 | + | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | - | //go:build darwin || linux || windows | |
| 5 | - | ||
| 6 | - | package main | |
| 4 | + | package purego_test | |
| 7 | 5 | ||
| 8 | 6 | import ( | |
| 9 | 7 | "os" | |
| 8 | + | "testing" | |
| 10 | 9 | ||
| 11 | 10 | _ "github.com/ebitengine/purego" | |
| 12 | 11 | ) | |
| 13 | 12 | ||
| 14 | - | func main() { | |
| 13 | + | func TestOS(t *testing.T) { | |
| 15 | 14 | // set and unset an environment variable since this calls into fakecgo. | |
| 16 | 15 | err := os.Setenv("TESTING", "SOMETHING") | |
| 17 | 16 | if err != nil { | |
| 18 | - | panic(err) | |
| 17 | + | t.Errorf("failed to Setenv: %s", err) | |
| 19 | 18 | } | |
| 20 | 19 | err = os.Unsetenv("TESTING") | |
| 21 | 20 | if err != nil { | |
| 22 | - | panic(err) | |
| 21 | + | t.Errorf("failed to Unsetenv: %s", err) | |
| 23 | 22 | } | |
| 24 | 23 | } | |
| @@ -1,24 +1,23 @@ | |||||
| 1 | // SPDX-License-Identifier: Apache-2.0 | 1 | // SPDX-License-Identifier: Apache-2.0 | ||
| 2 | - | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | + | // SPDX-FileCopyrightText: 2023 The Ebitengine Authors |
| 3 | 3 | ||||
| 4 | - | //go:build darwin || linux || windows | 4 | + | package purego_test |
| 5 | - | ||||
| 6 | - | package main | |||
| 7 | 5 | ||||
| 8 | import ( | 6 | import ( | ||
| 9 | "os" | 7 | "os" | ||
| 8 | + | "testing" | |||
| 10 | 9 | ||||
| 11 | _ "github.com/ebitengine/purego" | 10 | _ "github.com/ebitengine/purego" | ||
| 12 | ) | 11 | ) | ||
| 13 | 12 | ||||
| 14 | - | func main() { | 13 | + | func TestOS(t *testing.T) { |
| 15 | // set and unset an environment variable since this calls into fakecgo. | 14 | // set and unset an environment variable since this calls into fakecgo. | ||
| 16 | err := os.Setenv("TESTING", "SOMETHING") | 15 | err := os.Setenv("TESTING", "SOMETHING") | ||
| 17 | if err != nil { | 16 | if err != nil { | ||
| 18 | - | panic(err) | 17 | + | t.Errorf("failed to Setenv: %s", err) |
| 19 | } | 18 | } | ||
| 20 | err = os.Unsetenv("TESTING") | 19 | err = os.Unsetenv("TESTING") | ||
| 21 | if err != nil { | 20 | if err != nil { | ||
| 22 | - | panic(err) | 21 | + | t.Errorf("failed to Unsetenv: %s", err) |
| 23 | } | 22 | } | ||
| 24 | } | 23 | } | ||