github.com/ConsenSys/Quorum@v20.10.0+incompatible/cmd/utils/flags_test.go (about) 1 // Copyright 2014 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 29 "github.com/ethereum/go-ethereum/node" 30 "github.com/stretchr/testify/assert" 31 "gopkg.in/urfave/cli.v1" 32 ) 33 34 func TestSetPlugins_whenPluginsNotEnabled(t *testing.T) { 35 arbitraryNodeConfig := &node.Config{} 36 arbitraryCLIContext := cli.NewContext(nil, &flag.FlagSet{}, nil) 37 38 assert.NoError(t, SetPlugins(arbitraryCLIContext, arbitraryNodeConfig)) 39 40 assert.Nil(t, arbitraryNodeConfig.Plugins) 41 } 42 43 func TestSetPlugins_whenInvalidFlagsCombination(t *testing.T) { 44 arbitraryNodeConfig := &node.Config{} 45 fs := &flag.FlagSet{} 46 fs.String(PluginSettingsFlag.Name, "", "") 47 fs.Bool(PluginSkipVerifyFlag.Name, true, "") 48 fs.Bool(PluginLocalVerifyFlag.Name, true, "") 49 fs.String(PluginPublicKeyFlag.Name, "", "") 50 arbitraryCLIContext := cli.NewContext(nil, fs, nil) 51 assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginSettingsFlag.Name, "arbitrary value")) 52 53 verifyErrorMessage(t, arbitraryCLIContext, arbitraryNodeConfig, "only --plugins.skipverify or --plugins.localverify must be set") 54 55 assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginSkipVerifyFlag.Name, "false")) 56 assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginLocalVerifyFlag.Name, "false")) 57 assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginPublicKeyFlag.Name, "arbitry value")) 58 59 verifyErrorMessage(t, arbitraryCLIContext, arbitraryNodeConfig, "--plugins.localverify is required for setting --plugins.publickey") 60 } 61 62 func TestSetPlugins_whenInvalidPluginSettingsURL(t *testing.T) { 63 arbitraryNodeConfig := &node.Config{} 64 fs := &flag.FlagSet{} 65 fs.String(PluginSettingsFlag.Name, "", "") 66 arbitraryCLIContext := cli.NewContext(nil, fs, nil) 67 assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginSettingsFlag.Name, "arbitrary value")) 68 69 verifyErrorMessage(t, arbitraryCLIContext, arbitraryNodeConfig, "plugins: unable to create reader due to unsupported scheme ") 70 } 71 72 func TestSetImmutabilityThreshold(t *testing.T) { 73 fs := &flag.FlagSet{} 74 fs.Int(QuorumImmutabilityThreshold.Name, 0, "") 75 arbitraryCLIContext := cli.NewContext(nil, fs, nil) 76 assert.NoError(t, arbitraryCLIContext.GlobalSet(QuorumImmutabilityThreshold.Name, strconv.Itoa(100000))) 77 assert.True(t, arbitraryCLIContext.GlobalIsSet(QuorumImmutabilityThreshold.Name), "immutability threshold flag not set") 78 assert.Equal(t, 100000, arbitraryCLIContext.GlobalInt(QuorumImmutabilityThreshold.Name), "immutability threshold value not set") 79 } 80 81 func TestSetTimeOutForCall(t *testing.T) { 82 fs := &flag.FlagSet{} 83 fs.Int(EVMCallTimeOutFlag.Name, 0, "") 84 arbitraryCLIContext := cli.NewContext(nil, fs, nil) 85 assert.NoError(t, arbitraryCLIContext.GlobalSet(EVMCallTimeOutFlag.Name, strconv.Itoa(10))) 86 assert.True(t, arbitraryCLIContext.GlobalIsSet(EVMCallTimeOutFlag.Name), "timeoutforcall flag not set") 87 assert.Equal(t, 10, arbitraryCLIContext.GlobalInt(EVMCallTimeOutFlag.Name), "timeoutforcall value not set") 88 } 89 90 func TestSetPlugins_whenTypical(t *testing.T) { 91 tmpDir, err := ioutil.TempDir("", "q-") 92 if err != nil { 93 t.Fatal(err) 94 } 95 defer func() { 96 _ = os.RemoveAll(tmpDir) 97 }() 98 arbitraryJSONFile := path.Join(tmpDir, "arbitary.json") 99 if err := ioutil.WriteFile(arbitraryJSONFile, []byte("{}"), 0644); err != nil { 100 t.Fatal(err) 101 } 102 arbitraryNodeConfig := &node.Config{} 103 fs := &flag.FlagSet{} 104 fs.String(PluginSettingsFlag.Name, "", "") 105 arbitraryCLIContext := cli.NewContext(nil, fs, nil) 106 assert.NoError(t, arbitraryCLIContext.GlobalSet(PluginSettingsFlag.Name, "file://"+arbitraryJSONFile)) 107 108 assert.NoError(t, SetPlugins(arbitraryCLIContext, arbitraryNodeConfig)) 109 110 assert.NotNil(t, arbitraryNodeConfig.Plugins) 111 } 112 113 func verifyErrorMessage(t *testing.T, ctx *cli.Context, cfg *node.Config, expectedMsg string) { 114 err := SetPlugins(ctx, cfg) 115 assert.EqualError(t, err, expectedMsg) 116 } 117 118 func Test_SplitTagsFlag(t *testing.T) { 119 tests := []struct { 120 name string 121 args string 122 want map[string]string 123 }{ 124 { 125 "2 tags case", 126 "host=localhost,bzzkey=123", 127 map[string]string{ 128 "host": "localhost", 129 "bzzkey": "123", 130 }, 131 }, 132 { 133 "1 tag case", 134 "host=localhost123", 135 map[string]string{ 136 "host": "localhost123", 137 }, 138 }, 139 { 140 "empty case", 141 "", 142 map[string]string{}, 143 }, 144 { 145 "garbage", 146 "smth=smthelse=123", 147 map[string]string{}, 148 }, 149 } 150 for _, tt := range tests { 151 t.Run(tt.name, func(t *testing.T) { 152 if got := SplitTagsFlag(tt.args); !reflect.DeepEqual(got, tt.want) { 153 t.Errorf("splitTagsFlag() = %v, want %v", got, tt.want) 154 } 155 }) 156 } 157 }