aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/wayland/conn_test.go
blob: 30c66cb23f30bc8bdddfaf4a69dd62acc61f47a5 (plain)
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
57
58
59
60
61
62
63
64
65
66
package wayland

import (
	"errors"
	"os"
	"path/filepath"
	"reflect"
	"syscall"
	"testing"

	"hakurei.app/check"
)

func TestSecurityContextClose(t *testing.T) {
	// do not parallel: fd test not thread safe

	if err := (*SecurityContext)(nil).Close(); !reflect.DeepEqual(err, os.ErrInvalid) {
		t.Fatalf("Close: error = %v", err)
	}

	var ctx SecurityContext
	if f, err := os.Create(filepath.Join(t.TempDir(), "remove")); err != nil {
		t.Fatal(err)
	} else {
		ctx.bindPath = check.MustAbs(f.Name())
	}
	if err := syscall.Pipe2(ctx.closeFds[0:], syscall.O_CLOEXEC); err != nil {
		t.Fatalf("Pipe: error = %v", err)
	}
	t.Cleanup(func() { _ = syscall.Close(ctx.closeFds[0]); _ = syscall.Close(ctx.closeFds[1]) })

	if err := ctx.Close(); err != nil {
		t.Fatalf("Close: error = %v", err)
	} else if _, err = os.Stat(ctx.bindPath.String()); err == nil || !errors.Is(err, os.ErrNotExist) {
		t.Fatalf("Did not remove %q", ctx.bindPath)
	}

	wantErr := &Error{Cause: RCleanup, Path: ctx.bindPath.String(), Errno: errors.Join(syscall.EBADF, syscall.EBADF, &os.PathError{
		Op:   "remove",
		Path: ctx.bindPath.String(),
		Err:  syscall.ENOENT,
	})}
	if err := ctx.Close(); !reflect.DeepEqual(err, wantErr) {
		t.Fatalf("Close: error = %#v, want %#v", err, wantErr)
	}
}

func TestNewEnsure(t *testing.T) {
	existingDirPath := check.MustAbs(t.TempDir()).Append("dir")
	if err := os.MkdirAll(existingDirPath.String(), 0700); err != nil {
		t.Fatal(err)
	}
	nonexistent := check.MustAbs("/proc/nonexistent")

	wantErr := &Error{RCreate, existingDirPath.String(), nonexistent.String(), &os.PathError{
		Op:   "open",
		Path: existingDirPath.String(),
		Err:  syscall.EISDIR,
	}}
	if _, err := New(
		nonexistent,
		existingDirPath, "", "",
	); !reflect.DeepEqual(err, wantErr) {
		t.Fatalf("New: error = %#v, want %#v", err, wantErr)
	}
}