github.com/prysmaticlabs/prysm@v1.4.4/shared/bytesutil/bytes_test.go (about)

     1  package bytesutil_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
     7  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
     9  )
    10  
    11  func TestToBytes(t *testing.T) {
    12  	tests := []struct {
    13  		a uint64
    14  		b []byte
    15  	}{
    16  		{0, []byte{0}},
    17  		{1, []byte{1}},
    18  		{2, []byte{2}},
    19  		{253, []byte{253}},
    20  		{254, []byte{254}},
    21  		{255, []byte{255}},
    22  		{0, []byte{0, 0}},
    23  		{1, []byte{1, 0}},
    24  		{255, []byte{255, 0}},
    25  		{256, []byte{0, 1}},
    26  		{65534, []byte{254, 255}},
    27  		{65535, []byte{255, 255}},
    28  		{0, []byte{0, 0, 0}},
    29  		{255, []byte{255, 0, 0}},
    30  		{256, []byte{0, 1, 0}},
    31  		{65535, []byte{255, 255, 0}},
    32  		{65536, []byte{0, 0, 1}},
    33  		{16777215, []byte{255, 255, 255}},
    34  		{0, []byte{0, 0, 0, 0}},
    35  		{256, []byte{0, 1, 0, 0}},
    36  		{65536, []byte{0, 0, 1, 0}},
    37  		{16777216, []byte{0, 0, 0, 1}},
    38  		{16777217, []byte{1, 0, 0, 1}},
    39  		{4294967295, []byte{255, 255, 255, 255}},
    40  		{0, []byte{0, 0, 0, 0, 0, 0, 0, 0}},
    41  		{16777216, []byte{0, 0, 0, 1, 0, 0, 0, 0}},
    42  		{4294967296, []byte{0, 0, 0, 0, 1, 0, 0, 0}},
    43  		{4294967297, []byte{1, 0, 0, 0, 1, 0, 0, 0}},
    44  		{9223372036854775806, []byte{254, 255, 255, 255, 255, 255, 255, 127}},
    45  		{9223372036854775807, []byte{255, 255, 255, 255, 255, 255, 255, 127}},
    46  	}
    47  	for _, tt := range tests {
    48  		b := bytesutil.ToBytes(tt.a, len(tt.b))
    49  		assert.DeepEqual(t, tt.b, b)
    50  	}
    51  }
    52  
    53  func TestBytes1(t *testing.T) {
    54  	tests := []struct {
    55  		a uint64
    56  		b []byte
    57  	}{
    58  		{0, []byte{0}},
    59  		{1, []byte{1}},
    60  		{2, []byte{2}},
    61  		{253, []byte{253}},
    62  		{254, []byte{254}},
    63  		{255, []byte{255}},
    64  	}
    65  	for _, tt := range tests {
    66  		b := bytesutil.Bytes1(tt.a)
    67  		assert.DeepEqual(t, tt.b, b)
    68  	}
    69  }
    70  
    71  func TestBytes2(t *testing.T) {
    72  	tests := []struct {
    73  		a uint64
    74  		b []byte
    75  	}{
    76  		{0, []byte{0, 0}},
    77  		{1, []byte{1, 0}},
    78  		{255, []byte{255, 0}},
    79  		{256, []byte{0, 1}},
    80  		{65534, []byte{254, 255}},
    81  		{65535, []byte{255, 255}},
    82  	}
    83  	for _, tt := range tests {
    84  		b := bytesutil.Bytes2(tt.a)
    85  		assert.DeepEqual(t, tt.b, b)
    86  	}
    87  }
    88  
    89  func TestBytes3(t *testing.T) {
    90  	tests := []struct {
    91  		a uint64
    92  		b []byte
    93  	}{
    94  		{0, []byte{0, 0, 0}},
    95  		{255, []byte{255, 0, 0}},
    96  		{256, []byte{0, 1, 0}},
    97  		{65535, []byte{255, 255, 0}},
    98  		{65536, []byte{0, 0, 1}},
    99  		{16777215, []byte{255, 255, 255}},
   100  	}
   101  	for _, tt := range tests {
   102  		b := bytesutil.Bytes3(tt.a)
   103  		assert.DeepEqual(t, tt.b, b)
   104  	}
   105  }
   106  
   107  func TestBytes4(t *testing.T) {
   108  	tests := []struct {
   109  		a uint64
   110  		b []byte
   111  	}{
   112  		{0, []byte{0, 0, 0, 0}},
   113  		{256, []byte{0, 1, 0, 0}},
   114  		{65536, []byte{0, 0, 1, 0}},
   115  		{16777216, []byte{0, 0, 0, 1}},
   116  		{16777217, []byte{1, 0, 0, 1}},
   117  		{4294967295, []byte{255, 255, 255, 255}},
   118  	}
   119  	for _, tt := range tests {
   120  		b := bytesutil.Bytes4(tt.a)
   121  		assert.DeepEqual(t, tt.b, b)
   122  	}
   123  }
   124  
   125  func TestBytes8(t *testing.T) {
   126  	tests := []struct {
   127  		a uint64
   128  		b []byte
   129  	}{
   130  		{0, []byte{0, 0, 0, 0, 0, 0, 0, 0}},
   131  		{16777216, []byte{0, 0, 0, 1, 0, 0, 0, 0}},
   132  		{4294967296, []byte{0, 0, 0, 0, 1, 0, 0, 0}},
   133  		{4294967297, []byte{1, 0, 0, 0, 1, 0, 0, 0}},
   134  		{9223372036854775806, []byte{254, 255, 255, 255, 255, 255, 255, 127}},
   135  		{9223372036854775807, []byte{255, 255, 255, 255, 255, 255, 255, 127}},
   136  	}
   137  	for _, tt := range tests {
   138  		b := bytesutil.Bytes8(tt.a)
   139  		assert.DeepEqual(t, tt.b, b)
   140  	}
   141  }
   142  
   143  func TestFromBool(t *testing.T) {
   144  	tests := []byte{
   145  		0,
   146  		1,
   147  	}
   148  	for _, tt := range tests {
   149  		b := bytesutil.ToBool(tt)
   150  		c := bytesutil.FromBool(b)
   151  		assert.Equal(t, tt, c)
   152  	}
   153  }
   154  
   155  func TestFromBytes2(t *testing.T) {
   156  	tests := []uint64{
   157  		0,
   158  		1776,
   159  		96726,
   160  		(1 << 16) - 1,
   161  	}
   162  	for _, tt := range tests {
   163  		b := bytesutil.ToBytes(tt, 2)
   164  		c := bytesutil.FromBytes2(b)
   165  		assert.Equal(t, uint16(tt), c)
   166  	}
   167  }
   168  
   169  func TestFromBytes4(t *testing.T) {
   170  	tests := []uint64{
   171  		0,
   172  		1776,
   173  		96726,
   174  		4290997,
   175  		4294967295, // 2^32 - 1
   176  		4294967200,
   177  		3894948296,
   178  	}
   179  	for _, tt := range tests {
   180  		b := bytesutil.ToBytes(tt, 4)
   181  		c := bytesutil.FromBytes4(b)
   182  		if c != tt {
   183  			t.Errorf("Wanted %d but got %d", tt, c)
   184  		}
   185  		assert.Equal(t, tt, c)
   186  	}
   187  }
   188  
   189  func TestFromBytes8(t *testing.T) {
   190  	tests := []uint64{
   191  		0,
   192  		1776,
   193  		96726,
   194  		4290997,
   195  		922376854775806,
   196  		42893720984775807,
   197  		18446744073709551615,
   198  	}
   199  	for _, tt := range tests {
   200  		b := bytesutil.ToBytes(tt, 8)
   201  		c := bytesutil.FromBytes8(b)
   202  		assert.Equal(t, tt, c)
   203  	}
   204  }
   205  
   206  func TestTruncate(t *testing.T) {
   207  	tests := []struct {
   208  		a []byte
   209  		b []byte
   210  	}{
   211  		{[]byte{'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'},
   212  			[]byte{'A', 'C', 'D', 'E', 'F', 'G'}},
   213  		{[]byte{'A', 'C', 'D', 'E', 'F'},
   214  			[]byte{'A', 'C', 'D', 'E', 'F'}},
   215  		{[]byte{}, []byte{}},
   216  	}
   217  	for _, tt := range tests {
   218  		b := bytesutil.Trunc(tt.a)
   219  		assert.DeepEqual(t, tt.b, b)
   220  	}
   221  }
   222  
   223  func TestReverse(t *testing.T) {
   224  	tests := []struct {
   225  		input  [][32]byte
   226  		output [][32]byte
   227  	}{
   228  		{[][32]byte{{'A'}, {'B'}, {'C'}, {'D'}, {'E'}, {'F'}, {'G'}, {'H'}},
   229  			[][32]byte{{'H'}, {'G'}, {'F'}, {'E'}, {'D'}, {'C'}, {'B'}, {'A'}}},
   230  		{[][32]byte{{1}, {2}, {3}, {4}},
   231  			[][32]byte{{4}, {3}, {2}, {1}}},
   232  		{[][32]byte{}, [][32]byte{}},
   233  	}
   234  	for _, tt := range tests {
   235  		b := bytesutil.ReverseBytes32Slice(tt.input)
   236  		assert.DeepEqual(t, tt.output, b)
   237  	}
   238  }
   239  
   240  func TestSetBit(t *testing.T) {
   241  	tests := []struct {
   242  		a []byte
   243  		b int
   244  		c []byte
   245  	}{
   246  		{[]byte{0b00000000}, 1, []byte{0b00000010}},
   247  		{[]byte{0b00000010}, 7, []byte{0b10000010}},
   248  		{[]byte{0b10000010}, 9, []byte{0b10000010, 0b00000010}},
   249  		{[]byte{0b10000010}, 27, []byte{0b10000010, 0b00000000, 0b00000000, 0b00001000}},
   250  		{[]byte{0b10000010, 0b00000000}, 8, []byte{0b10000010, 0b00000001}},
   251  		{[]byte{0b10000010, 0b00000000}, 31, []byte{0b10000010, 0b00000000, 0b00000000, 0b10000000}},
   252  	}
   253  	for _, tt := range tests {
   254  		assert.DeepEqual(t, tt.c, bytesutil.SetBit(tt.a, tt.b))
   255  	}
   256  }
   257  
   258  func TestClearBit(t *testing.T) {
   259  	tests := []struct {
   260  		a []byte
   261  		b int
   262  		c []byte
   263  	}{
   264  		{[]byte{0b00000000}, 1, []byte{0b00000000}},
   265  		{[]byte{0b00000010}, 1, []byte{0b00000000}},
   266  		{[]byte{0b10000010}, 1, []byte{0b10000000}},
   267  		{[]byte{0b10000010}, 8, []byte{0b10000010}},
   268  		{[]byte{0b10000010, 0b00001111}, 7, []byte{0b00000010, 0b00001111}},
   269  		{[]byte{0b10000010, 0b00001111}, 10, []byte{0b10000010, 0b00001011}},
   270  	}
   271  	for _, tt := range tests {
   272  		assert.DeepEqual(t, tt.c, bytesutil.ClearBit(tt.a, tt.b))
   273  	}
   274  }
   275  
   276  func TestMakeEmptyBitfields(t *testing.T) {
   277  	tests := []struct {
   278  		a int
   279  		b int
   280  	}{
   281  		{0, 1},
   282  		{1, 1},
   283  		{2, 1},
   284  		{7, 1},
   285  		{8, 2},
   286  		{15, 2},
   287  		{16, 3},
   288  		{100, 13},
   289  		{104, 14},
   290  	}
   291  	for _, tt := range tests {
   292  		assert.DeepEqual(t, tt.b, len(bytesutil.MakeEmptyBitlists(tt.a)))
   293  	}
   294  }
   295  
   296  func TestHighestBitIndex(t *testing.T) {
   297  	tests := []struct {
   298  		a     []byte
   299  		b     int
   300  		error bool
   301  	}{
   302  		{nil, 0, true},
   303  		{[]byte{}, 0, true},
   304  		{[]byte{0b00000001}, 1, false},
   305  		{[]byte{0b10100101}, 8, false},
   306  		{[]byte{0x00, 0x00}, 0, false},
   307  		{[]byte{0xff, 0xa0}, 16, false},
   308  		{[]byte{12, 34, 56, 78}, 31, false},
   309  		{[]byte{255, 255, 255, 255}, 32, false},
   310  	}
   311  	for _, tt := range tests {
   312  		i, err := bytesutil.HighestBitIndex(tt.a)
   313  		if !tt.error {
   314  			require.NoError(t, err)
   315  			assert.DeepEqual(t, tt.b, i)
   316  		} else {
   317  			assert.ErrorContains(t, "input list can't be empty or nil", err)
   318  		}
   319  	}
   320  }
   321  
   322  func TestHighestBitIndexBelow(t *testing.T) {
   323  	tests := []struct {
   324  		a     []byte
   325  		b     int
   326  		c     int
   327  		error bool
   328  	}{
   329  		{nil, 0, 0, true},
   330  		{[]byte{}, 0, 0, true},
   331  		{[]byte{0b00010001}, 0, 0, false},
   332  		{[]byte{0b00010001}, 1, 1, false},
   333  		{[]byte{0b00010001}, 2, 1, false},
   334  		{[]byte{0b00010001}, 4, 1, false},
   335  		{[]byte{0b00010001}, 5, 5, false},
   336  		{[]byte{0b00010001}, 8, 5, false},
   337  		{[]byte{0b00010001, 0b00000000}, 0, 0, false},
   338  		{[]byte{0b00010001, 0b00000000}, 1, 1, false},
   339  		{[]byte{0b00010001, 0b00000000}, 2, 1, false},
   340  		{[]byte{0b00010001, 0b00000000}, 4, 1, false},
   341  		{[]byte{0b00010001, 0b00000000}, 5, 5, false},
   342  		{[]byte{0b00010001, 0b00000000}, 8, 5, false},
   343  		{[]byte{0b00010001, 0b00000000}, 15, 5, false},
   344  		{[]byte{0b00010001, 0b00000000}, 16, 5, false},
   345  		{[]byte{0b00010001, 0b00100010}, 8, 5, false},
   346  		{[]byte{0b00010001, 0b00100010}, 9, 5, false},
   347  		{[]byte{0b00010001, 0b00100010}, 10, 10, false},
   348  		{[]byte{0b00010001, 0b00100010}, 11, 10, false},
   349  		{[]byte{0b00010001, 0b00100010}, 14, 14, false},
   350  		{[]byte{0b00010001, 0b00100010}, 15, 14, false},
   351  		{[]byte{0b00010001, 0b00100010}, 24, 14, false},
   352  		{[]byte{0b00010001, 0b00100010, 0b10000000}, 23, 14, false},
   353  		{[]byte{0b00010001, 0b00100010, 0b10000000}, 24, 24, false},
   354  		{[]byte{0b00000000, 0b00000001, 0b00000011}, 17, 17, false},
   355  		{[]byte{0b00000000, 0b00000001, 0b00000011}, 18, 18, false},
   356  		{[]byte{12, 34, 56, 78}, 1000, 31, false},
   357  		{[]byte{255, 255, 255, 255}, 1000, 32, false},
   358  	}
   359  	for _, tt := range tests {
   360  		i, err := bytesutil.HighestBitIndexAt(tt.a, tt.b)
   361  		if !tt.error {
   362  			require.NoError(t, err)
   363  			assert.DeepEqual(t, tt.c, i)
   364  		} else {
   365  			assert.ErrorContains(t, "input list can't be empty or nil", err)
   366  		}
   367  	}
   368  }
   369  
   370  func TestUint64ToBytes_RoundTrip(t *testing.T) {
   371  	for i := uint64(0); i < 10000; i++ {
   372  		b := bytesutil.Uint64ToBytesBigEndian(i)
   373  		if got := bytesutil.BytesToUint64BigEndian(b); got != i {
   374  			t.Error("Round trip did not match original value")
   375  		}
   376  	}
   377  }
   378  
   379  func TestIsHex(t *testing.T) {
   380  	tests := []struct {
   381  		a []byte
   382  		b bool
   383  	}{
   384  		{nil, false},
   385  		{[]byte(""), false},
   386  		{[]byte("0x"), false},
   387  		{[]byte("0x0"), true},
   388  		{[]byte("foo"), false},
   389  		{[]byte("1234567890abcDEF"), false},
   390  		{[]byte("XYZ4567890abcDEF1234567890abcDEF1234567890abcDEF1234567890abcDEF"), false},
   391  		{[]byte("0x1234567890abcDEF1234567890abcDEF1234567890abcDEF1234567890abcDEF"), true},
   392  		{[]byte("1234567890abcDEF1234567890abcDEF1234567890abcDEF1234567890abcDEF"), false},
   393  	}
   394  	for _, tt := range tests {
   395  		isHex, err := bytesutil.IsHex(tt.a)
   396  		require.NoError(t, err)
   397  		assert.Equal(t, tt.b, isHex)
   398  	}
   399  }