2018-10-14 20:11:49 +02:00
|
|
|
package fusefrontend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOpenBackingDir(t *testing.T) {
|
|
|
|
cipherdir := test_helpers.InitFS(t)
|
|
|
|
args := Args{
|
|
|
|
Cipherdir: cipherdir,
|
|
|
|
}
|
|
|
|
fs := newTestFS(args)
|
|
|
|
|
|
|
|
code := fs.Mkdir("dir1", 0700, nil)
|
|
|
|
if !code.Ok() {
|
|
|
|
t.Fatal(code)
|
|
|
|
}
|
|
|
|
code = fs.Mkdir("dir1/dir2", 0700, nil)
|
|
|
|
if !code.Ok() {
|
|
|
|
t.Fatal(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
dirfd, cName, err := fs.openBackingDir("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
fs.GetAttr("xyz1234", nil)
|
|
|
|
dirfd, cName, err = fs.openBackingDir("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if cName != "." {
|
|
|
|
t.Fatal("cName should be .")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
dirfd, cName, err = fs.openBackingDir("dir1")
|
|
|
|
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)
|
|
|
|
|
|
|
|
dirfd, cName, err = fs.openBackingDir("dir1/dir2")
|
|
|
|
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 {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
|
|
|
|
n255 := strings.Repeat("n", 255)
|
|
|
|
path := "dir1/" + n255
|
|
|
|
fs.Mkdir(path, 0700, nil)
|
|
|
|
dirfd, cName, err = fs.openBackingDir(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenBackingDirPlaintextNames(t *testing.T) {
|
|
|
|
cipherdir := test_helpers.InitFS(t, "-plaintextnames")
|
|
|
|
args := Args{
|
|
|
|
Cipherdir: cipherdir,
|
|
|
|
PlaintextNames: true,
|
|
|
|
}
|
|
|
|
fs := newTestFS(args)
|
|
|
|
|
|
|
|
code := fs.Mkdir("dir1", 0700, nil)
|
|
|
|
if !code.Ok() {
|
|
|
|
t.Fatal(code)
|
|
|
|
}
|
|
|
|
code = fs.Mkdir("dir1/dir2", 0700, nil)
|
|
|
|
if !code.Ok() {
|
|
|
|
t.Fatal(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
dirfd, cName, err := fs.openBackingDir("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if cName != "." {
|
|
|
|
t.Fatal("cName should be .")
|
|
|
|
}
|
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)
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
dirfd, cName, err = fs.openBackingDir("dir1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
|
|
|
dirfd, cName, err = fs.openBackingDir("dir1/dir2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|