Files
cgofuse/fuse/host_test.go
2025-07-21 00:21:22 -03:00

97 lines
1.6 KiB
Go

package fuse
import (
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
type testfs struct {
FileSystemBase
init, dstr int
}
func (selffs *testfs) Init() {
selffs.init++
}
func (selffs *testfs) Destroy() {
selffs.dstr++
}
func (selffs *testfs) Getattr(path string, stat *Stat_t, fh uint64) (errc int) {
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) {
fill(".", nil, 0)
fill("..", nil, 0)
return 0
}
func testHost(t *testing.T, unmount bool) {
path, err := os.MkdirTemp("", "test")
if nil != err {
panic(err)
}
defer os.Remove(path)
mntp := filepath.Join(path, "m")
if runtime.GOOS != "windows" {
err = os.Mkdir(mntp, os.FileMode(0755))
if nil != err {
panic(err)
}
defer os.Remove(mntp)
}
done := make(chan bool)
tmch := time.After(3 * time.Second)
tstf := &testfs{}
host := NewFileSystemHost(tstf)
mres := error(nil)
ures := error(nil)
go func() {
mres = host.Mount(mntp, nil)
done <- true
}()
<-tmch
if unmount {
ures = host.Unmount()
} else {
ures = sendInterrupt()
}
<-done
if mres != nil {
t.Errorf("Mount failed: %s", err)
}
if ures != nil {
t.Errorf("Unmount failed: %s", err)
}
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) {
if runtime.GOOS != "windows" {
testHost(t, false)
}
}