github.com/prysmaticlabs/prysm@v1.4.4/shared/cmd/helpers_test.go (about) 1 package cmd 2 3 import ( 4 "flag" 5 "os" 6 "os/user" 7 "testing" 8 9 "github.com/golang/mock/gomock" 10 "github.com/pkg/errors" 11 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 12 "github.com/prysmaticlabs/prysm/shared/testutil/require" 13 "github.com/urfave/cli/v2" 14 ) 15 16 func TestEnterPassword(t *testing.T) { 17 type rets struct { 18 pw string 19 err error 20 } 21 var tt = []struct { 22 name string 23 rets []rets 24 expectedErr error 25 expectedPw string 26 }{ 27 { 28 "first_match", 29 []rets{{"abcd", nil}, {"abcd", nil}}, 30 nil, 31 "abcd", 32 }, 33 { 34 "first_match_with_newline", 35 []rets{{"abcd\n", nil}, {"abcd", nil}}, 36 nil, 37 "abcd", 38 }, 39 { 40 "first_match_with_newline_confirm", 41 []rets{{"abcd", nil}, {"abcd\n", nil}}, 42 nil, 43 "abcd", 44 }, 45 { 46 "first_match_both_newline", 47 []rets{{"abcd\n", nil}, {"abcd\n", nil}}, 48 nil, 49 "abcd", 50 }, 51 { 52 "second_match", 53 []rets{{"abcd", nil}, {"aba", nil}, {"abcd", nil}, {"abcd", nil}}, 54 nil, 55 "abcd", 56 }, 57 { 58 "cant_read", 59 []rets{{"pw", errors.New("i/o fail")}}, 60 errors.New("i/o fail"), 61 "", 62 }, 63 } 64 65 for _, tc := range tt { 66 t.Run(tc.name, func(t *testing.T) { 67 ctrl := gomock.NewController(t) 68 m := NewMockPasswordReader(ctrl) 69 for _, ret := range tc.rets { 70 m.EXPECT().ReadPassword().Return(ret.pw, ret.err) 71 } 72 pw, err := EnterPassword(true, m) 73 assert.Equal(t, tc.expectedPw, pw) 74 if tc.expectedErr != nil { 75 assert.ErrorContains(t, tc.expectedErr.Error(), err) 76 } else { 77 assert.NoError(t, err) 78 } 79 }) 80 } 81 } 82 83 func TestExpandSingleEndpointIfFile(t *testing.T) { 84 app := cli.App{} 85 set := flag.NewFlagSet("test", 0) 86 HTTPWeb3ProviderFlag := &cli.StringFlag{Name: "http-web3provider", Value: ""} 87 set.String(HTTPWeb3ProviderFlag.Name, "", "") 88 context := cli.NewContext(&app, set, nil) 89 90 // with nothing set 91 require.NoError(t, ExpandSingleEndpointIfFile(context, HTTPWeb3ProviderFlag)) 92 require.Equal(t, "", context.String(HTTPWeb3ProviderFlag.Name)) 93 94 // with url scheme 95 require.NoError(t, context.Set(HTTPWeb3ProviderFlag.Name, "http://localhost:8545")) 96 require.NoError(t, ExpandSingleEndpointIfFile(context, HTTPWeb3ProviderFlag)) 97 require.Equal(t, "http://localhost:8545", context.String(HTTPWeb3ProviderFlag.Name)) 98 99 // relative user home path 100 usr, err := user.Current() 101 require.NoError(t, err) 102 require.NoError(t, context.Set(HTTPWeb3ProviderFlag.Name, "~/relative/path.ipc")) 103 require.NoError(t, ExpandSingleEndpointIfFile(context, HTTPWeb3ProviderFlag)) 104 require.Equal(t, usr.HomeDir+"/relative/path.ipc", context.String(HTTPWeb3ProviderFlag.Name)) 105 106 // current dir path 107 curentdir, err := os.Getwd() 108 require.NoError(t, err) 109 require.NoError(t, context.Set(HTTPWeb3ProviderFlag.Name, "./path.ipc")) 110 require.NoError(t, ExpandSingleEndpointIfFile(context, HTTPWeb3ProviderFlag)) 111 require.Equal(t, curentdir+"/path.ipc", context.String(HTTPWeb3ProviderFlag.Name)) 112 } 113 114 func TestExpandWeb3EndpointsIfFile(t *testing.T) { 115 app := cli.App{} 116 set := flag.NewFlagSet("test", 0) 117 HTTPWeb3ProviderFlag := &cli.StringSliceFlag{Name: "fallback-web3provider", Value: cli.NewStringSlice()} 118 set.Var(cli.NewStringSlice(), HTTPWeb3ProviderFlag.Name, "") 119 context := cli.NewContext(&app, set, nil) 120 // with nothing set 121 require.NoError(t, ExpandWeb3EndpointsIfFile(context, HTTPWeb3ProviderFlag)) 122 require.DeepEqual(t, []string{}, context.StringSlice(HTTPWeb3ProviderFlag.Name)) 123 124 // with url scheme 125 require.NoError(t, context.Set(HTTPWeb3ProviderFlag.Name, "http://localhost:8545")) 126 require.NoError(t, ExpandWeb3EndpointsIfFile(context, HTTPWeb3ProviderFlag)) 127 require.DeepEqual(t, []string{"http://localhost:8545"}, context.StringSlice(HTTPWeb3ProviderFlag.Name)) 128 129 // reset context 130 set = flag.NewFlagSet("test", 0) 131 set.Var(cli.NewStringSlice(), HTTPWeb3ProviderFlag.Name, "") 132 context = cli.NewContext(&app, set, nil) 133 134 // relative user home path 135 usr, err := user.Current() 136 require.NoError(t, err) 137 require.NoError(t, context.Set(HTTPWeb3ProviderFlag.Name, "~/relative/path.ipc")) 138 require.NoError(t, ExpandWeb3EndpointsIfFile(context, HTTPWeb3ProviderFlag)) 139 require.DeepEqual(t, []string{usr.HomeDir + "/relative/path.ipc"}, context.StringSlice(HTTPWeb3ProviderFlag.Name)) 140 141 // reset context 142 set = flag.NewFlagSet("test", 0) 143 set.Var(cli.NewStringSlice(), HTTPWeb3ProviderFlag.Name, "") 144 context = cli.NewContext(&app, set, nil) 145 146 // current dir path 147 curentdir, err := os.Getwd() 148 require.NoError(t, err) 149 require.NoError(t, context.Set(HTTPWeb3ProviderFlag.Name, "./path.ipc")) 150 require.NoError(t, ExpandWeb3EndpointsIfFile(context, HTTPWeb3ProviderFlag)) 151 require.DeepEqual(t, []string{curentdir + "/path.ipc"}, context.StringSlice(HTTPWeb3ProviderFlag.Name)) 152 }