summaryrefslogtreecommitdiff
path: root/tests/cli/zerokey.go
blob: e9b9c5064e3e64f1c7d8a945aca7705c7cf99f77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cli

import (
	"io/ioutil"
	"os"
	"os/exec"
	"testing"

	"github.com/rfjakob/gocryptfs/v2/tests/test_helpers"
)

// TestZerokey verifies that `gocryptfs -zerokey` uses the same options as
// `gocryptfs -init`.
func TestZerokey(t *testing.T) {
	// Create FS
	dir := test_helpers.InitFS(t)

	// Change masterkey to all-zero using password change
	args := []string{"-q", "-passwd", "-masterkey",
		"00000000-00000000-00000000-00000000-00000000-00000000-00000000-00000000"}
	args = append(args, dir)
	cmd := exec.Command(test_helpers.GocryptfsBinary, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	p, err := cmd.StdinPipe()
	if err != nil {
		t.Fatal(err)
	}
	err = cmd.Start()
	if err != nil {
		t.Error(err)
	}
	// New password = old password
	p.Write([]byte("test\n"))
	p.Close()
	err = cmd.Wait()
	if err != nil {
		t.Error(err)
	}

	// Add content
	mnt := dir + ".mnt"
	test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo test")
	file1 := mnt + "/file1"
	err = ioutil.WriteFile(file1, []byte("somecontent"), 0600)
	if err != nil {
		t.Fatal(err)
	}
	test_helpers.UnmountPanic(mnt)

	// Mount using -zerokey and verify we get the same result
	test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo test")
	content, err := ioutil.ReadFile(file1)
	if err != nil {
		t.Error(err)
	} else if string(content) != "somecontent" {
		t.Errorf("wrong content: %q", string(content))
	}
	test_helpers.UnmountPanic(mnt)
}