Skip to content

Commit

Permalink
chore: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Jun 13, 2022
1 parent 550d8af commit 7b0ef6d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fraud/bad_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ func (p *BadEncodingProof) MarshalBinary() ([]byte, error) {
return badEncodingFraudProof.Marshal()
}

// UnmarshalBEFP converts given data to BadEncodingProof.
func UnmarshalBEFP(data []byte) (Proof, error) {
befp := &BadEncodingProof{}
if err := befp.UnmarshalBinary(data); err != nil {
return nil, err
}
return befp, nil
}

// UnmarshalBinary converts binary to BadEncodingProof
func (p *BadEncodingProof) UnmarshalBinary(data []byte) error {
in := pb.BadEncoding{}
Expand Down
70 changes: 70 additions & 0 deletions fraud/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package fraud

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

pubsub "github.com/libp2p/go-libp2p-pubsub"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"

"github.com/celestiaorg/celestia-node/ipld"
)

func TestService_RegisterUnmarshaler(t *testing.T) {
s := createService(t)
require.NoError(t, s.RegisterUnmarshaler(BadEncoding, UnmarshalBEFP))

require.Error(t, s.RegisterUnmarshaler(BadEncoding, UnmarshalBEFP))
}

func TestService_UnregisterUnmarshaler(t *testing.T) {
s := createService(t)
require.NoError(t, s.RegisterUnmarshaler(BadEncoding, UnmarshalBEFP))
require.NoError(t, s.UnregisterUnmarshaler(BadEncoding))

require.Error(t, s.UnregisterUnmarshaler(BadEncoding))
}

func TestService_SubscribeFails(t *testing.T) {
s := createService(t)

_, err := s.Subscribe(BadEncoding)
require.Error(t, err)
}

func TestService_Subscribe(t *testing.T) {
s := createService(t)
require.NoError(t, s.RegisterUnmarshaler(BadEncoding, UnmarshalBEFP))

_, err := s.Subscribe(BadEncoding)
require.NoError(t, err)
}

func TestService_BroadcastFails(t *testing.T) {
s := createService(t)
p := CreateBadEncodingProof(0, &ipld.ErrByzantine{
Index: 0,
Shares: make([]*ipld.ShareWithProof, 0),
},
)
require.Error(t, s.Broadcast(context.TODO(), p))
}

func createService(t *testing.T) Service {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
t.Cleanup(cancel)

// create mock network
net, err := mocknet.FullMeshLinked(ctx, 2)
require.NoError(t, err)

// create pubsub for host
ps, err := pubsub.NewGossipSub(ctx, net.Hosts()[0],
pubsub.WithMessageSignaturePolicy(pubsub.StrictNoSign))
require.NoError(t, err)

return NewService(ps, nil)
}

0 comments on commit 7b0ef6d

Please sign in to comment.