forked from btcsuite/btcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalletsvrwsntfns_test.go
187 lines (174 loc) · 5.87 KB
/
walletsvrwsntfns_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2014 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcjson_test
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/btcsuite/btcd/btcjson"
)
// TestWalletSvrWsNtfns tests all of the chain server websocket-specific
// notifications marshal and unmarshal into valid results include handling of
// optional fields being omitted in the marshalled command, while optional
// fields with defaults have the default assigned on unmarshalled commands.
func TestWalletSvrWsNtfns(t *testing.T) {
t.Parallel()
tests := []struct {
name string
newNtfn func() (interface{}, error)
staticNtfn func() interface{}
marshalled string
unmarshalled interface{}
}{
{
name: "accountbalance",
newNtfn: func() (interface{}, error) {
return btcjson.NewCmd("accountbalance", "acct", 1.25, true)
},
staticNtfn: func() interface{} {
return btcjson.NewAccountBalanceNtfn("acct", 1.25, true)
},
marshalled: `{"jsonrpc":"1.0","method":"accountbalance","params":["acct",1.25,true],"id":null}`,
unmarshalled: &btcjson.AccountBalanceNtfn{
Account: "acct",
Balance: 1.25,
Confirmed: true,
},
},
{
name: "btcdconnected",
newNtfn: func() (interface{}, error) {
return btcjson.NewCmd("btcdconnected", true)
},
staticNtfn: func() interface{} {
return btcjson.NewBtcdConnectedNtfn(true)
},
marshalled: `{"jsonrpc":"1.0","method":"btcdconnected","params":[true],"id":null}`,
unmarshalled: &btcjson.BtcdConnectedNtfn{
Connected: true,
},
},
{
name: "walletlockstate",
newNtfn: func() (interface{}, error) {
return btcjson.NewCmd("walletlockstate", true)
},
staticNtfn: func() interface{} {
return btcjson.NewWalletLockStateNtfn(true)
},
marshalled: `{"jsonrpc":"1.0","method":"walletlockstate","params":[true],"id":null}`,
unmarshalled: &btcjson.WalletLockStateNtfn{
Locked: true,
},
},
{
name: "newtx",
newNtfn: func() (interface{}, error) {
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"}`)
},
staticNtfn: func() interface{} {
result := btcjson.ListTransactionsResult{
Abandoned: false,
Account: "acct",
Address: "1Address",
BIP125Replaceable: "unknown",
Category: "send",
Amount: 1.5,
Fee: btcjson.Float64(0.0001),
Confirmations: 1,
TxID: "456",
WalletConflicts: []string{},
Time: 12345678,
TimeReceived: 12345876,
Trusted: true,
Vout: 789,
OtherAccount: "otheracct",
}
return btcjson.NewNewTxNtfn("acct", result)
},
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}`,
unmarshalled: &btcjson.NewTxNtfn{
Account: "acct",
Details: btcjson.ListTransactionsResult{
Abandoned: false,
Account: "acct",
Address: "1Address",
BIP125Replaceable: "unknown",
Category: "send",
Amount: 1.5,
Fee: btcjson.Float64(0.0001),
Confirmations: 1,
TxID: "456",
WalletConflicts: []string{},
Time: 12345678,
TimeReceived: 12345876,
Trusted: true,
Vout: 789,
OtherAccount: "otheracct",
},
},
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Marshal the notification as created by the new static
// creation function. The ID is nil for notifications.
marshalled, err := btcjson.MarshalCmd(nil, test.staticNtfn())
if err != nil {
t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
if !bytes.Equal(marshalled, []byte(test.marshalled)) {
t.Errorf("Test #%d (%s) unexpected marshalled data - "+
"got %s, want %s", i, test.name, marshalled,
test.marshalled)
continue
}
// Ensure the notification is created without error via the
// generic new notification creation function.
cmd, err := test.newNtfn()
if err != nil {
t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
i, test.name, err)
}
// Marshal the notification as created by the generic new
// notification creation function. The ID is nil for
// notifications.
marshalled, err = btcjson.MarshalCmd(nil, cmd)
if err != nil {
t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
if !bytes.Equal(marshalled, []byte(test.marshalled)) {
t.Errorf("Test #%d (%s) unexpected marshalled data - "+
"got %s, want %s", i, test.name, marshalled,
test.marshalled)
continue
}
var request btcjson.Request
if err := json.Unmarshal(marshalled, &request); err != nil {
t.Errorf("Test #%d (%s) unexpected error while "+
"unmarshalling JSON-RPC request: %v", i,
test.name, err)
continue
}
cmd, err = btcjson.UnmarshalCmd(&request)
if err != nil {
t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
if !reflect.DeepEqual(cmd, test.unmarshalled) {
t.Errorf("Test #%d (%s) unexpected unmarshalled command "+
"- got %s, want %s", i, test.name,
fmt.Sprintf("(%T) %+[1]v", cmd),
fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled))
continue
}
}
}