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

     1  package v0
     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  		RateLimits: &common.RateLimits{
    24  			IPLimits:     10,
    25  			PeerIDLimits: 5,
    26  			TopicLimits:  1,
    27  		},
    28  		TopicInterest: []common.TopicType{{0x01}, {0x02}, {0x03}, {0x04}},
    29  	}
    30  	data, err := rlp.EncodeToBytes(opts)
    31  	require.NoError(t, err)
    32  
    33  	var optsDecoded StatusOptions
    34  	err = rlp.DecodeBytes(data, &optsDecoded)
    35  	require.NoError(t, err)
    36  	require.EqualValues(t, opts, optsDecoded)
    37  }
    38  
    39  func TestBackwardCompatibility(t *testing.T) {
    40  	alist := []interface{}{
    41  		[]interface{}{"0", math.Float64bits(2.05)},
    42  	}
    43  	data, err := rlp.EncodeToBytes(alist)
    44  	require.NoError(t, err)
    45  
    46  	var optsDecoded StatusOptions
    47  	err = rlp.DecodeBytes(data, &optsDecoded)
    48  	require.NoError(t, err)
    49  	pow := math.Float64bits(2.05)
    50  	require.EqualValues(t, StatusOptions{PoWRequirement: &pow}, optsDecoded)
    51  }
    52  
    53  func TestForwardCompatibility(t *testing.T) {
    54  	pow := math.Float64bits(2.05)
    55  	alist := []interface{}{
    56  		[]interface{}{"0", pow},
    57  		[]interface{}{"99", uint(10)}, // some future option
    58  	}
    59  	data, err := rlp.EncodeToBytes(alist)
    60  	require.NoError(t, err)
    61  
    62  	var optsDecoded StatusOptions
    63  	err = rlp.DecodeBytes(data, &optsDecoded)
    64  	require.NoError(t, err)
    65  	require.EqualValues(t, StatusOptions{PoWRequirement: &pow}, optsDecoded)
    66  }
    67  
    68  func TestInitRLPKeyFields(t *testing.T) {
    69  	ifk := map[int]statusOptionKey{
    70  		0: "0",
    71  		1: "1",
    72  		2: "2",
    73  		3: "3",
    74  		4: "4",
    75  		5: "5",
    76  	}
    77  	kfi := map[statusOptionKey]int{
    78  		"0": 0,
    79  		"1": 1,
    80  		"2": 2,
    81  		"3": 3,
    82  		"4": 4,
    83  		"5": 5,
    84  	}
    85  
    86  	// Test that the kfi length matches the inited global keyFieldIdx length
    87  	require.Equal(t, len(kfi), len(keyFieldIdx))
    88  
    89  	// Test that each index of the kfi values matches the inited global keyFieldIdx of the same index
    90  	for k, v := range kfi {
    91  		require.Exactly(t, v, keyFieldIdx[k])
    92  	}
    93  
    94  	// Test that each index of the inited global keyFieldIdx values matches kfi values of the same index
    95  	for k, v := range keyFieldIdx {
    96  		require.Exactly(t, v, kfi[k])
    97  	}
    98  
    99  	// Test that the ifk length matches the inited global idxFieldKey length
   100  	require.Equal(t, len(ifk), len(idxFieldKey))
   101  
   102  	// Test that each index of the ifk values matches the inited global idxFieldKey of the same index
   103  	for k, v := range ifk {
   104  		require.Exactly(t, v, idxFieldKey[k])
   105  	}
   106  
   107  	// Test that each index of the inited global idxFieldKey values matches ifk values of the same index
   108  	for k, v := range idxFieldKey {
   109  		require.Exactly(t, v, ifk[k])
   110  	}
   111  }