libgocryptfs/internal/fusefrontend/names_test.go
Jakob Unterwurzacher 932efbd459 fusefrontend: make openBackingDir() symlink-safe
openBackingDir() used encryptPath(), which is not symlink-safe
itself. Drop encryptPath() and implement our own directory walk.

Adds three seconds to untar and two seconds to rm:

$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.MzG: gocryptfs v1.6-36-g8fb3c2f-dirty; go-fuse v20170619-66-g6df8ddc; 2018-10-14 go1.11
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 1.25078 s, 210 MB/s
READ:  262144000 bytes (262 MB, 250 MiB) copied, 1.0318 s, 254 MB/s
UNTAR: 20.941
MD5:   11.568
LS:    1.638
RM:    5.337
2019-01-01 16:24:09 +01:00

153 lines
2.9 KiB
Go

package fusefrontend
import (
"strings"
"syscall"
"testing"
"golang.org/x/sys/unix"
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
"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 .")
}
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
if err != nil {
t.Error(err)
}
err = syscallcompat.Faccessat(dirfd, ".", unix.R_OK)
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")
}
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
if err != nil {
t.Error(err)
}
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")
}
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
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)
}
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
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 .")
}
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
if err != nil {
t.Error(err)
}
err = syscallcompat.Faccessat(dirfd, ".", unix.R_OK)
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)
}
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
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)
}
err = syscallcompat.Faccessat(dirfd, cName, unix.R_OK)
if err != nil {
t.Error(err)
}
syscall.Close(dirfd)
}