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.
14 "github.com/btcsuite/btcd/btcjson"
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) {
26 newNtfn func() (interface{}, error)
27 staticNtfn func() interface{}
29 unmarshalled interface{}
32 name: "accountbalance",
33 newNtfn: func() (interface{}, error) {
34 return btcjson.NewCmd("accountbalance", "acct", 1.25, true)
36 staticNtfn: func() interface{} {
37 return btcjson.NewAccountBalanceNtfn("acct", 1.25, true)
39 marshalled: `{"jsonrpc":"1.0","method":"accountbalance","params":["acct",1.25,true],"id":null}`,
40 unmarshalled: &btcjson.AccountBalanceNtfn{
47 name: "btcdconnected",
48 newNtfn: func() (interface{}, error) {
49 return btcjson.NewCmd("btcdconnected", true)
51 staticNtfn: func() interface{} {
52 return btcjson.NewBtcdConnectedNtfn(true)
54 marshalled: `{"jsonrpc":"1.0","method":"btcdconnected","params":[true],"id":null}`,
55 unmarshalled: &btcjson.BtcdConnectedNtfn{
60 name: "walletlockstate",
61 newNtfn: func() (interface{}, error) {
62 return btcjson.NewCmd("walletlockstate", true)
64 staticNtfn: func() interface{} {
65 return btcjson.NewWalletLockStateNtfn(true)
67 marshalled: `{"jsonrpc":"1.0","method":"walletlockstate","params":[true],"id":null}`,
68 unmarshalled: &btcjson.WalletLockStateNtfn{
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"}`)
77 staticNtfn: func() interface{} {
78 result := btcjson.ListTransactionsResult{
82 BIP125Replaceable: "unknown",
85 Fee: btcjson.Float64(0.0001),
88 WalletConflicts: []string{},
90 TimeReceived: 12345876,
93 OtherAccount: "otheracct",
95 return btcjson.NewNewTxNtfn("acct", result)
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{
100 Details: btcjson.ListTransactionsResult{
104 BIP125Replaceable: "unknown",
107 Fee: btcjson.Float64(0.0001),
110 WalletConflicts: []string{},
112 TimeReceived: 12345876,
115 OtherAccount: "otheracct",
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(nil, test.staticNtfn())
127 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
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,
139 // Ensure the notification is created without error via the
140 // generic new notification creation function.
141 cmd, err := test.newNtfn()
143 t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
147 // Marshal the notification as created by the generic new
148 // notification creation function. The ID is nil for
150 marshalled, err = btcjson.MarshalCmd(nil, cmd)
152 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
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,
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,
172 cmd, err = btcjson.UnmarshalCmd(&request)
174 t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
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))