github.com/status-im/status-go@v1.1.0/waku/v1/status_options_test.go (about)

     1  package v1
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/ethereum/go-ethereum/rlp"
    10  	"github.com/status-im/status-go/waku/common"
    11  )
    12  
    13  func TestEncodeDecodeRLP(t *testing.T) {
    14  	pow := math.Float64bits(6.02)
    15  	lightNodeEnabled := true
    16  	confirmationsEnabled := true
    17  
    18  	opts := StatusOptions{
    19  		PoWRequirement:       &pow,
    20  		BloomFilter:          common.TopicType{0xaa, 0xbb, 0xcc, 0xdd}.ToBloom(),
    21  		LightNodeEnabled:     &lightNodeEnabled,
    22  		ConfirmationsEnabled: &confirmationsEnabled,
    23  		PacketRateLimits: &common.RateLimits{
    24  			IPLimits:     10,
    25  			PeerIDLimits: 5,
    26  			TopicLimits:  1,
    27  		},
    28  		TopicInterest: []common.TopicType{{0x01}, {0x02}, {0x03}, {0x04}},
    29  		BytesRateLimits: &common.RateLimits{
    30  			IPLimits:     10,
    31  			PeerIDLimits: 5,
    32  			TopicLimits:  1,
    33  		},
    34  	}
    35  	data, err := rlp.EncodeToBytes(opts)
    36  	require.NoError(t, err)
    37  
    38  	var optsDecoded StatusOptions
    39  	err = rlp.DecodeBytes(data, &optsDecoded)
    40  	require.NoError(t, err)
    41  	require.EqualValues(t, opts, optsDecoded)
    42  }
    43  
    44  func TestBackwardCompatibility(t *testing.T) {
    45  	alist := []interface{}{
    46  		[]interface{}{uint64(0), math.Float64bits(2.05)},
    47  	}
    48  	data, err := rlp.EncodeToBytes(alist)
    49  	require.NoError(t, err)
    50  
    51  	var optsDecoded StatusOptions
    52  	err = rlp.DecodeBytes(data, &optsDecoded)
    53  	require.NoError(t, err)
    54  	pow := math.Float64bits(2.05)
    55  	require.EqualValues(t, StatusOptions{PoWRequirement: &pow}, optsDecoded)
    56  }
    57  
    58  func TestForwardCompatibility(t *testing.T) {
    59  	pow := math.Float64bits(2.05)
    60  	alist := []interface{}{
    61  		[]interface{}{uint64(0), pow},
    62  		[]interface{}{uint64(99), uint(10)}, // some future option
    63  	}
    64  	data, err := rlp.EncodeToBytes(alist)
    65  	require.NoError(t, err)
    66  
    67  	var optsDecoded StatusOptions
    68  	err = rlp.DecodeBytes(data, &optsDecoded)
    69  	require.NoError(t, err)
    70  	require.EqualValues(t, StatusOptions{PoWRequirement: &pow}, optsDecoded)
    71  }
    72  
    73  func TestInitRLPKeyFields(t *testing.T) {
    74  	ifk := map[int]statusOptionKey{
    75  		0: 0,
    76  		1: 1,
    77  		2: 2,
    78  		3: 3,
    79  		4: 4,
    80  		5: 5,
    81  		6: 6,
    82  	}
    83  	kfi := map[statusOptionKey]int{
    84  		0: 0,
    85  		1: 1,
    86  		2: 2,
    87  		3: 3,
    88  		4: 4,
    89  		5: 5,
    90  		6: 6,
    91  	}
    92  
    93  	// Test that the kfi length matches the inited global keyFieldIdx length
    94  	require.Equal(t, len(kfi), len(keyFieldIdx))
    95  
    96  	// Test that each index of the kfi values matches the inited global keyFieldIdx of the same index
    97  	for k, v := range kfi {
    98  		require.Exactly(t, v, keyFieldIdx[k])
    99  	}
   100  
   101  	// Test that each index of the inited global keyFieldIdx values matches kfi values of the same index
   102  	for k, v := range keyFieldIdx {
   103  		require.Exactly(t, v, kfi[k])
   104  	}
   105  
   106  	// Test that the ifk length matches the inited global idxFieldKey length
   107  	require.Equal(t, len(ifk), len(idxFieldKey))
   108  
   109  	// Test that each index of the ifk values matches the inited global idxFieldKey of the same index
   110  	for k, v := range ifk {
   111  		require.Exactly(t, v, idxFieldKey[k])
   112  	}
   113  
   114  	// Test that each index of the inited global idxFieldKey values matches ifk values of the same index
   115  	for k, v := range idxFieldKey {
   116  		require.Exactly(t, v, ifk[k])
   117  	}
   118  }