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