@ -139,39 +139,82 @@ func TestNewContractAddress(t *testing.T) {
checkAddr ( t , common . HexToAddress ( "c9ddedf451bc62ce88bf9292afb13df35b670699" ) , caddr2 )
}
func TestLoadECDSAFile ( t * testing . T ) {
keyBytes := common . FromHex ( testPrivHex )
fileName0 := "test_key0"
fileName1 := "test_key1"
checkKey := func ( k * ecdsa . PrivateKey ) {
checkAddr ( t , PubkeyToAddress ( k . PublicKey ) , common . HexToAddress ( testAddrHex ) )
loadedKeyBytes := FromECDSA ( k )
if ! bytes . Equal ( loadedKeyBytes , keyBytes ) {
t . Fatalf ( "private key mismatch: want: %x have: %x" , keyBytes , loadedKeyBytes )
}
func TestLoadECDSA ( t * testing . T ) {
tests := [ ] struct {
input string
err string
} {
// good
{ input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" } ,
{ input : "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n" } ,
{ 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 )
defer os . Remove ( fileName0 )
for _ , test := range tests {
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 {
t . Fatal ( err )
}
checkKey ( key0 )
file := f . Name ( )
f . Close ( )
defer os . Remove ( file )
// again, this time with SaveECDSA instead of manual save:
err = SaveECDSA ( fileName1 , key0 )
if err != nil {
key , _ := HexToECDSA ( testPrivHex )
if err := SaveECDSA ( file , key ) ; err != nil {
t . Fatal ( err )
}
defer os . Remove ( fileName1 )
key1 , err := LoadECDSA ( fileName1 )
loaded , err := LoadECDSA ( file )
if err != nil {
t . Fatal ( err )
}
checkKey ( key1 )
if ! reflect . DeepEqual ( key , loaded ) {
t . Fatal ( "loaded key not equal to saved key" )
}
}
func TestValidateSignatureValues ( t * testing . T ) {