tests: add example_filesystems, test password and -masterkey mount

This commit is contained in:
Jakob Unterwurzacher 2015-11-15 15:05:15 +01:00
parent 296bdf3af2
commit 40882c6e49
6 changed files with 115 additions and 45 deletions

View File

@ -0,0 +1,12 @@
{
"EncryptedKey": "He757VFOKOWbMJqJ7HBs67SMSi3Vu8/2vgWNI6j1tVo4JBlNvrQSw6KkCh0lGrHrh6ICbPv4MyoyFdGa",
"ScryptObject": {
"Salt": "MeHSsxsnJwngAwptNzuXQlj7JtF1b0uzZuWvVV3cH3w=",
"N": 65536,
"R": 8,
"P": 1,
"KeyLen": 32
},
"Version": 2,
"FeatureFlags": null
}

View File

@ -0,0 +1,46 @@
package integration_tests
// Mount example filesystems and check that the file "status.txt" is there
import (
"io/ioutil"
"os"
"testing"
)
const statusTxtContent = "It works!\n"
// checkStatusTxt - read file "filename" and verify that it contains
// "It works!\n"
func checkStatusTxt(t *testing.T, filename string) {
contentBytes, err := ioutil.ReadFile(filename)
if err != nil {
t.Error(err)
}
content := string(contentBytes)
if content != statusTxtContent {
t.Errorf("Unexpected content: %s\n", content)
}
}
// Test example_filesystems/normal
// with password mount and -masterkey mount
func TestExampleFsNormal(t *testing.T) {
pDir := tmpDir + "TestExampleFsNormal/"
cDir := "example_filesystems/normal"
err := os.Mkdir(pDir, 0777)
if err != nil {
t.Fatal(err)
}
mount(cDir, pDir, "-extpass", "echo test")
checkStatusTxt(t, pDir + "status.txt")
unmount(pDir)
mount(cDir, pDir, "-masterkey", "74676e34-0b47c145-00dac61a-17a92316-"+
"bb57044c-e205b71f-65f4fdca-7cabd4b3")
checkStatusTxt(t, pDir + "status.txt")
unmount(pDir)
err = os.Remove(pDir)
if err != nil {
t.Error(err)
}
}

View File

@ -12,51 +12,63 @@ import (
// Note: the code assumes that all have a trailing slash
const tmpDir = "/tmp/gocryptfs_main_test/"
const plainDir = tmpDir + "plain/"
const cipherDir = tmpDir + "cipher/"
const defaultPlainDir = tmpDir + "plain/"
const defaultCipherDir = tmpDir + "cipher/"
const gocryptfsBinary = "../gocryptfs"
func resetTmpDir() {
fu := exec.Command("fusermount", "-z", "-u", plainDir)
fu := exec.Command("fusermount", "-z", "-u", defaultPlainDir)
fu.Run()
os.RemoveAll(tmpDir)
err := os.MkdirAll(plainDir, 0777)
err := os.RemoveAll(tmpDir)
if err != nil {
panic("Could not create plainDir")
fmt.Println(err)
os.Exit(1)
}
err = os.MkdirAll(cipherDir, 0777)
err = os.MkdirAll(defaultPlainDir, 0777)
if err != nil {
panic("Could not create cipherDir")
fmt.Println(err)
os.Exit(1)
}
}
func mount(extraArgs ...string) {
var args []string
args = append(args, extraArgs...)
//args = append(args, "--fusedebug")
args = append(args, cipherDir)
args = append(args, plainDir)
c := exec.Command(gocryptfsBinary, args...)
if testing.Verbose() {
c.Stdout = os.Stdout
c.Stderr = os.Stderr
}
err := c.Run()
err = os.MkdirAll(defaultCipherDir, 0777)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func unmount() error {
fu := exec.Command("fusermount", "-z", "-u", plainDir)
// mount CIPHERDIR "c" on PLAINDIR "p"
func mount(c string, p string, extraArgs ...string) {
var args []string
args = append(args, extraArgs...)
//args = append(args, "--fusedebug")
args = append(args, c)
args = append(args, p)
cmd := exec.Command(gocryptfsBinary, args...)
if testing.Verbose() {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
err := cmd.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// unmount PLAINDIR "p"
func unmount(p string) error {
fu := exec.Command("fusermount", "-u", "-z", p)
fu.Stdout = os.Stdout
fu.Stderr = os.Stderr
return fu.Run()
err := fu.Run()
if err != nil {
fmt.Println(err)
}
return err
}
// Return md5 string for file "filename"

View File

@ -25,9 +25,9 @@ func TestMain(m *testing.M) {
fmt.Printf("***** Testing with OpenSSL\n")
}
resetTmpDir()
mount("--zerokey")
mount(defaultCipherDir, defaultPlainDir, "--zerokey")
r := m.Run()
unmount()
unmount(defaultPlainDir)
if defaultonly {
os.Exit(r)
@ -37,25 +37,25 @@ func TestMain(m *testing.M) {
fmt.Printf("***** Testing with native Go crypto\n")
}
resetTmpDir()
mount("--zerokey", "--openssl=false")
mount(defaultCipherDir, defaultPlainDir, "--zerokey", "--openssl=false")
r = m.Run()
unmount()
unmount(defaultPlainDir)
if testing.Verbose() {
fmt.Printf("***** Testing \"--plaintextnames\"\n")
}
resetTmpDir()
mount("--zerokey", "--plaintextnames")
mount(defaultCipherDir, defaultPlainDir, "--zerokey", "--plaintextnames")
plaintextNames = true
r = m.Run()
unmount()
unmount(defaultPlainDir)
os.Exit(r)
}
// Write "n" zero bytes to filename "fn", read again, compare hash
func testWriteN(t *testing.T, fn string, n int) string {
file, err := os.Create(plainDir + fn)
file, err := os.Create(defaultPlainDir + fn)
if err != nil {
t.FailNow()
}
@ -68,12 +68,12 @@ func testWriteN(t *testing.T, fn string, n int) string {
}
file.Close()
verifySize(t, plainDir+fn, n)
verifySize(t, defaultPlainDir+fn, n)
bin := md5.Sum(d)
hashWant := hex.EncodeToString(bin[:])
hashActual := md5fn(plainDir + fn)
hashActual := md5fn(defaultPlainDir + fn)
if hashActual != hashWant {
t.Errorf("Wrong content, hashWant=%s hashActual=%s\n", hashWant, hashActual)
@ -99,7 +99,7 @@ func TestWrite1Mx100(t *testing.T) {
// Read and check 100 times to catch race conditions
var i int
for i = 0; i < 100; i++ {
hashActual := md5fn(plainDir + "1M")
hashActual := md5fn(defaultPlainDir + "1M")
if hashActual != hashWant {
fmt.Printf("Read corruption in loop # %d\n", i)
t.FailNow()
@ -110,7 +110,7 @@ func TestWrite1Mx100(t *testing.T) {
}
func TestTruncate(t *testing.T) {
fn := plainDir + "truncate"
fn := defaultPlainDir + "truncate"
file, err := os.Create(fn)
if err != nil {
t.FailNow()
@ -142,7 +142,7 @@ func TestTruncate(t *testing.T) {
}
func TestAppend(t *testing.T) {
fn := plainDir + "append"
fn := defaultPlainDir + "append"
file, err := os.Create(fn)
if err != nil {
t.FailNow()
@ -176,7 +176,7 @@ func TestAppend(t *testing.T) {
// Create a file with holes by writing to offset 0 (block #0) and
// offset 4096 (block #1).
func TestFileHoles(t *testing.T) {
fn := plainDir + "fileholes"
fn := defaultPlainDir + "fileholes"
file, err := os.Create(fn)
if err != nil {
t.Errorf("file create failed")
@ -203,7 +203,7 @@ func TestRmwRace(t *testing.T) {
runtime.GOMAXPROCS(10)
fn := plainDir + "rmwrace"
fn := defaultPlainDir + "rmwrace"
f1, err := os.Create(fn)
if err != nil {
t.Fatalf("file create failed")
@ -274,7 +274,7 @@ func TestRmwRace(t *testing.T) {
// With "--plaintextnames", the name "/gocryptfs.conf" is reserved.
// Otherwise there should be no restrictions.
func TestFiltered(t *testing.T) {
filteredFile := plainDir + "gocryptfs.conf"
filteredFile := defaultPlainDir + "gocryptfs.conf"
file, err := os.Create(filteredFile)
if plaintextNames == true && err == nil {
t.Errorf("should have failed but didn't")
@ -292,13 +292,13 @@ func TestFiltered(t *testing.T) {
}
func TestFilenameEncryption(t *testing.T) {
file, err := os.Create(plainDir + "TestFilenameEncryption.txt")
file, err := os.Create(defaultPlainDir + "TestFilenameEncryption.txt")
file.Close()
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(cipherDir + "TestFilenameEncryption.txt")
_, err = os.Stat(defaultCipherDir + "TestFilenameEncryption.txt")
if plaintextNames == true && err != nil {
t.Errorf("plaintextnames not working: %v", err)
} else if plaintextNames == false && err == nil {

View File

@ -12,7 +12,7 @@ func BenchmarkStreamWrite(t *testing.B) {
buf := make([]byte, 1024*1024)
t.SetBytes(int64(len(buf)))
file, err := os.Create(plainDir + "BenchmarkWrite")
file, err := os.Create(defaultPlainDir + "BenchmarkWrite")
if err != nil {
t.FailNow()
}
@ -33,7 +33,7 @@ func BenchmarkStreamRead(t *testing.B) {
buf := make([]byte, 1024*1024)
t.SetBytes(int64(len(buf)))
fn := plainDir + "BenchmarkWrite"
fn := defaultPlainDir + "BenchmarkWrite"
fi, err := os.Stat(fn)
if err != nil {
t.Fatal(err)
@ -80,7 +80,7 @@ func BenchmarkStreamRead(t *testing.B) {
// createFiles - create "count" files of size "size" bytes each
func createFiles(t *testing.B, count int, size int) {
dir := fmt.Sprintf("%s/createFiles_%d_%d", plainDir, count, size)
dir := fmt.Sprintf("%s/createFiles_%d_%d", defaultPlainDir, count, size)
err := os.Mkdir(dir, 0777)
if err != nil {
t.Fatal(err)