diff options
author | zelig <viktor.tron@gmail.com> | 2014-06-29 23:02:23 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2014-06-29 23:02:23 +0800 |
commit | 772e7e8c8df066a64726b23bde84b6025c5af318 (patch) | |
tree | 8ee0e9b19d6ce2747481a8890473db99c66b87cb /ethcrypto/keys_test.go | |
parent | e1ea41ee9cbe387221874fa6732b11d262a4ff12 (diff) | |
download | go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.gz go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.bz2 go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.lz go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.xz go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.zst go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.zip |
Key Manager
- keypair, keyring:
-- consistent naming of methods
-- error propagation
-- no panic
- keyManager: persist, import, export, initialize and (re)set keyring
- no global public "singleton" keyring, instead interface via keyManager
- keys test
- KeyStore interface, DB and File store implementations
Diffstat (limited to 'ethcrypto/keys_test.go')
-rw-r--r-- | ethcrypto/keys_test.go | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/ethcrypto/keys_test.go b/ethcrypto/keys_test.go new file mode 100644 index 000000000..3ebf4e818 --- /dev/null +++ b/ethcrypto/keys_test.go @@ -0,0 +1,122 @@ +package ethcrypto + +import ( + "github.com/ethereum/eth-go/ethdb" + // "io/ioutil" + "fmt" + "os" + "path" + "testing" +) + +// test if persistence layer works +func TestDBKeyManager(t *testing.T) { + memdb, _ := ethdb.NewMemDatabase() + keyManager0 := NewDBKeyManager(memdb) + err := keyManager0.Init("", 0, false) + if err != nil { + t.Error("Unexpected error: ", err) + } + keyManager1 := NewDBKeyManager(memdb) + err = keyManager1.Init("", 0, false) + if err != nil { + t.Error("Unexpected error: ", err) + } + if string(keyManager0.PrivateKey()) != string(keyManager1.PrivateKey()) { + t.Error("Expected private keys %x, %x, to be identical via db persistence", keyManager0.PrivateKey(), keyManager1.PrivateKey()) + } + err = keyManager1.Init("", 0, true) + if err != nil { + t.Error("Unexpected error: ", err) + } + if string(keyManager0.PrivateKey()) == string(keyManager1.PrivateKey()) { + t.Error("Expected private keys %x, %x, to be be different despite db persistence if force generate", keyManager0.PrivateKey(), keyManager1.PrivateKey()) + } +} + +func TestFileKeyManager(t *testing.T) { + basedir0 := "/tmp/ethtest0" + os.RemoveAll(basedir0) + os.Mkdir(basedir0, 0777) + + keyManager0 := NewFileKeyManager(basedir0) + err := keyManager0.Init("", 0, false) + if err != nil { + t.Error("Unexpected error: ", err) + } + + keyManager1 := NewFileKeyManager(basedir0) + + err = keyManager1.Init("", 0, false) + if err != nil { + t.Error("Unexpected error: ", err) + } + if string(keyManager0.PrivateKey()) != string(keyManager1.PrivateKey()) { + t.Error("Expected private keys %x, %x, to be identical via db persistence", keyManager0.PrivateKey(), keyManager1.PrivateKey()) + } + + err = keyManager1.Init("", 0, true) + if err != nil { + t.Error("Unexpected error: ", err) + } + if string(keyManager0.PrivateKey()) == string(keyManager1.PrivateKey()) { + t.Error("Expected private keys %x, %x, to be be different despite db persistence if force generate", keyManager0.PrivateKey(), keyManager1.PrivateKey()) + } +} + +// cursor errors +func TestCursorErrors(t *testing.T) { + memdb, _ := ethdb.NewMemDatabase() + keyManager0 := NewDBKeyManager(memdb) + err := keyManager0.Init("", 0, false) + err = keyManager0.Init("", 1, false) + if err == nil { + t.Error("Expected cursor error") + } + err = keyManager0.SetCursor(1) + if err == nil { + t.Error("Expected cursor error") + } +} + +func TestExportImport(t *testing.T) { + memdb, _ := ethdb.NewMemDatabase() + keyManager0 := NewDBKeyManager(memdb) + err := keyManager0.Init("", 0, false) + basedir0 := "/tmp/ethtest0" + os.RemoveAll(basedir0) + os.Mkdir(basedir0, 0777) + keyManager0.Export(basedir0) + + keyManager1 := NewFileKeyManager(basedir0) + err = keyManager1.Init("", 0, false) + if err != nil { + t.Error("Unexpected error: ", err) + } + fmt.Printf("keyRing: %v\n", keyManager0.KeyPair()) + fmt.Printf("keyRing: %v\n", keyManager1.KeyPair()) + if string(keyManager0.PrivateKey()) != string(keyManager1.PrivateKey()) { + t.Error("Expected private keys %x, %x, to be identical via export to filestore basedir", keyManager0.PrivateKey(), keyManager1.PrivateKey()) + } + path.Join("") + + // memdb, _ = ethdb.NewMemDatabase() + // keyManager2 := NewDBKeyManager(memdb) + // err = keyManager2.InitFromSecretsFile("", 0, path.Join(basedir0, "default.prv")) + // if err != nil { + // t.Error("Unexpected error: ", err) + // } + // if string(keyManager0.PrivateKey()) != string(keyManager2.PrivateKey()) { + // t.Error("Expected private keys %s, %s, to be identical via export/import prv", keyManager0.PrivateKey(), keyManager1.PrivateKey()) + // } + + // memdb, _ = ethdb.NewMemDatabase() + // keyManager3 := NewDBKeyManager(memdb) + // err = keyManager3.InitFromSecretsFile("", 0, path.Join(basedir0, "default.mne")) + // if err != nil { + // t.Error("Unexpected error: ", err) + // } + // if string(keyManager0.PrivateKey()) != string(keyManager3.PrivateKey()) { + // t.Error("Expected private keys %s, %s, to be identical via export/import mnemonic file", keyManager0.PrivateKey(), keyManager1.PrivateKey()) + // } +} |