github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/cmd/utils/flags_test.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package utils contains internal helper functions for go-ethereum commands.
    18  package utils
    19  
    20  import (
    21  	"flag"
    22  	"io/ioutil"
    23  	"os"
    24  	"path"
    25  	"reflect"
    26  	"strconv"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/kisexp/xdchain/common"
    31  	"github.com/kisexp/xdchain/eth"
    32  	"github.com/kisexp/xdchain/node"
    33  	"github.com/stretchr/testify/assert"
    34  	"github.com/stretchr/testify/require"
    35  	"gopkg.in/urfave/cli.v1"
    36  )
    37  
    38  func TestAuthorizationList(t *testing.T) {
    39  	value := "1=" + common.HexToHash("0xfa").Hex() + ",2=" + common.HexToHash("0x12").Hex()
    40  	result := map[uint64]common.Hash{
    41  		1: common.HexToHash("0xfa"),
    42  		2: common.HexToHash("0x12"),
    43  	}
    44  
    45  	arbitraryNodeConfig := &eth.Config{}
    46  	fs := &flag.FlagSet{}
    47  	fs.String(AuthorizationListFlag.Name, value, "")
    48  	arbitraryCLIContext := cli.NewContext(nil, fs, nil)
    49  	arbitraryCLIContext.GlobalSet(AuthorizationListFlag.Name, value)
    50  	setAuthorizationList(arbitraryCLIContext, arbitraryNodeConfig)
    51  	assert.Equal(t, result, arbitraryNodeConfig.AuthorizationList)
    52  
    53  	fs = &flag.FlagSet{}
    54  	fs.String(AuthorizationListFlag.Name, value, "")
    55  	arbitraryCLIContext = cli.NewContext(nil, fs, nil)
    56  	arbitraryCLIContext.GlobalSet(DeprecatedAuthorizationListFlag.Name, value) // old wlist flag
    57  	setAuthorizationList(arbitraryCLIContext, arbitraryNodeConfig)
    58  	assert.Equal(t, result, arbitraryNodeConfig.AuthorizationList)
    59  }
    60  
    61  func TestPrivateTrieCache(t *testing.T) {
    62  	arbitraryNodeConfig := &eth.Config{}
    63  	fs := &flag.FlagSet{}
    64  	fs.String(PrivateCacheTrieJournalFlag.Name, "test", "")
    65  	fs.String(CacheTrieJournalFlag.Name, "test", "")
    66  	arbitraryCLIContext := cli.NewContext(nil, fs, nil)
    67  	arbitraryCLIContext.GlobalSet(PrivateCacheTrieJournalFlag.Name, "test")
    68  	arbitraryCLIContext.GlobalSet(CacheTrieJournalFlag.Name, "test")
    69  	assert.Error(t, setQuorumConfig(arbitraryCLIContext, arbitraryNodeConfig), arbitraryCLIContext)
    70  
    71  	fs = &flag.FlagSet{}
    72  	fs.String(PrivateCacheTrieJournalFlag.Name, "test1", "")
    73  	fs.String(CacheTrieJournalFlag.Name, "test2", "")
    74  	arbitraryCLIContext = cli.NewContext(nil, fs, nil)
    75  	arbitraryCLIContext.GlobalSet(PrivateCacheTrieJournalFlag.Name, "test1")
    76  	arbitraryCLIContext.GlobalSet(CacheTrieJournalFlag.Name, "test2")
    77  	assert.NoError(t, setQuorumConfig(arbitraryCLIContext, arbitraryNodeConfig))
    78  }
    79  
    80  func TestSetPlugins_whenPluginsNotEnabled(t *testing.T) {
    81  	arbitraryNodeConfig := &node.Config{}
    82  	arbitraryCLIContext := cli.NewContext(nil, &flag.FlagSet{}, nil)
    83  
    84  	assert.NoError(t, SetPlugins(arbitraryCLIContext, arbitraryNodeConfig))
    85  
    86  	assert.Nil(t, arbitraryNodeConfig.Plugins)
    87  }
    88  
    89  func TestSetPlugins_whenInvalidFlagsCombination(t *testing.T) {
    90  	arbitraryNodeConfig := &node.Config{}
    91  	fs := &flag.FlagSet{}
    92  	fs.String(PluginSettingsFlag.Name, "", "")
    93  	fs.Bool(PluginSkipVerifyFlag.Name, true, "")
    94  	fs.Bool(PluginLocalVerifyFlag.Name, true, "")
    95  	fs.String(PluginPublicKeyFlag.Name, "", "")
    96  	arbitraryCLIContext := cli.NewContext(nil, fs, nil)
    97  	assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginSettingsFlag.Name, "arbitrary value"))
    98  
    99  	verifyErrorMessage(t, arbitraryCLIContext, arbitraryNodeConfig, "only --plugins.skipverify or --plugins.localverify must be set")
   100  
   101  	assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginSkipVerifyFlag.Name, "false"))
   102  	assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginLocalVerifyFlag.Name, "false"))
   103  	assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginPublicKeyFlag.Name, "arbitrary value"))
   104  
   105  	verifyErrorMessage(t, arbitraryCLIContext, arbitraryNodeConfig, "--plugins.localverify is required for setting --plugins.publickey")
   106  }
   107  
   108  func TestSetPlugins_whenInvalidPluginSettingsURL(t *testing.T) {
   109  	arbitraryNodeConfig := &node.Config{}
   110  	fs := &flag.FlagSet{}
   111  	fs.String(PluginSettingsFlag.Name, "", "")
   112  	arbitraryCLIContext := cli.NewContext(nil, fs, nil)
   113  	assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginSettingsFlag.Name, "arbitrary value"))
   114  
   115  	verifyErrorMessage(t, arbitraryCLIContext, arbitraryNodeConfig, "plugins: unable to create reader due to unsupported scheme ")
   116  }
   117  
   118  func TestSetImmutabilityThreshold(t *testing.T) {
   119  	fs := &flag.FlagSet{}
   120  	fs.Int(QuorumImmutabilityThreshold.Name, 0, "")
   121  	arbitraryCLIContext := cli.NewContext(nil, fs, nil)
   122  	assert.NoError(t, arbitraryCLIContext.GlobalSet(QuorumImmutabilityThreshold.Name, strconv.Itoa(100000)))
   123  	assert.True(t, arbitraryCLIContext.GlobalIsSet(QuorumImmutabilityThreshold.Name), "immutability threshold flag not set")
   124  	assert.Equal(t, 100000, arbitraryCLIContext.GlobalInt(QuorumImmutabilityThreshold.Name), "immutability threshold value not set")
   125  }
   126  
   127  func TestSetPlugins_whenTypical(t *testing.T) {
   128  	tmpDir, err := ioutil.TempDir("", "q-")
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	defer func() {
   133  		_ = os.RemoveAll(tmpDir)
   134  	}()
   135  	arbitraryJSONFile := path.Join(tmpDir, "arbitary.json")
   136  	if err := ioutil.WriteFile(arbitraryJSONFile, []byte("{}"), 0644); err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	arbitraryNodeConfig := &node.Config{}
   140  	fs := &flag.FlagSet{}
   141  	fs.String(PluginSettingsFlag.Name, "", "")
   142  	arbitraryCLIContext := cli.NewContext(nil, fs, nil)
   143  	assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginSettingsFlag.Name, "file://"+arbitraryJSONFile))
   144  
   145  	assert.NoError(t, SetPlugins(arbitraryCLIContext, arbitraryNodeConfig))
   146  
   147  	assert.NotNil(t, arbitraryNodeConfig.Plugins)
   148  }
   149  
   150  func verifyErrorMessage(t *testing.T, ctx *cli.Context, cfg *node.Config, expectedMsg string) {
   151  	err := SetPlugins(ctx, cfg)
   152  	assert.EqualError(t, err, expectedMsg)
   153  }
   154  
   155  func Test_SplitTagsFlag(t *testing.T) {
   156  	tests := []struct {
   157  		name string
   158  		args string
   159  		want map[string]string
   160  	}{
   161  		{
   162  			"2 tags case",
   163  			"host=localhost,bzzkey=123",
   164  			map[string]string{
   165  				"host":   "localhost",
   166  				"bzzkey": "123",
   167  			},
   168  		},
   169  		{
   170  			"1 tag case",
   171  			"host=localhost123",
   172  			map[string]string{
   173  				"host": "localhost123",
   174  			},
   175  		},
   176  		{
   177  			"empty case",
   178  			"",
   179  			map[string]string{},
   180  		},
   181  		{
   182  			"garbage",
   183  			"smth=smthelse=123",
   184  			map[string]string{},
   185  		},
   186  	}
   187  	for _, tt := range tests {
   188  		t.Run(tt.name, func(t *testing.T) {
   189  			if got := SplitTagsFlag(tt.args); !reflect.DeepEqual(got, tt.want) {
   190  				t.Errorf("splitTagsFlag() = %v, want %v", got, tt.want)
   191  			}
   192  		})
   193  	}
   194  }
   195  
   196  func TestQuorumConfigFlags(t *testing.T) {
   197  	fs := &flag.FlagSet{}
   198  	arbitraryCLIContext := cli.NewContext(nil, fs, nil)
   199  	arbitraryEthConfig := &eth.Config{}
   200  
   201  	fs.Int(EVMCallTimeOutFlag.Name, 0, "")
   202  	assert.NoError(t, arbitraryCLIContext.GlobalSet(EVMCallTimeOutFlag.Name, strconv.Itoa(12)))
   203  	fs.Bool(MultitenancyFlag.Name, false, "")
   204  	assert.NoError(t, arbitraryCLIContext.GlobalSet(MultitenancyFlag.Name, "true"))
   205  	fs.Bool(QuorumEnablePrivacyMarker.Name, true, "")
   206  	assert.NoError(t, arbitraryCLIContext.GlobalSet(QuorumEnablePrivacyMarker.Name, "true"))
   207  	fs.Uint64(IstanbulRequestTimeoutFlag.Name, 0, "")
   208  	assert.NoError(t, arbitraryCLIContext.GlobalSet(IstanbulRequestTimeoutFlag.Name, "23"))
   209  	fs.Uint64(IstanbulBlockPeriodFlag.Name, 0, "")
   210  	assert.NoError(t, arbitraryCLIContext.GlobalSet(IstanbulBlockPeriodFlag.Name, "34"))
   211  	fs.Bool(RaftModeFlag.Name, false, "")
   212  	assert.NoError(t, arbitraryCLIContext.GlobalSet(RaftModeFlag.Name, "true"))
   213  	fs.String(PrivateCacheTrieJournalFlag.Name, "", "")
   214  	assert.NoError(t, arbitraryCLIContext.GlobalSet(PrivateCacheTrieJournalFlag.Name, "myprivatetriecache"))
   215  
   216  	require.NoError(t, setQuorumConfig(arbitraryCLIContext, arbitraryEthConfig))
   217  
   218  	assert.True(t, arbitraryCLIContext.GlobalIsSet(EVMCallTimeOutFlag.Name), "EVMCallTimeOutFlag not set")
   219  	assert.True(t, arbitraryCLIContext.GlobalIsSet(MultitenancyFlag.Name), "MultitenancyFlag not set")
   220  	assert.True(t, arbitraryCLIContext.GlobalIsSet(RaftModeFlag.Name), "RaftModeFlag not set")
   221  
   222  	assert.Equal(t, 12*time.Second, arbitraryEthConfig.EVMCallTimeOut, "EVMCallTimeOut value is incorrect")
   223  	assert.Equal(t, true, arbitraryEthConfig.QuorumChainConfig.MultiTenantEnabled(), "MultitenancyFlag value is incorrect")
   224  	assert.Equal(t, true, arbitraryEthConfig.QuorumChainConfig.PrivacyMarkerEnabled(), "QuorumEnablePrivacyMarker value is incorrect")
   225  	assert.Equal(t, uint64(23), arbitraryEthConfig.Istanbul.RequestTimeout, "IstanbulRequestTimeoutFlag value is incorrect")
   226  	assert.Equal(t, uint64(34), arbitraryEthConfig.Istanbul.BlockPeriod, "IstanbulBlockPeriodFlag value is incorrect")
   227  	assert.Equal(t, true, arbitraryEthConfig.RaftMode, "RaftModeFlag value is incorrect")
   228  	assert.Equal(t, "myprivatetriecache", arbitraryEthConfig.PrivateTrieCleanCacheJournal, "PrivateTrieCleanCacheJournal value is incorrect")
   229  }