github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcjson/walletsvrwsntfns_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  // TestWalletSvrWsNtfns tests all of the chain server websocket-specific
    19  // notifications 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 TestWalletSvrWsNtfns(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	tests := []struct {
    26  		name         string
    27  		newNtfn      func() (interface{}, error)
    28  		staticNtfn   func() interface{}
    29  		marshalled   string
    30  		unmarshalled interface{}
    31  	}{
    32  		{
    33  			name: "accountbalance",
    34  			newNtfn: func() (interface{}, error) {
    35  				return btcjson.NewCmd("accountbalance", "acct", 1.25, true)
    36  			},
    37  			staticNtfn: func() interface{} {
    38  				return btcjson.NewAccountBalanceNtfn("acct", 1.25, true)
    39  			},
    40  			marshalled: `{"jsonrpc":"1.0","method":"accountbalance","params":["acct",1.25,true],"id":null}`,
    41  			unmarshalled: &btcjson.AccountBalanceNtfn{
    42  				Account:   "acct",
    43  				Balance:   1.25,
    44  				Confirmed: true,
    45  			},
    46  		},
    47  		{
    48  			name: "btcdconnected",
    49  			newNtfn: func() (interface{}, error) {
    50  				return btcjson.NewCmd("btcdconnected", true)
    51  			},
    52  			staticNtfn: func() interface{} {
    53  				return btcjson.NewBtcdConnectedNtfn(true)
    54  			},
    55  			marshalled: `{"jsonrpc":"1.0","method":"btcdconnected","params":[true],"id":null}`,
    56  			unmarshalled: &btcjson.BtcdConnectedNtfn{
    57  				Connected: true,
    58  			},
    59  		},
    60  		{
    61  			name: "walletlockstate",
    62  			newNtfn: func() (interface{}, error) {
    63  				return btcjson.NewCmd("walletlockstate", true)
    64  			},
    65  			staticNtfn: func() interface{} {
    66  				return btcjson.NewWalletLockStateNtfn(true)
    67  			},
    68  			marshalled: `{"jsonrpc":"1.0","method":"walletlockstate","params":[true],"id":null}`,
    69  			unmarshalled: &btcjson.WalletLockStateNtfn{
    70  				Locked: true,
    71  			},
    72  		},
    73  		{
    74  			name: "newtx",
    75  			newNtfn: func() (interface{}, error) {
    76  				return btcjson.NewCmd("newtx", "acct", `{"account":"acct","address":"1Address","category":"send","amount":1.5,"fee":0.0001,"confirmations":1,"txid":"456","walletconflicts":[],"time":12345678,"timereceived":12345876,"vout":789,"otheraccount":"otheracct"}`)
    77  			},
    78  			staticNtfn: func() interface{} {
    79  				result := btcjson.ListTransactionsResult{
    80  					Account:         "acct",
    81  					Address:         "1Address",
    82  					Category:        "send",
    83  					Amount:          1.5,
    84  					Fee:             btcjson.Float64(0.0001),
    85  					Confirmations:   1,
    86  					TxID:            "456",
    87  					WalletConflicts: []string{},
    88  					Time:            12345678,
    89  					TimeReceived:    12345876,
    90  					Vout:            789,
    91  					OtherAccount:    "otheracct",
    92  				}
    93  				return btcjson.NewNewTxNtfn("acct", result)
    94  			},
    95  			marshalled: `{"jsonrpc":"1.0","method":"newtx","params":["acct",{"account":"acct","address":"1Address","amount":1.5,"category":"send","confirmations":1,"fee":0.0001,"time":12345678,"timereceived":12345876,"txid":"456","vout":789,"walletconflicts":[],"otheraccount":"otheracct"}],"id":null}`,
    96  			unmarshalled: &btcjson.NewTxNtfn{
    97  				Account: "acct",
    98  				Details: btcjson.ListTransactionsResult{
    99  					Account:         "acct",
   100  					Address:         "1Address",
   101  					Category:        "send",
   102  					Amount:          1.5,
   103  					Fee:             btcjson.Float64(0.0001),
   104  					Confirmations:   1,
   105  					TxID:            "456",
   106  					WalletConflicts: []string{},
   107  					Time:            12345678,
   108  					TimeReceived:    12345876,
   109  					Vout:            789,
   110  					OtherAccount:    "otheracct",
   111  				},
   112  			},
   113  		},
   114  	}
   115  
   116  	t.Logf("Running %d tests", len(tests))
   117  	for i, test := range tests {
   118  		// Marshal the notification as created by the new static
   119  		// creation function.  The ID is nil for notifications.
   120  		marshalled, err := btcjson.MarshalCmd(nil, test.staticNtfn())
   121  		if err != nil {
   122  			t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
   123  				test.name, err)
   124  			continue
   125  		}
   126  
   127  		if !bytes.Equal(marshalled, []byte(test.marshalled)) {
   128  			t.Errorf("Test #%d (%s) unexpected marshalled data - "+
   129  				"got %s, want %s", i, test.name, marshalled,
   130  				test.marshalled)
   131  			continue
   132  		}
   133  
   134  		// Ensure the notification is created without error via the
   135  		// generic new notification creation function.
   136  		cmd, err := test.newNtfn()
   137  		if err != nil {
   138  			t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
   139  				i, test.name, err)
   140  		}
   141  
   142  		// Marshal the notification as created by the generic new
   143  		// notification creation function.    The ID is nil for
   144  		// notifications.
   145  		marshalled, err = btcjson.MarshalCmd(nil, cmd)
   146  		if err != nil {
   147  			t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
   148  				test.name, err)
   149  			continue
   150  		}
   151  
   152  		if !bytes.Equal(marshalled, []byte(test.marshalled)) {
   153  			t.Errorf("Test #%d (%s) unexpected marshalled data - "+
   154  				"got %s, want %s", i, test.name, marshalled,
   155  				test.marshalled)
   156  			continue
   157  		}
   158  
   159  		var request btcjson.Request
   160  		if err := json.Unmarshal(marshalled, &request); err != nil {
   161  			t.Errorf("Test #%d (%s) unexpected error while "+
   162  				"unmarshalling JSON-RPC request: %v", i,
   163  				test.name, err)
   164  			continue
   165  		}
   166  
   167  		cmd, err = btcjson.UnmarshalCmd(&request)
   168  		if err != nil {
   169  			t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
   170  				test.name, err)
   171  			continue
   172  		}
   173  
   174  		if !reflect.DeepEqual(cmd, test.unmarshalled) {
   175  			t.Errorf("Test #%d (%s) unexpected unmarshalled command "+
   176  				"- got %s, want %s", i, test.name,
   177  				fmt.Sprintf("(%T) %+[1]v", cmd),
   178  				fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled))
   179  			continue
   180  		}
   181  	}
   182  }