2018-10-14 20:11:49 +02:00
|
|
|
package fusefrontend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2020-07-11 20:15:47 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
|
|
|
|
2019-01-20 13:10:59 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2018-10-14 20:11:49 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/tests/test_helpers"
|
|
|
|
)
|
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
func TestPrepareAtSyscall(t *testing.T) {
|
2018-10-14 20:11:49 +02:00
|
|
|
cipherdir := test_helpers.InitFS(t)
|
2021-04-03 12:29:17 +02:00
|
|
|
t.Logf("cipherdir = %q", cipherdir)
|
2018-10-14 20:11:49 +02:00
|
|
|
args := Args{
|
|
|
|
Cipherdir: cipherdir,
|
|
|
|
}
|
2021-04-03 12:29:17 +02:00
|
|
|
rn := newTestFS(args)
|
2020-07-11 20:15:47 +02:00
|
|
|
out := &fuse.EntryOut{}
|
2018-10-14 20:11:49 +02:00
|
|
|
|
2021-04-03 12:29:17 +02:00
|
|
|
child, errno := rn.Mkdir(nil, "dir1", 0700, out)
|
2020-07-11 20:15:47 +02:00
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
2021-04-03 12:29:17 +02:00
|
|
|
rn.AddChild("dir1", child, false)
|
|
|
|
dir1 := toNode(child.Operations())
|
|
|
|
_, errno = dir1.Mkdir(nil, "dir2", 0700, out)
|
2020-07-11 20:15:47 +02:00
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
dirfd, cName, errno := rn.prepareAtSyscallMyself()
|
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
if cName != "." {
|
|
|
|
t.Fatal("cName should be .")
|
|
|
|
}
|
2019-01-03 15:59:54 +01:00
|
|
|
syscall.Close(dirfd)
|
|
|
|
|
|
|
|
// Again, but populate the cache for "" by looking up a non-existing file
|
2021-04-03 12:29:17 +02:00
|
|
|
rn.Lookup(nil, "xyz1234", &fuse.EntryOut{})
|
2021-06-26 16:22:37 +02:00
|
|
|
dirfd, cName, errno = rn.prepareAtSyscallMyself()
|
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2019-01-03 15:59:54 +01:00
|
|
|
}
|
|
|
|
if cName != "." {
|
|
|
|
t.Fatal("cName should be .")
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
err := syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-01-20 13:10:59 +01:00
|
|
|
err = syscallcompat.Faccessat(dirfd, ".", unix.R_OK)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
dirfd, cName, errno = rn.prepareAtSyscall("dir1")
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if cName == "" {
|
|
|
|
t.Fatal("cName should not be empty")
|
|
|
|
}
|
2019-01-20 13:10:59 +01:00
|
|
|
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
2019-01-03 17:48:04 +01:00
|
|
|
t.Error(err)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
dirfd, cName, errno = dir1.prepareAtSyscall("dir2")
|
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
if cName == "" {
|
|
|
|
t.Fatal("cName should not be empty")
|
|
|
|
}
|
2019-01-20 13:10:59 +01:00
|
|
|
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
2021-04-03 12:29:17 +02:00
|
|
|
t.Errorf("Faccessat(%d, %q): %v", dirfd, cName, err)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
|
|
|
|
n255 := strings.Repeat("n", 255)
|
2021-04-03 12:29:17 +02:00
|
|
|
dir1.Mkdir(nil, n255, 0700, out)
|
2021-06-26 16:22:37 +02:00
|
|
|
dirfd, cName, errno = dir1.prepareAtSyscall(n255)
|
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
if cName == "" {
|
|
|
|
t.Fatal("cName should not be empty")
|
|
|
|
}
|
|
|
|
if len(cName) >= 255 {
|
|
|
|
t.Fatalf("cName is too long: %q", cName)
|
|
|
|
}
|
2019-01-20 13:10:59 +01:00
|
|
|
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
2021-04-03 12:29:17 +02:00
|
|
|
t.Errorf("Faccessat(%d, %q): %v", dirfd, cName, err)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
func TestPrepareAtSyscallPlaintextnames(t *testing.T) {
|
2018-10-14 20:11:49 +02:00
|
|
|
cipherdir := test_helpers.InitFS(t, "-plaintextnames")
|
|
|
|
args := Args{
|
|
|
|
Cipherdir: cipherdir,
|
|
|
|
PlaintextNames: true,
|
|
|
|
}
|
2021-06-26 16:22:37 +02:00
|
|
|
rn := newTestFS(args)
|
2020-07-11 20:15:47 +02:00
|
|
|
out := &fuse.EntryOut{}
|
2018-10-14 20:11:49 +02:00
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
child, errno := rn.Mkdir(nil, "dir1", 0700, out)
|
2020-07-11 20:15:47 +02:00
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
2021-06-26 16:22:37 +02:00
|
|
|
rn.AddChild("dir1", child, false)
|
|
|
|
dir1 := toNode(child.Operations())
|
|
|
|
_, errno = dir1.Mkdir(nil, "dir2", 0700, out)
|
2020-07-11 20:15:47 +02:00
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
dirfd, cName, errno := rn.prepareAtSyscallMyself()
|
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
if cName != "." {
|
|
|
|
t.Fatal("cName should be .")
|
|
|
|
}
|
2021-06-26 16:22:37 +02:00
|
|
|
err := syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-01-20 13:10:59 +01:00
|
|
|
err = syscallcompat.Faccessat(dirfd, ".", unix.R_OK)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
dirfd, cName, errno = rn.prepareAtSyscall("dir1")
|
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
if cName != "dir1" {
|
|
|
|
t.Fatalf("wrong cName: %q", cName)
|
|
|
|
}
|
2019-01-20 13:10:59 +01:00
|
|
|
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
|
2021-06-26 16:22:37 +02:00
|
|
|
dirfd, cName, errno = dir1.prepareAtSyscall("dir2")
|
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
2018-10-14 20:11:49 +02:00
|
|
|
}
|
|
|
|
if cName != "dir2" {
|
|
|
|
t.Fatalf("wrong cName: %q", cName)
|
|
|
|
}
|
2019-01-20 13:10:59 +01:00
|
|
|
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
|
2018-10-14 20:11:49 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
}
|