github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/geth/accountcmd_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2016 Go Ethereum作者 10 //此文件是Go以太坊的一部分。 11 // 12 //Go以太坊是免费软件:您可以重新发布和/或修改它 13 //根据GNU通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊的分布希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU通用公共许可证了解更多详细信息。 21 // 22 //你应该已经收到一份GNU通用公共许可证的副本 23 //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package main 26 27 import ( 28 "io/ioutil" 29 "path/filepath" 30 "runtime" 31 "strings" 32 "testing" 33 34 "github.com/cespare/cp" 35 ) 36 37 //这些测试是与帐户相关的“烟雾测试” 38 //子命令和标志。 39 // 40 // 41 //复制到临时密钥库目录中。 42 43 func tmpDatadirWithKeystore(t *testing.T) string { 44 datadir := tmpdir(t) 45 keystore := filepath.Join(datadir, "keystore") 46 source := filepath.Join("..", "..", "accounts", "keystore", "testdata", "keystore") 47 if err := cp.CopyAll(keystore, source); err != nil { 48 t.Fatal(err) 49 } 50 return datadir 51 } 52 53 func TestAccountListEmpty(t *testing.T) { 54 geth := runGeth(t, "account", "list") 55 geth.ExpectExit() 56 } 57 58 func TestAccountList(t *testing.T) { 59 datadir := tmpDatadirWithKeystore(t) 60 geth := runGeth(t, "account", "list", "--datadir", datadir) 61 defer geth.ExpectExit() 62 if runtime.GOOS == "windows" { 63 geth.Expect(` 64 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://.datadir \keystore\utc--2016-03-22t12-57-55.920751759z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 65 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://.datadir \keystore\aaa 66 Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://.datadir \keystore\zzz 67 `) 68 } else { 69 geth.Expect(` 70 Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://.datadir/keystore/utc--2016-03-22t12-57-55.920751759z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 71 Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://.datadir/keystore/aaa 72 Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://.datadir/keystore/zzz 73 `) 74 } 75 } 76 77 func TestAccountNew(t *testing.T) { 78 geth := runGeth(t, "account", "new", "--lightkdf") 79 defer geth.ExpectExit() 80 geth.Expect(` 81 Your new account is locked with a password. Please give a password. Do not forget this password. 82 !! Unsupported terminal, password will be echoed. 83 Passphrase: {{.InputLine "foobar"}} 84 Repeat passphrase: {{.InputLine "foobar"}} 85 `) 86 geth.ExpectRegexp(`Address: \{[0-9a-f]{40}\}\n`) 87 } 88 89 func TestAccountNewBadRepeat(t *testing.T) { 90 geth := runGeth(t, "account", "new", "--lightkdf") 91 defer geth.ExpectExit() 92 geth.Expect(` 93 Your new account is locked with a password. Please give a password. Do not forget this password. 94 !! Unsupported terminal, password will be echoed. 95 Passphrase: {{.InputLine "something"}} 96 Repeat passphrase: {{.InputLine "something else"}} 97 Fatal: Passphrases do not match 98 `) 99 } 100 101 func TestAccountUpdate(t *testing.T) { 102 datadir := tmpDatadirWithKeystore(t) 103 geth := runGeth(t, "account", "update", 104 "--datadir", datadir, "--lightkdf", 105 "f466859ead1932d743d622cb74fc058882e8648a") 106 defer geth.ExpectExit() 107 geth.Expect(` 108 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 109 !! Unsupported terminal, password will be echoed. 110 Passphrase: {{.InputLine "foobar"}} 111 Please give a new password. Do not forget this password. 112 Passphrase: {{.InputLine "foobar2"}} 113 Repeat passphrase: {{.InputLine "foobar2"}} 114 `) 115 } 116 117 func TestWalletImport(t *testing.T) { 118 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 119 defer geth.ExpectExit() 120 geth.Expect(` 121 !! Unsupported terminal, password will be echoed. 122 Passphrase: {{.InputLine "foo"}} 123 Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f} 124 `) 125 126 files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore")) 127 if len(files) != 1 { 128 t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err) 129 } 130 } 131 132 func TestWalletImportBadPassword(t *testing.T) { 133 geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json") 134 defer geth.ExpectExit() 135 geth.Expect(` 136 !! Unsupported terminal, password will be echoed. 137 Passphrase: {{.InputLine "wrong"}} 138 Fatal: could not decrypt key with given passphrase 139 `) 140 } 141 142 func TestUnlockFlag(t *testing.T) { 143 datadir := tmpDatadirWithKeystore(t) 144 geth := runGeth(t, 145 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 146 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 147 "js", "testdata/empty.js") 148 geth.Expect(` 149 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 150 !! Unsupported terminal, password will be echoed. 151 Passphrase: {{.InputLine "foobar"}} 152 `) 153 geth.ExpectExit() 154 155 wantMessages := []string{ 156 "Unlocked account", 157 "=0xf466859eAD1932D743d622CB74FC058882E8648A", 158 } 159 for _, m := range wantMessages { 160 if !strings.Contains(geth.StderrText(), m) { 161 t.Errorf("stderr text does not contain %q", m) 162 } 163 } 164 } 165 166 func TestUnlockFlagWrongPassword(t *testing.T) { 167 datadir := tmpDatadirWithKeystore(t) 168 geth := runGeth(t, 169 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 170 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 171 defer geth.ExpectExit() 172 geth.Expect(` 173 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 174 !! Unsupported terminal, password will be echoed. 175 Passphrase: {{.InputLine "wrong1"}} 176 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3 177 Passphrase: {{.InputLine "wrong2"}} 178 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3 179 Passphrase: {{.InputLine "wrong3"}} 180 Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase) 181 `) 182 } 183 184 // 185 func TestUnlockFlagMultiIndex(t *testing.T) { 186 datadir := tmpDatadirWithKeystore(t) 187 geth := runGeth(t, 188 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 189 "--unlock", "0,2", 190 "js", "testdata/empty.js") 191 geth.Expect(` 192 Unlocking account 0 | Attempt 1/3 193 !! Unsupported terminal, password will be echoed. 194 Passphrase: {{.InputLine "foobar"}} 195 Unlocking account 2 | Attempt 1/3 196 Passphrase: {{.InputLine "foobar"}} 197 `) 198 geth.ExpectExit() 199 200 wantMessages := []string{ 201 "Unlocked account", 202 "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 203 "=0x289d485D9771714CCe91D3393D764E1311907ACc", 204 } 205 for _, m := range wantMessages { 206 if !strings.Contains(geth.StderrText(), m) { 207 t.Errorf("stderr text does not contain %q", m) 208 } 209 } 210 } 211 212 func TestUnlockFlagPasswordFile(t *testing.T) { 213 datadir := tmpDatadirWithKeystore(t) 214 geth := runGeth(t, 215 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 216 "--password", "testdata/passwords.txt", "--unlock", "0,2", 217 "js", "testdata/empty.js") 218 geth.ExpectExit() 219 220 wantMessages := []string{ 221 "Unlocked account", 222 "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8", 223 "=0x289d485D9771714CCe91D3393D764E1311907ACc", 224 } 225 for _, m := range wantMessages { 226 if !strings.Contains(geth.StderrText(), m) { 227 t.Errorf("stderr text does not contain %q", m) 228 } 229 } 230 } 231 232 func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) { 233 datadir := tmpDatadirWithKeystore(t) 234 geth := runGeth(t, 235 "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 236 "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2") 237 defer geth.ExpectExit() 238 geth.Expect(` 239 Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase) 240 `) 241 } 242 243 func TestUnlockFlagAmbiguous(t *testing.T) { 244 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 245 geth := runGeth(t, 246 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 247 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", 248 "js", "testdata/empty.js") 249 defer geth.ExpectExit() 250 251 //Helper对于Expect模板,返回绝对密钥存储路径。 252 geth.SetTemplateFunc("keypath", func(file string) string { 253 abs, _ := filepath.Abs(filepath.Join(store, file)) 254 return abs 255 }) 256 geth.Expect(` 257 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 258 !! Unsupported terminal, password will be echoed. 259 Passphrase: {{.InputLine "foobar"}} 260 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 261 keystore:// 262 keystore:// 263 Testing your passphrase against all of them... 264 Your passphrase unlocked keystore://{{关键路径“1”} } 265 In order to avoid this warning, you need to remove the following duplicate key files: 266 keystore://{{关键路径“2”} } 267 `) 268 geth.ExpectExit() 269 270 wantMessages := []string{ 271 "Unlocked account", 272 "=0xf466859eAD1932D743d622CB74FC058882E8648A", 273 } 274 for _, m := range wantMessages { 275 if !strings.Contains(geth.StderrText(), m) { 276 t.Errorf("stderr text does not contain %q", m) 277 } 278 } 279 } 280 281 func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) { 282 store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes") 283 geth := runGeth(t, 284 "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", 285 "--unlock", "f466859ead1932d743d622cb74fc058882e8648a") 286 defer geth.ExpectExit() 287 288 //Helper对于Expect模板,返回绝对密钥存储路径。 289 geth.SetTemplateFunc("keypath", func(file string) string { 290 abs, _ := filepath.Abs(filepath.Join(store, file)) 291 return abs 292 }) 293 geth.Expect(` 294 Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 295 !! Unsupported terminal, password will be echoed. 296 Passphrase: {{.InputLine "wrong"}} 297 Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a: 298 keystore://{{关键路径“1”} } 299 keystore://{{关键路径“2”} } 300 Testing your passphrase against all of them... 301 Fatal: None of the listed files could be unlocked. 302 `) 303 geth.ExpectExit() 304 }