github.com/xfond/eth-implementation@v1.8.9-0.20180514135602-f6bc65fc6811/common/types_test.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package common
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"math/big"
    23  	"strings"
    24  	"testing"
    25  )
    26  
    27  func TestBytesConversion(t *testing.T) {
    28  	bytes := []byte{5}
    29  	hash := BytesToHash(bytes)
    30  
    31  	var exp Hash
    32  	exp[31] = 5
    33  
    34  	if hash != exp {
    35  		t.Errorf("expected %x got %x", exp, hash)
    36  	}
    37  }
    38  
    39  func TestIsHexAddress(t *testing.T) {
    40  	tests := []struct {
    41  		str string
    42  		exp bool
    43  	}{
    44  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
    45  		{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
    46  		{"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
    47  		{"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
    48  		{"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
    49  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
    50  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
    51  		{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false},
    52  		{"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
    53  	}
    54  
    55  	for _, test := range tests {
    56  		if result := IsHexAddress(test.str); result != test.exp {
    57  			t.Errorf("IsHexAddress(%s) == %v; expected %v",
    58  				test.str, result, test.exp)
    59  		}
    60  	}
    61  }
    62  
    63  func TestHashJsonValidation(t *testing.T) {
    64  	var tests = []struct {
    65  		Prefix string
    66  		Size   int
    67  		Error  string
    68  	}{
    69  		{"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"},
    70  		{"0x", 66, "hex string has length 66, want 64 for common.Hash"},
    71  		{"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"},
    72  		{"0x", 0, "hex string has length 0, want 64 for common.Hash"},
    73  		{"0x", 64, ""},
    74  		{"0X", 64, ""},
    75  	}
    76  	for _, test := range tests {
    77  		input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"`
    78  		var v Hash
    79  		err := json.Unmarshal([]byte(input), &v)
    80  		if err == nil {
    81  			if test.Error != "" {
    82  				t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error)
    83  			}
    84  		} else {
    85  			if err.Error() != test.Error {
    86  				t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error)
    87  			}
    88  		}
    89  	}
    90  }
    91  
    92  func TestAddressUnmarshalJSON(t *testing.T) {
    93  	var tests = []struct {
    94  		Input     string
    95  		ShouldErr bool
    96  		Output    *big.Int
    97  	}{
    98  		{"", true, nil},
    99  		{`""`, true, nil},
   100  		{`"0x"`, true, nil},
   101  		{`"0x00"`, true, nil},
   102  		{`"0xG000000000000000000000000000000000000000"`, true, nil},
   103  		{`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)},
   104  		{`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)},
   105  	}
   106  	for i, test := range tests {
   107  		var v Address
   108  		err := json.Unmarshal([]byte(test.Input), &v)
   109  		if err != nil && !test.ShouldErr {
   110  			t.Errorf("test #%d: unexpected error: %v", i, err)
   111  		}
   112  		if err == nil {
   113  			if test.ShouldErr {
   114  				t.Errorf("test #%d: expected error, got none", i)
   115  			}
   116  			if v.Big().Cmp(test.Output) != 0 {
   117  				t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output)
   118  			}
   119  		}
   120  	}
   121  }
   122  
   123  func TestAddressHexChecksum(t *testing.T) {
   124  	var tests = []struct {
   125  		Input  string
   126  		Output string
   127  	}{
   128  		// Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification
   129  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"},
   130  		{"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"},
   131  		{"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"},
   132  		{"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"},
   133  		// Ensure that non-standard length input values are handled correctly
   134  		{"0xa", "0x000000000000000000000000000000000000000A"},
   135  		{"0x0a", "0x000000000000000000000000000000000000000A"},
   136  		{"0x00a", "0x000000000000000000000000000000000000000A"},
   137  		{"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"},
   138  	}
   139  	for i, test := range tests {
   140  		output := HexToAddress(test.Input).Hex()
   141  		if output != test.Output {
   142  			t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output)
   143  		}
   144  	}
   145  }
   146  
   147  func BenchmarkAddressHex(b *testing.B) {
   148  	testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
   149  	for n := 0; n < b.N; n++ {
   150  		testAddr.Hex()
   151  	}
   152  }
   153  
   154  func TestMixedcaseAccount_Address(t *testing.T) {
   155  
   156  	// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
   157  	// Note: 0X{checksum_addr} is not valid according to spec above
   158  
   159  	var res []struct {
   160  		A     MixedcaseAddress
   161  		Valid bool
   162  	}
   163  	if err := json.Unmarshal([]byte(`[
   164  		{"A" : "0xae967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
   165  		{"A" : "0xAe967917c465db8578ca9024c205720b1a3651A9", "Valid": true},
   166  		{"A" : "0XAe967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
   167  		{"A" : "0x1111111111111111111112222222222223333323", "Valid": true}
   168  		]`), &res); err != nil {
   169  		t.Fatal(err)
   170  	}
   171  
   172  	for _, r := range res {
   173  		if got := r.A.ValidChecksum(); got != r.Valid {
   174  			t.Errorf("Expected checksum %v, got checksum %v, input %v", r.Valid, got, r.A.String())
   175  		}
   176  	}
   177  
   178  	//These should throw exceptions:
   179  	var r2 []MixedcaseAddress
   180  	for _, r := range []string{
   181  		`["0x11111111111111111111122222222222233333"]`,     // Too short
   182  		`["0x111111111111111111111222222222222333332"]`,    // Too short
   183  		`["0x11111111111111111111122222222222233333234"]`,  // Too long
   184  		`["0x111111111111111111111222222222222333332344"]`, // Too long
   185  		`["1111111111111111111112222222222223333323"]`,     // Missing 0x
   186  		`["x1111111111111111111112222222222223333323"]`,    // Missing 0
   187  		`["0xG111111111111111111112222222222223333323"]`,   //Non-hex
   188  	} {
   189  		if err := json.Unmarshal([]byte(r), &r2); err == nil {
   190  			t.Errorf("Expected failure, input %v", r)
   191  		}
   192  
   193  	}
   194  
   195  }