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