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 // TestBtcWalletExtCmds tests all of the btcwallet extended commands marshal and
18 // unmarshal into valid results include handling of optional fields being
19 // omitted in the marshalled command, while optional fields with defaults have
20 // the default assigned on unmarshalled commands.
21 func TestBtcWalletExtCmds(t *testing.T) {
27 newCmd func() (interface{}, error)
28 staticCmd func() interface{}
30 unmarshalled interface{}
33 name: "createnewaccount",
34 newCmd: func() (interface{}, error) {
35 return btcjson.NewCmd("createnewaccount", "acct")
37 staticCmd: func() interface{} {
38 return btcjson.NewCreateNewAccountCmd("acct")
40 marshalled: `{"jsonrpc":"1.0","method":"createnewaccount","params":["acct"],"id":1}`,
41 unmarshalled: &btcjson.CreateNewAccountCmd{
47 newCmd: func() (interface{}, error) {
48 return btcjson.NewCmd("dumpwallet", "filename")
50 staticCmd: func() interface{} {
51 return btcjson.NewDumpWalletCmd("filename")
53 marshalled: `{"jsonrpc":"1.0","method":"dumpwallet","params":["filename"],"id":1}`,
54 unmarshalled: &btcjson.DumpWalletCmd{
59 name: "importaddress",
60 newCmd: func() (interface{}, error) {
61 return btcjson.NewCmd("importaddress", "1Address")
63 staticCmd: func() interface{} {
64 return btcjson.NewImportAddressCmd("1Address", nil)
66 marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address"],"id":1}`,
67 unmarshalled: &btcjson.ImportAddressCmd{
69 Rescan: btcjson.Bool(true),
73 name: "importaddress optional",
74 newCmd: func() (interface{}, error) {
75 return btcjson.NewCmd("importaddress", "1Address", false)
77 staticCmd: func() interface{} {
78 return btcjson.NewImportAddressCmd("1Address", btcjson.Bool(false))
80 marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address",false],"id":1}`,
81 unmarshalled: &btcjson.ImportAddressCmd{
83 Rescan: btcjson.Bool(false),
88 newCmd: func() (interface{}, error) {
89 return btcjson.NewCmd("importpubkey", "031234")
91 staticCmd: func() interface{} {
92 return btcjson.NewImportPubKeyCmd("031234", nil)
94 marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234"],"id":1}`,
95 unmarshalled: &btcjson.ImportPubKeyCmd{
97 Rescan: btcjson.Bool(true),
101 name: "importpubkey optional",
102 newCmd: func() (interface{}, error) {
103 return btcjson.NewCmd("importpubkey", "031234", false)
105 staticCmd: func() interface{} {
106 return btcjson.NewImportPubKeyCmd("031234", btcjson.Bool(false))
108 marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234",false],"id":1}`,
109 unmarshalled: &btcjson.ImportPubKeyCmd{
111 Rescan: btcjson.Bool(false),
115 name: "importwallet",
116 newCmd: func() (interface{}, error) {
117 return btcjson.NewCmd("importwallet", "filename")
119 staticCmd: func() interface{} {
120 return btcjson.NewImportWalletCmd("filename")
122 marshalled: `{"jsonrpc":"1.0","method":"importwallet","params":["filename"],"id":1}`,
123 unmarshalled: &btcjson.ImportWalletCmd{
124 Filename: "filename",
128 name: "renameaccount",
129 newCmd: func() (interface{}, error) {
130 return btcjson.NewCmd("renameaccount", "oldacct", "newacct")
132 staticCmd: func() interface{} {
133 return btcjson.NewRenameAccountCmd("oldacct", "newacct")
135 marshalled: `{"jsonrpc":"1.0","method":"renameaccount","params":["oldacct","newacct"],"id":1}`,
136 unmarshalled: &btcjson.RenameAccountCmd{
137 OldAccount: "oldacct",
138 NewAccount: "newacct",
143 t.Logf("Running %d tests", len(tests))
144 for i, test := range tests {
145 // Marshal the command as created by the new static command
146 // creation function.
147 marshalled, err := btcjson.MarshalCmd(testID, test.staticCmd())
149 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
154 if !bytes.Equal(marshalled, []byte(test.marshalled)) {
155 t.Errorf("Test #%d (%s) unexpected marshalled data - "+
156 "got %s, want %s", i, test.name, marshalled,
161 // Ensure the command is created without error via the generic
162 // new command creation function.
163 cmd, err := test.newCmd()
165 t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
169 // Marshal the command as created by the generic new command
170 // creation function.
171 marshalled, err = btcjson.MarshalCmd(testID, cmd)
173 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
178 if !bytes.Equal(marshalled, []byte(test.marshalled)) {
179 t.Errorf("Test #%d (%s) unexpected marshalled data - "+
180 "got %s, want %s", i, test.name, marshalled,
185 var request btcjson.Request
186 if err := json.Unmarshal(marshalled, &request); err != nil {
187 t.Errorf("Test #%d (%s) unexpected error while "+
188 "unmarshalling JSON-RPC request: %v", i,
193 cmd, err = btcjson.UnmarshalCmd(&request)
195 t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
200 if !reflect.DeepEqual(cmd, test.unmarshalled) {
201 t.Errorf("Test #%d (%s) unexpected unmarshalled command "+
202 "- got %s, want %s", i, test.name,
203 fmt.Sprintf("(%T) %+[1]v", cmd),
204 fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled))