diff --git a/cmd/geth/accountcmd_test.go b/cmd/geth/accountcmd_test.go index dd40346728..7f55ee0329 100644 --- a/cmd/geth/accountcmd_test.go +++ b/cmd/geth/accountcmd_test.go @@ -88,48 +88,36 @@ Path of the secret key file: .*UTC--.+--[0-9a-f]{40} `) } -func hexadecimal(count int) string { - chars := "0123456789abcdef" - var sb strings.Builder - for i := 0; i < count; i++ { - c := string(chars[i%len(chars)]) - sb.WriteString(c) - } - return sb.String() -} - func TestAccountImport(t *testing.T) { - dir := tmpdir(t) - keyfile := filepath.Join(dir, "key.prv") - key := hexadecimal(64) - if err := ioutil.WriteFile(keyfile, []byte(key), 0644); err != nil { - t.Error(err) + bytes64 := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + success := `Address: {[0-9a-f]{40}}` + failure := `Fatal: Failed to load the private key: expected 64 bytes, got \d+` + keyToMsg := make(map[string]string) + keyToMsg[bytes64] = success + keyToMsg[bytes64[:40]] = failure + keyToMsg[bytes64+"\n"] = success + keyToMsg[bytes64+"\r\n"] = success + keyToMsg[bytes64+"1"] = failure + keyToMsg[bytes64+"x"] = failure + keyToMsg[bytes64+bytes64] = failure + for key, msg := range keyToMsg { + importAccountWithExpect(t, key, msg) } - geth := runGeth(t, "account", "import", keyfile) - defer geth.ExpectExit() - geth.Expect(` -Your new account is locked with a password. Please give a password. Do not forget this password. -!! Unsupported terminal, password will be echoed. -Password: {{.InputLine "foobar"}} -Repeat password: {{.InputLine "foobar"}} -`) - geth.ExpectRegexp(` -Address: {[0-9a-f]{40}} -`) } -func TestAccountImportTooShort(t *testing.T) { +func importAccountWithExpect(t *testing.T, key string, expectedRegexp string) { dir := tmpdir(t) keyfile := filepath.Join(dir, "key.prv") - key := hexadecimal(40) if err := ioutil.WriteFile(keyfile, []byte(key), 0644); err != nil { t.Error(err) } - geth := runGeth(t, "account", "import", keyfile) + passwordFile := filepath.Join(dir, "password.txt") + if err := ioutil.WriteFile(passwordFile, []byte("foobar"), 0644); err != nil { + t.Error(err) + } + geth := runGeth(t, "account", "import", keyfile, "-password", passwordFile) defer geth.ExpectExit() - geth.Expect(` -Fatal: Failed to load the private key: expected 64 bytes, got 40 -`) + geth.ExpectRegexp(expectedRegexp) } func TestAccountNewBadRepeat(t *testing.T) { diff --git a/crypto/crypto.go b/crypto/crypto.go index de5b9b0712..ac8affbde2 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -170,15 +170,23 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) { return nil, err } size := stat.Size() - if size != 64 { + // Allow two extra chars for possible line ending to be checked later + if size < 64 || size > 66 { return nil, fmt.Errorf("expected 64 bytes, got %v", size) } buf, err := ioutil.ReadFile(file) if err != nil { return nil, err } + // Check line ending + maybeLineEnding := buf[64:] + for _, ch := range maybeLineEnding { + if ch != '\n' && ch != '\r' { + return nil, fmt.Errorf("expected 64 bytes, got %v", size) + } + } - key, err := hex.DecodeString(string(buf)) + key, err := hex.DecodeString(string(buf[:64])) if err != nil { return nil, err }