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