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