-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbtcwalletextcmds_test.go
209 lines (196 loc) · 5.97 KB
/
btcwalletextcmds_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
// Copyright (c) 2014 The btcsuite developers
// Copyright (c) 2018 The Flo developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package flojson_test
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/bitspill/flod/flojson"
)
// TestFloWalletExtCmds tests all of the flowallet extended commands marshal and
// unmarshal into valid results include handling of optional fields being
// omitted in the marshalled command, while optional fields with defaults have
// the default assigned on unmarshalled commands.
func TestFloWalletExtCmds(t *testing.T) {
t.Parallel()
testID := int(1)
tests := []struct {
name string
newCmd func() (interface{}, error)
staticCmd func() interface{}
marshalled string
unmarshalled interface{}
}{
{
name: "createnewaccount",
newCmd: func() (interface{}, error) {
return flojson.NewCmd("createnewaccount", "acct")
},
staticCmd: func() interface{} {
return flojson.NewCreateNewAccountCmd("acct")
},
marshalled: `{"jsonrpc":"1.0","method":"createnewaccount","params":["acct"],"id":1}`,
unmarshalled: &flojson.CreateNewAccountCmd{
Account: "acct",
},
},
{
name: "dumpwallet",
newCmd: func() (interface{}, error) {
return flojson.NewCmd("dumpwallet", "filename")
},
staticCmd: func() interface{} {
return flojson.NewDumpWalletCmd("filename")
},
marshalled: `{"jsonrpc":"1.0","method":"dumpwallet","params":["filename"],"id":1}`,
unmarshalled: &flojson.DumpWalletCmd{
Filename: "filename",
},
},
{
name: "importaddress",
newCmd: func() (interface{}, error) {
return flojson.NewCmd("importaddress", "1Address")
},
staticCmd: func() interface{} {
return flojson.NewImportAddressCmd("1Address", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address"],"id":1}`,
unmarshalled: &flojson.ImportAddressCmd{
Address: "1Address",
Rescan: flojson.Bool(true),
},
},
{
name: "importaddress optional",
newCmd: func() (interface{}, error) {
return flojson.NewCmd("importaddress", "1Address", false)
},
staticCmd: func() interface{} {
return flojson.NewImportAddressCmd("1Address", flojson.Bool(false))
},
marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address",false],"id":1}`,
unmarshalled: &flojson.ImportAddressCmd{
Address: "1Address",
Rescan: flojson.Bool(false),
},
},
{
name: "importpubkey",
newCmd: func() (interface{}, error) {
return flojson.NewCmd("importpubkey", "031234")
},
staticCmd: func() interface{} {
return flojson.NewImportPubKeyCmd("031234", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234"],"id":1}`,
unmarshalled: &flojson.ImportPubKeyCmd{
PubKey: "031234",
Rescan: flojson.Bool(true),
},
},
{
name: "importpubkey optional",
newCmd: func() (interface{}, error) {
return flojson.NewCmd("importpubkey", "031234", false)
},
staticCmd: func() interface{} {
return flojson.NewImportPubKeyCmd("031234", flojson.Bool(false))
},
marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234",false],"id":1}`,
unmarshalled: &flojson.ImportPubKeyCmd{
PubKey: "031234",
Rescan: flojson.Bool(false),
},
},
{
name: "importwallet",
newCmd: func() (interface{}, error) {
return flojson.NewCmd("importwallet", "filename")
},
staticCmd: func() interface{} {
return flojson.NewImportWalletCmd("filename")
},
marshalled: `{"jsonrpc":"1.0","method":"importwallet","params":["filename"],"id":1}`,
unmarshalled: &flojson.ImportWalletCmd{
Filename: "filename",
},
},
{
name: "renameaccount",
newCmd: func() (interface{}, error) {
return flojson.NewCmd("renameaccount", "oldacct", "newacct")
},
staticCmd: func() interface{} {
return flojson.NewRenameAccountCmd("oldacct", "newacct")
},
marshalled: `{"jsonrpc":"1.0","method":"renameaccount","params":["oldacct","newacct"],"id":1}`,
unmarshalled: &flojson.RenameAccountCmd{
OldAccount: "oldacct",
NewAccount: "newacct",
},
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Marshal the command as created by the new static command
// creation function.
marshalled, err := flojson.MarshalCmd(testID, test.staticCmd())
if err != nil {
t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
if !bytes.Equal(marshalled, []byte(test.marshalled)) {
t.Errorf("Test #%d (%s) unexpected marshalled data - "+
"got %s, want %s", i, test.name, marshalled,
test.marshalled)
continue
}
// Ensure the command is created without error via the generic
// new command creation function.
cmd, err := test.newCmd()
if err != nil {
t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
i, test.name, err)
}
// Marshal the command as created by the generic new command
// creation function.
marshalled, err = flojson.MarshalCmd(testID, cmd)
if err != nil {
t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
if !bytes.Equal(marshalled, []byte(test.marshalled)) {
t.Errorf("Test #%d (%s) unexpected marshalled data - "+
"got %s, want %s", i, test.name, marshalled,
test.marshalled)
continue
}
var request flojson.Request
if err := json.Unmarshal(marshalled, &request); err != nil {
t.Errorf("Test #%d (%s) unexpected error while "+
"unmarshalling JSON-RPC request: %v", i,
test.name, err)
continue
}
cmd, err = flojson.UnmarshalCmd(&request)
if err != nil {
t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
if !reflect.DeepEqual(cmd, test.unmarshalled) {
t.Errorf("Test #%d (%s) unexpected unmarshalled command "+
"- got %s, want %s", i, test.name,
fmt.Sprintf("(%T) %+[1]v", cmd),
fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled))
continue
}
}
}