github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcjson/walletsvrwscmds_test.go (about) 1 // Copyright (c) 2014 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package btcjson_test 7 8 import ( 9 "bytes" 10 "encoding/json" 11 "fmt" 12 "reflect" 13 "testing" 14 15 "github.com/dashpay/godash/btcjson" 16 ) 17 18 // TestWalletSvrWsCmds tests all of the wallet server websocket-specific 19 // commands marshal and unmarshal into valid results include handling of 20 // optional fields being omitted in the marshalled command, while optional 21 // fields with defaults have the default assigned on unmarshalled commands. 22 func TestWalletSvrWsCmds(t *testing.T) { 23 t.Parallel() 24 25 testID := int(1) 26 tests := []struct { 27 name string 28 newCmd func() (interface{}, error) 29 staticCmd func() interface{} 30 marshalled string 31 unmarshalled interface{} 32 }{ 33 { 34 name: "createencryptedwallet", 35 newCmd: func() (interface{}, error) { 36 return btcjson.NewCmd("createencryptedwallet", "pass") 37 }, 38 staticCmd: func() interface{} { 39 return btcjson.NewCreateEncryptedWalletCmd("pass") 40 }, 41 marshalled: `{"jsonrpc":"1.0","method":"createencryptedwallet","params":["pass"],"id":1}`, 42 unmarshalled: &btcjson.CreateEncryptedWalletCmd{Passphrase: "pass"}, 43 }, 44 { 45 name: "exportwatchingwallet", 46 newCmd: func() (interface{}, error) { 47 return btcjson.NewCmd("exportwatchingwallet") 48 }, 49 staticCmd: func() interface{} { 50 return btcjson.NewExportWatchingWalletCmd(nil, nil) 51 }, 52 marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":[],"id":1}`, 53 unmarshalled: &btcjson.ExportWatchingWalletCmd{ 54 Account: nil, 55 Download: btcjson.Bool(false), 56 }, 57 }, 58 { 59 name: "exportwatchingwallet optional1", 60 newCmd: func() (interface{}, error) { 61 return btcjson.NewCmd("exportwatchingwallet", "acct") 62 }, 63 staticCmd: func() interface{} { 64 return btcjson.NewExportWatchingWalletCmd(btcjson.String("acct"), nil) 65 }, 66 marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":["acct"],"id":1}`, 67 unmarshalled: &btcjson.ExportWatchingWalletCmd{ 68 Account: btcjson.String("acct"), 69 Download: btcjson.Bool(false), 70 }, 71 }, 72 { 73 name: "exportwatchingwallet optional2", 74 newCmd: func() (interface{}, error) { 75 return btcjson.NewCmd("exportwatchingwallet", "acct", true) 76 }, 77 staticCmd: func() interface{} { 78 return btcjson.NewExportWatchingWalletCmd(btcjson.String("acct"), 79 btcjson.Bool(true)) 80 }, 81 marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":["acct",true],"id":1}`, 82 unmarshalled: &btcjson.ExportWatchingWalletCmd{ 83 Account: btcjson.String("acct"), 84 Download: btcjson.Bool(true), 85 }, 86 }, 87 { 88 name: "getunconfirmedbalance", 89 newCmd: func() (interface{}, error) { 90 return btcjson.NewCmd("getunconfirmedbalance") 91 }, 92 staticCmd: func() interface{} { 93 return btcjson.NewGetUnconfirmedBalanceCmd(nil) 94 }, 95 marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":[],"id":1}`, 96 unmarshalled: &btcjson.GetUnconfirmedBalanceCmd{ 97 Account: nil, 98 }, 99 }, 100 { 101 name: "getunconfirmedbalance optional1", 102 newCmd: func() (interface{}, error) { 103 return btcjson.NewCmd("getunconfirmedbalance", "acct") 104 }, 105 staticCmd: func() interface{} { 106 return btcjson.NewGetUnconfirmedBalanceCmd(btcjson.String("acct")) 107 }, 108 marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":["acct"],"id":1}`, 109 unmarshalled: &btcjson.GetUnconfirmedBalanceCmd{ 110 Account: btcjson.String("acct"), 111 }, 112 }, 113 { 114 name: "listaddresstransactions", 115 newCmd: func() (interface{}, error) { 116 return btcjson.NewCmd("listaddresstransactions", `["1Address"]`) 117 }, 118 staticCmd: func() interface{} { 119 return btcjson.NewListAddressTransactionsCmd([]string{"1Address"}, nil) 120 }, 121 marshalled: `{"jsonrpc":"1.0","method":"listaddresstransactions","params":[["1Address"]],"id":1}`, 122 unmarshalled: &btcjson.ListAddressTransactionsCmd{ 123 Addresses: []string{"1Address"}, 124 Account: nil, 125 }, 126 }, 127 { 128 name: "listaddresstransactions optional1", 129 newCmd: func() (interface{}, error) { 130 return btcjson.NewCmd("listaddresstransactions", `["1Address"]`, "acct") 131 }, 132 staticCmd: func() interface{} { 133 return btcjson.NewListAddressTransactionsCmd([]string{"1Address"}, 134 btcjson.String("acct")) 135 }, 136 marshalled: `{"jsonrpc":"1.0","method":"listaddresstransactions","params":[["1Address"],"acct"],"id":1}`, 137 unmarshalled: &btcjson.ListAddressTransactionsCmd{ 138 Addresses: []string{"1Address"}, 139 Account: btcjson.String("acct"), 140 }, 141 }, 142 { 143 name: "listalltransactions", 144 newCmd: func() (interface{}, error) { 145 return btcjson.NewCmd("listalltransactions") 146 }, 147 staticCmd: func() interface{} { 148 return btcjson.NewListAllTransactionsCmd(nil) 149 }, 150 marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":[],"id":1}`, 151 unmarshalled: &btcjson.ListAllTransactionsCmd{ 152 Account: nil, 153 }, 154 }, 155 { 156 name: "listalltransactions optional", 157 newCmd: func() (interface{}, error) { 158 return btcjson.NewCmd("listalltransactions", "acct") 159 }, 160 staticCmd: func() interface{} { 161 return btcjson.NewListAllTransactionsCmd(btcjson.String("acct")) 162 }, 163 marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":["acct"],"id":1}`, 164 unmarshalled: &btcjson.ListAllTransactionsCmd{ 165 Account: btcjson.String("acct"), 166 }, 167 }, 168 { 169 name: "recoveraddresses", 170 newCmd: func() (interface{}, error) { 171 return btcjson.NewCmd("recoveraddresses", "acct", 10) 172 }, 173 staticCmd: func() interface{} { 174 return btcjson.NewRecoverAddressesCmd("acct", 10) 175 }, 176 marshalled: `{"jsonrpc":"1.0","method":"recoveraddresses","params":["acct",10],"id":1}`, 177 unmarshalled: &btcjson.RecoverAddressesCmd{ 178 Account: "acct", 179 N: 10, 180 }, 181 }, 182 { 183 name: "walletislocked", 184 newCmd: func() (interface{}, error) { 185 return btcjson.NewCmd("walletislocked") 186 }, 187 staticCmd: func() interface{} { 188 return btcjson.NewWalletIsLockedCmd() 189 }, 190 marshalled: `{"jsonrpc":"1.0","method":"walletislocked","params":[],"id":1}`, 191 unmarshalled: &btcjson.WalletIsLockedCmd{}, 192 }, 193 } 194 195 t.Logf("Running %d tests", len(tests)) 196 for i, test := range tests { 197 // Marshal the command as created by the new static command 198 // creation function. 199 marshalled, err := btcjson.MarshalCmd(testID, test.staticCmd()) 200 if err != nil { 201 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i, 202 test.name, err) 203 continue 204 } 205 206 if !bytes.Equal(marshalled, []byte(test.marshalled)) { 207 t.Errorf("Test #%d (%s) unexpected marshalled data - "+ 208 "got %s, want %s", i, test.name, marshalled, 209 test.marshalled) 210 continue 211 } 212 213 // Ensure the command is created without error via the generic 214 // new command creation function. 215 cmd, err := test.newCmd() 216 if err != nil { 217 t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ", 218 i, test.name, err) 219 } 220 221 // Marshal the command as created by the generic new command 222 // creation function. 223 marshalled, err = btcjson.MarshalCmd(testID, cmd) 224 if err != nil { 225 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i, 226 test.name, err) 227 continue 228 } 229 230 if !bytes.Equal(marshalled, []byte(test.marshalled)) { 231 t.Errorf("Test #%d (%s) unexpected marshalled data - "+ 232 "got %s, want %s", i, test.name, marshalled, 233 test.marshalled) 234 continue 235 } 236 237 var request btcjson.Request 238 if err := json.Unmarshal(marshalled, &request); err != nil { 239 t.Errorf("Test #%d (%s) unexpected error while "+ 240 "unmarshalling JSON-RPC request: %v", i, 241 test.name, err) 242 continue 243 } 244 245 cmd, err = btcjson.UnmarshalCmd(&request) 246 if err != nil { 247 t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i, 248 test.name, err) 249 continue 250 } 251 252 if !reflect.DeepEqual(cmd, test.unmarshalled) { 253 t.Errorf("Test #%d (%s) unexpected unmarshalled command "+ 254 "- got %s, want %s", i, test.name, 255 fmt.Sprintf("(%T) %+[1]v", cmd), 256 fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled)) 257 continue 258 } 259 } 260 }