Files
cgofuse/fuse/host_test.go
2025-07-22 20:10:14 -03:00

98 lines
1.7 KiB
Go

//go:build unix
package fuse
import (
"os"
"path/filepath"
"syscall"
"testing"
)
type testfs struct {
FileSystemBase
t *testing.T
init, dstr int
}
func (selffs *testfs) Init() {
if selffs.t != nil {
selffs.t.Log("Init() called more than once")
}
selffs.init++
}
func (selffs *testfs) Destroy() {
if selffs.t != nil {
selffs.t.Log("Destroy() called more than once")
}
selffs.dstr++
}
func (selffs *testfs) Getattr(path string, stat *Stat_t, fh uint64) (errc int) {
if selffs.t != nil {
selffs.t.Logf("Getattr(%s)", path)
}
switch path {
case "/":
stat.Mode = S_IFDIR | 0555
return 0
default:
return -ENOENT
}
}
func (selffs *testfs) Readdir(path string,
fill func(name string, stat *Stat_t, ofst int64) bool,
ofst int64,
fh uint64) (errc int) {
if selffs.t != nil {
selffs.t.Logf("Readdir(%s)", path)
}
fill(".", nil, 0)
fill("..", nil, 0)
return 0
}
func testHost(t *testing.T, unmount bool) {
mntp := filepath.Join(t.TempDir(), "m")
if err := os.Mkdir(mntp, os.FileMode(0755)); err != nil {
panic(err)
}
// defer os.Remove(mntp)
tstf := &testfs{t: t}
host := NewFileSystemHost(tstf)
mres := host.Mount(mntp)
_, _ = os.ReadDir(mntp)
ures := error(nil)
if unmount {
ures = host.Unmount()
} else {
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
ures = host.Done()
}
if mres != nil {
t.Errorf("Mount failed: %s", mres)
}
if ures != nil {
t.Errorf("Unmount failed: %s", ures)
}
if tstf.init != 1 {
t.Errorf("Init() called %v times; expected 1", tstf.init)
}
if tstf.dstr != 1 {
t.Errorf("Destroy() called %v times; expected 1", tstf.dstr)
}
}
func TestUnmount(t *testing.T) {
testHost(t, true)
}
func TestSignal(t *testing.T) {
testHost(t, false)
}