accountcmd_test.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. `)
  70. geth.ExpectRegexp(`Address: \{[0-9a-f]{40}\}\n`)
  71. }
  72. func TestAccountNewBadRepeat(t *testing.T) {
  73. geth := runGeth(t, "account", "new", "--lightkdf")
  74. defer geth.ExpectExit()
  75. geth.Expect(`
  76. Your new account is locked with a password. Please give a password. Do not forget this password.
  77. !! Unsupported terminal, password will be echoed.
  78. Passphrase: {{.InputLine "something"}}
  79. Repeat passphrase: {{.InputLine "something else"}}
  80. Fatal: Passphrases do not match
  81. `)
  82. }
  83. func TestAccountUpdate(t *testing.T) {
  84. datadir := tmpDatadirWithKeystore(t)
  85. geth := runGeth(t, "account", "update",
  86. "--datadir", datadir, "--lightkdf",
  87. "f466859ead1932d743d622cb74fc058882e8648a")
  88. defer geth.ExpectExit()
  89. geth.Expect(`
  90. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  91. !! Unsupported terminal, password will be echoed.
  92. Passphrase: {{.InputLine "foobar"}}
  93. Please give a new password. Do not forget this password.
  94. Passphrase: {{.InputLine "foobar2"}}
  95. Repeat passphrase: {{.InputLine "foobar2"}}
  96. `)
  97. }
  98. func TestWalletImport(t *testing.T) {
  99. geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
  100. defer geth.ExpectExit()
  101. geth.Expect(`
  102. !! Unsupported terminal, password will be echoed.
  103. Passphrase: {{.InputLine "foo"}}
  104. Address: {d4584b5f6229b7be90727b0fc8c6b91bb427821f}
  105. `)
  106. files, err := ioutil.ReadDir(filepath.Join(geth.Datadir, "keystore"))
  107. if len(files) != 1 {
  108. t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err)
  109. }
  110. }
  111. func TestWalletImportBadPassword(t *testing.T) {
  112. geth := runGeth(t, "wallet", "import", "--lightkdf", "testdata/guswallet.json")
  113. defer geth.ExpectExit()
  114. geth.Expect(`
  115. !! Unsupported terminal, password will be echoed.
  116. Passphrase: {{.InputLine "wrong"}}
  117. Fatal: could not decrypt key with given passphrase
  118. `)
  119. }
  120. func TestUnlockFlag(t *testing.T) {
  121. datadir := tmpDatadirWithKeystore(t)
  122. geth := runGeth(t,
  123. "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  124. "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
  125. "js", "testdata/empty.js")
  126. geth.Expect(`
  127. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  128. !! Unsupported terminal, password will be echoed.
  129. Passphrase: {{.InputLine "foobar"}}
  130. `)
  131. geth.ExpectExit()
  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. func TestUnlockFlagWrongPassword(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. defer geth.ExpectExit()
  148. geth.Expect(`
  149. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  150. !! Unsupported terminal, password will be echoed.
  151. Passphrase: {{.InputLine "wrong1"}}
  152. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
  153. Passphrase: {{.InputLine "wrong2"}}
  154. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
  155. Passphrase: {{.InputLine "wrong3"}}
  156. Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase)
  157. `)
  158. }
  159. // https://github.com/ethereum/go-ethereum/issues/1785
  160. func TestUnlockFlagMultiIndex(t *testing.T) {
  161. datadir := tmpDatadirWithKeystore(t)
  162. geth := runGeth(t,
  163. "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  164. "--unlock", "0,2",
  165. "js", "testdata/empty.js")
  166. geth.Expect(`
  167. Unlocking account 0 | Attempt 1/3
  168. !! Unsupported terminal, password will be echoed.
  169. Passphrase: {{.InputLine "foobar"}}
  170. Unlocking account 2 | Attempt 1/3
  171. Passphrase: {{.InputLine "foobar"}}
  172. `)
  173. geth.ExpectExit()
  174. wantMessages := []string{
  175. "Unlocked account",
  176. "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
  177. "=0x289d485D9771714CCe91D3393D764E1311907ACc",
  178. }
  179. for _, m := range wantMessages {
  180. if !strings.Contains(geth.StderrText(), m) {
  181. t.Errorf("stderr text does not contain %q", m)
  182. }
  183. }
  184. }
  185. func TestUnlockFlagPasswordFile(t *testing.T) {
  186. datadir := tmpDatadirWithKeystore(t)
  187. geth := runGeth(t,
  188. "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  189. "--password", "testdata/passwords.txt", "--unlock", "0,2",
  190. "js", "testdata/empty.js")
  191. geth.ExpectExit()
  192. wantMessages := []string{
  193. "Unlocked account",
  194. "=0x7EF5A6135f1FD6a02593eEdC869c6D41D934aef8",
  195. "=0x289d485D9771714CCe91D3393D764E1311907ACc",
  196. }
  197. for _, m := range wantMessages {
  198. if !strings.Contains(geth.StderrText(), m) {
  199. t.Errorf("stderr text does not contain %q", m)
  200. }
  201. }
  202. }
  203. func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
  204. datadir := tmpDatadirWithKeystore(t)
  205. geth := runGeth(t,
  206. "--datadir", datadir, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  207. "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
  208. defer geth.ExpectExit()
  209. geth.Expect(`
  210. Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase)
  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. // Helper for the expect template, returns absolute keystore path.
  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. wantMessages := []string{
  239. "Unlocked account",
  240. "=0xf466859eAD1932D743d622CB74FC058882E8648A",
  241. }
  242. for _, m := range wantMessages {
  243. if !strings.Contains(geth.StderrText(), m) {
  244. t.Errorf("stderr text does not contain %q", m)
  245. }
  246. }
  247. }
  248. func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
  249. store := filepath.Join("..", "..", "accounts", "keystore", "testdata", "dupes")
  250. geth := runGeth(t,
  251. "--keystore", store, "--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0",
  252. "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
  253. defer geth.ExpectExit()
  254. // Helper for the expect template, returns absolute keystore path.
  255. geth.SetTemplateFunc("keypath", func(file string) string {
  256. abs, _ := filepath.Abs(filepath.Join(store, file))
  257. return abs
  258. })
  259. geth.Expect(`
  260. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  261. !! Unsupported terminal, password will be echoed.
  262. Passphrase: {{.InputLine "wrong"}}
  263. Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
  264. keystore://{{keypath "1"}}
  265. keystore://{{keypath "2"}}
  266. Testing your passphrase against all of them...
  267. Fatal: None of the listed files could be unlocked.
  268. `)
  269. geth.ExpectExit()
  270. }