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

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/onflow/flow-go/network/channels"
    10  	p2pmsg "github.com/onflow/flow-go/network/p2p/message"
    11  )
    12  
    13  // TestErrActiveClusterIDsNotSetRoundTrip ensures correct error formatting for ErrActiveClusterIdsNotSet.
    14  func TestErrActiveClusterIDsNotSetRoundTrip(t *testing.T) {
    15  	topic := channels.Topic("test-topic")
    16  	err := NewActiveClusterIdsNotSetErr(topic)
    17  
    18  	// tests the error message formatting.
    19  	expectedErrMsg := fmt.Errorf("failed to validate cluster prefixed topic %s no active cluster IDs set", topic).Error()
    20  	assert.Equal(t, expectedErrMsg, err.Error(), "the error message should be correctly formatted")
    21  
    22  	// tests the IsErrActiveClusterIDsNotSet function.
    23  	assert.True(t, IsErrActiveClusterIDsNotSet(err), "IsErrActiveClusterIDsNotSet should return true for ErrActiveClusterIdsNotSet error")
    24  
    25  	// test IsErrActiveClusterIDsNotSet with a different error type.
    26  	dummyErr := fmt.Errorf("dummy error")
    27  	assert.False(t, IsErrActiveClusterIDsNotSet(dummyErr), "IsErrActiveClusterIDsNotSet should return false for non-ErrActiveClusterIdsNotSet error")
    28  }
    29  
    30  // TestErrDuplicateTopicRoundTrip ensures correct error formatting for DuplicateTopicErr.
    31  func TestDuplicateTopicErrRoundTrip(t *testing.T) {
    32  	expectedErrorMsg := fmt.Sprintf("duplicate topic found in %s control message type: %s", p2pmsg.CtrlMsgGraft, channels.TestNetworkChannel)
    33  	err := NewDuplicateTopicErr(channels.TestNetworkChannel.String(), 1, p2pmsg.CtrlMsgGraft)
    34  	assert.Equal(t, expectedErrorMsg, err.Error(), "the error message should be correctly formatted")
    35  	// tests the IsDuplicateTopicErr function.
    36  	assert.True(t, IsDuplicateTopicErr(err), "IsDuplicateTopicErr should return true for DuplicateTopicErr error")
    37  	// test IsDuplicateTopicErr with a different error type.
    38  	dummyErr := fmt.Errorf("dummy error")
    39  	assert.False(t, IsDuplicateTopicErr(dummyErr), "IsDuplicateTopicErr should return false for non-DuplicateTopicErr error")
    40  }
    41  
    42  // TestErrDuplicateTopicRoundTrip ensures correct error formatting for DuplicateTopicErr.
    43  func TestDuplicateMessageIDErrRoundTrip(t *testing.T) {
    44  	msgID := "flow-1804flkjnafo"
    45  	expectedErrMsg1 := fmt.Sprintf("duplicate message ID foud in %s control message type: %s", p2pmsg.CtrlMsgIHave, msgID)
    46  	expectedErrMsg2 := fmt.Sprintf("duplicate message ID foud in %s control message type: %s", p2pmsg.CtrlMsgIWant, msgID)
    47  	err := NewDuplicateMessageIDErr(msgID, 1, p2pmsg.CtrlMsgIHave)
    48  	assert.Equal(t, expectedErrMsg1, err.Error(), "the error message should be correctly formatted")
    49  	// tests the IsDuplicateTopicErr function.
    50  	assert.True(t, IsDuplicateMessageIDErr(err), "IsDuplicateMessageIDErr should return true for DuplicateMessageIDErr error")
    51  	err = NewDuplicateMessageIDErr(msgID, 1, p2pmsg.CtrlMsgIWant)
    52  	assert.Equal(t, expectedErrMsg2, err.Error(), "the error message should be correctly formatted")
    53  	// tests the IsDuplicateTopicErr function.
    54  	assert.True(t, IsDuplicateMessageIDErr(err), "IsDuplicateMessageIDErr should return true for DuplicateMessageIDErr error")
    55  	// test IsDuplicateMessageIDErr with a different error type.
    56  	dummyErr := fmt.Errorf("dummy error")
    57  	assert.False(t, IsDuplicateMessageIDErr(dummyErr), "IsDuplicateMessageIDErr should return false for non-DuplicateMessageIDErr error")
    58  }
    59  
    60  // TestIWantCacheMissThresholdErrRoundTrip ensures correct error formatting for IWantCacheMissThresholdErr.
    61  func TestIWantCacheMissThresholdErrRoundTrip(t *testing.T) {
    62  	err := NewIWantCacheMissThresholdErr(5, 10, 5)
    63  
    64  	// tests the error message formatting.
    65  	expectedErrMsg := "5/10 iWant cache misses exceeds the allowed threshold: 5"
    66  	assert.Equal(t, expectedErrMsg, err.Error(), "the error message should be correctly formatted")
    67  
    68  	// tests the IsIWantCacheMissThresholdErr function.
    69  	assert.True(t, IsIWantCacheMissThresholdErr(err), "IsIWantCacheMissThresholdErr should return true for IWantCacheMissThresholdErr error")
    70  
    71  	// test IsIWantCacheMissThresholdErr with a different error type.
    72  	dummyErr := fmt.Errorf("dummy error")
    73  	assert.False(t, IsIWantCacheMissThresholdErr(dummyErr), "IsIWantCacheMissThresholdErr should return false for non-IWantCacheMissThresholdErr error")
    74  }
    75  
    76  // TestIWantDuplicateMsgIDThresholdErrRoundTrip ensures correct error formatting for IWantDuplicateMsgIDThresholdErr.
    77  func TestIWantDuplicateMsgIDThresholdErrRoundTrip(t *testing.T) {
    78  	err := NewIWantDuplicateMsgIDThresholdErr(5, 10, 5)
    79  
    80  	// tests the error message formatting.
    81  	expectedErrMsg := "5/10 iWant duplicate message ids exceeds the allowed threshold: 5"
    82  	assert.Equal(t, expectedErrMsg, err.Error(), "the error message should be correctly formatted")
    83  
    84  	// tests the IsIWantDuplicateMsgIDThresholdErr function.
    85  	assert.True(t, IsIWantDuplicateMsgIDThresholdErr(err), "IsIWantDuplicateMsgIDThresholdErr should return true for IWantDuplicateMsgIDThresholdErr error")
    86  
    87  	// test IsIWantDuplicateMsgIDThresholdErr with a different error type.
    88  	dummyErr := fmt.Errorf("dummy error")
    89  	assert.False(t, IsIWantDuplicateMsgIDThresholdErr(dummyErr), "IsIWantDuplicateMsgIDThresholdErr should return false for non-IWantDuplicateMsgIDThresholdErr error")
    90  }
    91  
    92  // TestInvalidRpcPublishMessagesErrRoundTrip ensures correct error formatting for InvalidRpcPublishMessagesErr.
    93  func TestInvalidRpcPublishMessagesErrRoundTrip(t *testing.T) {
    94  	wrappedErr := fmt.Errorf("invalid topic")
    95  	err := NewInvalidRpcPublishMessagesErr(wrappedErr, 1)
    96  
    97  	// tests the error message formatting.
    98  	expectedErrMsg := "rpc publish messages validation failed 1 error(s) encountered: invalid topic"
    99  	assert.Equal(t, expectedErrMsg, err.Error(), "the error message should be correctly formatted")
   100  
   101  	// tests the IsInvalidRpcPublishMessagesErr function.
   102  	assert.True(t, IsInvalidRpcPublishMessagesErr(err), "IsInvalidRpcPublishMessagesErr should return true for InvalidRpcPublishMessagesErr error")
   103  
   104  	// test IsInvalidRpcPublishMessagesErr with a different error type.
   105  	dummyErr := fmt.Errorf("dummy error")
   106  	assert.False(t, IsInvalidRpcPublishMessagesErr(dummyErr), "IsInvalidRpcPublishMessagesErr should return false for non-InvalidRpcPublishMessagesErr error")
   107  }
   108  
   109  // TestErrDuplicateTopicIDThresholdExceededRoundTrip ensures correct error formatting for DuplicateTopicIDThresholdExceeded.
   110  func TestDuplicateTopicIDThresholdExceededRoundTrip(t *testing.T) {
   111  	expectedErrorMsg := "3/5 duplicate topic IDs exceed the allowed threshold: 2"
   112  	err := NewDuplicateTopicIDThresholdExceeded(3, 5, 2)
   113  	assert.Equal(t, expectedErrorMsg, err.Error(), "the error message should be correctly formatted")
   114  	// tests the IsDuplicateTopicIDThresholdExceeded function.
   115  	assert.True(t, IsDuplicateTopicIDThresholdExceeded(err), "IsDuplicateTopicIDThresholdExceeded should return true for DuplicateTopicIDThresholdExceeded error")
   116  	// test IsDuplicateTopicIDThresholdExceeded with a different error type.
   117  	dummyErr := fmt.Errorf("dummy error")
   118  	assert.False(t, IsDuplicateTopicIDThresholdExceeded(dummyErr), "IsDuplicateTopicIDThresholdExceeded should return false for non-DuplicateTopicIDThresholdExceeded error")
   119  }
   120  
   121  // TestErrInvalidTopicIDThresholdExceededRoundTrip ensures correct error formatting for InvalidTopicIDThresholdExceeded.
   122  func TestInvalidTopicIDThresholdExceededRoundTrip(t *testing.T) {
   123  	expectedErrorMsg := "8 invalid topic IDs exceed the allowed threshold: 5"
   124  	err := NewInvalidTopicIDThresholdExceeded(8, 5)
   125  	assert.Equal(t, expectedErrorMsg, err.Error(), "the error message should be correctly formatted")
   126  	// tests the IsInvalidTopicIDThresholdExceeded function.
   127  	assert.True(t, IsInvalidTopicIDThresholdExceeded(err), "IsInvalidTopicIDThresholdExceeded should return true for InvalidTopicIDThresholdExceeded error")
   128  	// test IsInvalidTopicIDThresholdExceeded with a different error type.
   129  	dummyErr := fmt.Errorf("dummy error")
   130  	assert.False(t, IsInvalidTopicIDThresholdExceeded(dummyErr), "IsInvalidTopicIDThresholdExceeded should return false for non-InvalidTopicIDThresholdExceeded error")
   131  }