mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-04-19 03:01:06 -04:00
* initial commit for cli integration of web3signer * resolving conflicts and execution * remove aggregation slot from proto * rem aggregation slot * define a sync message block root struct * fix sync message name * sync message block root struct * amend where sync committee block root is used * altered switch statement to return correct json request by type * fixing fork data import, types, and unit tests * reverting unwanted changes * reverting more unwanted changes * fixing deepsource issues * fixing formatting * more fixes for deepsource and code clean up * only want to fetch once for fetch validating public keys * adding more comments * new unit tests for requests and fixing a mapper issue * Update validator/client/validator.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update validator/accounts/wallet/wallet.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * adjusting comment * adjusting comment * fixing import organization * including more unit tests * adding new cli edit * adding in checks for wallet initialize * adding web3signer flags to main.go * some how resolved files did not save correctly * adding in check to make sure web flag only works with types imported and derived * Update validator/client/sync_committee.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update validator/client/aggregate.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update validator/accounts/wallet/wallet.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update cmd/validator/wallet/wallet.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update cmd/validator/wallet/wallet.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update cmd/validator/main.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update cmd/validator/flags/flags.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update cmd/validator/flags/flags.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update cmd/validator/wallet/wallet.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update cmd/validator/wallet/wallet.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * reverting changes that accidently got checked in * reverting * reverting * continuing to revert unintenteded changes * reverting * removing more unneeded changes * addressing review comment * initial refactor * adding in more clarifying comments * fixing mock * resolving desource issues * addressing gosec scan for helper go file * addressing gosec * trying to fix bazel build * removal of interface to fix build * fixing maligned struct * addressing deepsource * fixing deepsource * addressing efficiency of type checking * fixing bazel test failure * fixing go linter errors * gaz * web changes * add w3signer * new kind * proper use * align * adding prysm validator flags to help flags list * addressing root comment * ci lint * fixing standardapi tests * fixing accounts_test after removal of keymanager from rpc server * fixing more unit tests * Update cmd/validator/flags/flags.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update cmd/validator/flags/flags.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update validator/client/service.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update validator/client/service.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * addressing missed err checks * fixing mock tests * fixing gofmt * unskipping minimal e2e test and removing related TODOs * Update testing/endtoend/components/validator.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update testing/endtoend/components/validator.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * adding some error wrapers to clarify failure point * fixing bazel build with new error checks * taking preston's advice to make test fail faster to understand what's going on with the test * checking if genesis validators root is not zero hash * adding check for genesis validators root giving zero hash * fixing missing dependency * adding check for wallet * log all * fixing errors for http responses * switching marshal to pretty print * adding pretty sign request test * fixing base url setting * adding in check for web3signer and temporary wallet instead of having to open the wallet * refactoring web3signer to not require wallet * bazel build fix * fixing gazelle build * adding content type of request * fixing more bazel * removing unused code * removing unused comments * adding skip test back in * addressing a validation and error message * fix parse * body * fixing logic for datadir * improving error handling * show resp * fix * sign resp as str * point of pointer remove * sign resp * unmarshal sig resp * read body as str * adding more verbose logging * removing unused result * fixing unit test * reconfiguring files to properly nest code and mocks * fix build issue * using context when using client function calls * fixing based on suggestion * addressing comments * gaz * removing defined max timeout * reverting json print pretty * Update validator/accounts/wallet_edit.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * removing unneeded code restrictions * should not introduce new code that may impact existing key manager types * adjusting comments * adding in json validation * running go mod tidy * some logging * more logs * fixing typo * remove logs * testing without byte trim * fixing order or properties * gaz * tidy * reverting some logs * removing the confusing comments * Update validator/client/aggregate.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update validator/client/aggregate.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * addressing pr comments * editing bytes test * Run gazelle update-repos * run gazelle * improving unit test coverage * fixing text * fixing a potential escaped error Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
530 lines
13 KiB
Go
530 lines
13 KiB
Go
package bytesutil_test
|
|
|
|
import (
|
|
"bytes"
|
|
"reflect"
|
|
"testing"
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
)
|
|
|
|
func TestToBytes(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0}},
|
|
{1, []byte{1}},
|
|
{2, []byte{2}},
|
|
{253, []byte{253}},
|
|
{254, []byte{254}},
|
|
{255, []byte{255}},
|
|
{0, []byte{0, 0}},
|
|
{1, []byte{1, 0}},
|
|
{255, []byte{255, 0}},
|
|
{256, []byte{0, 1}},
|
|
{65534, []byte{254, 255}},
|
|
{65535, []byte{255, 255}},
|
|
{0, []byte{0, 0, 0}},
|
|
{255, []byte{255, 0, 0}},
|
|
{256, []byte{0, 1, 0}},
|
|
{65535, []byte{255, 255, 0}},
|
|
{65536, []byte{0, 0, 1}},
|
|
{16777215, []byte{255, 255, 255}},
|
|
{0, []byte{0, 0, 0, 0}},
|
|
{256, []byte{0, 1, 0, 0}},
|
|
{65536, []byte{0, 0, 1, 0}},
|
|
{16777216, []byte{0, 0, 0, 1}},
|
|
{16777217, []byte{1, 0, 0, 1}},
|
|
{4294967295, []byte{255, 255, 255, 255}},
|
|
{0, []byte{0, 0, 0, 0, 0, 0, 0, 0}},
|
|
{16777216, []byte{0, 0, 0, 1, 0, 0, 0, 0}},
|
|
{4294967296, []byte{0, 0, 0, 0, 1, 0, 0, 0}},
|
|
{4294967297, []byte{1, 0, 0, 0, 1, 0, 0, 0}},
|
|
{9223372036854775806, []byte{254, 255, 255, 255, 255, 255, 255, 127}},
|
|
{9223372036854775807, []byte{255, 255, 255, 255, 255, 255, 255, 127}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBytes(tt.a, len(tt.b))
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes1(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0}},
|
|
{1, []byte{1}},
|
|
{2, []byte{2}},
|
|
{253, []byte{253}},
|
|
{254, []byte{254}},
|
|
{255, []byte{255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes1(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes2(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0}},
|
|
{1, []byte{1, 0}},
|
|
{255, []byte{255, 0}},
|
|
{256, []byte{0, 1}},
|
|
{65534, []byte{254, 255}},
|
|
{65535, []byte{255, 255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes2(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes3(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0, 0}},
|
|
{255, []byte{255, 0, 0}},
|
|
{256, []byte{0, 1, 0}},
|
|
{65535, []byte{255, 255, 0}},
|
|
{65536, []byte{0, 0, 1}},
|
|
{16777215, []byte{255, 255, 255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes3(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes4(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0, 0, 0}},
|
|
{256, []byte{0, 1, 0, 0}},
|
|
{65536, []byte{0, 0, 1, 0}},
|
|
{16777216, []byte{0, 0, 0, 1}},
|
|
{16777217, []byte{1, 0, 0, 1}},
|
|
{4294967295, []byte{255, 255, 255, 255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes4(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestBytes8(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0, 0, 0, 0, 0, 0, 0}},
|
|
{16777216, []byte{0, 0, 0, 1, 0, 0, 0, 0}},
|
|
{4294967296, []byte{0, 0, 0, 0, 1, 0, 0, 0}},
|
|
{4294967297, []byte{1, 0, 0, 0, 1, 0, 0, 0}},
|
|
{9223372036854775806, []byte{254, 255, 255, 255, 255, 255, 255, 127}},
|
|
{9223372036854775807, []byte{255, 255, 255, 255, 255, 255, 255, 127}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Bytes8(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestFromBool(t *testing.T) {
|
|
tests := []byte{
|
|
0,
|
|
1,
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBool(tt)
|
|
c := bytesutil.FromBool(b)
|
|
assert.Equal(t, tt, c)
|
|
}
|
|
}
|
|
|
|
func TestFromBytes2(t *testing.T) {
|
|
tests := []uint64{
|
|
0,
|
|
1776,
|
|
96726,
|
|
(1 << 16) - 1,
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBytes(tt, 2)
|
|
c := bytesutil.FromBytes2(b)
|
|
assert.Equal(t, uint16(tt), c)
|
|
}
|
|
}
|
|
|
|
func TestFromBytes4(t *testing.T) {
|
|
tests := []uint64{
|
|
0,
|
|
1776,
|
|
96726,
|
|
4290997,
|
|
4294967295, // 2^32 - 1
|
|
4294967200,
|
|
3894948296,
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBytes(tt, 4)
|
|
c := bytesutil.FromBytes4(b)
|
|
if c != tt {
|
|
t.Errorf("Wanted %d but got %d", tt, c)
|
|
}
|
|
assert.Equal(t, tt, c)
|
|
}
|
|
}
|
|
|
|
func TestFromBytes8(t *testing.T) {
|
|
tests := []uint64{
|
|
0,
|
|
1776,
|
|
96726,
|
|
4290997,
|
|
922376854775806,
|
|
42893720984775807,
|
|
18446744073709551615,
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ToBytes(tt, 8)
|
|
c := bytesutil.FromBytes8(b)
|
|
assert.Equal(t, tt, c)
|
|
}
|
|
}
|
|
|
|
func TestTruncate(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b []byte
|
|
}{
|
|
{[]byte{'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'},
|
|
[]byte{'A', 'C', 'D', 'E', 'F', 'G'}},
|
|
{[]byte{'A', 'C', 'D', 'E', 'F'},
|
|
[]byte{'A', 'C', 'D', 'E', 'F'}},
|
|
{[]byte{}, []byte{}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.Trunc(tt.a)
|
|
assert.DeepEqual(t, tt.b, b)
|
|
}
|
|
}
|
|
|
|
func TestReverse(t *testing.T) {
|
|
tests := []struct {
|
|
input [][32]byte
|
|
output [][32]byte
|
|
}{
|
|
{[][32]byte{{'A'}, {'B'}, {'C'}, {'D'}, {'E'}, {'F'}, {'G'}, {'H'}},
|
|
[][32]byte{{'H'}, {'G'}, {'F'}, {'E'}, {'D'}, {'C'}, {'B'}, {'A'}}},
|
|
{[][32]byte{{1}, {2}, {3}, {4}},
|
|
[][32]byte{{4}, {3}, {2}, {1}}},
|
|
{[][32]byte{}, [][32]byte{}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := bytesutil.ReverseBytes32Slice(tt.input)
|
|
assert.DeepEqual(t, tt.output, b)
|
|
}
|
|
}
|
|
|
|
func TestSetBit(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b int
|
|
c []byte
|
|
}{
|
|
{[]byte{0b00000000}, 1, []byte{0b00000010}},
|
|
{[]byte{0b00000010}, 7, []byte{0b10000010}},
|
|
{[]byte{0b10000010}, 9, []byte{0b10000010, 0b00000010}},
|
|
{[]byte{0b10000010}, 27, []byte{0b10000010, 0b00000000, 0b00000000, 0b00001000}},
|
|
{[]byte{0b10000010, 0b00000000}, 8, []byte{0b10000010, 0b00000001}},
|
|
{[]byte{0b10000010, 0b00000000}, 31, []byte{0b10000010, 0b00000000, 0b00000000, 0b10000000}},
|
|
}
|
|
for _, tt := range tests {
|
|
assert.DeepEqual(t, tt.c, bytesutil.SetBit(tt.a, tt.b))
|
|
}
|
|
}
|
|
|
|
func TestClearBit(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b int
|
|
c []byte
|
|
}{
|
|
{[]byte{0b00000000}, 1, []byte{0b00000000}},
|
|
{[]byte{0b00000010}, 1, []byte{0b00000000}},
|
|
{[]byte{0b10000010}, 1, []byte{0b10000000}},
|
|
{[]byte{0b10000010}, 8, []byte{0b10000010}},
|
|
{[]byte{0b10000010, 0b00001111}, 7, []byte{0b00000010, 0b00001111}},
|
|
{[]byte{0b10000010, 0b00001111}, 10, []byte{0b10000010, 0b00001011}},
|
|
}
|
|
for _, tt := range tests {
|
|
assert.DeepEqual(t, tt.c, bytesutil.ClearBit(tt.a, tt.b))
|
|
}
|
|
}
|
|
|
|
func TestMakeEmptyBitfields(t *testing.T) {
|
|
tests := []struct {
|
|
a int
|
|
b int
|
|
}{
|
|
{0, 1},
|
|
{1, 1},
|
|
{2, 1},
|
|
{7, 1},
|
|
{8, 2},
|
|
{15, 2},
|
|
{16, 3},
|
|
{100, 13},
|
|
{104, 14},
|
|
}
|
|
for _, tt := range tests {
|
|
assert.DeepEqual(t, tt.b, len(bytesutil.MakeEmptyBitlists(tt.a)))
|
|
}
|
|
}
|
|
|
|
func TestHighestBitIndex(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b int
|
|
error bool
|
|
}{
|
|
{nil, 0, true},
|
|
{[]byte{}, 0, true},
|
|
{[]byte{0b00000001}, 1, false},
|
|
{[]byte{0b10100101}, 8, false},
|
|
{[]byte{0x00, 0x00}, 0, false},
|
|
{[]byte{0xff, 0xa0}, 16, false},
|
|
{[]byte{12, 34, 56, 78}, 31, false},
|
|
{[]byte{255, 255, 255, 255}, 32, false},
|
|
}
|
|
for _, tt := range tests {
|
|
i, err := bytesutil.HighestBitIndex(tt.a)
|
|
if !tt.error {
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, tt.b, i)
|
|
} else {
|
|
assert.ErrorContains(t, "input list can't be empty or nil", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHighestBitIndexBelow(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b int
|
|
c int
|
|
error bool
|
|
}{
|
|
{nil, 0, 0, true},
|
|
{[]byte{}, 0, 0, true},
|
|
{[]byte{0b00010001}, 0, 0, false},
|
|
{[]byte{0b00010001}, 1, 1, false},
|
|
{[]byte{0b00010001}, 2, 1, false},
|
|
{[]byte{0b00010001}, 4, 1, false},
|
|
{[]byte{0b00010001}, 5, 5, false},
|
|
{[]byte{0b00010001}, 8, 5, false},
|
|
{[]byte{0b00010001, 0b00000000}, 0, 0, false},
|
|
{[]byte{0b00010001, 0b00000000}, 1, 1, false},
|
|
{[]byte{0b00010001, 0b00000000}, 2, 1, false},
|
|
{[]byte{0b00010001, 0b00000000}, 4, 1, false},
|
|
{[]byte{0b00010001, 0b00000000}, 5, 5, false},
|
|
{[]byte{0b00010001, 0b00000000}, 8, 5, false},
|
|
{[]byte{0b00010001, 0b00000000}, 15, 5, false},
|
|
{[]byte{0b00010001, 0b00000000}, 16, 5, false},
|
|
{[]byte{0b00010001, 0b00100010}, 8, 5, false},
|
|
{[]byte{0b00010001, 0b00100010}, 9, 5, false},
|
|
{[]byte{0b00010001, 0b00100010}, 10, 10, false},
|
|
{[]byte{0b00010001, 0b00100010}, 11, 10, false},
|
|
{[]byte{0b00010001, 0b00100010}, 14, 14, false},
|
|
{[]byte{0b00010001, 0b00100010}, 15, 14, false},
|
|
{[]byte{0b00010001, 0b00100010}, 24, 14, false},
|
|
{[]byte{0b00010001, 0b00100010, 0b10000000}, 23, 14, false},
|
|
{[]byte{0b00010001, 0b00100010, 0b10000000}, 24, 24, false},
|
|
{[]byte{0b00000000, 0b00000001, 0b00000011}, 17, 17, false},
|
|
{[]byte{0b00000000, 0b00000001, 0b00000011}, 18, 18, false},
|
|
{[]byte{12, 34, 56, 78}, 1000, 31, false},
|
|
{[]byte{255, 255, 255, 255}, 1000, 32, false},
|
|
}
|
|
for _, tt := range tests {
|
|
i, err := bytesutil.HighestBitIndexAt(tt.a, tt.b)
|
|
if !tt.error {
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, tt.c, i)
|
|
} else {
|
|
assert.ErrorContains(t, "input list can't be empty or nil", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUint64ToBytes_RoundTrip(t *testing.T) {
|
|
for i := uint64(0); i < 10000; i++ {
|
|
b := bytesutil.Uint64ToBytesBigEndian(i)
|
|
if got := bytesutil.BytesToUint64BigEndian(b); got != i {
|
|
t.Error("Round trip did not match original value")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsHex(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b bool
|
|
}{
|
|
{nil, false},
|
|
{[]byte(""), false},
|
|
{[]byte("0x"), false},
|
|
{[]byte("0x0"), true},
|
|
{[]byte("foo"), false},
|
|
{[]byte("1234567890abcDEF"), false},
|
|
{[]byte("XYZ4567890abcDEF1234567890abcDEF1234567890abcDEF1234567890abcDEF"), false},
|
|
{[]byte("0x1234567890abcDEF1234567890abcDEF1234567890abcDEF1234567890abcDEF"), true},
|
|
{[]byte("1234567890abcDEF1234567890abcDEF1234567890abcDEF1234567890abcDEF"), false},
|
|
}
|
|
for _, tt := range tests {
|
|
isHex := bytesutil.IsHex(tt.a)
|
|
assert.Equal(t, tt.b, isHex)
|
|
}
|
|
}
|
|
|
|
func TestSafeCopyRootAtIndex(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input [][]byte
|
|
idx uint64
|
|
want []byte
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "index out of range in non-empty slice",
|
|
input: [][]byte{{0x1}, {0x2}},
|
|
idx: 2,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "index out of range in empty slice",
|
|
input: [][]byte{},
|
|
idx: 0,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "nil input",
|
|
input: nil,
|
|
idx: 3,
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "correct copy",
|
|
input: [][]byte{{0x1}, {0x2}},
|
|
idx: 1,
|
|
want: bytesutil.PadTo([]byte{0x2}, 32),
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := bytesutil.SafeCopyRootAtIndex(tt.input, tt.idx)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("SafeCopyRootAtIndex() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("SafeCopyRootAtIndex() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSafeCopy2dBytes(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input [][]byte
|
|
}{
|
|
{
|
|
name: "nil input",
|
|
input: nil,
|
|
},
|
|
{
|
|
name: "correct copy",
|
|
input: [][]byte{{0x1}, {0x2}},
|
|
},
|
|
{
|
|
name: "empty",
|
|
input: [][]byte{},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := bytesutil.SafeCopy2dBytes(tt.input); !reflect.DeepEqual(got, tt.input) {
|
|
t.Errorf("SafeCopy2dBytes() = %v, want %v", got, tt.input)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBytesInvalidInputs(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("Test panicked: %v", r)
|
|
}
|
|
}()
|
|
rawBytes := bytesutil.ToBytes(100, -10)
|
|
assert.DeepEqual(t, []byte{}, rawBytes)
|
|
|
|
_, err := bytesutil.HighestBitIndexAt([]byte{'A', 'B', 'C'}, -5)
|
|
assert.ErrorContains(t, "index is negative", err)
|
|
|
|
// There should be no panic
|
|
_ = bytesutil.ClearBit([]byte{'C', 'D', 'E'}, -7)
|
|
res := bytesutil.FromBytes4([]byte{})
|
|
assert.Equal(t, res, uint64(0))
|
|
newRes := bytesutil.FromBytes2([]byte{})
|
|
assert.Equal(t, newRes, uint16(0))
|
|
res = bytesutil.FromBytes8([]byte{})
|
|
assert.Equal(t, res, uint64(0))
|
|
|
|
intRes := bytesutil.ToLowInt64([]byte{})
|
|
assert.Equal(t, intRes, int64(0))
|
|
}
|
|
|
|
func TestReverseByteOrder(t *testing.T) {
|
|
input := []byte{0, 1, 2, 3, 4, 5}
|
|
expectedResult := []byte{5, 4, 3, 2, 1, 0}
|
|
output := bytesutil.ReverseByteOrder(input)
|
|
|
|
// check that the input is not modified and the output is reversed
|
|
assert.Equal(t, bytes.Equal(input, []byte{0, 1, 2, 3, 4, 5}), true)
|
|
assert.Equal(t, bytes.Equal(expectedResult, output), true)
|
|
}
|
|
|
|
func TestSafeCopy2d32Bytes(t *testing.T) {
|
|
input := make([][32]byte, 2)
|
|
input[0] = bytesutil.ToBytes32([]byte{'a'})
|
|
input[1] = bytesutil.ToBytes32([]byte{'b'})
|
|
output := bytesutil.SafeCopy2d32Bytes(input)
|
|
assert.Equal(t, false, &input == &output, "No copy was made")
|
|
assert.DeepEqual(t, input, output)
|
|
}
|
|
|
|
func TestNonZeroRoot(t *testing.T) {
|
|
input := make([]byte, fieldparams.RootLength)
|
|
output := bytesutil.NonZeroRoot(input)
|
|
assert.Equal(t, false, output)
|
|
copy(input[2:], "a")
|
|
copy(input[3:], "b")
|
|
output = bytesutil.NonZeroRoot(input)
|
|
assert.Equal(t, true, output)
|
|
}
|