github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/test/message.go (about)

     1  package p2ptest
     2  
     3  import (
     4  	"testing"
     5  
     6  	pb "github.com/libp2p/go-libp2p-pubsub/pb"
     7  	"github.com/libp2p/go-libp2p/core/peer"
     8  
     9  	"github.com/onflow/flow-go/utils/unittest"
    10  )
    11  
    12  // WithFrom is a test helper that returns a function that sets the from field of a pubsub message to the given peer id.
    13  func WithFrom(from peer.ID) func(*pb.Message) {
    14  	return func(m *pb.Message) {
    15  		m.From = []byte(from)
    16  	}
    17  }
    18  
    19  // WithTopic is a test helper that returns a function that sets the topic of a pubsub message to the given topic.
    20  func WithTopic(topic string) func(*pb.Message) {
    21  	return func(m *pb.Message) {
    22  		m.Topic = &topic
    23  	}
    24  }
    25  
    26  // WithoutSignature is a test helper that returns a function that sets the signature of a pubsub message to nil, effectively removing the signature.
    27  func WithoutSignature() func(*pb.Message) {
    28  	return func(m *pb.Message) {
    29  		m.Signature = nil
    30  	}
    31  }
    32  
    33  // WithoutSignerId is a test helper that returns a function that sets the from field of a pubsub message to nil, effectively removing the signer id.
    34  func WithoutSignerId() func(*pb.Message) {
    35  	return func(m *pb.Message) {
    36  		m.From = nil
    37  	}
    38  }
    39  
    40  // PubsubMessageFixture is a test helper that returns a random pubsub message with the given options applied.
    41  // If no options are provided, the message will be random.
    42  // Args:
    43  //
    44  //	t: testing.T
    45  //
    46  // opt: variadic list of options to apply to the message
    47  // Returns:
    48  // *pb.Message: pubsub message
    49  func PubsubMessageFixture(t *testing.T, opts ...func(*pb.Message)) *pb.Message {
    50  	topic := unittest.RandomStringFixture(t, 10)
    51  
    52  	m := &pb.Message{
    53  		Data:      unittest.RandomByteSlice(t, 100),
    54  		Topic:     &topic,
    55  		Signature: unittest.RandomByteSlice(t, 100),
    56  		From:      unittest.RandomByteSlice(t, 100),
    57  		Seqno:     unittest.RandomByteSlice(t, 100),
    58  	}
    59  
    60  	for _, opt := range opts {
    61  		opt(m)
    62  	}
    63  
    64  	return m
    65  }