github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcjson/chainsvrwscmds_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  // TestChainSvrWsCmds tests all of the chain server websocket-specific commands
    19  // marshal and unmarshal into valid results include handling of optional fields
    20  // being omitted in the marshalled command, while optional fields with defaults
    21  // have the default assigned on unmarshalled commands.
    22  func TestChainSvrWsCmds(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: "authenticate",
    35  			newCmd: func() (interface{}, error) {
    36  				return btcjson.NewCmd("authenticate", "user", "pass")
    37  			},
    38  			staticCmd: func() interface{} {
    39  				return btcjson.NewAuthenticateCmd("user", "pass")
    40  			},
    41  			marshalled:   `{"jsonrpc":"1.0","method":"authenticate","params":["user","pass"],"id":1}`,
    42  			unmarshalled: &btcjson.AuthenticateCmd{Username: "user", Passphrase: "pass"},
    43  		},
    44  		{
    45  			name: "notifyblocks",
    46  			newCmd: func() (interface{}, error) {
    47  				return btcjson.NewCmd("notifyblocks")
    48  			},
    49  			staticCmd: func() interface{} {
    50  				return btcjson.NewNotifyBlocksCmd()
    51  			},
    52  			marshalled:   `{"jsonrpc":"1.0","method":"notifyblocks","params":[],"id":1}`,
    53  			unmarshalled: &btcjson.NotifyBlocksCmd{},
    54  		},
    55  		{
    56  			name: "stopnotifyblocks",
    57  			newCmd: func() (interface{}, error) {
    58  				return btcjson.NewCmd("stopnotifyblocks")
    59  			},
    60  			staticCmd: func() interface{} {
    61  				return btcjson.NewStopNotifyBlocksCmd()
    62  			},
    63  			marshalled:   `{"jsonrpc":"1.0","method":"stopnotifyblocks","params":[],"id":1}`,
    64  			unmarshalled: &btcjson.StopNotifyBlocksCmd{},
    65  		},
    66  		{
    67  			name: "notifynewtransactions",
    68  			newCmd: func() (interface{}, error) {
    69  				return btcjson.NewCmd("notifynewtransactions")
    70  			},
    71  			staticCmd: func() interface{} {
    72  				return btcjson.NewNotifyNewTransactionsCmd(nil)
    73  			},
    74  			marshalled: `{"jsonrpc":"1.0","method":"notifynewtransactions","params":[],"id":1}`,
    75  			unmarshalled: &btcjson.NotifyNewTransactionsCmd{
    76  				Verbose: btcjson.Bool(false),
    77  			},
    78  		},
    79  		{
    80  			name: "notifynewtransactions optional",
    81  			newCmd: func() (interface{}, error) {
    82  				return btcjson.NewCmd("notifynewtransactions", true)
    83  			},
    84  			staticCmd: func() interface{} {
    85  				return btcjson.NewNotifyNewTransactionsCmd(btcjson.Bool(true))
    86  			},
    87  			marshalled: `{"jsonrpc":"1.0","method":"notifynewtransactions","params":[true],"id":1}`,
    88  			unmarshalled: &btcjson.NotifyNewTransactionsCmd{
    89  				Verbose: btcjson.Bool(true),
    90  			},
    91  		},
    92  		{
    93  			name: "stopnotifynewtransactions",
    94  			newCmd: func() (interface{}, error) {
    95  				return btcjson.NewCmd("stopnotifynewtransactions")
    96  			},
    97  			staticCmd: func() interface{} {
    98  				return btcjson.NewStopNotifyNewTransactionsCmd()
    99  			},
   100  			marshalled:   `{"jsonrpc":"1.0","method":"stopnotifynewtransactions","params":[],"id":1}`,
   101  			unmarshalled: &btcjson.StopNotifyNewTransactionsCmd{},
   102  		},
   103  		{
   104  			name: "notifyreceived",
   105  			newCmd: func() (interface{}, error) {
   106  				return btcjson.NewCmd("notifyreceived", []string{"1Address"})
   107  			},
   108  			staticCmd: func() interface{} {
   109  				return btcjson.NewNotifyReceivedCmd([]string{"1Address"})
   110  			},
   111  			marshalled: `{"jsonrpc":"1.0","method":"notifyreceived","params":[["1Address"]],"id":1}`,
   112  			unmarshalled: &btcjson.NotifyReceivedCmd{
   113  				Addresses: []string{"1Address"},
   114  			},
   115  		},
   116  		{
   117  			name: "stopnotifyreceived",
   118  			newCmd: func() (interface{}, error) {
   119  				return btcjson.NewCmd("stopnotifyreceived", []string{"1Address"})
   120  			},
   121  			staticCmd: func() interface{} {
   122  				return btcjson.NewStopNotifyReceivedCmd([]string{"1Address"})
   123  			},
   124  			marshalled: `{"jsonrpc":"1.0","method":"stopnotifyreceived","params":[["1Address"]],"id":1}`,
   125  			unmarshalled: &btcjson.StopNotifyReceivedCmd{
   126  				Addresses: []string{"1Address"},
   127  			},
   128  		},
   129  		{
   130  			name: "notifyspent",
   131  			newCmd: func() (interface{}, error) {
   132  				return btcjson.NewCmd("notifyspent", `[{"hash":"123","index":0}]`)
   133  			},
   134  			staticCmd: func() interface{} {
   135  				ops := []btcjson.OutPoint{{Hash: "123", Index: 0}}
   136  				return btcjson.NewNotifySpentCmd(ops)
   137  			},
   138  			marshalled: `{"jsonrpc":"1.0","method":"notifyspent","params":[[{"hash":"123","index":0}]],"id":1}`,
   139  			unmarshalled: &btcjson.NotifySpentCmd{
   140  				OutPoints: []btcjson.OutPoint{{Hash: "123", Index: 0}},
   141  			},
   142  		},
   143  		{
   144  			name: "stopnotifyspent",
   145  			newCmd: func() (interface{}, error) {
   146  				return btcjson.NewCmd("stopnotifyspent", `[{"hash":"123","index":0}]`)
   147  			},
   148  			staticCmd: func() interface{} {
   149  				ops := []btcjson.OutPoint{{Hash: "123", Index: 0}}
   150  				return btcjson.NewStopNotifySpentCmd(ops)
   151  			},
   152  			marshalled: `{"jsonrpc":"1.0","method":"stopnotifyspent","params":[[{"hash":"123","index":0}]],"id":1}`,
   153  			unmarshalled: &btcjson.StopNotifySpentCmd{
   154  				OutPoints: []btcjson.OutPoint{{Hash: "123", Index: 0}},
   155  			},
   156  		},
   157  		{
   158  			name: "rescan",
   159  			newCmd: func() (interface{}, error) {
   160  				return btcjson.NewCmd("rescan", "123", `["1Address"]`, `[{"hash":"0000000000000000000000000000000000000000000000000000000000000123","index":0}]`)
   161  			},
   162  			staticCmd: func() interface{} {
   163  				addrs := []string{"1Address"}
   164  				ops := []btcjson.OutPoint{{
   165  					Hash:  "0000000000000000000000000000000000000000000000000000000000000123",
   166  					Index: 0,
   167  				}}
   168  				return btcjson.NewRescanCmd("123", addrs, ops, nil)
   169  			},
   170  			marshalled: `{"jsonrpc":"1.0","method":"rescan","params":["123",["1Address"],[{"hash":"0000000000000000000000000000000000000000000000000000000000000123","index":0}]],"id":1}`,
   171  			unmarshalled: &btcjson.RescanCmd{
   172  				BeginBlock: "123",
   173  				Addresses:  []string{"1Address"},
   174  				OutPoints:  []btcjson.OutPoint{{Hash: "0000000000000000000000000000000000000000000000000000000000000123", Index: 0}},
   175  				EndBlock:   nil,
   176  			},
   177  		},
   178  		{
   179  			name: "rescan optional",
   180  			newCmd: func() (interface{}, error) {
   181  				return btcjson.NewCmd("rescan", "123", `["1Address"]`, `[{"hash":"123","index":0}]`, "456")
   182  			},
   183  			staticCmd: func() interface{} {
   184  				addrs := []string{"1Address"}
   185  				ops := []btcjson.OutPoint{{Hash: "123", Index: 0}}
   186  				return btcjson.NewRescanCmd("123", addrs, ops, btcjson.String("456"))
   187  			},
   188  			marshalled: `{"jsonrpc":"1.0","method":"rescan","params":["123",["1Address"],[{"hash":"123","index":0}],"456"],"id":1}`,
   189  			unmarshalled: &btcjson.RescanCmd{
   190  				BeginBlock: "123",
   191  				Addresses:  []string{"1Address"},
   192  				OutPoints:  []btcjson.OutPoint{{Hash: "123", Index: 0}},
   193  				EndBlock:   btcjson.String("456"),
   194  			},
   195  		},
   196  	}
   197  
   198  	t.Logf("Running %d tests", len(tests))
   199  	for i, test := range tests {
   200  		// Marshal the command as created by the new static command
   201  		// creation function.
   202  		marshalled, err := btcjson.MarshalCmd(testID, test.staticCmd())
   203  		if err != nil {
   204  			t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
   205  				test.name, err)
   206  			continue
   207  		}
   208  
   209  		if !bytes.Equal(marshalled, []byte(test.marshalled)) {
   210  			t.Errorf("Test #%d (%s) unexpected marshalled data - "+
   211  				"got %s, want %s", i, test.name, marshalled,
   212  				test.marshalled)
   213  			continue
   214  		}
   215  
   216  		// Ensure the command is created without error via the generic
   217  		// new command creation function.
   218  		cmd, err := test.newCmd()
   219  		if err != nil {
   220  			t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
   221  				i, test.name, err)
   222  		}
   223  
   224  		// Marshal the command as created by the generic new command
   225  		// creation function.
   226  		marshalled, err = btcjson.MarshalCmd(testID, cmd)
   227  		if err != nil {
   228  			t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
   229  				test.name, err)
   230  			continue
   231  		}
   232  
   233  		if !bytes.Equal(marshalled, []byte(test.marshalled)) {
   234  			t.Errorf("Test #%d (%s) unexpected marshalled data - "+
   235  				"got %s, want %s", i, test.name, marshalled,
   236  				test.marshalled)
   237  			continue
   238  		}
   239  
   240  		var request btcjson.Request
   241  		if err := json.Unmarshal(marshalled, &request); err != nil {
   242  			t.Errorf("Test #%d (%s) unexpected error while "+
   243  				"unmarshalling JSON-RPC request: %v", i,
   244  				test.name, err)
   245  			continue
   246  		}
   247  
   248  		cmd, err = btcjson.UnmarshalCmd(&request)
   249  		if err != nil {
   250  			t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
   251  				test.name, err)
   252  			continue
   253  		}
   254  
   255  		if !reflect.DeepEqual(cmd, test.unmarshalled) {
   256  			t.Errorf("Test #%d (%s) unexpected unmarshalled command "+
   257  				"- got %s, want %s", i, test.name,
   258  				fmt.Sprintf("(%T) %+[1]v", cmd),
   259  				fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled))
   260  			continue
   261  		}
   262  	}
   263  }