accountcmd_plugin_test.go 8.1 KB

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