2013-12-13 18:30:23 +01:00
|
|
|
// Copyright (c) 2013 Conformal Systems LLC.
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// this has to be in the real package so we can mock up structs
|
|
|
|
package btcws
|
|
|
|
|
|
|
|
import (
|
2014-07-03 02:44:43 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2013-12-13 18:30:23 +01:00
|
|
|
"github.com/conformal/btcjson"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
2014-11-20 06:33:56 +01:00
|
|
|
var testAccount = "account"
|
|
|
|
|
2013-12-13 18:30:23 +01:00
|
|
|
var cmdtests = []struct {
|
|
|
|
name string
|
|
|
|
f func() (btcjson.Cmd, error)
|
|
|
|
result btcjson.Cmd // after marshal and unmarshal
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "createencryptedwallet",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewCreateEncryptedWalletCmd(
|
|
|
|
float64(1),
|
|
|
|
"banana"), nil
|
|
|
|
},
|
|
|
|
result: &CreateEncryptedWalletCmd{
|
2014-01-24 17:14:44 +01:00
|
|
|
id: float64(1),
|
|
|
|
Passphrase: "banana",
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
},
|
2014-11-10 17:33:58 +01:00
|
|
|
{
|
|
|
|
name: "createnewaccount",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewCreateNewAccountCmd(
|
|
|
|
float64(1),
|
|
|
|
"account"), nil
|
|
|
|
},
|
|
|
|
result: &CreateNewAccountCmd{
|
|
|
|
id: float64(1),
|
|
|
|
Account: "account",
|
|
|
|
},
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
{
|
|
|
|
name: "getbestblock",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewGetBestBlockCmd(float64(1)), nil
|
|
|
|
},
|
|
|
|
result: &GetBestBlockCmd{
|
|
|
|
id: float64(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "getcurrentnet",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewGetCurrentNetCmd(float64(1)), nil
|
|
|
|
},
|
|
|
|
result: &GetCurrentNetCmd{
|
|
|
|
id: float64(1),
|
|
|
|
},
|
|
|
|
},
|
2013-12-30 19:53:43 +01:00
|
|
|
{
|
|
|
|
name: "getunconfirmedbalance no optargs",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewGetUnconfirmedBalanceCmd(float64(1))
|
|
|
|
},
|
|
|
|
result: &GetUnconfirmedBalanceCmd{
|
|
|
|
id: float64(1),
|
|
|
|
Account: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "getunconfirmedbalance one optarg",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewGetUnconfirmedBalanceCmd(float64(1),
|
2014-11-20 06:33:56 +01:00
|
|
|
testAccount)
|
2013-12-30 19:53:43 +01:00
|
|
|
},
|
|
|
|
result: &GetUnconfirmedBalanceCmd{
|
|
|
|
id: float64(1),
|
2014-11-20 06:33:56 +01:00
|
|
|
Account: testAccount,
|
2013-12-30 19:53:43 +01:00
|
|
|
},
|
|
|
|
},
|
2013-12-20 21:00:31 +01:00
|
|
|
{
|
|
|
|
name: "listaddresstransactions no optargs",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
addrs := []string{
|
|
|
|
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
|
|
|
|
}
|
|
|
|
return NewListAddressTransactionsCmd(
|
|
|
|
float64(1),
|
|
|
|
addrs)
|
|
|
|
},
|
|
|
|
result: &ListAddressTransactionsCmd{
|
|
|
|
id: float64(1),
|
|
|
|
Account: "",
|
|
|
|
Addresses: []string{
|
|
|
|
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "listaddresstransactions one optarg",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
addrs := []string{
|
|
|
|
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
|
|
|
|
}
|
|
|
|
return NewListAddressTransactionsCmd(
|
|
|
|
float64(1),
|
|
|
|
addrs,
|
2014-11-20 06:33:56 +01:00
|
|
|
testAccount)
|
2013-12-20 21:00:31 +01:00
|
|
|
},
|
|
|
|
result: &ListAddressTransactionsCmd{
|
|
|
|
id: float64(1),
|
2014-11-20 06:33:56 +01:00
|
|
|
Account: testAccount,
|
2013-12-20 21:00:31 +01:00
|
|
|
Addresses: []string{
|
|
|
|
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
{
|
|
|
|
name: "listalltransactions no optargs",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewListAllTransactionsCmd(float64(1))
|
|
|
|
},
|
|
|
|
result: &ListAllTransactionsCmd{
|
|
|
|
id: float64(1),
|
2014-11-20 06:33:56 +01:00
|
|
|
Account: nil,
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "listalltransactions one optarg",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewListAllTransactionsCmd(
|
|
|
|
float64(1),
|
2014-11-20 06:33:56 +01:00
|
|
|
testAccount)
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
result: &ListAllTransactionsCmd{
|
|
|
|
id: float64(1),
|
2014-11-20 06:33:56 +01:00
|
|
|
Account: &testAccount,
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-04-15 05:45:37 +02:00
|
|
|
name: "notifyreceived",
|
2013-12-13 18:30:23 +01:00
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
addrs := []string{
|
|
|
|
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
|
|
|
|
}
|
2014-04-15 05:45:37 +02:00
|
|
|
return NewNotifyReceivedCmd(
|
2013-12-13 18:30:23 +01:00
|
|
|
float64(1),
|
|
|
|
addrs), nil
|
|
|
|
},
|
2014-04-15 05:45:37 +02:00
|
|
|
result: &NotifyReceivedCmd{
|
2013-12-13 18:30:23 +01:00
|
|
|
id: float64(1),
|
|
|
|
Addresses: []string{
|
|
|
|
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-02-08 23:03:22 +01:00
|
|
|
{
|
2014-04-15 07:02:44 +02:00
|
|
|
name: "notifynewtransactions",
|
2014-02-08 23:03:22 +01:00
|
|
|
f: func() (btcjson.Cmd, error) {
|
2014-04-15 07:02:44 +02:00
|
|
|
return NewNotifyNewTransactionsCmd(
|
2014-02-08 23:03:22 +01:00
|
|
|
float64(1),
|
2014-02-10 15:11:25 +01:00
|
|
|
true)
|
2014-02-08 23:03:22 +01:00
|
|
|
},
|
2014-04-15 07:02:44 +02:00
|
|
|
result: &NotifyNewTransactionsCmd{
|
2014-02-08 23:03:22 +01:00
|
|
|
id: float64(1),
|
|
|
|
Verbose: true,
|
|
|
|
},
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
{
|
|
|
|
name: "notifyspent",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
2014-05-06 17:49:46 +02:00
|
|
|
ops := []OutPoint{
|
|
|
|
{
|
|
|
|
Hash: "000102030405060708091011121314" +
|
|
|
|
"1516171819202122232425262728" +
|
|
|
|
"293031",
|
|
|
|
Index: 1,
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
}
|
2014-05-06 17:49:46 +02:00
|
|
|
return NewNotifySpentCmd(float64(1), ops), nil
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
result: &NotifySpentCmd{
|
|
|
|
id: float64(1),
|
2014-05-06 17:49:46 +02:00
|
|
|
OutPoints: []OutPoint{
|
|
|
|
{
|
|
|
|
Hash: "000102030405060708091011121314" +
|
|
|
|
"1516171819202122232425262728" +
|
|
|
|
"293031",
|
|
|
|
Index: 1,
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-11-10 17:33:58 +01:00
|
|
|
{
|
|
|
|
name: "renameaccount",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewRenameAccountCmd(
|
|
|
|
float64(1),
|
|
|
|
"old",
|
|
|
|
"new"), nil
|
|
|
|
},
|
|
|
|
result: &RenameAccountCmd{
|
|
|
|
id: float64(1),
|
|
|
|
OldAccount: "old",
|
|
|
|
NewAccount: "new",
|
|
|
|
},
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
{
|
|
|
|
name: "rescan no optargs",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
2014-03-21 20:08:54 +01:00
|
|
|
addrs := []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"}
|
2014-04-10 21:33:21 +02:00
|
|
|
ops := []OutPoint{
|
|
|
|
{
|
|
|
|
Hash: "000102030405060708091011121314" +
|
|
|
|
"1516171819202122232425262728" +
|
|
|
|
"293031",
|
2014-03-21 20:08:54 +01:00
|
|
|
Index: 1,
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
}
|
|
|
|
return NewRescanCmd(
|
|
|
|
float64(1),
|
2014-07-17 03:35:11 +02:00
|
|
|
"0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
|
2014-03-21 20:08:54 +01:00
|
|
|
addrs,
|
|
|
|
ops)
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
result: &RescanCmd{
|
|
|
|
id: float64(1),
|
2014-07-17 03:35:11 +02:00
|
|
|
BeginBlock: "0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
|
2014-03-21 20:08:54 +01:00
|
|
|
Addresses: []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"},
|
2014-04-10 21:33:21 +02:00
|
|
|
OutPoints: []OutPoint{
|
|
|
|
{
|
|
|
|
Hash: "000102030405060708091011121314" +
|
|
|
|
"1516171819202122232425262728" +
|
|
|
|
"293031",
|
2014-03-21 20:08:54 +01:00
|
|
|
Index: 1,
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
2014-07-17 03:35:11 +02:00
|
|
|
EndBlock: "",
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rescan one optarg",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
2014-03-21 20:08:54 +01:00
|
|
|
addrs := []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"}
|
2014-04-10 21:33:21 +02:00
|
|
|
ops := []OutPoint{
|
|
|
|
{
|
|
|
|
Hash: "000102030405060708091011121314" +
|
|
|
|
"1516171819202122232425262728" +
|
|
|
|
"293031",
|
2014-03-21 20:08:54 +01:00
|
|
|
Index: 1,
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
}
|
|
|
|
return NewRescanCmd(
|
|
|
|
float64(1),
|
2014-07-17 03:35:11 +02:00
|
|
|
"0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
|
2013-12-13 18:30:23 +01:00
|
|
|
addrs,
|
2014-03-21 20:08:54 +01:00
|
|
|
ops,
|
2014-07-17 03:35:11 +02:00
|
|
|
"0000000000000001c091ada69f444dc0282ecaabe4808ddbb2532e5555db0c03")
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
result: &RescanCmd{
|
|
|
|
id: float64(1),
|
2014-07-17 03:35:11 +02:00
|
|
|
BeginBlock: "0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
|
2014-03-21 20:08:54 +01:00
|
|
|
Addresses: []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"},
|
2014-04-10 21:33:21 +02:00
|
|
|
OutPoints: []OutPoint{
|
|
|
|
{
|
|
|
|
Hash: "000102030405060708091011121314" +
|
|
|
|
"1516171819202122232425262728" +
|
|
|
|
"293031",
|
2014-03-21 20:08:54 +01:00
|
|
|
Index: 1,
|
|
|
|
},
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
2014-07-17 03:35:11 +02:00
|
|
|
EndBlock: "0000000000000001c091ada69f444dc0282ecaabe4808ddbb2532e5555db0c03",
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "walletislocked no optargs",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewWalletIsLockedCmd(float64(1))
|
|
|
|
},
|
|
|
|
result: &WalletIsLockedCmd{
|
|
|
|
id: float64(1),
|
|
|
|
Account: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "walletislocked one optarg",
|
|
|
|
f: func() (btcjson.Cmd, error) {
|
|
|
|
return NewWalletIsLockedCmd(
|
|
|
|
float64(1),
|
2014-11-20 06:33:56 +01:00
|
|
|
testAccount)
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
result: &WalletIsLockedCmd{
|
|
|
|
id: float64(1),
|
2014-11-20 06:33:56 +01:00
|
|
|
Account: testAccount,
|
2013-12-13 18:30:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCmds(t *testing.T) {
|
|
|
|
for _, test := range cmdtests {
|
|
|
|
c, err := test.f()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: failed to run func: %v",
|
|
|
|
test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mc, err := c.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: failed to marshal cmd: %v",
|
|
|
|
test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c2, err := btcjson.ParseMarshaledCmd(mc)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: failed to ummarshal cmd: %v",
|
|
|
|
test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(test.result, c2) {
|
|
|
|
t.Errorf("%s: unmarshal not as expected. "+
|
|
|
|
"got %v wanted %v", test.name, spew.Sdump(c2),
|
|
|
|
spew.Sdump(test.result))
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(c, c2) {
|
|
|
|
t.Errorf("%s: unmarshal not as we started with. "+
|
|
|
|
"got %v wanted %v", test.name, spew.Sdump(c2),
|
|
|
|
spew.Sdump(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
// id from Id func must match result.
|
|
|
|
if c.Id() != test.result.Id() {
|
|
|
|
t.Errorf("%s: Id returned incorrect id. "+
|
|
|
|
"got %v wanted %v", test.name, c.Id(),
|
|
|
|
test.result.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
// method from Method func must match result.
|
|
|
|
if c.Method() != test.result.Method() {
|
|
|
|
t.Errorf("%s: Method returned incorrect method. "+
|
|
|
|
"got %v wanted %v", test.name, c.Method(),
|
|
|
|
test.result.Method())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read marshaled command back into c. Should still
|
|
|
|
// match result.
|
2014-04-11 16:04:53 +02:00
|
|
|
if err := c.UnmarshalJSON(mc); err != nil {
|
|
|
|
t.Errorf("%s: error while unmarshalling: %v", test.name,
|
|
|
|
err)
|
|
|
|
}
|
2013-12-13 18:30:23 +01:00
|
|
|
if !reflect.DeepEqual(test.result, c) {
|
|
|
|
t.Errorf("%s: unmarshal not as expected. "+
|
|
|
|
"got %v wanted %v", test.name, spew.Sdump(c),
|
|
|
|
spew.Sdump(test.result))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|