From 0c32d2722fc32523829029953bf9ade2933f4a7c Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 19 Jun 2017 17:49:16 +0200 Subject: [PATCH] #17 - better support passwords in tests, more cli tests --- cmd/utils.go | 40 +++++++++++++++++++++++++++++++++++++--- glide.lock | 15 +++++---------- tests/keys.sh | 46 ++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 82 insertions(+), 19 deletions(-) diff --git a/cmd/utils.go b/cmd/utils.go index f2faa892b..d082bbbc9 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -1,20 +1,49 @@ package cmd import ( + "bufio" "fmt" + "os" + "strings" "github.com/bgentry/speakeasy" + "github.com/mattn/go-isatty" "github.com/pkg/errors" "github.com/spf13/viper" - keys "github.com/tendermint/go-crypto/keys" + data "github.com/tendermint/go-wire/data" "github.com/tendermint/tmlibs/cli" + + keys "github.com/tendermint/go-crypto/keys" ) const PassLength = 10 -func getPassword(prompt string) (string, error) { - pass, err := speakeasy.Ask(prompt) +// if we read from non-tty, we just need to init the buffer reader once, +// in case we try to read multiple passwords (eg. update) +var buf *bufio.Reader + +func inputIsTty() bool { + return isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()) +} + +func stdinPassword() (string, error) { + if buf == nil { + buf = bufio.NewReader(os.Stdin) + } + pass, err := buf.ReadString('\n') + if err != nil { + return "", err + } + return strings.TrimSpace(pass), nil +} + +func getPassword(prompt string) (pass string, err error) { + if inputIsTty() { + pass, err = speakeasy.Ask(prompt) + } else { + pass, err = stdinPassword() + } if err != nil { return "", err } @@ -25,6 +54,11 @@ func getPassword(prompt string) (string, error) { } func getCheckPassword(prompt, prompt2 string) (string, error) { + // simple read on no-tty + if !inputIsTty() { + return getPassword(prompt) + } + // TODO: own function??? pass, err := getPassword(prompt) if err != nil { diff --git a/glide.lock b/glide.lock index 82dd5111e..71292ce11 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ hash: 3bcee9fbccf29d21217b24b6a83ec51e1514f37b2ae5d8718cf6c5df80f4fb2c -updated: 2017-05-15T09:40:53.073691731-04:00 +updated: 2017-06-19T17:16:58.037568333+02:00 imports: - name: github.com/bgentry/speakeasy version: 4aabc24848ce5fd31929f7d1e4ea74d3709c14cd @@ -17,8 +17,6 @@ imports: - hdkeychain - name: github.com/btcsuite/fastsha256 version: 637e656429416087660c84436a2a035d69d54e2e -- name: github.com/clipperhouse/typewriter - version: c1a48da378ebb7db1db9f35981b5cc24bf2e5b85 - name: github.com/fsnotify/fsnotify version: 4da3e2cfbabc9f751898f250b49f2439785783a1 - name: github.com/go-kit/kit @@ -60,6 +58,8 @@ imports: version: b84e30acd515aadc4b783ad4ff83aff3299bdfe0 - name: github.com/magiconair/properties version: 51463bfca2576e06c62a8504b5c0f06d61312647 +- name: github.com/mattn/go-isatty + version: 9622e0cc9d8f9be434ca605520ff9a16808fee47 - name: github.com/mitchellh/mapstructure version: cc8532a8e9a55ea36402aa21efdf403a60d34096 - name: github.com/pelletier/go-buffruneio @@ -88,12 +88,12 @@ imports: - edwards25519 - extra25519 - name: github.com/tendermint/go-wire - version: 97beaedf0f4dbc035309157c92be3b30cc6e5d74 + version: 5f88da3dbc1a72844e6dfaf274ce87f851d488eb subpackages: - data - data/base58 - name: github.com/tendermint/tmlibs - version: 8f5a175ff4c869fedde710615a11f5745ff69bf3 + version: bd9d0d1637dadf1330e167189d5e5031aadcda6f subpackages: - cli - common @@ -119,11 +119,6 @@ imports: subpackages: - transform - unicode/norm -- name: golang.org/x/tools - version: 144c6642b5d832d6c44a53dad6ee61665dd432ce - subpackages: - - go/ast/astutil - - imports - name: gopkg.in/go-playground/validator.v9 version: 6d8c18553ea1ac493d049edd6f102f52e618f085 - name: gopkg.in/yaml.v2 diff --git a/tests/keys.sh b/tests/keys.sh index b4bdcdd40..82c8083ec 100755 --- a/tests/keys.sh +++ b/tests/keys.sh @@ -12,17 +12,51 @@ oneTimeSetUp() { newKey(){ assertNotNull "keyname required" "$1" KEYPASS=${2:-qwertyuiop} - (echo $KEYPASS; echo $KEYPASS) | ${EXE} new $1 >/dev/null 2>&1 + KEY=$(echo $KEYPASS | ${EXE} new $1) assertTrue "created $1" $? + return $? } -testMakeKeys() { +# updateKey +updateKey() { + (echo $2; echo $3) | keys update $1 > /dev/null + return $? +} + +test00MakeKeys() { USER=demouser - assertFalse "already user $USER" "${EXE} list | grep $USER" - assertEquals "1" `${EXE} list | wc -l` + assertFalse "already user $USER" "${EXE} get $USER" newKey $USER - assertTrue "no user $USER" "${EXE} list | grep $USER" - assertEquals "2" `${EXE} list | wc -l` + assertTrue "no user $USER" "${EXE} get $USER" + # make sure bad password not accepted + assertFalse "accepts short password" "echo 123 | keys new badpass" +} + +test01ListKeys() { + # one line plus the number of keys + assertEquals "2" $(keys list | wc -l) + newKey foobar + assertEquals "3" $(keys list | wc -l) + # we got the proper name here... + assertEquals "foobar" $(keys list -o json | jq .[1].name | tr -d \" ) + # we get all names in normal output + EXPECTEDNAMES=$(echo demouser; echo foobar) + TEXTNAMES=$(keys list | tail -n +2 | cut -f1) + assertEquals "$EXPECTEDNAMES" "$TEXTNAMES" + # let's make sure the addresses match! + assertEquals "text and json addresses don't match" $(keys list | tail -1 | cut -f3) $(keys list -o json | jq .[1].address | tr -d \") +} + +test02updateKeys() { + USER=changer + PASS1=awsedrftgyhu + PASS2=S4H.9j.D9S7hso + PASS3=h8ybO7GY6d2 + + newKey $USER $PASS1 + assertFalse "accepts invalid pass" "updateKey $USER $PASS2 $PASS2" + assertTrue "doesn't update" "updateKey $USER $PASS1 $PASS2" + assertTrue "takes new key after update" "updateKey $USER $PASS2 $PASS3" } # load and run these tests with shunit2!