2016-06-15 22:43:31 +02:00
|
|
|
package readpassword
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2016-06-15 22:43:31 +02:00
|
|
|
)
|
|
|
|
|
2018-02-18 14:26:54 +01:00
|
|
|
var testPw = []byte("test")
|
|
|
|
|
2016-06-15 22:43:31 +02:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// Shut up info output
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Enabled = false
|
2016-06-16 21:56:23 +02:00
|
|
|
os.Exit(m.Run())
|
2016-06-15 22:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtpass(t *testing.T) {
|
|
|
|
p1 := "ads2q4tw41reg52"
|
2019-03-03 13:25:30 +01:00
|
|
|
p2 := string(readPasswordExtpass([]string{"echo " + p1}))
|
2016-06-15 22:43:31 +02:00
|
|
|
if p1 != p2 {
|
|
|
|
t.Errorf("p1=%q != p2=%q", p1, p2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnceExtpass(t *testing.T) {
|
|
|
|
p1 := "lkadsf0923rdfi48rqwhdsf"
|
2019-03-03 13:25:30 +01:00
|
|
|
p2 := string(Once([]string{"echo " + p1}, "", ""))
|
|
|
|
if p1 != p2 {
|
|
|
|
t.Errorf("p1=%q != p2=%q", p1, p2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// extpass with two arguments
|
|
|
|
func TestOnceExtpass2(t *testing.T) {
|
|
|
|
p1 := "foo"
|
|
|
|
p2 := string(Once([]string{"echo", p1}, "", ""))
|
|
|
|
if p1 != p2 {
|
|
|
|
t.Errorf("p1=%q != p2=%q", p1, p2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// extpass with three arguments
|
|
|
|
func TestOnceExtpass3(t *testing.T) {
|
|
|
|
p1 := "foo bar baz"
|
|
|
|
p2 := string(Once([]string{"echo", "foo", "bar", "baz"}, "", ""))
|
|
|
|
if p1 != p2 {
|
|
|
|
t.Errorf("p1=%q != p2=%q", p1, p2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnceExtpassSpaces(t *testing.T) {
|
|
|
|
p1 := "mypassword"
|
|
|
|
p2 := string(Once([]string{"cat", "passfile_test_files/file with spaces.txt"}, "", ""))
|
2016-06-15 22:43:31 +02:00
|
|
|
if p1 != p2 {
|
|
|
|
t.Errorf("p1=%q != p2=%q", p1, p2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwiceExtpass(t *testing.T) {
|
|
|
|
p1 := "w5w44t3wfe45srz434"
|
2019-03-03 13:25:30 +01:00
|
|
|
p2 := string(Once([]string{"echo " + p1}, "", ""))
|
2016-06-15 22:43:31 +02:00
|
|
|
if p1 != p2 {
|
|
|
|
t.Errorf("p1=%q != p2=%q", p1, p2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When extpass returns an empty string, we should crash.
|
2018-12-15 17:09:38 +01:00
|
|
|
//
|
|
|
|
// The TEST_SLAVE magic is explained at
|
|
|
|
// https://talks.golang.org/2014/testing.slide#23 .
|
2016-06-15 22:43:31 +02:00
|
|
|
func TestExtpassEmpty(t *testing.T) {
|
|
|
|
if os.Getenv("TEST_SLAVE") == "1" {
|
2019-03-03 13:25:30 +01:00
|
|
|
readPasswordExtpass([]string{"echo"})
|
2016-06-15 22:43:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cmd := exec.Command(os.Args[0], "-test.run=TestExtpassEmpty$")
|
|
|
|
cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Fatal("empty password should have failed")
|
|
|
|
}
|