accountcmd_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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", "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")
  41. geth.expectExit()
  42. }
  43. func TestAccountList(t *testing.T) {
  44. datadir := tmpDatadirWithKeystore(t)
  45. geth := runGeth(t, "--datadir", datadir, "account")
  46. defer geth.expectExit()
  47. if runtime.GOOS == "windows" {
  48. geth.expect(`
  49. Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} {{.Datadir}}\keystore\UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
  50. Account #1: {f466859ead1932d743d622cb74fc058882e8648a} {{.Datadir}}\keystore\aaa
  51. Account #2: {289d485d9771714cce91d3393d764e1311907acc} {{.Datadir}}\keystore\zzz
  52. `)
  53. } else {
  54. geth.expect(`
  55. Account #0: {7ef5a6135f1fd6a02593eedc869c6d41d934aef8} {{.Datadir}}/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
  56. Account #1: {f466859ead1932d743d622cb74fc058882e8648a} {{.Datadir}}/keystore/aaa
  57. Account #2: {289d485d9771714cce91d3393d764e1311907acc} {{.Datadir}}/keystore/zzz
  58. `)
  59. }
  60. }
  61. func TestAccountNew(t *testing.T) {
  62. geth := runGeth(t, "--lightkdf", "account", "new")
  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, "--lightkdf", "account", "new")
  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,
  86. "--datadir", datadir, "--lightkdf",
  87. "account", "update", "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, "--lightkdf", "wallet", "import", "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, "--lightkdf", "wallet", "import", "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", "--dev",
  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 f466859ead1932d743d622cb74fc058882e8648a",
  134. }
  135. for _, m := range wantMessages {
  136. if strings.Index(geth.stderrText(), m) == -1 {
  137. t.Errorf("stderr text does not contain %q", m)
  138. }
  139. }
  140. }
  141. func TestUnlockFlagWrongPassword(t *testing.T) {
  142. datadir := tmpDatadirWithKeystore(t)
  143. geth := runGeth(t,
  144. "--datadir", datadir, "--nat", "none", "--nodiscover", "--dev",
  145. "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
  146. defer geth.expectExit()
  147. geth.expect(`
  148. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  149. !! Unsupported terminal, password will be echoed.
  150. Passphrase: {{.InputLine "wrong1"}}
  151. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 2/3
  152. Passphrase: {{.InputLine "wrong2"}}
  153. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 3/3
  154. Passphrase: {{.InputLine "wrong3"}}
  155. Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could not decrypt key with given passphrase)
  156. `)
  157. }
  158. // https://github.com/ethereum/go-ethereum/issues/1785
  159. func TestUnlockFlagMultiIndex(t *testing.T) {
  160. datadir := tmpDatadirWithKeystore(t)
  161. geth := runGeth(t,
  162. "--datadir", datadir, "--nat", "none", "--nodiscover", "--dev",
  163. "--unlock", "0,2",
  164. "js", "testdata/empty.js")
  165. geth.expect(`
  166. Unlocking account 0 | Attempt 1/3
  167. !! Unsupported terminal, password will be echoed.
  168. Passphrase: {{.InputLine "foobar"}}
  169. Unlocking account 2 | Attempt 1/3
  170. Passphrase: {{.InputLine "foobar"}}
  171. `)
  172. geth.expectExit()
  173. wantMessages := []string{
  174. "Unlocked account 7ef5a6135f1fd6a02593eedc869c6d41d934aef8",
  175. "Unlocked account 289d485d9771714cce91d3393d764e1311907acc",
  176. }
  177. for _, m := range wantMessages {
  178. if strings.Index(geth.stderrText(), m) == -1 {
  179. t.Errorf("stderr text does not contain %q", m)
  180. }
  181. }
  182. }
  183. func TestUnlockFlagPasswordFile(t *testing.T) {
  184. datadir := tmpDatadirWithKeystore(t)
  185. geth := runGeth(t,
  186. "--datadir", datadir, "--nat", "none", "--nodiscover", "--dev",
  187. "--password", "testdata/passwords.txt", "--unlock", "0,2",
  188. "js", "testdata/empty.js")
  189. geth.expectExit()
  190. wantMessages := []string{
  191. "Unlocked account 7ef5a6135f1fd6a02593eedc869c6d41d934aef8",
  192. "Unlocked account 289d485d9771714cce91d3393d764e1311907acc",
  193. }
  194. for _, m := range wantMessages {
  195. if strings.Index(geth.stderrText(), m) == -1 {
  196. t.Errorf("stderr text does not contain %q", m)
  197. }
  198. }
  199. }
  200. func TestUnlockFlagPasswordFileWrongPassword(t *testing.T) {
  201. datadir := tmpDatadirWithKeystore(t)
  202. geth := runGeth(t,
  203. "--datadir", datadir, "--nat", "none", "--nodiscover", "--dev",
  204. "--password", "testdata/wrong-passwords.txt", "--unlock", "0,2")
  205. defer geth.expectExit()
  206. geth.expect(`
  207. Fatal: Failed to unlock account 0 (could not decrypt key with given passphrase)
  208. `)
  209. }
  210. func TestUnlockFlagAmbiguous(t *testing.T) {
  211. store := filepath.Join("..", "..", "accounts", "testdata", "dupes")
  212. geth := runGeth(t,
  213. "--keystore", store, "--nat", "none", "--nodiscover", "--dev",
  214. "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
  215. "js", "testdata/empty.js")
  216. defer geth.expectExit()
  217. // Helper for the expect template, returns absolute keystore path.
  218. geth.setTemplateFunc("keypath", func(file string) string {
  219. abs, _ := filepath.Abs(filepath.Join(store, file))
  220. return abs
  221. })
  222. geth.expect(`
  223. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  224. !! Unsupported terminal, password will be echoed.
  225. Passphrase: {{.InputLine "foobar"}}
  226. Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
  227. {{keypath "1"}}
  228. {{keypath "2"}}
  229. Testing your passphrase against all of them...
  230. Your passphrase unlocked {{keypath "1"}}
  231. In order to avoid this warning, you need to remove the following duplicate key files:
  232. {{keypath "2"}}
  233. `)
  234. geth.expectExit()
  235. wantMessages := []string{
  236. "Unlocked account f466859ead1932d743d622cb74fc058882e8648a",
  237. }
  238. for _, m := range wantMessages {
  239. if strings.Index(geth.stderrText(), m) == -1 {
  240. t.Errorf("stderr text does not contain %q", m)
  241. }
  242. }
  243. }
  244. func TestUnlockFlagAmbiguousWrongPassword(t *testing.T) {
  245. store := filepath.Join("..", "..", "accounts", "testdata", "dupes")
  246. geth := runGeth(t,
  247. "--keystore", store, "--nat", "none", "--nodiscover", "--dev",
  248. "--unlock", "f466859ead1932d743d622cb74fc058882e8648a")
  249. defer geth.expectExit()
  250. // Helper for the expect template, returns absolute keystore path.
  251. geth.setTemplateFunc("keypath", func(file string) string {
  252. abs, _ := filepath.Abs(filepath.Join(store, file))
  253. return abs
  254. })
  255. geth.expect(`
  256. Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
  257. !! Unsupported terminal, password will be echoed.
  258. Passphrase: {{.InputLine "wrong"}}
  259. Multiple key files exist for address f466859ead1932d743d622cb74fc058882e8648a:
  260. {{keypath "1"}}
  261. {{keypath "2"}}
  262. Testing your passphrase against all of them...
  263. Fatal: None of the listed files could be unlocked.
  264. `)
  265. geth.expectExit()
  266. }