github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/btcjson/btcwalletextcmds_test.go (about) 1 // Copyright (c) 2014 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package btcjson_test 7 8 import ( 9 "bytes" 10 "encoding/json" 11 "fmt" 12 "reflect" 13 "testing" 14 15 "github.com/BlockABC/godash/btcjson" 16 ) 17 18 // TestBtcWalletExtCmds tests all of the btcwallet extended commands marshal and 19 // unmarshal into valid results include handling of optional fields being 20 // omitted in the marshalled command, while optional fields with defaults have 21 // the default assigned on unmarshalled commands. 22 func TestBtcWalletExtCmds(t *testing.T) { 23 t.Parallel() 24 25 testID := int(1) 26 tests := []struct { 27 name string 28 newCmd func() (interface{}, error) 29 staticCmd func() interface{} 30 marshalled string 31 unmarshalled interface{} 32 }{ 33 { 34 name: "createnewaccount", 35 newCmd: func() (interface{}, error) { 36 return btcjson.NewCmd("createnewaccount", "acct") 37 }, 38 staticCmd: func() interface{} { 39 return btcjson.NewCreateNewAccountCmd("acct") 40 }, 41 marshalled: `{"jsonrpc":"1.0","method":"createnewaccount","params":["acct"],"id":1}`, 42 unmarshalled: &btcjson.CreateNewAccountCmd{ 43 Account: "acct", 44 }, 45 }, 46 { 47 name: "dumpwallet", 48 newCmd: func() (interface{}, error) { 49 return btcjson.NewCmd("dumpwallet", "filename") 50 }, 51 staticCmd: func() interface{} { 52 return btcjson.NewDumpWalletCmd("filename") 53 }, 54 marshalled: `{"jsonrpc":"1.0","method":"dumpwallet","params":["filename"],"id":1}`, 55 unmarshalled: &btcjson.DumpWalletCmd{ 56 Filename: "filename", 57 }, 58 }, 59 { 60 name: "importaddress", 61 newCmd: func() (interface{}, error) { 62 return btcjson.NewCmd("importaddress", "1Address") 63 }, 64 staticCmd: func() interface{} { 65 return btcjson.NewImportAddressCmd("1Address", nil) 66 }, 67 marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address"],"id":1}`, 68 unmarshalled: &btcjson.ImportAddressCmd{ 69 Address: "1Address", 70 Rescan: btcjson.Bool(true), 71 }, 72 }, 73 { 74 name: "importaddress optional", 75 newCmd: func() (interface{}, error) { 76 return btcjson.NewCmd("importaddress", "1Address", false) 77 }, 78 staticCmd: func() interface{} { 79 return btcjson.NewImportAddressCmd("1Address", btcjson.Bool(false)) 80 }, 81 marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address",false],"id":1}`, 82 unmarshalled: &btcjson.ImportAddressCmd{ 83 Address: "1Address", 84 Rescan: btcjson.Bool(false), 85 }, 86 }, 87 { 88 name: "importpubkey", 89 newCmd: func() (interface{}, error) { 90 return btcjson.NewCmd("importpubkey", "031234") 91 }, 92 staticCmd: func() interface{} { 93 return btcjson.NewImportPubKeyCmd("031234", nil) 94 }, 95 marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234"],"id":1}`, 96 unmarshalled: &btcjson.ImportPubKeyCmd{ 97 PubKey: "031234", 98 Rescan: btcjson.Bool(true), 99 }, 100 }, 101 { 102 name: "importpubkey optional", 103 newCmd: func() (interface{}, error) { 104 return btcjson.NewCmd("importpubkey", "031234", false) 105 }, 106 staticCmd: func() interface{} { 107 return btcjson.NewImportPubKeyCmd("031234", btcjson.Bool(false)) 108 }, 109 marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234",false],"id":1}`, 110 unmarshalled: &btcjson.ImportPubKeyCmd{ 111 PubKey: "031234", 112 Rescan: btcjson.Bool(false), 113 }, 114 }, 115 { 116 name: "importwallet", 117 newCmd: func() (interface{}, error) { 118 return btcjson.NewCmd("importwallet", "filename") 119 }, 120 staticCmd: func() interface{} { 121 return btcjson.NewImportWalletCmd("filename") 122 }, 123 marshalled: `{"jsonrpc":"1.0","method":"importwallet","params":["filename"],"id":1}`, 124 unmarshalled: &btcjson.ImportWalletCmd{ 125 Filename: "filename", 126 }, 127 }, 128 { 129 name: "renameaccount", 130 newCmd: func() (interface{}, error) { 131 return btcjson.NewCmd("renameaccount", "oldacct", "newacct") 132 }, 133 staticCmd: func() interface{} { 134 return btcjson.NewRenameAccountCmd("oldacct", "newacct") 135 }, 136 marshalled: `{"jsonrpc":"1.0","method":"renameaccount","params":["oldacct","newacct"],"id":1}`, 137 unmarshalled: &btcjson.RenameAccountCmd{ 138 OldAccount: "oldacct", 139 NewAccount: "newacct", 140 }, 141 }, 142 } 143 144 t.Logf("Running %d tests", len(tests)) 145 for i, test := range tests { 146 // Marshal the command as created by the new static command 147 // creation function. 148 marshalled, err := btcjson.MarshalCmd(testID, test.staticCmd()) 149 if err != nil { 150 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i, 151 test.name, err) 152 continue 153 } 154 155 if !bytes.Equal(marshalled, []byte(test.marshalled)) { 156 t.Errorf("Test #%d (%s) unexpected marshalled data - "+ 157 "got %s, want %s", i, test.name, marshalled, 158 test.marshalled) 159 continue 160 } 161 162 // Ensure the command is created without error via the generic 163 // new command creation function. 164 cmd, err := test.newCmd() 165 if err != nil { 166 t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ", 167 i, test.name, err) 168 } 169 170 // Marshal the command as created by the generic new command 171 // creation function. 172 marshalled, err = btcjson.MarshalCmd(testID, cmd) 173 if err != nil { 174 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i, 175 test.name, err) 176 continue 177 } 178 179 if !bytes.Equal(marshalled, []byte(test.marshalled)) { 180 t.Errorf("Test #%d (%s) unexpected marshalled data - "+ 181 "got %s, want %s", i, test.name, marshalled, 182 test.marshalled) 183 continue 184 } 185 186 var request btcjson.Request 187 if err := json.Unmarshal(marshalled, &request); err != nil { 188 t.Errorf("Test #%d (%s) unexpected error while "+ 189 "unmarshalling JSON-RPC request: %v", i, 190 test.name, err) 191 continue 192 } 193 194 cmd, err = btcjson.UnmarshalCmd(&request) 195 if err != nil { 196 t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i, 197 test.name, err) 198 continue 199 } 200 201 if !reflect.DeepEqual(cmd, test.unmarshalled) { 202 t.Errorf("Test #%d (%s) unexpected unmarshalled command "+ 203 "- got %s, want %s", i, test.name, 204 fmt.Sprintf("(%T) %+[1]v", cmd), 205 fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled)) 206 continue 207 } 208 } 209 }