github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/btcjson/btcdextcmds_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  // TestBtcdExtCmds tests all of the btcd extended commands marshal and unmarshal
    19  // into valid results include handling of optional fields being omitted in the
    20  // marshalled command, while optional fields with defaults have the default
    21  // assigned on unmarshalled commands.
    22  func TestBtcdExtCmds(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: "debuglevel",
    35  			newCmd: func() (interface{}, error) {
    36  				return btcjson.NewCmd("debuglevel", "trace")
    37  			},
    38  			staticCmd: func() interface{} {
    39  				return btcjson.NewDebugLevelCmd("trace")
    40  			},
    41  			marshalled: `{"jsonrpc":"1.0","method":"debuglevel","params":["trace"],"id":1}`,
    42  			unmarshalled: &btcjson.DebugLevelCmd{
    43  				LevelSpec: "trace",
    44  			},
    45  		},
    46  		{
    47  			name: "node",
    48  			newCmd: func() (interface{}, error) {
    49  				return btcjson.NewCmd("node", btcjson.NRemove, "1.1.1.1")
    50  			},
    51  			staticCmd: func() interface{} {
    52  				return btcjson.NewNodeCmd("remove", "1.1.1.1", nil)
    53  			},
    54  			marshalled: `{"jsonrpc":"1.0","method":"node","params":["remove","1.1.1.1"],"id":1}`,
    55  			unmarshalled: &btcjson.NodeCmd{
    56  				SubCmd: btcjson.NRemove,
    57  				Target: "1.1.1.1",
    58  			},
    59  		},
    60  		{
    61  			name: "node",
    62  			newCmd: func() (interface{}, error) {
    63  				return btcjson.NewCmd("node", btcjson.NDisconnect, "1.1.1.1")
    64  			},
    65  			staticCmd: func() interface{} {
    66  				return btcjson.NewNodeCmd("disconnect", "1.1.1.1", nil)
    67  			},
    68  			marshalled: `{"jsonrpc":"1.0","method":"node","params":["disconnect","1.1.1.1"],"id":1}`,
    69  			unmarshalled: &btcjson.NodeCmd{
    70  				SubCmd: btcjson.NDisconnect,
    71  				Target: "1.1.1.1",
    72  			},
    73  		},
    74  		{
    75  			name: "node",
    76  			newCmd: func() (interface{}, error) {
    77  				return btcjson.NewCmd("node", btcjson.NConnect, "1.1.1.1", "perm")
    78  			},
    79  			staticCmd: func() interface{} {
    80  				return btcjson.NewNodeCmd("connect", "1.1.1.1", btcjson.String("perm"))
    81  			},
    82  			marshalled: `{"jsonrpc":"1.0","method":"node","params":["connect","1.1.1.1","perm"],"id":1}`,
    83  			unmarshalled: &btcjson.NodeCmd{
    84  				SubCmd:        btcjson.NConnect,
    85  				Target:        "1.1.1.1",
    86  				ConnectSubCmd: btcjson.String("perm"),
    87  			},
    88  		},
    89  		{
    90  			name: "node",
    91  			newCmd: func() (interface{}, error) {
    92  				return btcjson.NewCmd("node", btcjson.NConnect, "1.1.1.1", "temp")
    93  			},
    94  			staticCmd: func() interface{} {
    95  				return btcjson.NewNodeCmd("connect", "1.1.1.1", btcjson.String("temp"))
    96  			},
    97  			marshalled: `{"jsonrpc":"1.0","method":"node","params":["connect","1.1.1.1","temp"],"id":1}`,
    98  			unmarshalled: &btcjson.NodeCmd{
    99  				SubCmd:        btcjson.NConnect,
   100  				Target:        "1.1.1.1",
   101  				ConnectSubCmd: btcjson.String("temp"),
   102  			},
   103  		},
   104  		{
   105  			name: "generate",
   106  			newCmd: func() (interface{}, error) {
   107  				return btcjson.NewCmd("generate", 1)
   108  			},
   109  			staticCmd: func() interface{} {
   110  				return btcjson.NewGenerateCmd(1)
   111  			},
   112  			marshalled: `{"jsonrpc":"1.0","method":"generate","params":[1],"id":1}`,
   113  			unmarshalled: &btcjson.GenerateCmd{
   114  				NumBlocks: 1,
   115  			},
   116  		},
   117  		{
   118  			name: "getbestblock",
   119  			newCmd: func() (interface{}, error) {
   120  				return btcjson.NewCmd("getbestblock")
   121  			},
   122  			staticCmd: func() interface{} {
   123  				return btcjson.NewGetBestBlockCmd()
   124  			},
   125  			marshalled:   `{"jsonrpc":"1.0","method":"getbestblock","params":[],"id":1}`,
   126  			unmarshalled: &btcjson.GetBestBlockCmd{},
   127  		},
   128  		{
   129  			name: "getcurrentnet",
   130  			newCmd: func() (interface{}, error) {
   131  				return btcjson.NewCmd("getcurrentnet")
   132  			},
   133  			staticCmd: func() interface{} {
   134  				return btcjson.NewGetCurrentNetCmd()
   135  			},
   136  			marshalled:   `{"jsonrpc":"1.0","method":"getcurrentnet","params":[],"id":1}`,
   137  			unmarshalled: &btcjson.GetCurrentNetCmd{},
   138  		},
   139  	}
   140  
   141  	t.Logf("Running %d tests", len(tests))
   142  	for i, test := range tests {
   143  		// Marshal the command as created by the new static command
   144  		// creation function.
   145  		marshalled, err := btcjson.MarshalCmd(testID, test.staticCmd())
   146  		if err != nil {
   147  			t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
   148  				test.name, err)
   149  			continue
   150  		}
   151  
   152  		if !bytes.Equal(marshalled, []byte(test.marshalled)) {
   153  			t.Errorf("Test #%d (%s) unexpected marshalled data - "+
   154  				"got %s, want %s", i, test.name, marshalled,
   155  				test.marshalled)
   156  			continue
   157  		}
   158  
   159  		// Ensure the command is created without error via the generic
   160  		// new command creation function.
   161  		cmd, err := test.newCmd()
   162  		if err != nil {
   163  			t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
   164  				i, test.name, err)
   165  		}
   166  
   167  		// Marshal the command as created by the generic new command
   168  		// creation function.
   169  		marshalled, err = btcjson.MarshalCmd(testID, cmd)
   170  		if err != nil {
   171  			t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
   172  				test.name, err)
   173  			continue
   174  		}
   175  
   176  		if !bytes.Equal(marshalled, []byte(test.marshalled)) {
   177  			t.Errorf("Test #%d (%s) unexpected marshalled data - "+
   178  				"got %s, want %s", i, test.name, marshalled,
   179  				test.marshalled)
   180  			continue
   181  		}
   182  
   183  		var request btcjson.Request
   184  		if err := json.Unmarshal(marshalled, &request); err != nil {
   185  			t.Errorf("Test #%d (%s) unexpected error while "+
   186  				"unmarshalling JSON-RPC request: %v", i,
   187  				test.name, err)
   188  			continue
   189  		}
   190  
   191  		cmd, err = btcjson.UnmarshalCmd(&request)
   192  		if err != nil {
   193  			t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
   194  				test.name, err)
   195  			continue
   196  		}
   197  
   198  		if !reflect.DeepEqual(cmd, test.unmarshalled) {
   199  			t.Errorf("Test #%d (%s) unexpected unmarshalled command "+
   200  				"- got %s, want %s", i, test.name,
   201  				fmt.Sprintf("(%T) %+[1]v", cmd),
   202  				fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled))
   203  			continue
   204  		}
   205  	}
   206  }