Skip to content

Commit

Permalink
tests: add TestPasswdMasterkeyStdin
Browse files Browse the repository at this point in the history
Tests that `gocryptfs -passwd -masterkey=stdin` works.
This was fixed by ff04b1d.

Fixes #461
  • Loading branch information
rfjakob committed May 9, 2020
1 parent ff04b1d commit 3ef5634
Showing 1 changed file with 63 additions and 8 deletions.
71 changes: 63 additions & 8 deletions tests/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,32 +167,85 @@ func TestPasswd(t *testing.T) {
}
}

// cp copies file at `src` to `dst`, overwriting
// `dst` if it already exists. Calls t.Fatal on failure.
func cp(t *testing.T, src string, dst string) {
conf, err := ioutil.ReadFile(src)
if err != nil {
t.Fatal(err)
}
syscall.Unlink(dst)
err = ioutil.WriteFile(dst, conf, 0600)
if err != nil {
t.Fatal(err)
}
}

// Test -passwd with -masterkey
func TestPasswdMasterkey(t *testing.T) {
// Create FS
dir := test_helpers.InitFS(t)
// Overwrite with config with known master key
conf, err := ioutil.ReadFile("gocryptfs.conf.b9e5ba23")
cp(t, "gocryptfs.conf.b9e5ba23", dir+"/gocryptfs.conf")
// 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)
}
syscall.Unlink(dir + "/gocryptfs.conf")
err = ioutil.WriteFile(dir+"/gocryptfs.conf", conf, 0600)
test_helpers.UnmountPanic(mnt)
// Change password using stdin
args := []string{"-q", "-passwd", "-masterkey",
"b9e5ba23-981a22b8-c8d790d8-627add29-f680513f-b7b7035f-d203fb83-21d82205"}
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
p.Write([]byte("newpasswd\n"))
p.Close()
err = cmd.Wait()
if err != nil {
t.Error(err)
}
// Mount and verify
test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo newpasswd")
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)
}

// Test -passwd with -masterkey=stdin
func TestPasswdMasterkeyStdin(t *testing.T) {
// Create FS
dir := test_helpers.InitFS(t)
// Overwrite with config with known master key
cp(t, "gocryptfs.conf.b9e5ba23", dir+"/gocryptfs.conf")
// Add content
mnt := dir + ".mnt"
test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo test")
file1 := mnt + "/file1"
err = ioutil.WriteFile(file1, []byte("somecontent"), 0600)
err := ioutil.WriteFile(file1, []byte("somecontent"), 0600)
if err != nil {
t.Fatal(err)
}
test_helpers.UnmountPanic(mnt)
// Change password using stdin
args := []string{"-q", "-passwd", "-masterkey",
"b9e5ba23-981a22b8-c8d790d8-627add29-f680513f-b7b7035f-d203fb83-21d82205"}
args := []string{"-q", "-passwd", "-masterkey=stdin"}
args = append(args, dir)
cmd := exec.Command(test_helpers.GocryptfsBinary, args...)
cmd.Stdout = os.Stdout
Expand All @@ -205,18 +258,20 @@ func TestPasswdMasterkey(t *testing.T) {
if err != nil {
t.Error(err)
}
// Masterkey
p.Write([]byte("b9e5ba23-981a22b8-c8d790d8-627add29-f680513f-b7b7035f-d203fb83-21d82205\n"))
// New password
p.Write([]byte("newpasswd\n"))
p.Close()
err = cmd.Wait()
if err != nil {
t.Error(err)
t.Fatal(err)
}
// Mount and verify
test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo newpasswd")
content, err := ioutil.ReadFile(file1)
if err != nil {
t.Error(err)
t.Fatal(err)
} else if string(content) != "somecontent" {
t.Errorf("wrong content: %q", string(content))
}
Expand Down

0 comments on commit 3ef5634

Please sign in to comment.