OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / btcjson / btcwalletextcmds_test.go
1 // Copyright (c) 2014 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package btcjson_test
6
7 import (
8         "bytes"
9         "encoding/json"
10         "fmt"
11         "reflect"
12         "testing"
13
14         "github.com/btcsuite/btcd/btcjson"
15 )
16
17 // TestBtcWalletExtCmds tests all of the btcwallet extended commands marshal and
18 // unmarshal into valid results include handling of optional fields being
19 // omitted in the marshalled command, while optional fields with defaults have
20 // the default assigned on unmarshalled commands.
21 func TestBtcWalletExtCmds(t *testing.T) {
22         t.Parallel()
23
24         testID := int(1)
25         tests := []struct {
26                 name         string
27                 newCmd       func() (interface{}, error)
28                 staticCmd    func() interface{}
29                 marshalled   string
30                 unmarshalled interface{}
31         }{
32                 {
33                         name: "createnewaccount",
34                         newCmd: func() (interface{}, error) {
35                                 return btcjson.NewCmd("createnewaccount", "acct")
36                         },
37                         staticCmd: func() interface{} {
38                                 return btcjson.NewCreateNewAccountCmd("acct")
39                         },
40                         marshalled: `{"jsonrpc":"1.0","method":"createnewaccount","params":["acct"],"id":1}`,
41                         unmarshalled: &btcjson.CreateNewAccountCmd{
42                                 Account: "acct",
43                         },
44                 },
45                 {
46                         name: "dumpwallet",
47                         newCmd: func() (interface{}, error) {
48                                 return btcjson.NewCmd("dumpwallet", "filename")
49                         },
50                         staticCmd: func() interface{} {
51                                 return btcjson.NewDumpWalletCmd("filename")
52                         },
53                         marshalled: `{"jsonrpc":"1.0","method":"dumpwallet","params":["filename"],"id":1}`,
54                         unmarshalled: &btcjson.DumpWalletCmd{
55                                 Filename: "filename",
56                         },
57                 },
58                 {
59                         name: "importaddress",
60                         newCmd: func() (interface{}, error) {
61                                 return btcjson.NewCmd("importaddress", "1Address")
62                         },
63                         staticCmd: func() interface{} {
64                                 return btcjson.NewImportAddressCmd("1Address", nil)
65                         },
66                         marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address"],"id":1}`,
67                         unmarshalled: &btcjson.ImportAddressCmd{
68                                 Address: "1Address",
69                                 Rescan:  btcjson.Bool(true),
70                         },
71                 },
72                 {
73                         name: "importaddress optional",
74                         newCmd: func() (interface{}, error) {
75                                 return btcjson.NewCmd("importaddress", "1Address", false)
76                         },
77                         staticCmd: func() interface{} {
78                                 return btcjson.NewImportAddressCmd("1Address", btcjson.Bool(false))
79                         },
80                         marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address",false],"id":1}`,
81                         unmarshalled: &btcjson.ImportAddressCmd{
82                                 Address: "1Address",
83                                 Rescan:  btcjson.Bool(false),
84                         },
85                 },
86                 {
87                         name: "importpubkey",
88                         newCmd: func() (interface{}, error) {
89                                 return btcjson.NewCmd("importpubkey", "031234")
90                         },
91                         staticCmd: func() interface{} {
92                                 return btcjson.NewImportPubKeyCmd("031234", nil)
93                         },
94                         marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234"],"id":1}`,
95                         unmarshalled: &btcjson.ImportPubKeyCmd{
96                                 PubKey: "031234",
97                                 Rescan: btcjson.Bool(true),
98                         },
99                 },
100                 {
101                         name: "importpubkey optional",
102                         newCmd: func() (interface{}, error) {
103                                 return btcjson.NewCmd("importpubkey", "031234", false)
104                         },
105                         staticCmd: func() interface{} {
106                                 return btcjson.NewImportPubKeyCmd("031234", btcjson.Bool(false))
107                         },
108                         marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234",false],"id":1}`,
109                         unmarshalled: &btcjson.ImportPubKeyCmd{
110                                 PubKey: "031234",
111                                 Rescan: btcjson.Bool(false),
112                         },
113                 },
114                 {
115                         name: "importwallet",
116                         newCmd: func() (interface{}, error) {
117                                 return btcjson.NewCmd("importwallet", "filename")
118                         },
119                         staticCmd: func() interface{} {
120                                 return btcjson.NewImportWalletCmd("filename")
121                         },
122                         marshalled: `{"jsonrpc":"1.0","method":"importwallet","params":["filename"],"id":1}`,
123                         unmarshalled: &btcjson.ImportWalletCmd{
124                                 Filename: "filename",
125                         },
126                 },
127                 {
128                         name: "renameaccount",
129                         newCmd: func() (interface{}, error) {
130                                 return btcjson.NewCmd("renameaccount", "oldacct", "newacct")
131                         },
132                         staticCmd: func() interface{} {
133                                 return btcjson.NewRenameAccountCmd("oldacct", "newacct")
134                         },
135                         marshalled: `{"jsonrpc":"1.0","method":"renameaccount","params":["oldacct","newacct"],"id":1}`,
136                         unmarshalled: &btcjson.RenameAccountCmd{
137                                 OldAccount: "oldacct",
138                                 NewAccount: "newacct",
139                         },
140                 },
141         }
142
143         t.Logf("Running %d tests", len(tests))
144         for i, test := range tests {
145                 // Marshal the command as created by the new static command
146                 // creation function.
147                 marshalled, err := btcjson.MarshalCmd(testID, test.staticCmd())
148                 if err != nil {
149                         t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
150                                 test.name, err)
151                         continue
152                 }
153
154                 if !bytes.Equal(marshalled, []byte(test.marshalled)) {
155                         t.Errorf("Test #%d (%s) unexpected marshalled data - "+
156                                 "got %s, want %s", i, test.name, marshalled,
157                                 test.marshalled)
158                         continue
159                 }
160
161                 // Ensure the command is created without error via the generic
162                 // new command creation function.
163                 cmd, err := test.newCmd()
164                 if err != nil {
165                         t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
166                                 i, test.name, err)
167                 }
168
169                 // Marshal the command as created by the generic new command
170                 // creation function.
171                 marshalled, err = btcjson.MarshalCmd(testID, cmd)
172                 if err != nil {
173                         t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
174                                 test.name, err)
175                         continue
176                 }
177
178                 if !bytes.Equal(marshalled, []byte(test.marshalled)) {
179                         t.Errorf("Test #%d (%s) unexpected marshalled data - "+
180                                 "got %s, want %s", i, test.name, marshalled,
181                                 test.marshalled)
182                         continue
183                 }
184
185                 var request btcjson.Request
186                 if err := json.Unmarshal(marshalled, &request); err != nil {
187                         t.Errorf("Test #%d (%s) unexpected error while "+
188                                 "unmarshalling JSON-RPC request: %v", i,
189                                 test.name, err)
190                         continue
191                 }
192
193                 cmd, err = btcjson.UnmarshalCmd(&request)
194                 if err != nil {
195                         t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
196                                 test.name, err)
197                         continue
198                 }
199
200                 if !reflect.DeepEqual(cmd, test.unmarshalled) {
201                         t.Errorf("Test #%d (%s) unexpected unmarshalled command "+
202                                 "- got %s, want %s", i, test.name,
203                                 fmt.Sprintf("(%T) %+[1]v", cmd),
204                                 fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled))
205                         continue
206                 }
207         }
208 }