@ -139,39 +139,82 @@ func TestNewContractAddress(t *testing.T) {
checkAddr ( t , common . HexToAddress ( "c9ddedf451bc62ce88bf9292afb13df35b670699" ) , caddr2 )
checkAddr ( t , common . HexToAddress ( "c9ddedf451bc62ce88bf9292afb13df35b670699" ) , caddr2 )
}
}
func TestLoadECDSAFile ( t * testing . T ) {
func TestLoadECDSA ( t * testing . T ) {
keyBytes := common . FromHex ( testPrivHex )
tests := [ ] struct {
fileName0 := "test_key0"
input string
fileName1 := "test_key1"
err string
checkKey := func ( k * ecdsa . PrivateKey ) {
} {
checkAddr ( t , PubkeyToAddress ( k . PublicKey ) , common . HexToAddress ( testAddrHex ) )
// good
loadedKeyBytes := FromECDSA ( k )
{ input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" } ,
if ! bytes . Equal ( loadedKeyBytes , keyBytes ) {
{ input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n" } ,
t . Fatalf ( "private key mismatch: want: %x have: %x" , keyBytes , loadedKeyBytes )
{ input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n\r" } ,
}
{ input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\r\n" } ,
{ input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n\n" } ,
{ input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n\r" } ,
// bad
{
input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde" ,
err : "key file too short, want 64 hex characters" ,
} ,
{
input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde\n" ,
err : "key file too short, want 64 hex characters" ,
} ,
{
input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdeX" ,
err : "invalid hex character 'X' in private key" ,
} ,
{
input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdefX" ,
err : "invalid character 'X' at end of key file" ,
} ,
{
input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n\n\n" ,
err : "key file too long, want 64 hex characters" ,
} ,
}
}
ioutil . WriteFile ( fileName0 , [ ] byte ( testPrivHex ) , 0600 )
for _ , test := range tests {
defer os . Remove ( fileName0 )
f , err := ioutil . TempFile ( "" , "loadecdsa_test.*.txt" )
if err != nil {
t . Fatal ( err )
}
filename := f . Name ( )
f . WriteString ( test . input )
f . Close ( )
_ , err = LoadECDSA ( filename )
switch {
case err != nil && test . err == "" :
t . Fatalf ( "unexpected error for input %q:\n %v" , test . input , err )
case err != nil && err . Error ( ) != test . err :
t . Fatalf ( "wrong error for input %q:\n %v" , test . input , err )
case err == nil && test . err != "" :
t . Fatalf ( "LoadECDSA did not return error for input %q" , test . input )
}
}
}
key0 , err := LoadECDSA ( fileName0 )
func TestSaveECDSA ( t * testing . T ) {
f , err := ioutil . TempFile ( "" , "saveecdsa_test.*.txt" )
if err != nil {
if err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
checkKey ( key0 )
file := f . Name ( )
f . Close ( )
defer os . Remove ( file )
// again, this time with SaveECDSA instead of manual save:
key , _ := HexToECDSA ( testPrivHex )
err = SaveECDSA ( fileName1 , key0 )
if err := SaveECDSA ( file , key ) ; err != nil {
if err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
defer os . Remove ( fileName1 )
loaded , err := LoadECDSA ( file )
key1 , err := LoadECDSA ( fileName1 )
if err != nil {
if err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
checkKey ( key1 )
if ! reflect . DeepEqual ( key , loaded ) {
t . Fatal ( "loaded key not equal to saved key" )
}
}
}
func TestValidateSignatureValues ( t * testing . T ) {
func TestValidateSignatureValues ( t * testing . T ) {