github.com/palcoin-project/palcd@v1.0.0/btcjson/btcwalletextcmds_test.go (about)

     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.
     4  
     5  package btcjson_test
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/json"
    10  	"fmt"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/palcoin-project/palcd/btcjson"
    15  )
    16  
    17  // TestBtcWalletExtCmds tests all of the palcwallet 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) {
    22  	t.Parallel()
    23  
    24  	testID := int(1)
    25  	tests := []struct {
    26  		name         string
    27  		newCmd       func() (interface{}, error)
    28  		staticCmd    func() interface{}
    29  		marshalled   string
    30  		unmarshalled interface{}
    31  	}{
    32  		{
    33  			name: "createnewaccount",
    34  			newCmd: func() (interface{}, error) {
    35  				return btcjson.NewCmd("createnewaccount", "acct")
    36  			},
    37  			staticCmd: func() interface{} {
    38  				return btcjson.NewCreateNewAccountCmd("acct")
    39  			},
    40  			marshalled: `{"jsonrpc":"1.0","method":"createnewaccount","params":["acct"],"id":1}`,
    41  			unmarshalled: &btcjson.CreateNewAccountCmd{
    42  				Account: "acct",
    43  			},
    44  		},
    45  		{
    46  			name: "dumpwallet",
    47  			newCmd: func() (interface{}, error) {
    48  				return btcjson.NewCmd("dumpwallet", "filename")
    49  			},
    50  			staticCmd: func() interface{} {
    51  				return btcjson.NewDumpWalletCmd("filename")
    52  			},
    53  			marshalled: `{"jsonrpc":"1.0","method":"dumpwallet","params":["filename"],"id":1}`,
    54  			unmarshalled: &btcjson.DumpWalletCmd{
    55  				Filename: "filename",
    56  			},
    57  		},
    58  		{
    59  			name: "importaddress",
    60  			newCmd: func() (interface{}, error) {
    61  				return btcjson.NewCmd("importaddress", "1Address", "")
    62  			},
    63  			staticCmd: func() interface{} {
    64  				return btcjson.NewImportAddressCmd("1Address", "", nil)
    65  			},
    66  			marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address",""],"id":1}`,
    67  			unmarshalled: &btcjson.ImportAddressCmd{
    68  				Address: "1Address",
    69  				Rescan:  btcjson.Bool(true),
    70  			},
    71  		},
    72  		{
    73  			name: "importaddress optional",
    74  			newCmd: func() (interface{}, error) {
    75  				return btcjson.NewCmd("importaddress", "1Address", "acct", false)
    76  			},
    77  			staticCmd: func() interface{} {
    78  				return btcjson.NewImportAddressCmd("1Address", "acct", btcjson.Bool(false))
    79  			},
    80  			marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address","acct",false],"id":1}`,
    81  			unmarshalled: &btcjson.ImportAddressCmd{
    82  				Address: "1Address",
    83  				Account: "acct",
    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(btcjson.RpcVersion1, 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(btcjson.RpcVersion1, 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  }