github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/neatio/accountcmd_test.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 "runtime" 7 "strings" 8 "testing" 9 10 "github.com/cespare/cp" 11 ) 12 13 14 func tmpDatadirWithKeystore(t *testing.T) string { 15 datadir := tmpdir(t) 16 keystore := filepath.Join(datadir, "keystore") 17 source := filepath.Join("..", "..", "accounts", "keystore", "testdata", "keystore") 18 if err := cp.CopyAll(keystore, source); err != nil { 19 t.Fatal(err) 20 } 21 return datadir 22 } 23 24 func TestAccountListEmpty(t *testing.T) { 25 geth := runGeth(t, "account", "list") 26 geth.ExpectExit() 27 } 28 29 func TestAccountList(t *testing.T) { 30 datadir := tmpDatadirWithKeystore(t) 31 geth := runGeth(t, "account", "list", "--datadir", datadir) 32 defer geth.ExpectExit() 33 if runtime.GOOS == "windows" { 34 geth.Expect(` 35 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 36 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}\keystore\aaa 37 Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}\keystore\zzz 38 `) 39 } else { 40 geth.Expect(` 41 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 42 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa 43 Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}/keystore/zzz 44 `) 45 } 46 } 47 48 func TestAccountNew(t *testing.T) { 49 geth := runGeth(t, "account", "new", "--lightkdf") 50 defer geth.ExpectExit() 51 geth.Expect(` 52 Your new account is locked with a password. Please give a password. Do not forget this password. 53 !! Unsupported terminal, password will be echoed. 54 Passphrase: {{.InputLine "foobar"}} 55 Repeat passphrase: {{.InputLine "foobar"}} 56 `) 57 geth.ExpectRegexp(`Address: \{[0-9a-f]{40}\}\n`) 58 } 59 60 func TestAccountNewBadRepeat(t *testing.T) { 61 geth := runGeth(t, "account", "new", "--lightkdf") 62 defer geth.ExpectExit() 63 geth.Expect(` 64 Your new account is locked with a password. Please give a password. Do not forget this password. 65 !! Unsupported terminal, password will be echoed. 66 Passphrase: {{.InputLine "something"}} 67 Repeat passphrase: {{.InputLine "something else"}} 68 Fatal: Passphrases do not match 69 `) 70 } 71 72 func TestAccountUpdate(t *testing.T) { 73 datadir := tmpDatadirWithKeystore(t) 74 geth := runGeth(t, "account", "update", 75 "--datadir", datadir, "--lightkdf", 76 "f466859ead1932d743d622cb74fc058882e8648a") 77 defer geth.ExpectExit() 78 geth.Expect(` 79 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 80 !! Unsupported terminal, password will be echoed. 81 Passphrase: {{.InputLine "foobar"}} 82 Please give a new password. Do not forget this password. 83 Passphrase: {{.InputLine "foobar2"}} 84 Repeat passphrase: {{.InputLine "foobar2"}} 85 `) 86 } 87 88 func TestWalletImport(t *testing.T) { 89 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 90 defer geth.ExpectExit() 91 geth.Expect(` 92 !! Unsupported terminal, password will be echoed. 93 Passphrase: {{.InputLine "foo"}} 94 Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f} 95 `) 96 97 files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore")) 98 if len(files) != 1 { 99 t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err) 100 } 101 } 102 103 func TestWalletImportBadPassword(t *testing.T) { 104 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 105 defer geth.ExpectExit() 106 geth.Expect(` 107 !! Unsupported terminal, password will be echoed. 108 Passphrase: {{.InputLine "wrong"}} 109 Fatal: could not decrypt key with given passphrase 110 `) 111 } 112 113 func TestUnlockFlag(t *testing.T) { 114 datadir := tmpDatadirWithKeystore(t) 115 geth := runGeth(t, 116 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 117 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 118 "js", "testdata/empty.js") 119 geth.Expect(` 120 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 121 !! Unsupported terminal, password will be echoed. 122 Passphrase: {{.InputLine "foobar"}} 123 `) 124 geth.ExpectExit() 125 126 wantMessages := []string{ 127 "Unlocked account", 128 "=0xf466859eAD1932D743d622CB74FC058882E8648A", 129 } 130 for _, m := range wantMessages { 131 if !strings.Contains(geth.StderrText(), m) { 132 t.Errorf("stderr text does not contain %q", m) 133 } 134 } 135 } 136 137 func TestUnlockFlagWrongPassword(t *testing.T) { 138 datadir := tmpDatadirWithKeystore(t) 139 geth := runGeth(t, 140 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 141 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 142 defer geth.ExpectExit() 143 geth.Expect(` 144 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 145 !! Unsupported terminal, password will be echoed. 146 Passphrase: {{.InputLine "wrong1"}} 147 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3 148 Passphrase: {{.InputLine "wrong2"}} 149 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3 150 Passphrase: {{.InputLine "wrong3"}} 151 Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase) 152 `) 153 } 154 155 func TestUnlockFlagMultiIndex(t *testing.T) { 156 datadir := tmpDatadirWithKeystore(t) 157 geth := runGeth(t, 158 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 159 "--unlock", "0,2", 160 "js", "testdata/empty.js") 161 geth.Expect(` 162 Unlocking account 0 | Attempt 1/3 163 !! Unsupported terminal, password will be echoed. 164 Passphrase: {{.InputLine "foobar"}} 165 Unlocking account 2 | Attempt 1/3 166 Passphrase: {{.InputLine "foobar"}} 167 `) 168 geth.ExpectExit() 169 170 wantMessages := []string{ 171 "Unlocked account", 172 "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 173 "=0x289d485D9771714CCe91D3393D764E1311907ACc", 174 } 175 for _, m := range wantMessages { 176 if !strings.Contains(geth.StderrText(), m) { 177 t.Errorf("stderr text does not contain %q", m) 178 } 179 } 180 } 181 182 func TestUnlockFlagPasswordFile(t *testing.T) { 183 datadir := tmpDatadirWithKeystore(t) 184 geth := runGeth(t, 185 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 186 "--password", "testdata/passwords.txt", "--unlock", "0,2", 187 "js", "testdata/empty.js") 188 geth.ExpectExit() 189 190 wantMessages := []string{ 191 "Unlocked account", 192 "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 193 "=0x289d485D9771714CCe91D3393D764E1311907ACc", 194 } 195 for _, m := range wantMessages { 196 if !strings.Contains(geth.StderrText(), m) { 197 t.Errorf("stderr text does not contain %q", m) 198 } 199 } 200 } 201 202 func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) { 203 datadir := tmpDatadirWithKeystore(t) 204 geth := runGeth(t, 205 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 206 "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2") 207 defer geth.ExpectExit() 208 geth.Expect(` 209 Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase) 210 `) 211 } 212 213 func TestUnlockFlagAmbiguous(t *testing.T) { 214 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 215 geth := runGeth(t, 216 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 217 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 218 "js", "testdata/empty.js") 219 defer geth.ExpectExit() 220 221 geth.SetTemplateFunc("keypath", func(file string) string { 222 abs, _ := filepath.Abs(filepath.Join(store, file)) 223 return abs 224 }) 225 geth.Expect(` 226 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 227 !! Unsupported terminal, password will be echoed. 228 Passphrase: {{.InputLine "foobar"}} 229 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 230 keystore://{{keypath "1"}} 231 keystore://{{keypath "2"}} 232 Testing your passphrase against all of them... 233 Your passphrase unlocked keystore://{{keypath "1"}} 234 In order to avoid this warning, you need to remove the following duplicate key files: 235 keystore://{{keypath "2"}} 236 `) 237 geth.ExpectExit() 238 239 wantMessages := []string{ 240 "Unlocked account", 241 "=0xf466859eAD1932D743d622CB74FC058882E8648A", 242 } 243 for _, m := range wantMessages { 244 if !strings.Contains(geth.StderrText(), m) { 245 t.Errorf("stderr text does not contain %q", m) 246 } 247 } 248 } 249 250 func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) { 251 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 252 geth := runGeth(t, 253 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 254 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 255 defer geth.ExpectExit() 256 257 geth.SetTemplateFunc("keypath", func(file string) string { 258 abs, _ := filepath.Abs(filepath.Join(store, file)) 259 return abs 260 }) 261 geth.Expect(` 262 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 263 !! Unsupported terminal, password will be echoed. 264 Passphrase: {{.InputLine "wrong"}} 265 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 266 keystore://{{keypath "1"}} 267 keystore://{{keypath "2"}} 268 Testing your passphrase against all of them... 269 Fatal: None of the listed files could be unlocked. 270 `) 271 geth.ExpectExit() 272 }