forked from Finschia/finschia-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oc_cmds_test.go
233 lines (197 loc) · 6.4 KB
/
oc_cmds_test.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package server
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
cfg "github.com/line/ostracon/config"
"github.com/line/ostracon/crypto"
tmjson "github.com/line/ostracon/libs/json"
"github.com/line/ostracon/libs/log"
tmos "github.com/line/ostracon/libs/os"
tmrand "github.com/line/ostracon/libs/rand"
"github.com/line/ostracon/p2p"
"github.com/line/ostracon/privval"
"github.com/line/ostracon/types"
tmtime "github.com/line/ostracon/types/time"
)
var (
logger = log.NewOCLogger(log.NewSyncWriter(os.Stdout))
)
func TestShowValidator(t *testing.T) {
testCommon := newPrecedenceCommon(t)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)
if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
}
// ostracon init & create the server config file
initFilesWithConfig(serverCtx.Config)
output := captureStdout(t, func() { ShowValidatorCmd().ExecuteContext(ctx) })
// output must match the locally stored priv_validator key
privKey := loadFilePVKey(t, serverCtx.Config.PrivValidatorKeyFile())
bz, err := tmjson.Marshal(privKey.PubKey)
require.NoError(t, err)
require.Equal(t, string(bz), output)
}
func TestShowValidatorWithKMS(t *testing.T) {
testCommon := newPrecedenceCommon(t)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)
if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
}
// ostracon init & create the server config file
initFilesWithConfig(serverCtx.Config)
chainID, err := loadChainID(serverCtx.Config)
require.NoError(t, err)
// remove config file
if tmos.FileExists(serverCtx.Config.PrivValidatorKeyFile()) {
err := os.Remove(serverCtx.Config.PrivValidatorKeyFile())
require.NoError(t, err)
}
privval.WithMockKMS(t, t.TempDir(), chainID, func(addr string, privKey crypto.PrivKey) {
serverCtx.Config.PrivValidatorListenAddr = addr
require.NoFileExists(t, serverCtx.Config.PrivValidatorKeyFile())
output := captureStdout(t, func() { ShowValidatorCmd().ExecuteContext(ctx) })
require.NoError(t, err)
// output must contains the KMS public key
bz, err := tmjson.Marshal(privKey.PubKey())
require.NoError(t, err)
expected := string(bz)
require.Contains(t, output, expected)
})
}
func TestShowValidatorWithInefficientKMSAddress(t *testing.T) {
testCommon := newPrecedenceCommon(t)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)
if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
}
// ostracon init & create the server config file
initFilesWithConfig(serverCtx.Config)
// remove config file
if tmos.FileExists(serverCtx.Config.PrivValidatorKeyFile()) {
err := os.Remove(serverCtx.Config.PrivValidatorKeyFile())
require.NoError(t, err)
}
serverCtx.Config.PrivValidatorListenAddr = "127.0.0.1:inefficient"
err := ShowValidatorCmd().ExecuteContext(ctx)
require.Error(t, err)
}
func TestLoadChainID(t *testing.T) {
expected := "c57861"
config := cfg.ResetTestRootWithChainID("TestLoadChainID", expected)
defer func() {
var _ = os.RemoveAll(config.RootDir)
}()
require.FileExists(t, config.GenesisFile())
genDoc, err := types.GenesisDocFromFile(config.GenesisFile())
require.NoError(t, err)
require.Equal(t, expected, genDoc.ChainID)
chainID, err := loadChainID(config)
require.NoError(t, err)
require.Equal(t, expected, chainID)
}
func TestLoadChainIDWithoutStateDB(t *testing.T) {
expected := "c34091"
config := cfg.ResetTestRootWithChainID("TestLoadChainID", expected)
defer func() {
var _ = os.RemoveAll(config.RootDir)
}()
config.DBBackend = "goleveldb"
config.DBPath = "/../path with containing chars that cannot be used\\/:*?\"<>|\x00"
_, err := loadChainID(config)
require.Error(t, err)
}
func initFilesWithConfig(config *cfg.Config) error {
// private validator
privValKeyFile := config.PrivValidatorKeyFile()
privValStateFile := config.PrivValidatorStateFile()
privKeyType := config.PrivValidatorKeyType()
var pv *privval.FilePV
if tmos.FileExists(privValKeyFile) {
pv = privval.LoadFilePV(privValKeyFile, privValStateFile)
logger.Info("Found private validator", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
} else {
var err error
pv, err = privval.GenFilePV(privValKeyFile, privValStateFile, privKeyType)
if err != nil {
return err
}
if pv != nil {
pv.Save()
}
logger.Info("Generated private validator", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
}
nodeKeyFile := config.NodeKeyFile()
if tmos.FileExists(nodeKeyFile) {
logger.Info("Found node key", "path", nodeKeyFile)
} else {
if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil {
return err
}
logger.Info("Generated node key", "path", nodeKeyFile)
}
// genesis file
genFile := config.GenesisFile()
if tmos.FileExists(genFile) {
logger.Info("Found genesis file", "path", genFile)
} else {
genDoc := types.GenesisDoc{
ChainID: fmt.Sprintf("test-chain-%v", tmrand.Str(6)),
GenesisTime: tmtime.Now(),
ConsensusParams: types.DefaultConsensusParams(),
VoterParams: types.DefaultVoterParams(),
}
pubKey, err := pv.GetPubKey()
if err != nil {
return fmt.Errorf("can't get pubkey: %w", err)
}
genDoc.Validators = []types.GenesisValidator{{
Address: pubKey.Address(),
PubKey: pubKey,
Power: 10,
}}
if err := genDoc.SaveAs(genFile); err != nil {
return err
}
logger.Info("Generated genesis file", "path", genFile)
}
return nil
}
func loadFilePVKey(t *testing.T, file string) privval.FilePVKey {
// output must match the locally stored priv_validator key
keyJSONBytes, err := ioutil.ReadFile(file)
require.NoError(t, err)
privKey := privval.FilePVKey{}
err = tmjson.Unmarshal(keyJSONBytes, &privKey)
require.NoError(t, err)
return privKey
}
func captureStdout(t *testing.T, fnc func()) string {
t.Helper()
backup := os.Stdout
defer func() {
os.Stdout = backup
}()
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("fail pipe: %v", err)
}
os.Stdout = w
fnc()
w.Close()
var buffer bytes.Buffer
if n, err := buffer.ReadFrom(r); err != nil {
t.Fatalf("fail read buf: %v - number: %v", err, n)
}
output := buffer.String()
return output[:len(output)-1]
}