Allow newline after account

This commit is contained in:
Adam Schmideg 2020-03-04 12:56:31 +01:00
parent a66d88ea2c
commit fe0bc499bb
2 changed files with 30 additions and 34 deletions

View file

@ -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) { func TestAccountImport(t *testing.T) {
dir := tmpdir(t) bytes64 := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
keyfile := filepath.Join(dir, "key.prv") success := `Address: {[0-9a-f]{40}}`
key := hexadecimal(64) failure := `Fatal: Failed to load the private key: expected 64 bytes, got \d+`
if err := ioutil.WriteFile(keyfile, []byte(key), 0644); err != nil { keyToMsg := make(map[string]string)
t.Error(err) 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) dir := tmpdir(t)
keyfile := filepath.Join(dir, "key.prv") keyfile := filepath.Join(dir, "key.prv")
key := hexadecimal(40)
if err := ioutil.WriteFile(keyfile, []byte(key), 0644); err != nil { if err := ioutil.WriteFile(keyfile, []byte(key), 0644); err != nil {
t.Error(err) 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() defer geth.ExpectExit()
geth.Expect(` geth.ExpectRegexp(expectedRegexp)
Fatal: Failed to load the private key: expected 64 bytes, got 40
`)
} }
func TestAccountNewBadRepeat(t *testing.T) { func TestAccountNewBadRepeat(t *testing.T) {

View file

@ -170,15 +170,23 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
return nil, err return nil, err
} }
size := stat.Size() 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) return nil, fmt.Errorf("expected 64 bytes, got %v", size)
} }
buf, err := ioutil.ReadFile(file) buf, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
return nil, err 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 { if err != nil {
return nil, err return nil, err
} }