2016-06-06 23:57:42 +02:00
|
|
|
package test_helpers
|
2015-11-12 21:02:44 +01:00
|
|
|
|
|
|
|
import (
|
2017-05-07 21:01:39 +02:00
|
|
|
"bytes"
|
2015-11-12 21:02:44 +01:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2016-11-10 23:32:51 +01:00
|
|
|
"encoding/json"
|
2015-11-14 17:16:17 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-12-06 23:03:37 +01:00
|
|
|
"log"
|
2016-11-10 23:32:51 +01:00
|
|
|
"net"
|
2015-11-14 17:16:17 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-02-07 13:28:55 +01:00
|
|
|
"path/filepath"
|
2015-12-13 20:10:52 +01:00
|
|
|
"syscall"
|
2015-11-12 21:02:44 +01:00
|
|
|
"testing"
|
2016-11-10 23:32:51 +01:00
|
|
|
"time"
|
2015-11-27 23:34:55 +01:00
|
|
|
|
2016-11-10 23:32:51 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/ctlsock"
|
2016-02-06 19:20:54 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
2019-01-03 13:32:13 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2015-11-12 21:02:44 +01:00
|
|
|
)
|
|
|
|
|
2018-10-10 22:24:20 +02:00
|
|
|
// TmpDir will be created inside this directory, set in init() to
|
|
|
|
// $TMPDIR/gocryptfs-test-parent .
|
|
|
|
var testParentDir = ""
|
2016-10-02 06:14:18 +02:00
|
|
|
|
|
|
|
// GocryptfsBinary is the assumed path to the gocryptfs build.
|
2016-06-06 23:57:42 +02:00
|
|
|
const GocryptfsBinary = "../../gocryptfs"
|
2015-11-15 13:38:19 +01:00
|
|
|
|
2017-05-07 12:22:15 +02:00
|
|
|
// UnmountScript is the fusermount/umount compatibility wrapper script
|
2017-02-15 23:13:33 +01:00
|
|
|
const UnmountScript = "../fuse-unmount.bash"
|
|
|
|
|
2017-05-07 21:01:39 +02:00
|
|
|
// X255 contains 255 uppercase "X". This can be used as a maximum-length filename.
|
|
|
|
var X255 string
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// TmpDir is a unique temporary directory. "go test" runs package tests in parallel. We create a
|
|
|
|
// unique TmpDir in init() so the tests do not interfere.
|
2016-06-30 00:57:14 +02:00
|
|
|
var TmpDir string
|
2016-09-24 22:45:25 +02:00
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// DefaultPlainDir is TmpDir + "/default-plain"
|
2016-06-30 00:57:14 +02:00
|
|
|
var DefaultPlainDir string
|
2016-09-24 22:45:25 +02:00
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// DefaultCipherDir is TmpDir + "/default-cipher"
|
2016-06-30 00:57:14 +02:00
|
|
|
var DefaultCipherDir string
|
|
|
|
|
2018-10-10 22:24:20 +02:00
|
|
|
// SwitchTMPDIR changes TMPDIR and hence the directory the test are performed in.
|
|
|
|
// This is used when you want to perform tests on a special filesystem. The
|
|
|
|
// xattr tests cannot run on tmpfs and use /var/tmp instead of /tmp.
|
|
|
|
func SwitchTMPDIR(newDir string) {
|
|
|
|
os.Setenv("TMPDIR", newDir)
|
2018-03-24 21:37:41 +01:00
|
|
|
doInit()
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:57:14 +02:00
|
|
|
func init() {
|
2018-03-24 21:37:41 +01:00
|
|
|
doInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func doInit() {
|
2017-05-07 21:01:39 +02:00
|
|
|
X255 = string(bytes.Repeat([]byte("X"), 255))
|
2019-01-01 22:01:49 +01:00
|
|
|
MountInfo = make(map[string]mountInfo)
|
2017-05-07 21:01:39 +02:00
|
|
|
|
2018-10-10 22:24:20 +02:00
|
|
|
testParentDir := os.TempDir() + "/gocryptfs-test-parent"
|
2016-06-30 00:57:14 +02:00
|
|
|
os.MkdirAll(testParentDir, 0700)
|
|
|
|
var err error
|
|
|
|
TmpDir, err = ioutil.TempDir(testParentDir, "")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
DefaultPlainDir = TmpDir + "/default-plain"
|
|
|
|
DefaultCipherDir = TmpDir + "/default-cipher"
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// ResetTmpDir deletes TmpDir, create new dir tree:
|
2016-06-16 21:06:03 +02:00
|
|
|
//
|
|
|
|
// TmpDir
|
|
|
|
// |-- DefaultPlainDir
|
|
|
|
// *-- DefaultCipherDir
|
|
|
|
// *-- gocryptfs.diriv
|
2016-10-08 19:22:59 +02:00
|
|
|
func ResetTmpDir(createDirIV bool) {
|
2016-06-30 00:57:14 +02:00
|
|
|
// Try to unmount and delete everything
|
2016-06-06 23:57:42 +02:00
|
|
|
entries, err := ioutil.ReadDir(TmpDir)
|
2016-04-10 19:43:37 +02:00
|
|
|
if err == nil {
|
|
|
|
for _, e := range entries {
|
2016-06-30 00:57:14 +02:00
|
|
|
d := filepath.Join(TmpDir, e.Name())
|
|
|
|
err = os.Remove(d)
|
|
|
|
if err != nil {
|
2016-09-25 14:57:04 +02:00
|
|
|
pe := err.(*os.PathError)
|
|
|
|
if pe.Err == syscall.EBUSY {
|
|
|
|
if testing.Verbose() {
|
|
|
|
fmt.Printf("Remove failed: %v. Maybe still mounted?\n", pe)
|
|
|
|
}
|
|
|
|
err = UnmountErr(d)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else if pe.Err != syscall.ENOTEMPTY {
|
|
|
|
panic("Unhandled error: " + pe.Err.Error())
|
2016-07-11 20:40:53 +02:00
|
|
|
}
|
2016-07-11 20:31:36 +02:00
|
|
|
err = os.RemoveAll(d)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-06-30 00:57:14 +02:00
|
|
|
}
|
2016-04-10 19:43:37 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-30 00:57:14 +02:00
|
|
|
err = os.Mkdir(DefaultPlainDir, 0700)
|
2015-11-15 15:05:15 +01:00
|
|
|
if err != nil {
|
2016-06-30 00:57:14 +02:00
|
|
|
panic(err)
|
2015-11-15 15:05:15 +01:00
|
|
|
}
|
2016-06-30 00:57:14 +02:00
|
|
|
err = os.Mkdir(DefaultCipherDir, 0700)
|
2015-11-12 21:02:44 +01:00
|
|
|
if err != nil {
|
2016-06-30 00:57:14 +02:00
|
|
|
panic(err)
|
2015-11-12 21:02:44 +01:00
|
|
|
}
|
2016-10-08 19:22:59 +02:00
|
|
|
if createDirIV {
|
2019-01-03 13:32:13 +01:00
|
|
|
// Open cipherdir (following symlinks)
|
2019-01-03 18:11:07 +01:00
|
|
|
dirfd, err := syscall.Open(DefaultCipherDir, syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
|
2019-01-03 13:32:13 +01:00
|
|
|
if err == nil {
|
|
|
|
err = nametransform.WriteDirIVAt(dirfd)
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
}
|
2016-02-07 13:28:55 +01:00
|
|
|
if err != nil {
|
2016-06-30 00:57:14 +02:00
|
|
|
panic(err)
|
2016-02-07 13:28:55 +01:00
|
|
|
}
|
2015-11-27 21:50:11 +01:00
|
|
|
}
|
2015-11-12 21:02:44 +01:00
|
|
|
}
|
|
|
|
|
2016-06-16 21:06:03 +02:00
|
|
|
// InitFS calls "gocryptfs -init" on a new directory in TmpDir, passing
|
2016-09-24 22:45:25 +02:00
|
|
|
// "extraArgs" in addition to useful defaults.
|
2016-06-27 21:39:02 +02:00
|
|
|
//
|
|
|
|
// The returned cipherdir has NO trailing slash.
|
2016-06-16 21:06:03 +02:00
|
|
|
func InitFS(t *testing.T, extraArgs ...string) string {
|
|
|
|
dir, err := ioutil.TempDir(TmpDir, "")
|
|
|
|
if err != nil {
|
2017-12-06 23:03:37 +01:00
|
|
|
if t != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2016-06-16 21:06:03 +02:00
|
|
|
}
|
|
|
|
args := []string{"-q", "-init", "-extpass", "echo test", "-scryptn=10"}
|
|
|
|
args = append(args, extraArgs...)
|
|
|
|
args = append(args, dir)
|
|
|
|
|
|
|
|
cmd := exec.Command(GocryptfsBinary, args...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2017-12-06 23:03:37 +01:00
|
|
|
if t != nil {
|
|
|
|
t.Fatalf("InitFS with args %v failed: %v", args, err)
|
|
|
|
} else {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2016-06-16 21:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Md5fn returns an md5 string for file "filename"
|
2016-06-06 23:57:42 +02:00
|
|
|
func Md5fn(filename string) string {
|
2015-11-12 21:02:44 +01:00
|
|
|
buf, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ReadFile: %v\n", err)
|
|
|
|
return ""
|
|
|
|
}
|
2016-06-06 23:57:42 +02:00
|
|
|
return Md5hex(buf)
|
2015-11-12 21:02:44 +01:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Md5hex returns an md5 string for "buf"
|
2016-06-06 23:57:42 +02:00
|
|
|
func Md5hex(buf []byte) string {
|
2015-11-12 21:02:44 +01:00
|
|
|
rawHash := md5.Sum(buf)
|
|
|
|
hash := hex.EncodeToString(rawHash[:])
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// VerifySize checks that the file size equals "want". This checks:
|
2015-11-12 21:02:44 +01:00
|
|
|
// 1) Size reported by Stat()
|
|
|
|
// 2) Number of bytes returned when reading the whole file
|
2016-06-06 23:57:42 +02:00
|
|
|
func VerifySize(t *testing.T, path string, want int) {
|
2015-11-12 21:02:44 +01:00
|
|
|
buf, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ReadFile failed: %v", err)
|
|
|
|
} else if len(buf) != want {
|
|
|
|
t.Errorf("wrong read size: got=%d want=%d", len(buf), want)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Stat failed: %v", err)
|
|
|
|
} else if fi.Size() != int64(want) {
|
|
|
|
t.Errorf("wrong stat file size, got=%d want=%d", fi.Size(), want)
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 20:02:18 +01:00
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// TestMkdirRmdir creates and deletes a directory
|
2016-06-06 23:57:42 +02:00
|
|
|
func TestMkdirRmdir(t *testing.T, plainDir string) {
|
2016-06-30 00:57:14 +02:00
|
|
|
dir := plainDir + "/dir1"
|
2015-12-10 20:02:18 +01:00
|
|
|
err := os.Mkdir(dir, 0777)
|
|
|
|
if err != nil {
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-10 20:02:18 +01:00
|
|
|
}
|
|
|
|
err = syscall.Rmdir(dir)
|
|
|
|
if err != nil {
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-10 20:02:18 +01:00
|
|
|
}
|
2018-04-27 20:19:51 +02:00
|
|
|
// Create a directory and put a file in it
|
|
|
|
// Trying to rmdir it should fail with ENOTEMPTY
|
|
|
|
err = os.Mkdir(dir, 0777)
|
|
|
|
if err != nil {
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
f, err := os.Create(dir + "/file")
|
|
|
|
if err != nil {
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
err = syscall.Rmdir(dir)
|
|
|
|
errno := err.(syscall.Errno)
|
|
|
|
if errno != syscall.ENOTEMPTY {
|
2018-04-27 20:19:51 +02:00
|
|
|
t.Errorf("Should have gotten ENOTEMPTY, got %v", errno)
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
2018-04-27 20:19:51 +02:00
|
|
|
err = syscall.Unlink(dir + "/file")
|
|
|
|
if err != nil {
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
2018-04-27 20:19:51 +02:00
|
|
|
err = syscall.Rmdir(dir)
|
|
|
|
if err != nil {
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
// We should also be able to remove a directory we do not have permissions to
|
|
|
|
// read or write
|
|
|
|
err = os.Mkdir(dir, 0000)
|
|
|
|
if err != nil {
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
err = syscall.Rmdir(dir)
|
|
|
|
if err != nil {
|
2016-04-10 19:43:37 +02:00
|
|
|
// Make sure the directory can cleaned up by the next test run
|
|
|
|
os.Chmod(dir, 0700)
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
2015-12-10 20:02:18 +01:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// TestRename creates and renames a file
|
2016-06-06 23:57:42 +02:00
|
|
|
func TestRename(t *testing.T, plainDir string) {
|
2016-06-30 00:57:14 +02:00
|
|
|
file1 := plainDir + "/rename1"
|
|
|
|
file2 := plainDir + "/rename2"
|
2015-12-10 20:02:18 +01:00
|
|
|
err := ioutil.WriteFile(file1, []byte("content"), 0777)
|
|
|
|
if err != nil {
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-10 20:02:18 +01:00
|
|
|
}
|
|
|
|
err = syscall.Rename(file1, file2)
|
|
|
|
if err != nil {
|
2016-11-26 15:17:15 +01:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2015-12-10 20:02:18 +01:00
|
|
|
}
|
|
|
|
syscall.Unlink(file2)
|
|
|
|
}
|
2016-02-07 10:55:13 +01:00
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// VerifyExistence checks in 3 ways that "path" exists:
|
2018-08-11 22:37:22 +02:00
|
|
|
// stat, open, readdir. Returns true if the path exists, false otherwise.
|
|
|
|
// Panics if the result is inconsistent.
|
2016-06-06 23:57:42 +02:00
|
|
|
func VerifyExistence(path string) bool {
|
2019-01-04 17:32:27 +01:00
|
|
|
// Check if file can be stat()ed
|
2018-08-11 22:37:22 +02:00
|
|
|
stat := true
|
2019-01-04 17:32:27 +01:00
|
|
|
fi, err := os.Stat(path)
|
2016-02-07 10:55:13 +01:00
|
|
|
if err != nil {
|
2018-08-11 22:37:22 +02:00
|
|
|
stat = false
|
2016-02-07 10:55:13 +01:00
|
|
|
}
|
2019-01-04 17:32:27 +01:00
|
|
|
// Check if file can be opened
|
2018-08-11 22:37:22 +02:00
|
|
|
open := true
|
2016-02-07 10:55:13 +01:00
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
2018-08-11 22:37:22 +02:00
|
|
|
open = false
|
2016-02-07 10:55:13 +01:00
|
|
|
}
|
|
|
|
fd.Close()
|
2019-01-04 17:32:27 +01:00
|
|
|
// Check if file shows up in directory listing
|
2018-08-11 22:37:22 +02:00
|
|
|
readdir := false
|
2016-02-07 10:55:13 +01:00
|
|
|
dir := filepath.Dir(path)
|
|
|
|
name := filepath.Base(path)
|
2019-01-04 17:32:27 +01:00
|
|
|
d, err := os.Open(dir)
|
|
|
|
if err != nil && open == true {
|
|
|
|
log.Panicf("we can open the file but not the parent dir!? err=%v", err)
|
|
|
|
} else if err == nil {
|
|
|
|
defer d.Close()
|
|
|
|
listing, err := d.Readdirnames(0)
|
|
|
|
if stat && fi.IsDir() && err != nil {
|
|
|
|
log.Panicf("It's a directory, but readdirnames failed: %v", err)
|
|
|
|
}
|
|
|
|
for _, entry := range listing {
|
|
|
|
if entry == name {
|
2018-08-11 22:37:22 +02:00
|
|
|
readdir = true
|
|
|
|
}
|
2016-02-07 10:55:13 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-11 22:37:22 +02:00
|
|
|
// If the result is consistent, return it.
|
|
|
|
if stat == open && open == readdir {
|
|
|
|
return stat
|
|
|
|
}
|
2019-01-04 17:32:27 +01:00
|
|
|
log.Panicf("inconsistent result on %q: stat=%v open=%v readdir=%v, path=%q", name, stat, open, readdir, path)
|
2016-02-07 10:55:13 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-07-02 19:43:57 +02:00
|
|
|
|
|
|
|
// Du returns the disk usage of the file "fd" points to, in bytes.
|
|
|
|
// Same as "du --block-size=1".
|
2016-10-04 21:48:53 +02:00
|
|
|
func Du(t *testing.T, fd int) (nBytes int64) {
|
2016-07-02 19:43:57 +02:00
|
|
|
var st syscall.Stat_t
|
|
|
|
err := syscall.Fstat(fd, &st)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-04 21:48:53 +02:00
|
|
|
// st.Blocks = number of 512-byte blocks
|
|
|
|
return st.Blocks * 512
|
2016-07-02 19:43:57 +02:00
|
|
|
}
|
2016-11-10 23:32:51 +01:00
|
|
|
|
|
|
|
// QueryCtlSock sends a request to the control socket at "socketPath" and
|
|
|
|
// returns the response.
|
|
|
|
func QueryCtlSock(t *testing.T, socketPath string, req ctlsock.RequestStruct) (response ctlsock.ResponseStruct) {
|
|
|
|
conn, err := net.DialTimeout("unix", socketPath, 1*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
conn.SetDeadline(time.Now().Add(time.Second))
|
|
|
|
msg, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = conn.Write(msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-16 18:47:04 +01:00
|
|
|
buf := make([]byte, ctlsock.ReadBufSize)
|
2016-11-10 23:32:51 +01:00
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buf = buf[:n]
|
|
|
|
json.Unmarshal(buf, &response)
|
|
|
|
return response
|
|
|
|
}
|
2018-04-01 21:23:32 +02:00
|
|
|
|
2018-04-08 20:24:29 +02:00
|
|
|
// ExtractCmdExitCode extracts the exit code from an error value that was
|
|
|
|
// returned from exec / cmd.Run()
|
2018-04-01 21:23:32 +02:00
|
|
|
func ExtractCmdExitCode(err error) int {
|
|
|
|
if err == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
// OMG this is convoluted
|
2018-06-07 22:50:30 +02:00
|
|
|
if err2, ok := err.(*exec.ExitError); ok {
|
|
|
|
return err2.Sys().(syscall.WaitStatus).ExitStatus()
|
|
|
|
}
|
|
|
|
if err2, ok := err.(*os.PathError); ok {
|
|
|
|
return int(err2.Err.(syscall.Errno))
|
|
|
|
}
|
|
|
|
log.Panicf("could not decode error %#v", err)
|
|
|
|
return 0
|
2018-04-01 21:23:32 +02:00
|
|
|
}
|