2020-05-12 15:01:02 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-22 05:53:03 +00:00
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
2020-05-20 15:23:22 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-05-12 15:01:02 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/cmd/mock"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
2020-10-22 05:53:03 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-05-12 15:01:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEnterPassword(t *testing.T) {
|
|
|
|
type rets struct {
|
2020-05-14 22:11:52 +00:00
|
|
|
pw string
|
2020-05-12 15:01:02 +00:00
|
|
|
err error
|
|
|
|
}
|
2020-05-14 22:11:52 +00:00
|
|
|
var tt = []struct {
|
|
|
|
name string
|
|
|
|
rets []rets
|
2020-05-12 15:01:02 +00:00
|
|
|
expectedErr error
|
2020-05-14 22:11:52 +00:00
|
|
|
expectedPw string
|
2020-05-12 15:01:02 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"first_match",
|
|
|
|
[]rets{{"abcd", nil}, {"abcd", nil}},
|
|
|
|
nil,
|
|
|
|
"abcd",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"first_match_with_newline",
|
|
|
|
[]rets{{"abcd\n", nil}, {"abcd", nil}},
|
|
|
|
nil,
|
|
|
|
"abcd",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"first_match_with_newline_confirm",
|
|
|
|
[]rets{{"abcd", nil}, {"abcd\n", nil}},
|
|
|
|
nil,
|
|
|
|
"abcd",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"first_match_both_newline",
|
|
|
|
[]rets{{"abcd\n", nil}, {"abcd\n", nil}},
|
|
|
|
nil,
|
|
|
|
"abcd",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"second_match",
|
|
|
|
[]rets{{"abcd", nil}, {"aba", nil}, {"abcd", nil}, {"abcd", nil}},
|
|
|
|
nil,
|
|
|
|
"abcd",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cant_read",
|
|
|
|
[]rets{{"pw", errors.New("i/o fail")}},
|
|
|
|
errors.New("i/o fail"),
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
2023-10-20 16:45:33 +00:00
|
|
|
m := mock.NewPasswordReader(ctrl)
|
2020-05-12 15:01:02 +00:00
|
|
|
for _, ret := range tc.rets {
|
|
|
|
m.EXPECT().ReadPassword().Return(ret.pw, ret.err)
|
|
|
|
}
|
|
|
|
pw, err := EnterPassword(true, m)
|
2020-07-19 21:08:29 +00:00
|
|
|
assert.Equal(t, tc.expectedPw, pw)
|
2020-08-25 15:23:06 +00:00
|
|
|
if tc.expectedErr != nil {
|
|
|
|
assert.ErrorContains(t, tc.expectedErr.Error(), err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
2020-05-12 15:01:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 05:53:03 +00:00
|
|
|
|
2020-12-11 11:15:04 +00:00
|
|
|
func TestExpandSingleEndpointIfFile(t *testing.T) {
|
2020-10-22 05:53:03 +00:00
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
2023-03-16 12:15:02 +00:00
|
|
|
ExecutionEndpointFlag := &cli.StringFlag{Name: "execution-endpoint", Value: ""}
|
|
|
|
set.String(ExecutionEndpointFlag.Name, "", "")
|
2020-10-22 05:53:03 +00:00
|
|
|
context := cli.NewContext(&app, set, nil)
|
|
|
|
|
2020-10-23 03:57:19 +00:00
|
|
|
// with nothing set
|
2023-03-16 12:15:02 +00:00
|
|
|
require.NoError(t, ExpandSingleEndpointIfFile(context, ExecutionEndpointFlag))
|
|
|
|
require.Equal(t, "", context.String(ExecutionEndpointFlag.Name))
|
2020-10-23 03:57:19 +00:00
|
|
|
|
2020-10-22 05:53:03 +00:00
|
|
|
// with url scheme
|
2023-03-16 12:15:02 +00:00
|
|
|
require.NoError(t, context.Set(ExecutionEndpointFlag.Name, "http://localhost:8545"))
|
|
|
|
require.NoError(t, ExpandSingleEndpointIfFile(context, ExecutionEndpointFlag))
|
|
|
|
require.Equal(t, "http://localhost:8545", context.String(ExecutionEndpointFlag.Name))
|
2020-10-22 05:53:03 +00:00
|
|
|
|
|
|
|
// relative user home path
|
|
|
|
usr, err := user.Current()
|
|
|
|
require.NoError(t, err)
|
2023-03-16 12:15:02 +00:00
|
|
|
require.NoError(t, context.Set(ExecutionEndpointFlag.Name, "~/relative/path.ipc"))
|
|
|
|
require.NoError(t, ExpandSingleEndpointIfFile(context, ExecutionEndpointFlag))
|
|
|
|
require.Equal(t, usr.HomeDir+"/relative/path.ipc", context.String(ExecutionEndpointFlag.Name))
|
2020-10-22 05:53:03 +00:00
|
|
|
|
|
|
|
// current dir path
|
|
|
|
curentdir, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
2023-03-16 12:15:02 +00:00
|
|
|
require.NoError(t, context.Set(ExecutionEndpointFlag.Name, "./path.ipc"))
|
|
|
|
require.NoError(t, ExpandSingleEndpointIfFile(context, ExecutionEndpointFlag))
|
|
|
|
require.Equal(t, curentdir+"/path.ipc", context.String(ExecutionEndpointFlag.Name))
|
2020-10-22 05:53:03 +00:00
|
|
|
}
|