github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/common/types_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package common
    13  
    14  import (
    15  	"encoding/json"
    16  	"math/big"
    17  	"strings"
    18  	"testing"
    19  )
    20  
    21  func TestBytesConversion(t *testing.T) {
    22  	bytes := []byte{5}
    23  	hash := BytesToHash(bytes)
    24  
    25  	var exp Hash
    26  	exp[31] = 5
    27  
    28  	if hash != exp {
    29  		t.Errorf("expected %x got %x", exp, hash)
    30  	}
    31  }
    32  
    33  func TestIsHexAddress(t *testing.T) {
    34  	tests := []struct {
    35  		str string
    36  		exp bool
    37  	}{
    38  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
    39  		{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
    40  		{"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
    41  		{"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
    42  		{"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
    43  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
    44  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
    45  		{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false},
    46  		{"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
    47  	}
    48  
    49  	for _, test := range tests {
    50  		if result := IsHexAddress(test.str); result != test.exp {
    51  			t.Errorf("IsHexAddress(%s) == %v; expected %v",
    52  				test.str, result, test.exp)
    53  		}
    54  	}
    55  }
    56  
    57  func TestHashJsonValidation(t *testing.T) {
    58  	var tests = []struct {
    59  		Prefix string
    60  		Size   int
    61  		Error  string
    62  	}{
    63  		{"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"},
    64  		{"0x", 66, "hex string has length 66, want 64 for common.Hash"},
    65  		{"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"},
    66  		{"0x", 0, "hex string has length 0, want 64 for common.Hash"},
    67  		{"0x", 64, ""},
    68  		{"0X", 64, ""},
    69  	}
    70  	for _, test := range tests {
    71  		input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"`
    72  		var v Hash
    73  		err := json.Unmarshal([]byte(input), &v)
    74  		if err == nil {
    75  			if test.Error != "" {
    76  				t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error)
    77  			}
    78  		} else {
    79  			if err.Error() != test.Error {
    80  				t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error)
    81  			}
    82  		}
    83  	}
    84  }
    85  
    86  func TestAddressUnmarshalJSON(t *testing.T) {
    87  	var tests = []struct {
    88  		Input     string
    89  		ShouldErr bool
    90  		Output    *big.Int
    91  	}{
    92  		{"", true, nil},
    93  		{`""`, true, nil},
    94  		{`"0x"`, true, nil},
    95  		{`"0x00"`, true, nil},
    96  		{`"0xG000000000000000000000000000000000000000"`, true, nil},
    97  		{`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)},
    98  		{`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)},
    99  	}
   100  	for i, test := range tests {
   101  		var v Address
   102  		err := json.Unmarshal([]byte(test.Input), &v)
   103  		if err != nil && !test.ShouldErr {
   104  			t.Errorf("test #%d: unexpected error: %v", i, err)
   105  		}
   106  		if err == nil {
   107  			if test.ShouldErr {
   108  				t.Errorf("test #%d: expected error, got none", i)
   109  			}
   110  			if v.Big().Cmp(test.Output) != 0 {
   111  				t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output)
   112  			}
   113  		}
   114  	}
   115  }
   116  
   117  func TestAddressHexChecksum(t *testing.T) {
   118  	var tests = []struct {
   119  		Input  string
   120  		Output string
   121  	}{
   122  		// Test cases
   123  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"},
   124  		{"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"},
   125  		{"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"},
   126  		{"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"},
   127  		// Ensure that non-standard length input values are handled correctly
   128  		{"0xa", "0x000000000000000000000000000000000000000A"},
   129  		{"0x0a", "0x000000000000000000000000000000000000000A"},
   130  		{"0x00a", "0x000000000000000000000000000000000000000A"},
   131  		{"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"},
   132  	}
   133  	for i, test := range tests {
   134  		output := HexToAddress(test.Input).Hex()
   135  		if output != test.Output {
   136  			t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output)
   137  		}
   138  	}
   139  }
   140  
   141  func BenchmarkAddressHex(b *testing.B) {
   142  	testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
   143  	for n := 0; n < b.N; n++ {
   144  		testAddr.Hex()
   145  	}
   146  }