github.com/ConsenSys/Quorum@v20.10.0+incompatible/cmd/geth/accountcmd_plugin_test.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/ethereum/go-ethereum/cmd/utils"
    10  	"github.com/ethereum/go-ethereum/node"
    11  	"github.com/ethereum/go-ethereum/plugin"
    12  	"github.com/stretchr/testify/require"
    13  	"gopkg.in/urfave/cli.v1"
    14  )
    15  
    16  // newAccountPluginCLIContext creates a cli.Context setup with the core account plugin CLI flags.
    17  // args sets the values of the flags.
    18  func newAccountPluginCLIContext(args []string) *cli.Context {
    19  	fs := &flag.FlagSet{}
    20  	fs.String(utils.PluginSettingsFlag.Name, "", "")
    21  	fs.String(utils.AccountPluginNewAccountConfigFlag.Name, "", "")
    22  	_ = fs.Parse(args)
    23  
    24  	return cli.NewContext(nil, fs, nil)
    25  }
    26  
    27  type mockConfigNodeMaker struct {
    28  	do func(ctx *cli.Context) (*node.Node, gethConfig)
    29  }
    30  
    31  func (m *mockConfigNodeMaker) makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
    32  	return m.do(ctx)
    33  }
    34  
    35  func TestListPluginAccounts_ErrIfCLIFlagNotSet(t *testing.T) {
    36  	var args []string
    37  	ctx := newAccountPluginCLIContext(args)
    38  
    39  	_, err := listPluginAccounts(ctx)
    40  	require.EqualError(t, err, "--plugins required")
    41  }
    42  
    43  func TestListPluginAccounts_ErrIfUnsupportedPluginInConfig(t *testing.T) {
    44  	var unsupportedPlugin plugin.PluginInterfaceName = "somename"
    45  	pluginSettings := plugin.Settings{
    46  		Providers: map[plugin.PluginInterfaceName]plugin.PluginDefinition{
    47  			unsupportedPlugin: {},
    48  		},
    49  	}
    50  
    51  	args := []string{
    52  		"--plugins", "/path/to/config.json",
    53  	}
    54  	ctx := newAccountPluginCLIContext(args)
    55  
    56  	makeConfigNodeDelegate = &mockConfigNodeMaker{
    57  		do: func(ctx *cli.Context) (*node.Node, gethConfig) {
    58  			return nil, gethConfig{
    59  				Node: node.Config{
    60  					Plugins: &pluginSettings,
    61  				},
    62  			}
    63  		},
    64  	}
    65  
    66  	_, err := listPluginAccounts(ctx)
    67  	require.EqualError(t, err, "unsupported plugins configured: [somename]")
    68  }
    69  
    70  func TestCreatePluginAccount_ErrIfCLIFlagsNotSet(t *testing.T) {
    71  	tests := []struct {
    72  		name string
    73  		args []string
    74  	}{
    75  		{
    76  			name: "no plugin flags",
    77  			args: []string{},
    78  		},
    79  		{
    80  			name: "only plugin settings flag",
    81  			args: []string{"--plugins", "/path/to/config.json"},
    82  		},
    83  		{
    84  			name: "only new plugin account config settings flag",
    85  			args: []string{"--plugins.account.config", "/path/to/new-acct-config.json"},
    86  		},
    87  	}
    88  
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			ctx := newAccountPluginCLIContext(tt.args)
    92  
    93  			_, err := createPluginAccount(ctx)
    94  			require.EqualError(t, err, "--plugins and --plugins.account.config flags must be set")
    95  		})
    96  	}
    97  }
    98  
    99  func TestCreatePluginAccount_ErrIfInvalidNewAccountConfig(t *testing.T) {
   100  	tests := []struct {
   101  		name       string
   102  		flagValue  string
   103  		wantErrMsg string
   104  	}{
   105  		{
   106  			name:       "json: invalid json",
   107  			flagValue:  "{invalidjson: abc}",
   108  			wantErrMsg: "invalid account creation config provided: invalid character 'i' looking for beginning of object key string",
   109  		},
   110  		{
   111  			name:       "file: does not exist",
   112  			flagValue:  "file://doesnotexist",
   113  			wantErrMsg: "invalid account creation config provided: open doesnotexist: no such file or directory",
   114  		},
   115  		{
   116  			name:       "env: not set",
   117  			flagValue:  "env://notset",
   118  			wantErrMsg: "invalid account creation config provided: env variable notset not found",
   119  		},
   120  	}
   121  
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			args := []string{
   125  				"--plugins", "/path/to/config.json",
   126  				"--plugins.account.config", tt.flagValue,
   127  			}
   128  			ctx := newAccountPluginCLIContext(args)
   129  			_, err := createPluginAccount(ctx)
   130  			require.EqualError(t, err, tt.wantErrMsg)
   131  		})
   132  	}
   133  }
   134  
   135  func TestCreatePluginAccount_ErrIfUnsupportedPluginInConfig(t *testing.T) {
   136  	var unsupportedPlugin plugin.PluginInterfaceName = "somename"
   137  	pluginSettings := plugin.Settings{
   138  		Providers: map[plugin.PluginInterfaceName]plugin.PluginDefinition{
   139  			unsupportedPlugin: {},
   140  		},
   141  	}
   142  
   143  	args := []string{
   144  		"--plugins", "/path/to/config.json",
   145  		"--plugins.account.config", "{}",
   146  	}
   147  	ctx := newAccountPluginCLIContext(args)
   148  
   149  	makeConfigNodeDelegate = &mockConfigNodeMaker{
   150  		do: func(ctx *cli.Context) (*node.Node, gethConfig) {
   151  			return nil, gethConfig{
   152  				Node: node.Config{
   153  					Plugins: &pluginSettings,
   154  				},
   155  			}
   156  		},
   157  	}
   158  
   159  	_, err := createPluginAccount(ctx)
   160  	require.EqualError(t, err, "unsupported plugins configured: [somename]")
   161  }
   162  
   163  func TestImportPluginAccount_ErrIfNoArg(t *testing.T) {
   164  	var args []string
   165  	ctx := newAccountPluginCLIContext(args)
   166  
   167  	_, err := importPluginAccount(ctx)
   168  	require.EqualError(t, err, "keyfile must be given as argument")
   169  }
   170  
   171  func TestImportPluginAccount_ErrIfInvalidRawkey(t *testing.T) {
   172  	args := []string{"/incorrect/path/to/file.key"}
   173  	ctx := newAccountPluginCLIContext(args)
   174  
   175  	_, err := importPluginAccount(ctx)
   176  	require.EqualError(t, err, "Failed to load the private key: open /incorrect/path/to/file.key: no such file or directory")
   177  }
   178  
   179  func TestImportPluginAccount_ErrIfCLIFlagsNotSet(t *testing.T) {
   180  	tmpfile, err := ioutil.TempFile("", "rawkey")
   181  	require.NoError(t, err)
   182  	t.Log("creating tmp file", "path", tmpfile.Name())
   183  	defer os.Remove(tmpfile.Name())
   184  	_, err = tmpfile.Write([]byte("1fe8f1ad4053326db20529257ac9401f2e6c769ef1d736b8c2f5aba5f787c72b"))
   185  	require.NoError(t, err)
   186  	err = tmpfile.Close()
   187  	require.NoError(t, err)
   188  
   189  	tests := []struct {
   190  		name string
   191  		args []string
   192  	}{
   193  		{
   194  			name: "no plugin flags",
   195  			args: []string{tmpfile.Name()},
   196  		},
   197  		{
   198  			name: "only plugin settings flag",
   199  			args: []string{"--plugins", "/path/to/config.json", tmpfile.Name()},
   200  		},
   201  		{
   202  			name: "only new plugin account config settings flag",
   203  			args: []string{"--plugins.account.config", "/path/to/new-acct-config.json", tmpfile.Name()},
   204  		},
   205  	}
   206  
   207  	for _, tt := range tests {
   208  		t.Run(tt.name, func(t *testing.T) {
   209  			ctx := newAccountPluginCLIContext(tt.args)
   210  
   211  			_, err := importPluginAccount(ctx)
   212  			require.EqualError(t, err, "--plugins and --plugins.account.config flags must be set")
   213  		})
   214  	}
   215  }
   216  
   217  func TestImportPluginAccount_ErrIfInvalidNewAccountConfig(t *testing.T) {
   218  	tmpfile, err := ioutil.TempFile("", "rawkey")
   219  	require.NoError(t, err)
   220  	t.Log("creating tmp file", "path", tmpfile.Name())
   221  	defer os.Remove(tmpfile.Name())
   222  	_, err = tmpfile.Write([]byte("1fe8f1ad4053326db20529257ac9401f2e6c769ef1d736b8c2f5aba5f787c72b"))
   223  	require.NoError(t, err)
   224  	err = tmpfile.Close()
   225  	require.NoError(t, err)
   226  
   227  	tests := []struct {
   228  		name       string
   229  		flagValue  string
   230  		wantErrMsg string
   231  	}{
   232  		{
   233  			name:       "json: invalid json",
   234  			flagValue:  "{invalidjson: abc}",
   235  			wantErrMsg: "invalid account creation config provided: invalid character 'i' looking for beginning of object key string",
   236  		},
   237  		{
   238  			name:       "file: does not exist",
   239  			flagValue:  "file://doesnotexist",
   240  			wantErrMsg: "invalid account creation config provided: open doesnotexist: no such file or directory",
   241  		},
   242  		{
   243  			name:       "env: not set",
   244  			flagValue:  "env://notset",
   245  			wantErrMsg: "invalid account creation config provided: env variable notset not found",
   246  		},
   247  	}
   248  
   249  	for _, tt := range tests {
   250  		t.Run(tt.name, func(t *testing.T) {
   251  			args := []string{
   252  				"--plugins", "/path/to/config.json",
   253  				"--plugins.account.config", tt.flagValue,
   254  				tmpfile.Name(),
   255  			}
   256  			ctx := newAccountPluginCLIContext(args)
   257  			_, err := importPluginAccount(ctx)
   258  			require.EqualError(t, err, tt.wantErrMsg)
   259  		})
   260  	}
   261  }
   262  
   263  func TestImportPluginAccount_ErrIfUnsupportedPluginInConfig(t *testing.T) {
   264  	tmpfile, err := ioutil.TempFile("", "rawkey")
   265  	require.NoError(t, err)
   266  	t.Log("creating tmp file", "path", tmpfile.Name())
   267  	defer os.Remove(tmpfile.Name())
   268  	_, err = tmpfile.Write([]byte("1fe8f1ad4053326db20529257ac9401f2e6c769ef1d736b8c2f5aba5f787c72b"))
   269  	require.NoError(t, err)
   270  	err = tmpfile.Close()
   271  	require.NoError(t, err)
   272  
   273  	var unsupportedPlugin plugin.PluginInterfaceName = "somename"
   274  	pluginSettings := plugin.Settings{
   275  		Providers: map[plugin.PluginInterfaceName]plugin.PluginDefinition{
   276  			unsupportedPlugin: {},
   277  		},
   278  	}
   279  
   280  	args := []string{
   281  		"--plugins", "/path/to/config.json",
   282  		"--plugins.account.config", "{}",
   283  		tmpfile.Name(),
   284  	}
   285  	ctx := newAccountPluginCLIContext(args)
   286  
   287  	makeConfigNodeDelegate = &mockConfigNodeMaker{
   288  		do: func(ctx *cli.Context) (*node.Node, gethConfig) {
   289  			return nil, gethConfig{
   290  				Node: node.Config{
   291  					Plugins: &pluginSettings,
   292  				},
   293  			}
   294  		},
   295  	}
   296  
   297  	_, err = importPluginAccount(ctx)
   298  	require.EqualError(t, err, "unsupported plugins configured: [somename]")
   299  }