From 0b32ae436b52db9ee08aaeb3f2ce16e5402306b4 Mon Sep 17 00:00:00 2001 From: Jacky Wang Date: Mon, 13 Jul 2020 01:37:04 -0700 Subject: [PATCH] [node.sh] added more test cases --- cmd/harmony/blsloader/helper.go | 5 +- cmd/harmony/blsloader/helper_test.go | 183 +++++++++++++++++++++++ cmd/harmony/blsloader/params.go | 1 - cmd/harmony/blsloader/passphrase_test.go | 2 +- cmd/harmony/blsloader/utils.go | 2 +- 5 files changed, 189 insertions(+), 4 deletions(-) diff --git a/cmd/harmony/blsloader/helper.go b/cmd/harmony/blsloader/helper.go index 96e9eb83c..45770a3b1 100644 --- a/cmd/harmony/blsloader/helper.go +++ b/cmd/harmony/blsloader/helper.go @@ -80,7 +80,10 @@ func (loader *blsDirLoader) loadKeys() (multibls.PrivateKeys, error) { if err != nil { return err } - decrypter := loader.decrypters[filepath.Ext(path)] + decrypter, exist := loader.decrypters[filepath.Ext(path)] + if !exist { + return nil + } secret, err := decrypter.decryptFile(path) if err != nil { return err diff --git a/cmd/harmony/blsloader/helper_test.go b/cmd/harmony/blsloader/helper_test.go index cbc52e7c7..1aeccb994 100644 --- a/cmd/harmony/blsloader/helper_test.go +++ b/cmd/harmony/blsloader/helper_test.go @@ -1 +1,184 @@ package blsloader + +import ( + "bytes" + "errors" + "os" + "path/filepath" + "testing" + + "github.com/ethereum/go-ethereum/common" + bls_core "github.com/harmony-one/bls/ffi/go/bls" +) + +const ( + testExt = ".test1" +) + +func TestNewMultiKeyLoader(t *testing.T) { + tests := []struct { + keyFiles []string + decrypters []keyDecrypter + expErr error + }{ + { + keyFiles: []string{ + "test/keyfile1.key", + "keyfile2.bls", + }, + decrypters: []keyDecrypter{ + &passDecrypter{}, + &kmsDecrypter{}, + }, + expErr: nil, + }, + { + keyFiles: []string{ + "test/keyfile1.key", + "keyfile2.bls", + }, + decrypters: []keyDecrypter{ + &passDecrypter{}, + }, + expErr: errors.New("unsupported key extension"), + }, + } + for i, test := range tests { + _, err := newMultiKeyLoader(test.keyFiles, test.decrypters) + if assErr := assertError(err, test.expErr); assErr != nil { + t.Errorf("Test %v: %v", i, assErr) + } + } +} + +func TestMultiKeyLoader_loadKeys(t *testing.T) { + setTestConsole(newTestConsole()) + + unitTestDir := filepath.Join(baseTestDir, t.Name()) + os.Remove(unitTestDir) + os.MkdirAll(unitTestDir, 0700) + + keyFile1 := filepath.Join(unitTestDir, testKeys[0].publicKey+basicKeyExt) + if err := writeFile(keyFile1, testKeys[0].keyFileData); err != nil { + t.Fatal(err) + } + keyFile2 := filepath.Join(unitTestDir, testKeys[1].publicKey+testExt) + if err := writeFile(keyFile2, testKeys[1].keyFileData); err != nil { + t.Fatal(err) + } + passFile1 := filepath.Join(unitTestDir, testKeys[0].publicKey+passExt) + if err := writeFile(passFile1, testKeys[0].passphrase); err != nil { + t.Fatal(err) + } + decrypters := map[string]keyDecrypter{ + basicKeyExt: &passDecrypter{pps: []passProvider{&dynamicPassProvider{}}}, + testExt: newTestPassDecrypter(), + } + + loader := &multiKeyLoader{ + keyFiles: []string{keyFile1, keyFile2}, + decrypters: decrypters, + loadedSecrets: make([]*bls_core.SecretKey, 0, 2), + } + keys, err := loader.loadKeys() + if err != nil { + t.Fatal(err) + } + if len(keys) != 2 { + t.Fatalf("unexpected number of keys: %v / 2", len(keys)) + } + gotPubs := [][]byte{ + keys[0].Pub.Bytes[:], + keys[1].Pub.Bytes[:], + } + expPubs := [][]byte{ + common.Hex2Bytes(testKeys[0].publicKey), + common.Hex2Bytes(testKeys[1].publicKey), + } + for i := range gotPubs { + got, exp := gotPubs[i], expPubs[i] + if !bytes.Equal(got, exp) { + t.Fatalf("%v pubkey unexpected: %x / %x", i, got, exp) + } + } +} + +func TestBlsDirLoader(t *testing.T) { + setTestConsole(newTestConsole()) + + unitTestDir := filepath.Join(baseTestDir, t.Name()) + os.Remove(unitTestDir) + os.MkdirAll(unitTestDir, 0700) + + keyFile1 := filepath.Join(unitTestDir, testKeys[0].publicKey+basicKeyExt) + if err := writeFile(keyFile1, testKeys[0].keyFileData); err != nil { + t.Fatal(err) + } + keyFile2 := filepath.Join(unitTestDir, testKeys[1].publicKey+testExt) + if err := writeFile(keyFile2, testKeys[1].keyFileData); err != nil { + t.Fatal(err) + } + passFile1 := filepath.Join(unitTestDir, testKeys[0].publicKey+passExt) + if err := writeFile(passFile1, testKeys[0].passphrase); err != nil { + t.Fatal(err) + } + // write a file without the given extension + if err := writeFile(filepath.Join(unitTestDir, "unknown.ext"), "random string"); err != nil { + t.Fatal(err) + } + decrypters := []keyDecrypter{ + &passDecrypter{pps: []passProvider{&dynamicPassProvider{}}}, + newTestPassDecrypter(), + } + + loader, err := newBlsDirLoader(unitTestDir, decrypters) + if err != nil { + t.Fatal(err) + } + keys, err := loader.loadKeys() + if err != nil { + t.Fatal(err) + } + + if len(keys) != 2 { + t.Fatalf("unexpected number of keys: %v / 2", len(keys)) + } + gotPubs := [][]byte{ + keys[0].Pub.Bytes[:], + keys[1].Pub.Bytes[:], + } + expPubs := [][]byte{ + common.Hex2Bytes(testKeys[0].publicKey), + common.Hex2Bytes(testKeys[1].publicKey), + } + for i := range gotPubs { + got, exp := gotPubs[i], expPubs[i] + if !bytes.Equal(got, exp) { + t.Fatalf("%v pubkey unexpected: %x / %x", i, got, exp) + } + } +} + +type testPassDecrypter struct { + pd passDecrypter +} + +func newTestPassDecrypter() *testPassDecrypter { + provider := &testPassProvider{m: map[string]string{ + testKeys[0].publicKey: testKeys[0].passphrase, + testKeys[1].publicKey: testKeys[1].passphrase, + }} + return &testPassDecrypter{ + pd: passDecrypter{ + pps: []passProvider{provider}, + }, + } +} + +func (decrypter *testPassDecrypter) extension() string { + return testExt +} + +func (decrypter *testPassDecrypter) decryptFile(keyFile string) (*bls_core.SecretKey, error) { + return decrypter.pd.decryptFile(keyFile) +} diff --git a/cmd/harmony/blsloader/params.go b/cmd/harmony/blsloader/params.go index ed691417b..e64169b53 100644 --- a/cmd/harmony/blsloader/params.go +++ b/cmd/harmony/blsloader/params.go @@ -16,6 +16,5 @@ const ( ) const ( - defWritePassDirMode = 0700 defWritePassFileMode = 0600 ) diff --git a/cmd/harmony/blsloader/passphrase_test.go b/cmd/harmony/blsloader/passphrase_test.go index 77ef350f9..791b60c7a 100644 --- a/cmd/harmony/blsloader/passphrase_test.go +++ b/cmd/harmony/blsloader/passphrase_test.go @@ -206,7 +206,7 @@ func makeTestPassProvider() *testPassProvider { func (provider *testPassProvider) getPassphrase(keyFile string) (string, error) { basename := filepath.Base(keyFile) - publicKey := strings.TrimSuffix(basename, basicKeyExt) + publicKey := strings.TrimSuffix(basename, filepath.Ext(basename)) pass, exist := provider.m[publicKey] if !exist { return "", errors.New("passphrase not exist") diff --git a/cmd/harmony/blsloader/utils.go b/cmd/harmony/blsloader/utils.go index dc010bdd7..25d57d5da 100644 --- a/cmd/harmony/blsloader/utils.go +++ b/cmd/harmony/blsloader/utils.go @@ -38,7 +38,7 @@ func checkIsDir(path string) error { if err != nil { return err } - if info.IsDir() { + if !info.IsDir() { return fmt.Errorf("%v is a file", path) } return nil