File: syscall_test.go

package info (click to toggle)
golang-github-ebitengine-purego 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,452 kB
  • sloc: asm: 31,862; ansic: 1,001; cpp: 8; makefile: 3
file content (56 lines) | stat: -rw-r--r-- 1,317 bytes parent folder | download
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors

package purego_test

import (
	"os"
	"runtime"
	"testing"
	"unsafe"

	"github.com/ebitengine/purego"
	"github.com/ebitengine/purego/internal/load"
)

func TestOS(t *testing.T) {
	// set and unset an environment variable since this calls into fakecgo.
	err := os.Setenv("TESTING", "SOMETHING")
	if err != nil {
		t.Errorf("failed to Setenv: %s", err)
	}
	err = os.Unsetenv("TESTING")
	if err != nil {
		t.Errorf("failed to Unsetenv: %s", err)
	}
}

func TestErrno(t *testing.T) {
	if runtime.GOOS != "darwin" {
		t.Skip("platform does not support returning errno from syscall")
	}

	libc, err := load.OpenLibrary("/usr/lib/libSystem.B.dylib")
	if err != nil {
		t.Fatal(err)
	}

	openSym, err := load.OpenSymbol(libc, "open")
	if err != nil {
		t.Fatal(err)
	}

	r1, _, errno := purego.SyscallN(openSym, uintptr(unsafe.Pointer(&[]byte("_file_that_does_not_exist_\x00")[0])), uintptr(os.O_RDWR))
	if int32(r1) != -1 {
		t.Errorf("open returned %d, wanted -1", r1)
	}

	var strerror func(int32) string
	purego.RegisterLibFunc(&strerror, libc, "strerror")

	const expected = "No such file or directory"
	got := strerror(int32(errno))
	if got != expected {
		t.Errorf("strerror returned %q, wanted \"%s\"", got, expected)
	}
}