prysm-pulse/encoding/bytesutil/bytes_test.go

291 lines
8.0 KiB
Go
Raw Normal View History

package bytesutil_test
import (
"bytes"
"fmt"
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v5/testing/assert"
)
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)
Applies assertion funcs to shared tests (part 1) (#6626) * Update kv aggregated_test.go * Update block_test.go * Update forkchoice_test.go * Update unaggregated_test.go * Update prepare_forkchoice_test.go * Update prune_expired_test.go * Update atts service_test.go * Update service_attester_test.go * Update service_proposer_test.go * Upate exit service_test.go * Gaz * Merge branch 'master' of github.com:prysmaticlabs/prysm * Move averageBalance from log.go to info.go * Move avg balance from log.go to info.go * Add info test * Remove unused logEpochData in log.go * Gaz * Merge branch 'master' of github.com:prysmaticlabs/prysm * Merge branch 'master' of github.com:prysmaticlabs/prysm * aggregation tests * attestation util tests * bench util tests * block util tests * herumi tests * bytesutil tests * Merge refs/heads/master into testutil-shared * Merge refs/heads/master into testutil-shared * Merge refs/heads/master into testutil-shared * Merge refs/heads/master into testutil-shared * Merge refs/heads/master into testutil-shared * Update shared/aggregation/attestations/attestations_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Merge branch 'master' of github.com:prysmaticlabs/prysm into testutil-shared * Fixed ordering * Merge branch 'testutil-shared' of github.com:prysmaticlabs/prysm into testutil-shared * Update shared/aggregation/attestations/attestations_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Update shared/bytesutil/bytes_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
2020-07-18 16:31:42 +00:00
assert.DeepEqual(t, tt.b, b)
}
}
Bound Initial Sync Cache Size (#4844) * bound initial sync * fix lint * Revert "Better block attestation inclusion (#4838)" This reverts commit 090d9627feed47281f9ab9660bb4d9854c491a7a. * add memory pool * more fixes * revert changes * add hack * revert hack * push halving * bring back hack * increase cache size * more fixes * more changes * new fixes * add test * add reverse test * more tests and clean up * add helper * more cleanup and tests * fix test * remove code * set gc percent flag * lint * lint * Fix comment formatting * Fix some formatting * inverse if statement * remove debug log * Apply suggestions from code review Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com> * Update beacon-chain/state/getters.go Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com> * Update beacon-chain/db/kv/state.go * integrate state generator * gaz * fixes * terence's review * reduce bound further * fix test * separate into new files * gaz * mod build file * add test * revert changes * fix test * Update beacon-chain/core/helpers/slot_epoch.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> * handle edge case * add back test * fix test again * handle edge case * Update beacon-chain/blockchain/init_sync_process_block.go * Update beacon-chain/blockchain/init_sync_process_block.go * Update beacon-chain/stategen/service_test.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/blockchain/init_sync_process_block.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/stategen/service.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/stategen/service.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * raul's review * raul's review * fix refs * terence's review * one more fix * Update beacon-chain/blockchain/init_sync_process_block.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-02-18 21:10:54 +00:00
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}}},
Bound Initial Sync Cache Size (#4844) * bound initial sync * fix lint * Revert "Better block attestation inclusion (#4838)" This reverts commit 090d9627feed47281f9ab9660bb4d9854c491a7a. * add memory pool * more fixes * revert changes * add hack * revert hack * push halving * bring back hack * increase cache size * more fixes * more changes * new fixes * add test * add reverse test * more tests and clean up * add helper * more cleanup and tests * fix test * remove code * set gc percent flag * lint * lint * Fix comment formatting * Fix some formatting * inverse if statement * remove debug log * Apply suggestions from code review Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com> * Update beacon-chain/state/getters.go Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com> * Update beacon-chain/db/kv/state.go * integrate state generator * gaz * fixes * terence's review * reduce bound further * fix test * separate into new files * gaz * mod build file * add test * revert changes * fix test * Update beacon-chain/core/helpers/slot_epoch.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> * handle edge case * add back test * fix test again * handle edge case * Update beacon-chain/blockchain/init_sync_process_block.go * Update beacon-chain/blockchain/init_sync_process_block.go * Update beacon-chain/stategen/service_test.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/blockchain/init_sync_process_block.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/stategen/service.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/stategen/service.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * raul's review * raul's review * fix refs * terence's review * one more fix * Update beacon-chain/blockchain/init_sync_process_block.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-02-18 21:10:54 +00:00
{[][32]byte{}, [][32]byte{}},
}
for _, tt := range tests {
b := bytesutil.ReverseBytes32Slice(tt.input)
Applies assertion funcs to shared tests (part 1) (#6626) * Update kv aggregated_test.go * Update block_test.go * Update forkchoice_test.go * Update unaggregated_test.go * Update prepare_forkchoice_test.go * Update prune_expired_test.go * Update atts service_test.go * Update service_attester_test.go * Update service_proposer_test.go * Upate exit service_test.go * Gaz * Merge branch 'master' of github.com:prysmaticlabs/prysm * Move averageBalance from log.go to info.go * Move avg balance from log.go to info.go * Add info test * Remove unused logEpochData in log.go * Gaz * Merge branch 'master' of github.com:prysmaticlabs/prysm * Merge branch 'master' of github.com:prysmaticlabs/prysm * aggregation tests * attestation util tests * bench util tests * block util tests * herumi tests * bytesutil tests * Merge refs/heads/master into testutil-shared * Merge refs/heads/master into testutil-shared * Merge refs/heads/master into testutil-shared * Merge refs/heads/master into testutil-shared * Merge refs/heads/master into testutil-shared * Update shared/aggregation/attestations/attestations_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Merge branch 'master' of github.com:prysmaticlabs/prysm into testutil-shared * Fixed ordering * Merge branch 'testutil-shared' of github.com:prysmaticlabs/prysm into testutil-shared * Update shared/aggregation/attestations/attestations_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Update shared/bytesutil/bytes_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
2020-07-18 16:31:42 +00:00
assert.DeepEqual(t, tt.output, b)
Bound Initial Sync Cache Size (#4844) * bound initial sync * fix lint * Revert "Better block attestation inclusion (#4838)" This reverts commit 090d9627feed47281f9ab9660bb4d9854c491a7a. * add memory pool * more fixes * revert changes * add hack * revert hack * push halving * bring back hack * increase cache size * more fixes * more changes * new fixes * add test * add reverse test * more tests and clean up * add helper * more cleanup and tests * fix test * remove code * set gc percent flag * lint * lint * Fix comment formatting * Fix some formatting * inverse if statement * remove debug log * Apply suggestions from code review Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com> * Update beacon-chain/state/getters.go Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com> * Update beacon-chain/db/kv/state.go * integrate state generator * gaz * fixes * terence's review * reduce bound further * fix test * separate into new files * gaz * mod build file * add test * revert changes * fix test * Update beacon-chain/core/helpers/slot_epoch.go Co-Authored-By: terence tsao <terence@prysmaticlabs.com> * handle edge case * add back test * fix test again * handle edge case * Update beacon-chain/blockchain/init_sync_process_block.go * Update beacon-chain/blockchain/init_sync_process_block.go * Update beacon-chain/stategen/service_test.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/blockchain/init_sync_process_block.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/stategen/service.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/stategen/service.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * raul's review * raul's review * fix refs * terence's review * one more fix * Update beacon-chain/blockchain/init_sync_process_block.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Ivan Martinez <ivanthegreatdev@gmail.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-02-18 21:10:54 +00:00
}
}
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)
}
2022-01-24 10:24:38 +00:00
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)
}
Web3Signer: CLI implementation (#10056) * 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>
2022-01-31 16:44:17 +00:00
func TestSafeCopy2dHexUtilBytes(t *testing.T) {
input := make([]hexutil.Bytes, 2)
input[0] = hexutil.Bytes{'a'}
input[1] = hexutil.Bytes{'b'}
output := bytesutil.SafeCopy2dHexUtilBytes(input)
assert.DeepEqual(t, output, [][]byte{{'a'}, {'b'}})
}
func TestToBytes48Array(t *testing.T) {
tests := []struct {
a [][]byte
b [][48]byte
}{
{[][]byte{{0}}, [][48]byte{{0}}},
{[][]byte{{253}}, [][48]byte{{253}}},
{[][]byte{{254, 255, 255, 255, 255, 255, 255, 127}},
[][48]byte{{254, 255, 255, 255, 255, 255, 255, 127}}},
{[][]byte{{255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255}},
[][48]byte{{255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255}},
},
}
for _, tt := range tests {
b := bytesutil.ToBytes48Array(tt.a)
assert.DeepEqual(t, tt.b, b)
}
}
func TestToBytes20(t *testing.T) {
tests := []struct {
a []byte
b [20]byte
}{
{nil, [20]byte{}},
{[]byte{}, [20]byte{}},
{[]byte{1}, [20]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{[]byte{1, 2, 3}, [20]byte{1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{[]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}},
{[]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}, [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}},
}
for _, tt := range tests {
b := bytesutil.ToBytes20(tt.a)
assert.DeepEqual(t, tt.b, b)
}
}
func BenchmarkToBytes32(b *testing.B) {
x := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}
for i := 0; i < b.N; i++ {
bytesutil.ToBytes32(x)
}
}
func TestFromBytes48Array(t *testing.T) {
tests := []struct {
a [][]byte
b [][48]byte
}{
{[][]byte{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[][48]byte{{0}}},
{[][]byte{{253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[][48]byte{{253}}},
{[][]byte{{254, 255, 255, 255, 255, 255, 255, 127, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[][48]byte{{254, 255, 255, 255, 255, 255, 255, 127}}},
{[][]byte{{255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255}},
[][48]byte{{255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255}},
},
}
for _, tt := range tests {
a := bytesutil.FromBytes48Array(tt.b)
assert.DeepEqual(t, tt.a, a)
}
}
func TestSafeCopyBytes_Copy(t *testing.T) {
slice := make([]byte, 32)
slice[0] = 'A'
copiedSlice := bytesutil.SafeCopyBytes(slice)
assert.NotEqual(t, fmt.Sprintf("%p", slice), fmt.Sprintf("%p", copiedSlice))
assert.Equal(t, slice[0], copiedSlice[0])
slice[1] = 'B'
assert.NotEqual(t, slice[1], copiedSlice[1])
}
func BenchmarkSafeCopyBytes(b *testing.B) {
dSlice := make([][]byte, 900000)
for i := 0; i < 900000; i++ {
slice := make([]byte, 32)
slice[0] = 'A'
dSlice[i] = slice
}
b.ReportAllocs()
b.ResetTimer()
b.Run("Copy Bytes", func(b *testing.B) {
cSlice := bytesutil.SafeCopy2dBytes(dSlice)
a := cSlice
_ = a
})
}