github.com/prysmaticlabs/prysm@v1.4.4/shared/promptutil/validate_test.go (about) 1 package promptutil 2 3 import ( 4 "io/ioutil" 5 "os" 6 "testing" 7 8 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 9 "github.com/prysmaticlabs/prysm/shared/testutil/require" 10 ) 11 12 func TestValidatePasswordInput(t *testing.T) { 13 tests := []struct { 14 name string 15 input string 16 wantedErr string 17 }{ 18 { 19 name: "no numbers nor special characters", 20 input: "abcdefghijklmnopqrs", 21 wantedErr: errPasswordWeak.Error(), 22 }, 23 { 24 name: "number and letters but no special characters", 25 input: "abcdefghijklmnopqrs2020", 26 wantedErr: errPasswordWeak.Error(), 27 }, 28 { 29 name: "numbers, letters, special characters, but too short", 30 input: "abc2$", 31 wantedErr: errPasswordWeak.Error(), 32 }, 33 { 34 name: "proper length and strong password", 35 input: "%Str0ngpassword32kjAjsd22020$%", 36 }, 37 { 38 name: "password format correct but weak entropy score", 39 input: "aaaaaaa1$", 40 wantedErr: "password is too easy to guess, try a stronger password", 41 }, 42 { 43 name: "allow spaces", 44 input: "x*329293@aAJSD i22903saj", 45 }, 46 { 47 name: "strong password from LastPass", 48 input: "jXl!q5pkQnXsyT6dbJ3X5plQ!9%iqJCTr&*UIoaDu#b6GYJD##^GI3qniKdr240f", 49 }, 50 { 51 name: "allow underscores", 52 input: "jXl!q5pkQn_syT6dbJ3X5plQ_9_iqJCTr_*UIoaDu#b6GYJD##^GI3qniKdr240f", 53 }, 54 { 55 name: "only numbers and symbols should fail", 56 input: "123493489223423_23923929", 57 wantedErr: errPasswordWeak.Error(), 58 }, 59 } 60 for _, tt := range tests { 61 t.Run(tt.name, func(t *testing.T) { 62 err := ValidatePasswordInput(tt.input) 63 if tt.wantedErr != "" { 64 assert.ErrorContains(t, tt.wantedErr, err) 65 } else { 66 assert.NoError(t, err) 67 } 68 }) 69 } 70 } 71 72 func TestIsValidUnicode(t *testing.T) { 73 tests := []struct { 74 name string 75 input string 76 want bool 77 }{ 78 { 79 name: "Regular alphanumeric", 80 input: "Someone23xx", 81 want: true, 82 }, 83 { 84 name: "Unicode strings separated by a space character", 85 input: "x*329293@aAJSD i22903saj", 86 want: true, 87 }, 88 { 89 name: "Japanese", 90 input: "僕は絵お見るのが好きです", 91 want: true, 92 }, 93 { 94 name: "Other foreign", 95 input: "Etérium", 96 want: true, 97 }, 98 } 99 for _, tt := range tests { 100 t.Run(tt.name, func(t *testing.T) { 101 if got := IsValidUnicode(tt.input); got != tt.want { 102 t.Errorf("isValidUnicode() = %v, want %v", got, tt.want) 103 } 104 }) 105 } 106 } 107 108 func TestDefaultAndValidatePrompt(t *testing.T) { 109 tests := []struct { 110 name string 111 input string 112 def string 113 want string 114 wantError bool 115 }{ 116 { 117 name: "number", 118 input: "3", 119 def: "0", 120 want: "3", 121 }, 122 { 123 name: "empty return default", 124 input: "", 125 def: "0", 126 want: "0", 127 }, 128 { 129 name: "empty return default no zero", 130 input: "", 131 def: "3", 132 want: "3", 133 }, 134 { 135 name: "empty return default, no zero", 136 input: "a", 137 def: "0", 138 want: "", 139 wantError: true, 140 }, 141 } 142 for _, tt := range tests { 143 t.Run(tt.name, func(t *testing.T) { 144 content := []byte(tt.input + "\n") 145 tmpfile, err := ioutil.TempFile("", "content") 146 require.NoError(t, err) 147 defer func() { 148 err := os.Remove(tmpfile.Name()) 149 require.NoError(t, err) 150 }() 151 152 _, err = tmpfile.Write(content) 153 require.NoError(t, err) 154 155 _, err = tmpfile.Seek(0, 0) 156 require.NoError(t, err) 157 oldStdin := os.Stdin 158 defer func() { os.Stdin = oldStdin }() // Restore original Stdin 159 os.Stdin = tmpfile 160 got, err := DefaultAndValidatePrompt(tt.name, tt.def, ValidateNumber) 161 if !tt.wantError { 162 require.NoError(t, err) 163 } 164 assert.Equal(t, tt.want, got) 165 err = tmpfile.Close() 166 require.NoError(t, err) 167 }) 168 } 169 } 170 171 func TestValidatePhrase(t *testing.T) { 172 wantedPhrase := "wanted phrase" 173 174 t.Run("correct input", func(t *testing.T) { 175 assert.NoError(t, ValidatePhrase(wantedPhrase, wantedPhrase)) 176 }) 177 t.Run("correct input with whitespace", func(t *testing.T) { 178 assert.NoError(t, ValidatePhrase(" wanted phrase ", wantedPhrase)) 179 }) 180 t.Run("incorrect input", func(t *testing.T) { 181 err := ValidatePhrase("foo", wantedPhrase) 182 assert.NotNil(t, err) 183 assert.ErrorContains(t, errIncorrectPhrase.Error(), err) 184 }) 185 t.Run("wrong letter case", func(t *testing.T) { 186 err := ValidatePhrase("Wanted Phrase", wantedPhrase) 187 assert.NotNil(t, err) 188 assert.ErrorContains(t, errIncorrectPhrase.Error(), err) 189 }) 190 }