Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace bytes.Equal with assert.DeepEqual in tests #8318

Merged
merged 11 commits into from
Jan 22, 2021
4 changes: 1 addition & 3 deletions beacon-chain/blockchain/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ func TestSaveHead_Different(t *testing.T) {

cachedRoot, err := service.HeadRoot(context.Background())
require.NoError(t, err)
if !bytes.Equal(cachedRoot, newRoot[:]) {
t.Error("Head did not change")
}
assert.DeepEqual(t, cachedRoot, newRoot[:], "Head did not change")
assert.DeepEqual(t, newHeadSignedBlock, service.headBlock(), "Head did not change")
assert.DeepEqual(t, headState.CloneInnerState(), service.headState(ctx).CloneInnerState(), "Head did not change")
}
Expand Down
9 changes: 1 addition & 8 deletions beacon-chain/core/blocks/randao_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package blocks_test

import (
"bytes"
"context"
"encoding/binary"
"testing"
Expand Down Expand Up @@ -64,13 +63,7 @@ func TestProcessRandao_SignatureVerifiesAndUpdatesLatestStateMixes(t *testing.T)
require.NoError(t, err, "Unexpected error processing block randao")
currentEpoch := helpers.CurrentEpoch(beaconState)
mix := newState.RandaoMixes()[currentEpoch%params.BeaconConfig().EpochsPerHistoricalVector]

if bytes.Equal(mix, params.BeaconConfig().ZeroHash[:]) {
t.Errorf(
"Expected empty signature to be overwritten by randao reveal, received %v",
params.BeaconConfig().EmptySignature,
)
}
assert.DeepNotEqual(t, params.BeaconConfig().ZeroHash[:], mix, "Expected empty signature to be overwritten by randao reveal")
}

func TestRandaoSignatureSet_OK(t *testing.T) {
Expand Down
20 changes: 7 additions & 13 deletions beacon-chain/core/state/skip_slot_cache_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package state_test

import (
"bytes"
"context"
"sync"
"testing"
Expand All @@ -11,6 +10,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/sszutil"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

Expand Down Expand Up @@ -113,9 +113,8 @@ func TestSkipSlotCache_ConcurrentMixup(t *testing.T) {

tmp1, err := state.ProcessSlots(context.Background(), expected1.Copy(), problemSlot+1)
require.NoError(t, err)
if gotRoot := tmp1.StateRoots()[problemSlot]; !bytes.Equal(gotRoot, expectedRoot1[:]) {
t.Fatalf("state roots for chain 1 are bad, expected root doesn't match: %x <> %x", gotRoot, expectedRoot1[:])
}
gotRoot := tmp1.StateRoots()[problemSlot]
require.DeepEqual(t, expectedRoot1[:], gotRoot, "State roots for chain 1 are bad, expected root doesn't match")

expected2, err := state.ProcessSlots(context.Background(), state2.Copy(), problemSlot)
require.NoError(t, err)
Expand All @@ -125,9 +124,8 @@ func TestSkipSlotCache_ConcurrentMixup(t *testing.T) {

tmp2, err := state.ProcessSlots(context.Background(), expected2.Copy(), problemSlot+1)
require.NoError(t, err)
if gotRoot := tmp2.StateRoots()[problemSlot]; !bytes.Equal(gotRoot, expectedRoot2[:]) {
t.Fatalf("state roots for chain 2 are bad, expected root doesn't match %x <> %x", gotRoot, expectedRoot2[:])
}
gotRoot = tmp2.StateRoots()[problemSlot]
require.DeepEqual(t, expectedRoot2[:], gotRoot, "State roots for chain 2 are bad, expected root doesn't match")

var wg sync.WaitGroup
wg.Add(len(setups))
Expand All @@ -139,13 +137,9 @@ func TestSkipSlotCache_ConcurrentMixup(t *testing.T) {
roots := outState.StateRoots()
gotRoot := roots[problemSlot]
if i%2 == 0 {
if !bytes.Equal(gotRoot, expectedRoot1[:]) {
t.Errorf("unexpected root on chain 1, item %3d: %x", i, gotRoot)
}
assert.DeepEqual(t, expectedRoot1[:], gotRoot, "Unexpected root on chain 1")
} else {
if !bytes.Equal(gotRoot, expectedRoot2[:]) {
t.Errorf("unexpected root on chain 2, item %3d: %x", i, gotRoot)
}
assert.DeepEqual(t, expectedRoot2[:], gotRoot, "Unexpected root on chain 2")
}
wg.Done()
}
Expand Down
5 changes: 1 addition & 4 deletions beacon-chain/core/state/transition_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package state_test

import (
"bytes"
"context"
"encoding/binary"
"fmt"
Expand Down Expand Up @@ -101,9 +100,7 @@ func TestExecuteStateTransition_FullProcess(t *testing.T) {

mix, err := beaconState.RandaoMixAtIndex(1)
require.NoError(t, err)
if bytes.Equal(mix, oldMix) {
t.Errorf("Did not expect new and old randao mix to equal, %#x == %#x", mix, oldMix)
}
assert.DeepNotEqual(t, oldMix, mix, "Did not expect new and old randao mix to equal")
}

func TestExecuteStateTransitionNoVerify_FullProcess(t *testing.T) {
Expand Down
11 changes: 3 additions & 8 deletions beacon-chain/db/kv/migration_archived_index_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package kv

import (
"bytes"
"context"
"fmt"
"testing"

"github.com/prysmaticlabs/prysm/shared/bytesutil"
Expand Down Expand Up @@ -34,9 +32,7 @@ func Test_migrateArchivedIndex(t *testing.T) {
eval: func(t *testing.T, db *bbolt.DB) {
err := db.View(func(tx *bbolt.Tx) error {
v := tx.Bucket(archivedRootBucket).Get(bytesutil.Uint64ToBytesLittleEndian(2048))
if !bytes.Equal(v, []byte("foo")) {
return fmt.Errorf("did not receive correct data for key 2048, wanted 'foo' got %s", v)
}
assert.DeepEqual(t, []byte("foo"), v, "Did not receive correct data for key 2048")
return nil
})
assert.NoError(t, err)
Expand Down Expand Up @@ -66,9 +62,8 @@ func Test_migrateArchivedIndex(t *testing.T) {
eval: func(t *testing.T, db *bbolt.DB) {
err := db.View(func(tx *bbolt.Tx) error {
k := uint64(2048)
if v := tx.Bucket(stateSlotIndicesBucket).Get(bytesutil.Uint64ToBytesBigEndian(k)); !bytes.Equal(v, []byte("foo")) {
return fmt.Errorf("did not receive correct data for key %d, wanted 'foo' got %v", k, v)
}
v := tx.Bucket(stateSlotIndicesBucket).Get(bytesutil.Uint64ToBytesBigEndian(k))
assert.DeepEqual(t, []byte("foo"), v, "Did not receive correct data for key %d", k)
return nil
})
assert.NoError(t, err)
Expand Down
11 changes: 3 additions & 8 deletions beacon-chain/db/kv/migration_block_slot_index_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package kv

import (
"bytes"
"fmt"
"testing"

"github.com/prysmaticlabs/prysm/shared/bytesutil"
Expand Down Expand Up @@ -30,9 +28,7 @@ func Test_migrateBlockSlotIndex(t *testing.T) {
eval: func(t *testing.T, db *bbolt.DB) {
err := db.View(func(tx *bbolt.Tx) error {
v := tx.Bucket(blockSlotIndicesBucket).Get([]byte("2048"))
if !bytes.Equal(v, []byte("foo")) {
return fmt.Errorf("did not receive correct data for key 2048, wanted 'foo' got %s", v)
}
assert.DeepEqual(t, []byte("foo"), v, "Did not receive correct data for key 2048")
return nil
})
assert.NoError(t, err)
Expand All @@ -49,9 +45,8 @@ func Test_migrateBlockSlotIndex(t *testing.T) {
eval: func(t *testing.T, db *bbolt.DB) {
err := db.View(func(tx *bbolt.Tx) error {
k := uint64(2048)
if v := tx.Bucket(blockSlotIndicesBucket).Get(bytesutil.Uint64ToBytesBigEndian(k)); !bytes.Equal(v, []byte("foo")) {
return fmt.Errorf("did not receive correct data for key %d, wanted 'foo' got %v", k, v)
}
v := tx.Bucket(blockSlotIndicesBucket).Get(bytesutil.Uint64ToBytesBigEndian(k))
assert.DeepEqual(t, []byte("foo"), v, "Did not receive correct data for key %d", k)
return nil
})
assert.NoError(t, err)
Expand Down
15 changes: 5 additions & 10 deletions beacon-chain/p2p/fork_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package p2p

import (
"bytes"
"context"
"math/rand"
"os"
Expand Down Expand Up @@ -249,12 +248,8 @@ func TestDiscv5_AddRetrieveForkEntryENR(t *testing.T) {

resp, err := retrieveForkEntry(localNode.Node().Record())
require.NoError(t, err)
if !bytes.Equal(resp.CurrentForkDigest, want[:]) {
t.Errorf("Wanted fork digest: %v, received %v", want, resp.CurrentForkDigest)
}
if !bytes.Equal(resp.NextForkVersion, nextForkVersion) {
t.Errorf("Wanted next fork version: %v, received %v", nextForkVersion, resp.NextForkVersion)
}
assert.DeepEqual(t, want[:], resp.CurrentForkDigest)
assert.DeepEqual(t, nextForkVersion, resp.NextForkVersion)
assert.Equal(t, nextForkEpoch, resp.NextForkEpoch, "Unexpected next fork epoch")
}

Expand All @@ -273,7 +268,7 @@ func TestAddForkEntry_Genesis(t *testing.T) {
require.NoError(t, err)
forkEntry, err := retrieveForkEntry(localNode.Node().Record())
require.NoError(t, err)
if !bytes.Equal(forkEntry.NextForkVersion, params.BeaconConfig().GenesisForkVersion) {
t.Errorf("Wanted Next Fork Version to be equal to genesis fork version, instead got %#x", forkEntry.NextForkVersion)
}
assert.DeepEqual(t,
params.BeaconConfig().GenesisForkVersion, forkEntry.NextForkVersion,
"Wanted Next Fork Version to be equal to genesis fork version")
}
6 changes: 2 additions & 4 deletions beacon-chain/p2p/options_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package p2p

import (
"bytes"
"crypto/rand"
"encoding/hex"
"io/ioutil"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

Expand Down Expand Up @@ -40,9 +40,7 @@ func TestPrivateKeyLoading(t *testing.T) {
require.NoError(t, err)
newRaw, err := newPkey.Raw()
require.NoError(t, err)
if !bytes.Equal(newRaw, rawBytes) {
t.Errorf("Private keys do not match got %#x but wanted %#x", rawBytes, newRaw)
}
assert.DeepEqual(t, rawBytes, newRaw, "Private keys do not match")
}

func TestIPV6Support(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions beacon-chain/p2p/types/types_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"bytes"
"encoding/hex"
"testing"

Expand Down Expand Up @@ -132,11 +131,11 @@ func TestSSZUint64(t *testing.T) {

serializedBytes, err := s.MarshalSSZ()
require.NoError(t, err)
require.Equal(t, true, bytes.Equal(serializedBytes, tt.serializedBytes), "serialized does not equal")
require.DeepEqual(t, tt.serializedBytes, serializedBytes)

htr, err := s.HashTreeRoot()
require.NoError(t, err)
require.Equal(t, true, bytes.Equal(htr[:], tt.root), "root does not equal")
require.DeepEqual(t, tt.root, htr[:])
})
}
}
Expand Down Expand Up @@ -172,7 +171,7 @@ func TestSSZBytes_HashTreeRoot(t *testing.T) {
s := SSZBytes(tt.actualValue)
htr, err := s.HashTreeRoot()
require.NoError(t, err)
require.Equal(t, true, bytes.Equal(htr[:], tt.root), "root does not equal")
require.DeepEqual(t, tt.root, htr[:])
})
}
}
Expand Down
6 changes: 1 addition & 5 deletions beacon-chain/rpc/beaconv1/blocks_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package beaconv1

import (
"bytes"
"context"
"reflect"
"testing"
Expand Down Expand Up @@ -488,10 +487,7 @@ func TestServer_GetBlockRoot(t *testing.T) {
return
}
require.NoError(t, err)

if !bytes.Equal(blockRootResp.Data.Root, tt.want) {
t.Errorf("Expected hashes to equal, expected: %#x, received: %#x", tt.want, blockRootResp.Data.Root)
}
assert.DeepEqual(t, tt.want, blockRootResp.Data.Root)
})
}
}
Expand Down
Loading