Merge pull request #1061 from harmony-ek/non_interactive_passphrase
Support non-interactive passphrasepull/1063/head
commit
066354758b
@ -0,0 +1,44 @@ |
||||
package utils |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
"testing" |
||||
|
||||
"github.com/pkg/errors" |
||||
|
||||
testutils "github.com/harmony-one/harmony/internal/utils/testing" |
||||
) |
||||
|
||||
func exerciseGetPassphraseFromSource(t *testing.T, source, expected string) { |
||||
if actual, err := GetPassphraseFromSource(source); err != nil { |
||||
t.Fatal(errors.Wrap(err, "cannot read passphrase")) |
||||
} else if actual != expected { |
||||
t.Errorf("expected passphrase %#v; got %#v", expected, actual) |
||||
} |
||||
} |
||||
|
||||
func TestGetPassphraseFromSource_Pass(t *testing.T) { |
||||
exerciseGetPassphraseFromSource(t, "pass:hello world", "hello world") |
||||
} |
||||
|
||||
func TestGetPassphraseFromSource_File(t *testing.T) { |
||||
expected := "\nhello world\n" |
||||
t.Run("stdin", func(t *testing.T) { |
||||
f := testutils.NewTempFileWithContents(t, []byte(expected)) |
||||
savedStdin := os.Stdin |
||||
defer func() { os.Stdin = savedStdin }() |
||||
os.Stdin = f |
||||
exerciseGetPassphraseFromSource(t, "stdin", expected) |
||||
}) |
||||
t.Run("file", func(t *testing.T) { |
||||
f := testutils.NewTempFileWithContents(t, []byte(expected)) |
||||
defer testutils.CloseAndRemoveTempFile(t, f) |
||||
exerciseGetPassphraseFromSource(t, "file:"+f.Name(), expected) |
||||
}) |
||||
t.Run("fd", func(t *testing.T) { |
||||
f := testutils.NewTempFileWithContents(t, []byte(expected)) |
||||
defer testutils.CloseAndRemoveTempFile(t, f) |
||||
exerciseGetPassphraseFromSource(t, fmt.Sprintf("fd:%d", f.Fd()), expected) |
||||
}) |
||||
} |
@ -0,0 +1,46 @@ |
||||
package testutils |
||||
|
||||
import ( |
||||
"io" |
||||
"io/ioutil" |
||||
"os" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/pkg/errors" |
||||
) |
||||
|
||||
// NewTempFile creates a new, empty temp file for testing. Errors are fatal.
|
||||
func NewTempFile(t *testing.T) *os.File { |
||||
pattern := strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_") |
||||
f, err := ioutil.TempFile("", pattern) |
||||
if err != nil { |
||||
t.Fatal(errors.Wrap(err, "cannot create temp file")) |
||||
} |
||||
return f |
||||
} |
||||
|
||||
// NewTempFileWithContents creates a new, empty temp file for testing,
|
||||
// with the given contents. The read/write offset is set to the beginning.
|
||||
// Errors are fatal.
|
||||
func NewTempFileWithContents(t *testing.T, contents []byte) *os.File { |
||||
f := NewTempFile(t) |
||||
if _, err := f.Write(contents); err != nil { |
||||
t.Fatal(errors.Wrapf(err, "cannot write contents into %s", f.Name())) |
||||
} |
||||
if _, err := f.Seek(0, io.SeekStart); err != nil { |
||||
t.Fatal(errors.Wrapf(err, "cannot rewind test file %s", f.Name())) |
||||
} |
||||
return f |
||||
} |
||||
|
||||
// CloseAndRemoveTempFile closes/removes the temp file. Errors are logged.
|
||||
func CloseAndRemoveTempFile(t *testing.T, f *os.File) { |
||||
fn := f.Name() |
||||
if err := f.Close(); err != nil { |
||||
t.Log(errors.Wrapf(err, "cannot close test file %s", fn)) |
||||
} |
||||
if err := os.Remove(fn); err != nil { |
||||
t.Log(errors.Wrapf(err, "cannot remove test file %s", fn)) |
||||
} |
||||
} |
Loading…
Reference in new issue