From 80836edcdddcf62a2f12e4cceed8f82f987de57d Mon Sep 17 00:00:00 2001 From: Jacky Wang Date: Thu, 9 Jul 2020 01:25:09 -0700 Subject: [PATCH] [node.sh] added more end to end test cases --- cmd/harmony/blsloader/console.go | 2 + cmd/harmony/blsloader/console_test.go | 12 +++-- cmd/harmony/blsloader/helper.go | 5 ++ cmd/harmony/blsloader/loader_test.go | 66 ++++++++++++++++++++++++++- cmd/harmony/blsloader/utils.go | 4 +- 5 files changed, 83 insertions(+), 6 deletions(-) diff --git a/cmd/harmony/blsloader/console.go b/cmd/harmony/blsloader/console.go index f4a5d5e49..065366d9d 100644 --- a/cmd/harmony/blsloader/console.go +++ b/cmd/harmony/blsloader/console.go @@ -24,10 +24,12 @@ type consoleItf interface { type stdConsole struct{} func (console *stdConsole) readPassword() (string, error) { + fmt.Println("trapped") b, err := terminal.ReadPassword(syscall.Stdin) if err != nil { return "", err } + fmt.Println("escaped") return strings.TrimSpace(string(b)), nil } diff --git a/cmd/harmony/blsloader/console_test.go b/cmd/harmony/blsloader/console_test.go index 5c3f12f67..467daac57 100644 --- a/cmd/harmony/blsloader/console_test.go +++ b/cmd/harmony/blsloader/console_test.go @@ -22,18 +22,20 @@ func newTestConsole() *testConsole { } func (tc *testConsole) readPassword() (string, error) { + fmt.Println("reading password") return tc.readln() } func (tc *testConsole) readln() (string, error) { select { - case <-time.After(10 * time.Second): + case <-time.After(2 * time.Second): + fmt.Println("timed out") return "", errors.New("timed out") - case <-tc.In: - msg, ok := <-tc.In + case msg, ok := <-tc.In: if !ok { return "", errors.New("in channel closed") } + fmt.Println("read in") return msg, nil } } @@ -54,12 +56,16 @@ func (tc *testConsole) printf(format string, a ...interface{}) { } func (tc *testConsole) checkClean() (bool, string) { + fmt.Println("check clean") select { case msg := <-tc.In: + fmt.Println("not good") return false, "extra in message: " + msg case msg := <-tc.Out: + fmt.Println("not good") return false, "extra out message: " + msg default: + fmt.Println("good") return true, "" } } diff --git a/cmd/harmony/blsloader/helper.go b/cmd/harmony/blsloader/helper.go index f545959f9..c5b300c71 100644 --- a/cmd/harmony/blsloader/helper.go +++ b/cmd/harmony/blsloader/helper.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" + "github.com/harmony-one/harmony/crypto/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/multibls" ) @@ -24,17 +26,20 @@ type basicSingleBlsLoader struct { // loadKeys load bls keys from a single bls file func (loader *basicSingleBlsLoader) loadKeys() (multibls.PrivateKeys, error) { + fmt.Println("load keys") providers, err := loader.getPassProviders() if err != nil { fmt.Println("not loaded 1") return multibls.PrivateKeys{}, err } + fmt.Println("provider got") secretKey, err := loadBasicKey(loader.blsKeyFile, providers) if err != nil { fmt.Println("not loaded 2") return multibls.PrivateKeys{}, err } fmt.Println("loaded secret key") + console.printf("loaded bls key %x\n", bls.WrapperFromPrivateKey(secretKey).Pub.Bytes) return multibls.GetPrivateKeys(secretKey), nil } diff --git a/cmd/harmony/blsloader/loader_test.go b/cmd/harmony/blsloader/loader_test.go index f57e87170..2ee0240a1 100644 --- a/cmd/harmony/blsloader/loader_test.go +++ b/cmd/harmony/blsloader/loader_test.go @@ -89,16 +89,73 @@ func TestLoadKeys_SingleBls_File(t *testing.T) { expErr error }{ { - // load the default pass file + // load the default pass file with file cfg: Config{ BlsKeyFile: &validTestKeys[0].path, PassSrcType: PassSrcFile, }, inputs: []string{}, - expOutputs: []string{}, + expOutputs: []string{ + fmt.Sprintf("loaded bls key %s\n", validTestKeys[0].publicKey), + }, expPubKeys: []string{validTestKeys[0].publicKey}, }, + { + // load the default pass file with file + cfg: Config{ + BlsKeyFile: &validTestKeys[1].path, + PassSrcType: PassSrcFile, + }, + inputs: []string{}, + + expOutputs: []string{ + fmt.Sprintf("loaded bls key %s\n", validTestKeys[1].publicKey), + }, + expPubKeys: []string{validTestKeys[1].publicKey}, + }, + { + // load key file with prompt + cfg: Config{ + BlsKeyFile: &validTestKeys[1].path, + PassSrcType: PassSrcPrompt, + }, + inputs: []string{validTestKeys[1].passphrase}, + + expOutputs: []string{ + fmt.Sprintf("Enter passphrase for the BLS key file %s:", validTestKeys[1].path), + fmt.Sprintf("loaded bls key %s\n", validTestKeys[1].publicKey), + }, + expPubKeys: []string{validTestKeys[1].publicKey}, + }, + { + // Automatically use pass file + cfg: Config{ + BlsKeyFile: &validTestKeys[1].path, + PassSrcType: PassSrcAuto, + }, + inputs: []string{}, + + expOutputs: []string{ + fmt.Sprintf("loaded bls key %s\n", validTestKeys[1].publicKey), + }, + expPubKeys: []string{validTestKeys[1].publicKey}, + }, + { + // Automatically use prompt + cfg: Config{ + BlsKeyFile: &emptyPassTestKeys[1].path, + PassSrcType: PassSrcAuto, + }, + inputs: []string{emptyPassTestKeys[1].passphrase}, + + expOutputs: []string{ + "unable to get passphrase from passphrase file testData/blskey_emptypass/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.pass: cannot open passphrase file\n", + fmt.Sprintf("Enter passphrase for the BLS key file %s:", emptyPassTestKeys[1].path), + fmt.Sprintf("loaded bls key %s\n", emptyPassTestKeys[1].publicKey), + }, + expPubKeys: []string{emptyPassTestKeys[1].publicKey}, + }, } for i, test := range tests { ts := &testSuite{ @@ -174,6 +231,8 @@ func (ts *testSuite) checkResult() error { return err default: } + fmt.Println("got outputs:", ts.gotOutputs) + fmt.Println("expect outputs:", ts.expOutputs) if isClean, msg := ts.console.checkClean(); !isClean { return fmt.Errorf("console not clean: %v", msg) } @@ -183,6 +242,9 @@ func (ts *testSuite) checkResult() error { if err := ts.checkKeys(); err != nil { return err } + if err := ts.checkOutputs(); err != nil { + return err + } return nil } diff --git a/cmd/harmony/blsloader/utils.go b/cmd/harmony/blsloader/utils.go index e511d8cb6..9e97344f2 100644 --- a/cmd/harmony/blsloader/utils.go +++ b/cmd/harmony/blsloader/utils.go @@ -28,7 +28,7 @@ func loadBasicKey(blsKeyFile string, pps []passProvider) (*bls_core.SecretKey, e secretKey, err := loadBasicKeyWithProvider(blsKeyFile, pp) if err != nil { console.println(err) - return nil, err + continue } return secretKey, nil } @@ -108,7 +108,9 @@ func promptGetPassword(prompt string) (string, error) { if !strings.HasSuffix(prompt, ":") { prompt += ":" } + fmt.Println("before print prompt", prompt) console.print(prompt) + fmt.Println("after print prompt", prompt) return console.readPassword() }