github.com/lbryio/lbcd@v0.22.119/btcjson/walletsvrwscmds_test.go (about) 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/lbryio/lbcd/btcjson" 15 ) 16 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) { 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: "createencryptedwallet", 34 newCmd: func() (interface{}, error) { 35 return btcjson.NewCmd("createencryptedwallet", "pass") 36 }, 37 staticCmd: func() interface{} { 38 return btcjson.NewCreateEncryptedWalletCmd("pass") 39 }, 40 marshalled: `{"jsonrpc":"1.0","method":"createencryptedwallet","params":["pass"],"id":1}`, 41 unmarshalled: &btcjson.CreateEncryptedWalletCmd{Passphrase: "pass"}, 42 }, 43 { 44 name: "exportwatchingwallet", 45 newCmd: func() (interface{}, error) { 46 return btcjson.NewCmd("exportwatchingwallet") 47 }, 48 staticCmd: func() interface{} { 49 return btcjson.NewExportWatchingWalletCmd(nil, nil) 50 }, 51 marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":[],"id":1}`, 52 unmarshalled: &btcjson.ExportWatchingWalletCmd{ 53 Account: nil, 54 Download: btcjson.Bool(false), 55 }, 56 }, 57 { 58 name: "exportwatchingwallet optional1", 59 newCmd: func() (interface{}, error) { 60 return btcjson.NewCmd("exportwatchingwallet", "acct") 61 }, 62 staticCmd: func() interface{} { 63 return btcjson.NewExportWatchingWalletCmd(btcjson.String("acct"), nil) 64 }, 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), 69 }, 70 }, 71 { 72 name: "exportwatchingwallet optional2", 73 newCmd: func() (interface{}, error) { 74 return btcjson.NewCmd("exportwatchingwallet", btcjson.String("acct"), true) 75 }, 76 staticCmd: func() interface{} { 77 return btcjson.NewExportWatchingWalletCmd(btcjson.String("acct"), 78 btcjson.Bool(true)) 79 }, 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), 84 }, 85 }, 86 { 87 name: "getunconfirmedbalance", 88 newCmd: func() (interface{}, error) { 89 return btcjson.NewCmd("getunconfirmedbalance") 90 }, 91 staticCmd: func() interface{} { 92 return btcjson.NewGetUnconfirmedBalanceCmd(nil) 93 }, 94 marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":[],"id":1}`, 95 unmarshalled: &btcjson.GetUnconfirmedBalanceCmd{ 96 Account: btcjson.String("default"), 97 }, 98 }, 99 { 100 name: "getunconfirmedbalance optional1", 101 newCmd: func() (interface{}, error) { 102 return btcjson.NewCmd("getunconfirmedbalance", "acct") 103 }, 104 staticCmd: func() interface{} { 105 return btcjson.NewGetUnconfirmedBalanceCmd(btcjson.String("acct")) 106 }, 107 marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":["acct"],"id":1}`, 108 unmarshalled: &btcjson.GetUnconfirmedBalanceCmd{ 109 Account: btcjson.String("acct"), 110 }, 111 }, 112 { 113 name: "listaddresstransactions", 114 newCmd: func() (interface{}, error) { 115 return btcjson.NewCmd("listaddresstransactions", `["1Address"]`) 116 }, 117 staticCmd: func() interface{} { 118 return btcjson.NewListAddressTransactionsCmd([]string{"1Address"}, nil) 119 }, 120 marshalled: `{"jsonrpc":"1.0","method":"listaddresstransactions","params":[["1Address"]],"id":1}`, 121 unmarshalled: &btcjson.ListAddressTransactionsCmd{ 122 Addresses: []string{"1Address"}, 123 Account: btcjson.String("default"), 124 }, 125 }, 126 { 127 name: "listaddresstransactions optional1", 128 newCmd: func() (interface{}, error) { 129 return btcjson.NewCmd("listaddresstransactions", `["1Address"]`, "acct") 130 }, 131 staticCmd: func() interface{} { 132 return btcjson.NewListAddressTransactionsCmd([]string{"1Address"}, 133 btcjson.String("acct")) 134 }, 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"), 139 }, 140 }, 141 { 142 name: "listalltransactions", 143 newCmd: func() (interface{}, error) { 144 return btcjson.NewCmd("listalltransactions") 145 }, 146 staticCmd: func() interface{} { 147 return btcjson.NewListAllTransactionsCmd(nil) 148 }, 149 marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":[],"id":1}`, 150 unmarshalled: &btcjson.ListAllTransactionsCmd{ 151 Account: btcjson.String("default"), 152 }, 153 }, 154 { 155 name: "listalltransactions optional", 156 newCmd: func() (interface{}, error) { 157 return btcjson.NewCmd("listalltransactions", "acct") 158 }, 159 staticCmd: func() interface{} { 160 return btcjson.NewListAllTransactionsCmd(btcjson.String("acct")) 161 }, 162 marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":["acct"],"id":1}`, 163 unmarshalled: &btcjson.ListAllTransactionsCmd{ 164 Account: btcjson.String("acct"), 165 }, 166 }, 167 { 168 name: "recoveraddresses", 169 newCmd: func() (interface{}, error) { 170 return btcjson.NewCmd("recoveraddresses", "acct", 10) 171 }, 172 staticCmd: func() interface{} { 173 return btcjson.NewRecoverAddressesCmd("acct", 10) 174 }, 175 marshalled: `{"jsonrpc":"1.0","method":"recoveraddresses","params":["acct",10],"id":1}`, 176 unmarshalled: &btcjson.RecoverAddressesCmd{ 177 Account: "acct", 178 N: 10, 179 }, 180 }, 181 { 182 name: "walletislocked", 183 newCmd: func() (interface{}, error) { 184 return btcjson.NewCmd("walletislocked") 185 }, 186 staticCmd: func() interface{} { 187 return btcjson.NewWalletIsLockedCmd() 188 }, 189 marshalled: `{"jsonrpc":"1.0","method":"walletislocked","params":[],"id":1}`, 190 unmarshalled: &btcjson.WalletIsLockedCmd{}, 191 }, 192 } 193 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(btcjson.RpcVersion1, testID, test.staticCmd()) 199 if err != nil { 200 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i, 201 test.name, err) 202 continue 203 } 204 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, 208 test.marshalled) 209 continue 210 } 211 212 // Ensure the command is created without error via the generic 213 // new command creation function. 214 cmd, err := test.newCmd() 215 if err != nil { 216 t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ", 217 i, test.name, err) 218 } 219 220 // Marshal the command as created by the generic new command 221 // creation function. 222 marshalled, err = btcjson.MarshalCmd(btcjson.RpcVersion1, testID, cmd) 223 if err != nil { 224 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i, 225 test.name, err) 226 continue 227 } 228 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, 232 test.marshalled) 233 continue 234 } 235 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, 240 test.name, err) 241 continue 242 } 243 244 cmd, err = btcjson.UnmarshalCmd(&request) 245 if err != nil { 246 t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i, 247 test.name, err) 248 continue 249 } 250 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)) 256 continue 257 } 258 } 259 }