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