accountcmd_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "io/ioutil"
  19. "path/filepath"
  20. "runtime"
  21. "strings"
  22. "testing"
  23. "github.com/cespare/cp"
  24. )
  25. // These tests are 'smoke tests' for the account related
  26. // subcommands and flags.
  27. //
  28. // For most tests, the test files from package accounts
  29. // are copied into a temporary keystore directory.
  30. func tmpDatadirWithKeystore(t *testing.T) string {
  31. datadir := tmpdir(t)
  32. keystore := filepath.Join(datadir, "keystore")
  33. source := filepath.Join("..", "..", "accounts", "keystore", "testdata", "keystore")
  34. if err := cp.CopyAll(keystore, source); err != nil {
  35. t.Fatal(err)
  36. }
  37. return datadir
  38. }
  39. func TestAccountListEmpty(t *testing.T) {
  40. geth := runGeth(t, "account", "list")
  41. geth.ExpectExit()
  42. }
  43. func TestAccountList(t *testing.T) {
  44. datadir := tmpDatadirWithKeystore(t)
  45. geth := runGeth(t, "account", "list", "--datadir", datadir)
  46. defer geth.ExpectExit()
  47. if runtime.GOOS == "windows" {
  48. geth.Expect(`
  49. Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
  50. Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}\keystore\aaa
  51. Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}\keystore\zzz
  52. `)
  53. } else {
  54. geth.Expect(`
  55. Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} keystore://{{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
  56. Account #1: {f466859ead1932d743d622cb74fc058882e8648a} keystore://{{.Datadir}}/keystore/aaa
  57. Account #2: {289d485d9771714cce91d3393d764e1311907acc} keystore://{{.Datadir}}/keystore/zzz
  58. `)
  59. }
  60. }
  61. func TestAccountNew(t *testing.T) {
  62. geth := runGeth(t, "account", "new", "--lightkdf")
  63. defer geth.ExpectExit()
  64. geth.Expect(`
  65. Your new account is locked with a password. Please give a password. Do not forget this password.
  66. !! Unsupported terminal, password will be echoed.
  67. Passphrase: {{.InputLine "foobar"}}
  68. Repeat passphrase: {{.InputLine "foobar"}}
  69. Your new key was generated
  70. `)
  71. geth.ExpectRegexp(`
  72. Public address of the key: 0x[0-9a-fA-F]{40}
  73. Path of the secret key file: .*UTC--.+--[0-9a-f]{40}
  74. - You can share your public address with anyone. Others need it to interact with you.
  75. - You must NEVER share the secret key with anyone! The key controls access to your funds!
  76. - You must BACKUP your key file! Without the key, it's impossible to access account funds!
  77. - You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
  78. `)
  79. }
  80. func TestAccountNewBadRepeat(t *testing.T) {
  81. geth := runGeth(t, "account", "new", "--lightkdf")
  82. defer geth.ExpectExit()
  83. geth.Expect(`
  84. Your new account is locked with a password. Please give a password. Do not forget this password.
  85. !! Unsupported terminal, password will be echoed.
  86. Passphrase: {{.InputLine "something"}}
  87. Repeat passphrase: {{.InputLine "something else"}}
  88. Fatal: Passphrases do not match
  89. `)
  90. }
  91. func TestAccountUpdate(t *testing.T) {
  92. datadir := tmpDatadirWithKeystore(t)
  93. geth := runGeth(t, "account", "update",
  94. "--datadir", datadir, "--lightkdf",
  95. "f466859ead1932d743d622cb74fc058882e8648a")
  96. defer geth.ExpectExit()
  97. geth.Expect(`
  98. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  99. !! Unsupported terminal, password will be echoed.
  100. Passphrase: {{.InputLine "foobar"}}
  101. Please give a new password. Do not forget this password.
  102. Passphrase: {{.InputLine "foobar2"}}
  103. Repeat passphrase: {{.InputLine "foobar2"}}
  104. `)
  105. }
  106. func TestWalletImport(t *testing.T) {
  107. geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
  108. defer geth.ExpectExit()
  109. geth.Expect(`
  110. !! Unsupported terminal, password will be echoed.
  111. Passphrase: {{.InputLine "foo"}}
  112. Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f}
  113. `)
  114. files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore"))
  115. if len(files) != 1 {
  116. t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err)
  117. }
  118. }
  119. func TestWalletImportBadPassword(t *testing.T) {
  120. geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
  121. defer geth.ExpectExit()
  122. geth.Expect(`
  123. !! Unsupported terminal, password will be echoed.
  124. Passphrase: {{.InputLine "wrong"}}
  125. Fatal: could not decrypt key with given passphrase
  126. `)
  127. }
  128. func TestUnlockFlag(t *testing.T) {
  129. datadir := tmpDatadirWithKeystore(t)
  130. geth := runGeth(t,
  131. "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  132. "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
  133. "js", "testdata/empty.js")
  134. geth.Expect(`
  135. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  136. !! Unsupported terminal, password will be echoed.
  137. Passphrase: {{.InputLine "foobar"}}
  138. `)
  139. geth.ExpectExit()
  140. wantMessages := []string{
  141. "Unlocked account",
  142. "=0xf466859eAD1932D743d622CB74FC058882E8648A",
  143. }
  144. for _, m := range wantMessages {
  145. if !strings.Contains(geth.StderrText(), m) {
  146. t.Errorf("stderr text does not contain %q", m)
  147. }
  148. }
  149. }
  150. func TestUnlockFlagWrongPassword(t *testing.T) {
  151. datadir := tmpDatadirWithKeystore(t)
  152. geth := runGeth(t,
  153. "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  154. "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
  155. defer geth.ExpectExit()
  156. geth.Expect(`
  157. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  158. !! Unsupported terminal, password will be echoed.
  159. Passphrase: {{.InputLine "wrong1"}}
  160. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
  161. Passphrase: {{.InputLine "wrong2"}}
  162. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
  163. Passphrase: {{.InputLine "wrong3"}}
  164. Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase)
  165. `)
  166. }
  167. // https://github.com/ethereum/go-ethereum/issues/1785
  168. func TestUnlockFlagMultiIndex(t *testing.T) {
  169. datadir := tmpDatadirWithKeystore(t)
  170. geth := runGeth(t,
  171. "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  172. "--unlock", "0,2",
  173. "js", "testdata/empty.js")
  174. geth.Expect(`
  175. Unlocking account 0 | Attempt 1/3
  176. !! Unsupported terminal, password will be echoed.
  177. Passphrase: {{.InputLine "foobar"}}
  178. Unlocking account 2 | Attempt 1/3
  179. Passphrase: {{.InputLine "foobar"}}
  180. `)
  181. geth.ExpectExit()
  182. wantMessages := []string{
  183. "Unlocked account",
  184. "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
  185. "=0x289d485D9771714CCe91D3393D764E1311907ACc",
  186. }
  187. for _, m := range wantMessages {
  188. if !strings.Contains(geth.StderrText(), m) {
  189. t.Errorf("stderr text does not contain %q", m)
  190. }
  191. }
  192. }
  193. func TestUnlockFlagPasswordFile(t *testing.T) {
  194. datadir := tmpDatadirWithKeystore(t)
  195. geth := runGeth(t,
  196. "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  197. "--password", "testdata/passwords.txt", "--unlock", "0,2",
  198. "js", "testdata/empty.js")
  199. geth.ExpectExit()
  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. func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
  212. datadir := tmpDatadirWithKeystore(t)
  213. geth := runGeth(t,
  214. "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  215. "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
  216. defer geth.ExpectExit()
  217. geth.Expect(`
  218. Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase)
  219. `)
  220. }
  221. func TestUnlockFlagAmbiguous(t *testing.T) {
  222. store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
  223. geth := runGeth(t,
  224. "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  225. "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
  226. "js", "testdata/empty.js")
  227. defer geth.ExpectExit()
  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. wantMessages := []string{
  247. "Unlocked account",
  248. "=0xf466859eAD1932D743d622CB74FC058882E8648A",
  249. }
  250. for _, m := range wantMessages {
  251. if !strings.Contains(geth.StderrText(), m) {
  252. t.Errorf("stderr text does not contain %q", m)
  253. }
  254. }
  255. }
  256. func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
  257. store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
  258. geth := runGeth(t,
  259. "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  260. "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
  261. defer geth.ExpectExit()
  262. // Helper for the expect template, returns absolute keystore path.
  263. geth.SetTemplateFunc("keypath", func(file string) string {
  264. abs, _ := filepath.Abs(filepath.Join(store, file))
  265. return abs
  266. })
  267. geth.Expect(`
  268. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  269. !! Unsupported terminal, password will be echoed.
  270. Passphrase: {{.InputLine "wrong"}}
  271. Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
  272. keystore://{{keypath "1"}}
  273. keystore://{{keypath "2"}}
  274. Testing your passphrase against all of them...
  275. Fatal: None of the listed files could be unlocked.
  276. `)
  277. geth.ExpectExit()
  278. }