github.com/coltonfike/e2c@v21.1.0+incompatible/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  	"database/sql/driver"
    21  	"encoding/base64"
    22  	"encoding/json"
    23  	"math/big"
    24  	"reflect"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestBytesConversion(t *testing.T) {
    32  	bytes := []byte{5}
    33  	hash := BytesToHash(bytes)
    34  
    35  	var exp Hash
    36  	exp[31] = 5
    37  
    38  	if hash != exp {
    39  		t.Errorf("expected %x got %x", exp, hash)
    40  	}
    41  }
    42  
    43  func TestIsHexAddress(t *testing.T) {
    44  	tests := []struct {
    45  		str string
    46  		exp bool
    47  	}{
    48  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
    49  		{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
    50  		{"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
    51  		{"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
    52  		{"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
    53  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
    54  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
    55  		{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false},
    56  		{"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
    57  	}
    58  
    59  	for _, test := range tests {
    60  		if result := IsHexAddress(test.str); result != test.exp {
    61  			t.Errorf("IsHexAddress(%s) == %v; expected %v",
    62  				test.str, result, test.exp)
    63  		}
    64  	}
    65  }
    66  
    67  func TestHashJsonValidation(t *testing.T) {
    68  	var tests = []struct {
    69  		Prefix string
    70  		Size   int
    71  		Error  string
    72  	}{
    73  		{"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"},
    74  		{"0x", 66, "hex string has length 66, want 64 for common.Hash"},
    75  		{"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"},
    76  		{"0x", 0, "hex string has length 0, want 64 for common.Hash"},
    77  		{"0x", 64, ""},
    78  		{"0X", 64, ""},
    79  	}
    80  	for _, test := range tests {
    81  		input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"`
    82  		var v Hash
    83  		err := json.Unmarshal([]byte(input), &v)
    84  		if err == nil {
    85  			if test.Error != "" {
    86  				t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error)
    87  			}
    88  		} else {
    89  			if err.Error() != test.Error {
    90  				t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error)
    91  			}
    92  		}
    93  	}
    94  }
    95  
    96  func TestAddressUnmarshalJSON(t *testing.T) {
    97  	var tests = []struct {
    98  		Input     string
    99  		ShouldErr bool
   100  		Output    *big.Int
   101  	}{
   102  		{"", true, nil},
   103  		{`""`, true, nil},
   104  		{`"0x"`, true, nil},
   105  		{`"0x00"`, true, nil},
   106  		{`"0xG000000000000000000000000000000000000000"`, true, nil},
   107  		{`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)},
   108  		{`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)},
   109  	}
   110  	for i, test := range tests {
   111  		var v Address
   112  		err := json.Unmarshal([]byte(test.Input), &v)
   113  		if err != nil && !test.ShouldErr {
   114  			t.Errorf("test #%d: unexpected error: %v", i, err)
   115  		}
   116  		if err == nil {
   117  			if test.ShouldErr {
   118  				t.Errorf("test #%d: expected error, got none", i)
   119  			}
   120  			if got := new(big.Int).SetBytes(v.Bytes()); got.Cmp(test.Output) != 0 {
   121  				t.Errorf("test #%d: address mismatch: have %v, want %v", i, got, test.Output)
   122  			}
   123  		}
   124  	}
   125  }
   126  
   127  func TestAddressHexChecksum(t *testing.T) {
   128  	var tests = []struct {
   129  		Input  string
   130  		Output string
   131  	}{
   132  		// Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification
   133  		{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"},
   134  		{"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"},
   135  		{"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"},
   136  		{"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"},
   137  		// Ensure that non-standard length input values are handled correctly
   138  		{"0xa", "0x000000000000000000000000000000000000000A"},
   139  		{"0x0a", "0x000000000000000000000000000000000000000A"},
   140  		{"0x00a", "0x000000000000000000000000000000000000000A"},
   141  		{"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"},
   142  	}
   143  	for i, test := range tests {
   144  		output := HexToAddress(test.Input).Hex()
   145  		if output != test.Output {
   146  			t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output)
   147  		}
   148  	}
   149  }
   150  
   151  func BenchmarkAddressHex(b *testing.B) {
   152  	testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
   153  	for n := 0; n < b.N; n++ {
   154  		testAddr.Hex()
   155  	}
   156  }
   157  
   158  func TestMixedcaseAccount_Address(t *testing.T) {
   159  
   160  	// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
   161  	// Note: 0X{checksum_addr} is not valid according to spec above
   162  
   163  	var res []struct {
   164  		A     MixedcaseAddress
   165  		Valid bool
   166  	}
   167  	if err := json.Unmarshal([]byte(`[
   168  		{"A" : "0xae967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
   169  		{"A" : "0xAe967917c465db8578ca9024c205720b1a3651A9", "Valid": true},
   170  		{"A" : "0XAe967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
   171  		{"A" : "0x1111111111111111111112222222222223333323", "Valid": true}
   172  		]`), &res); err != nil {
   173  		t.Fatal(err)
   174  	}
   175  
   176  	for _, r := range res {
   177  		if got := r.A.ValidChecksum(); got != r.Valid {
   178  			t.Errorf("Expected checksum %v, got checksum %v, input %v", r.Valid, got, r.A.String())
   179  		}
   180  	}
   181  
   182  	//These should throw exceptions:
   183  	var r2 []MixedcaseAddress
   184  	for _, r := range []string{
   185  		`["0x11111111111111111111122222222222233333"]`,     // Too short
   186  		`["0x111111111111111111111222222222222333332"]`,    // Too short
   187  		`["0x11111111111111111111122222222222233333234"]`,  // Too long
   188  		`["0x111111111111111111111222222222222333332344"]`, // Too long
   189  		`["1111111111111111111112222222222223333323"]`,     // Missing 0x
   190  		`["x1111111111111111111112222222222223333323"]`,    // Missing 0
   191  		`["0xG111111111111111111112222222222223333323"]`,   //Non-hex
   192  	} {
   193  		if err := json.Unmarshal([]byte(r), &r2); err == nil {
   194  			t.Errorf("Expected failure, input %v", r)
   195  		}
   196  
   197  	}
   198  
   199  }
   200  
   201  func TestBytesToEncryptedPayloadHash_whenTypical(t *testing.T) {
   202  	arbitraryBytes := []byte{10}
   203  	var expected EncryptedPayloadHash
   204  	expected[EncryptedPayloadHashLength-1] = 10
   205  
   206  	actual := BytesToEncryptedPayloadHash(arbitraryBytes)
   207  
   208  	assert.Equal(t, expected, actual)
   209  }
   210  
   211  func TestEncryptedPayloadHash_Bytes(t *testing.T) {
   212  	arbitraryBytes := []byte{10}
   213  	h := BytesToEncryptedPayloadHash(arbitraryBytes)
   214  
   215  	actual := h.Bytes()
   216  
   217  	assert.Equal(t, arbitraryBytes[0], actual[EncryptedPayloadHashLength-1])
   218  }
   219  
   220  func TestEncryptedPayloadHash_BytesTypeRef(t *testing.T) {
   221  	arbitraryBytes := []byte{10}
   222  	h := BytesToEncryptedPayloadHash(arbitraryBytes)
   223  	expected := h.Hex()
   224  
   225  	bt := h.BytesTypeRef()
   226  	actual := bt.String()
   227  
   228  	assert.Equal(t, expected, actual)
   229  }
   230  
   231  func TestEncryptedPayloadHash_ToBase64(t *testing.T) {
   232  	arbitraryBytes := []byte{10}
   233  	h := BytesToEncryptedPayloadHash(arbitraryBytes)
   234  	expected := base64.StdEncoding.EncodeToString(h.Bytes())
   235  
   236  	actual := h.ToBase64()
   237  
   238  	assert.Equal(t, expected, actual)
   239  }
   240  
   241  func TestEmptyEncryptedPayloadHash(t *testing.T) {
   242  
   243  	emptyHash := EncryptedPayloadHash{}
   244  
   245  	assert.True(t, EmptyEncryptedPayloadHash(emptyHash))
   246  }
   247  
   248  func TestEncryptedPayloadHashes_whenTypical(t *testing.T) {
   249  	arbitraryBytes1 := []byte{10}
   250  	arbitraryBytes2 := []byte{5}
   251  	h, err := Base64sToEncryptedPayloadHashes([]string{base64.StdEncoding.EncodeToString(arbitraryBytes1), base64.StdEncoding.EncodeToString(arbitraryBytes2)})
   252  	if err != nil {
   253  		t.Fatalf("must be able to convert but fail due to %s", err)
   254  	}
   255  
   256  	arbitraryBytes3 := []byte{7}
   257  	newItem := BytesToEncryptedPayloadHash(arbitraryBytes3)
   258  	h.Add(newItem)
   259  
   260  	assert.False(t, h.NotExist(newItem))
   261  }
   262  
   263  func TestHash_Scan(t *testing.T) {
   264  	type args struct {
   265  		src interface{}
   266  	}
   267  	tests := []struct {
   268  		name    string
   269  		args    args
   270  		wantErr bool
   271  	}{
   272  		{
   273  			name: "working scan",
   274  			args: args{src: []byte{
   275  				0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
   276  				0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
   277  				0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
   278  				0x10, 0x00,
   279  			}},
   280  			wantErr: false,
   281  		},
   282  		{
   283  			name:    "non working scan",
   284  			args:    args{src: int64(1234567890)},
   285  			wantErr: true,
   286  		},
   287  		{
   288  			name: "invalid length scan",
   289  			args: args{src: []byte{
   290  				0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
   291  				0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
   292  				0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
   293  			}},
   294  			wantErr: true,
   295  		},
   296  	}
   297  	for _, tt := range tests {
   298  		t.Run(tt.name, func(t *testing.T) {
   299  			h := &Hash{}
   300  			if err := h.Scan(tt.args.src); (err != nil) != tt.wantErr {
   301  				t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr)
   302  			}
   303  
   304  			if !tt.wantErr {
   305  				for i := range h {
   306  					if h[i] != tt.args.src.([]byte)[i] {
   307  						t.Errorf(
   308  							"Hash.Scan() didn't scan the %d src correctly (have %X, want %X)",
   309  							i, h[i], tt.args.src.([]byte)[i],
   310  						)
   311  					}
   312  				}
   313  			}
   314  		})
   315  	}
   316  }
   317  
   318  func TestHash_Value(t *testing.T) {
   319  	b := []byte{
   320  		0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
   321  		0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
   322  		0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
   323  		0x10, 0x00,
   324  	}
   325  	var usedH Hash
   326  	usedH.SetBytes(b)
   327  	tests := []struct {
   328  		name    string
   329  		h       Hash
   330  		want    driver.Value
   331  		wantErr bool
   332  	}{
   333  		{
   334  			name:    "Working value",
   335  			h:       usedH,
   336  			want:    b,
   337  			wantErr: false,
   338  		},
   339  	}
   340  	for _, tt := range tests {
   341  		t.Run(tt.name, func(t *testing.T) {
   342  			got, err := tt.h.Value()
   343  			if (err != nil) != tt.wantErr {
   344  				t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr)
   345  				return
   346  			}
   347  			if !reflect.DeepEqual(got, tt.want) {
   348  				t.Errorf("Hash.Value() = %v, want %v", got, tt.want)
   349  			}
   350  		})
   351  	}
   352  }
   353  
   354  func TestAddress_Scan(t *testing.T) {
   355  	type args struct {
   356  		src interface{}
   357  	}
   358  	tests := []struct {
   359  		name    string
   360  		args    args
   361  		wantErr bool
   362  	}{
   363  		{
   364  			name: "working scan",
   365  			args: args{src: []byte{
   366  				0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
   367  				0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
   368  			}},
   369  			wantErr: false,
   370  		},
   371  		{
   372  			name:    "non working scan",
   373  			args:    args{src: int64(1234567890)},
   374  			wantErr: true,
   375  		},
   376  		{
   377  			name: "invalid length scan",
   378  			args: args{src: []byte{
   379  				0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
   380  				0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a,
   381  			}},
   382  			wantErr: true,
   383  		},
   384  	}
   385  	for _, tt := range tests {
   386  		t.Run(tt.name, func(t *testing.T) {
   387  			a := &Address{}
   388  			if err := a.Scan(tt.args.src); (err != nil) != tt.wantErr {
   389  				t.Errorf("Address.Scan() error = %v, wantErr %v", err, tt.wantErr)
   390  			}
   391  
   392  			if !tt.wantErr {
   393  				for i := range a {
   394  					if a[i] != tt.args.src.([]byte)[i] {
   395  						t.Errorf(
   396  							"Address.Scan() didn't scan the %d src correctly (have %X, want %X)",
   397  							i, a[i], tt.args.src.([]byte)[i],
   398  						)
   399  					}
   400  				}
   401  			}
   402  		})
   403  	}
   404  }
   405  
   406  func TestAddress_Value(t *testing.T) {
   407  	b := []byte{
   408  		0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
   409  		0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
   410  	}
   411  	var usedA Address
   412  	usedA.SetBytes(b)
   413  	tests := []struct {
   414  		name    string
   415  		a       Address
   416  		want    driver.Value
   417  		wantErr bool
   418  	}{
   419  		{
   420  			name:    "Working value",
   421  			a:       usedA,
   422  			want:    b,
   423  			wantErr: false,
   424  		},
   425  	}
   426  	for _, tt := range tests {
   427  		t.Run(tt.name, func(t *testing.T) {
   428  			got, err := tt.a.Value()
   429  			if (err != nil) != tt.wantErr {
   430  				t.Errorf("Address.Value() error = %v, wantErr %v", err, tt.wantErr)
   431  				return
   432  			}
   433  			if !reflect.DeepEqual(got, tt.want) {
   434  				t.Errorf("Address.Value() = %v, want %v", got, tt.want)
   435  			}
   436  		})
   437  	}
   438  }
   439  
   440  func TestFormatTerminalString_Value(t *testing.T) {
   441  	assert.Equal(t, "", FormatTerminalString(nil))
   442  	assert.Equal(t, "", FormatTerminalString([]byte{}))
   443  	b := []byte{
   444  		0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd,
   445  	}
   446  	str := FormatTerminalString(b)
   447  	assert.Equal(t, "123456…90abcd", str)
   448  	str = FormatTerminalString(b[1:])
   449  	assert.Equal(t, "34567890abcd", str)
   450  }