aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/keys_test.go
blob: 46e40110c2c76045440d658f02fecf2291992c2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package crypto

import (
    "github.com/ethereum/go-ethereum/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())
    // }
}