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.
14 "github.com/btcsuite/btcd/btcjson"
17 // TestWalletSvrWsCmds tests all of the wallet server websocket-specific
18 // commands marshal and unmarshal into valid results include handling of
19 // optional fields being omitted in the marshalled command, while optional
20 // fields with defaults have the default assigned on unmarshalled commands.
21 func TestWalletSvrWsCmds(t *testing.T) {
27 newCmd func() (interface{}, error)
28 staticCmd func() interface{}
30 unmarshalled interface{}
33 name: "createencryptedwallet",
34 newCmd: func() (interface{}, error) {
35 return btcjson.NewCmd("createencryptedwallet", "pass")
37 staticCmd: func() interface{} {
38 return btcjson.NewCreateEncryptedWalletCmd("pass")
40 marshalled: `{"jsonrpc":"1.0","method":"createencryptedwallet","params":["pass"],"id":1}`,
41 unmarshalled: &btcjson.CreateEncryptedWalletCmd{Passphrase: "pass"},
44 name: "exportwatchingwallet",
45 newCmd: func() (interface{}, error) {
46 return btcjson.NewCmd("exportwatchingwallet")
48 staticCmd: func() interface{} {
49 return btcjson.NewExportWatchingWalletCmd(nil, nil)
51 marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":[],"id":1}`,
52 unmarshalled: &btcjson.ExportWatchingWalletCmd{
54 Download: btcjson.Bool(false),
58 name: "exportwatchingwallet optional1",
59 newCmd: func() (interface{}, error) {
60 return btcjson.NewCmd("exportwatchingwallet", "acct")
62 staticCmd: func() interface{} {
63 return btcjson.NewExportWatchingWalletCmd(btcjson.String("acct"), nil)
65 marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":["acct"],"id":1}`,
66 unmarshalled: &btcjson.ExportWatchingWalletCmd{
67 Account: btcjson.String("acct"),
68 Download: btcjson.Bool(false),
72 name: "exportwatchingwallet optional2",
73 newCmd: func() (interface{}, error) {
74 return btcjson.NewCmd("exportwatchingwallet", "acct", true)
76 staticCmd: func() interface{} {
77 return btcjson.NewExportWatchingWalletCmd(btcjson.String("acct"),
80 marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":["acct",true],"id":1}`,
81 unmarshalled: &btcjson.ExportWatchingWalletCmd{
82 Account: btcjson.String("acct"),
83 Download: btcjson.Bool(true),
87 name: "getunconfirmedbalance",
88 newCmd: func() (interface{}, error) {
89 return btcjson.NewCmd("getunconfirmedbalance")
91 staticCmd: func() interface{} {
92 return btcjson.NewGetUnconfirmedBalanceCmd(nil)
94 marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":[],"id":1}`,
95 unmarshalled: &btcjson.GetUnconfirmedBalanceCmd{
100 name: "getunconfirmedbalance optional1",
101 newCmd: func() (interface{}, error) {
102 return btcjson.NewCmd("getunconfirmedbalance", "acct")
104 staticCmd: func() interface{} {
105 return btcjson.NewGetUnconfirmedBalanceCmd(btcjson.String("acct"))
107 marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":["acct"],"id":1}`,
108 unmarshalled: &btcjson.GetUnconfirmedBalanceCmd{
109 Account: btcjson.String("acct"),
113 name: "listaddresstransactions",
114 newCmd: func() (interface{}, error) {
115 return btcjson.NewCmd("listaddresstransactions", `["1Address"]`)
117 staticCmd: func() interface{} {
118 return btcjson.NewListAddressTransactionsCmd([]string{"1Address"}, nil)
120 marshalled: `{"jsonrpc":"1.0","method":"listaddresstransactions","params":[["1Address"]],"id":1}`,
121 unmarshalled: &btcjson.ListAddressTransactionsCmd{
122 Addresses: []string{"1Address"},
127 name: "listaddresstransactions optional1",
128 newCmd: func() (interface{}, error) {
129 return btcjson.NewCmd("listaddresstransactions", `["1Address"]`, "acct")
131 staticCmd: func() interface{} {
132 return btcjson.NewListAddressTransactionsCmd([]string{"1Address"},
133 btcjson.String("acct"))
135 marshalled: `{"jsonrpc":"1.0","method":"listaddresstransactions","params":[["1Address"],"acct"],"id":1}`,
136 unmarshalled: &btcjson.ListAddressTransactionsCmd{
137 Addresses: []string{"1Address"},
138 Account: btcjson.String("acct"),
142 name: "listalltransactions",
143 newCmd: func() (interface{}, error) {
144 return btcjson.NewCmd("listalltransactions")
146 staticCmd: func() interface{} {
147 return btcjson.NewListAllTransactionsCmd(nil)
149 marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":[],"id":1}`,
150 unmarshalled: &btcjson.ListAllTransactionsCmd{
155 name: "listalltransactions optional",
156 newCmd: func() (interface{}, error) {
157 return btcjson.NewCmd("listalltransactions", "acct")
159 staticCmd: func() interface{} {
160 return btcjson.NewListAllTransactionsCmd(btcjson.String("acct"))
162 marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":["acct"],"id":1}`,
163 unmarshalled: &btcjson.ListAllTransactionsCmd{
164 Account: btcjson.String("acct"),
168 name: "recoveraddresses",
169 newCmd: func() (interface{}, error) {
170 return btcjson.NewCmd("recoveraddresses", "acct", 10)
172 staticCmd: func() interface{} {
173 return btcjson.NewRecoverAddressesCmd("acct", 10)
175 marshalled: `{"jsonrpc":"1.0","method":"recoveraddresses","params":["acct",10],"id":1}`,
176 unmarshalled: &btcjson.RecoverAddressesCmd{
182 name: "walletislocked",
183 newCmd: func() (interface{}, error) {
184 return btcjson.NewCmd("walletislocked")
186 staticCmd: func() interface{} {
187 return btcjson.NewWalletIsLockedCmd()
189 marshalled: `{"jsonrpc":"1.0","method":"walletislocked","params":[],"id":1}`,
190 unmarshalled: &btcjson.WalletIsLockedCmd{},
194 t.Logf("Running %d tests", len(tests))
195 for i, test := range tests {
196 // Marshal the command as created by the new static command
197 // creation function.
198 marshalled, err := btcjson.MarshalCmd(testID, test.staticCmd())
200 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
205 if !bytes.Equal(marshalled, []byte(test.marshalled)) {
206 t.Errorf("Test #%d (%s) unexpected marshalled data - "+
207 "got %s, want %s", i, test.name, marshalled,
212 // Ensure the command is created without error via the generic
213 // new command creation function.
214 cmd, err := test.newCmd()
216 t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
220 // Marshal the command as created by the generic new command
221 // creation function.
222 marshalled, err = btcjson.MarshalCmd(testID, cmd)
224 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
229 if !bytes.Equal(marshalled, []byte(test.marshalled)) {
230 t.Errorf("Test #%d (%s) unexpected marshalled data - "+
231 "got %s, want %s", i, test.name, marshalled,
236 var request btcjson.Request
237 if err := json.Unmarshal(marshalled, &request); err != nil {
238 t.Errorf("Test #%d (%s) unexpected error while "+
239 "unmarshalling JSON-RPC request: %v", i,
244 cmd, err = btcjson.UnmarshalCmd(&request)
246 t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
251 if !reflect.DeepEqual(cmd, test.unmarshalled) {
252 t.Errorf("Test #%d (%s) unexpected unmarshalled command "+
253 "- got %s, want %s", i, test.name,
254 fmt.Sprintf("(%T) %+[1]v", cmd),
255 fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled))