github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/pebble/lookup_test.go (about)

     1  package pebble
     2  
     3  import (
     4  	"encoding/binary"
     5  	"math"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/onflow/flow-go/model/flow"
    11  )
    12  
    13  // Test_lookupKey_Bytes tests the lookup key encoding.
    14  func Test_lookupKey_Bytes(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	expectedHeight := uint64(777)
    18  	key := newLookupKey(expectedHeight, flow.RegisterID{Owner: "owner", Key: "key"})
    19  
    20  	// Test prefix
    21  	require.Equal(t, byte(codeRegister), key.Bytes()[0])
    22  
    23  	// Test encoded Owner and Key
    24  	require.Equal(t, []byte("owner/key/"), key.Bytes()[1:11])
    25  
    26  	// Test encoded height
    27  	actualHeight := binary.BigEndian.Uint64(key.Bytes()[11:])
    28  	require.Equal(t, math.MaxUint64-actualHeight, expectedHeight)
    29  
    30  	// Test everything together
    31  	resultLookupKey := []byte{codeRegister}
    32  	resultLookupKey = append(resultLookupKey, []byte("owner/key/\xff\xff\xff\xff\xff\xff\xfc\xf6")...)
    33  	require.Equal(t, resultLookupKey, key.Bytes())
    34  
    35  	decodedHeight, decodedReg, err := lookupKeyToRegisterID(key.encoded)
    36  	require.NoError(t, err)
    37  
    38  	require.Equal(t, expectedHeight, decodedHeight)
    39  	require.Equal(t, "owner", decodedReg.Owner)
    40  	require.Equal(t, "key", decodedReg.Key)
    41  }
    42  
    43  func Test_decodeKey_Bytes(t *testing.T) {
    44  	height := uint64(10)
    45  
    46  	cases := []struct {
    47  		owner string
    48  		key   string
    49  	}{
    50  		{owner: "owneraddress", key: "public/storage/hasslash-in-key"},
    51  		{owner: "owneraddress", key: ""},
    52  		{owner: "", key: "somekey"},
    53  		{owner: "", key: ""},
    54  	}
    55  
    56  	for _, c := range cases {
    57  		owner, key := c.owner, c.key
    58  
    59  		lookupKey := newLookupKey(height, flow.RegisterID{Owner: owner, Key: key})
    60  		decodedHeight, decodedReg, err := lookupKeyToRegisterID(lookupKey.Bytes())
    61  		require.NoError(t, err)
    62  
    63  		require.Equal(t, height, decodedHeight)
    64  		require.Equal(t, owner, decodedReg.Owner)
    65  		require.Equal(t, key, decodedReg.Key)
    66  	}
    67  }
    68  
    69  func Test_decodeKey_fail(t *testing.T) {
    70  	var err error
    71  	// less than min length (10)
    72  	_, _, err = lookupKeyToRegisterID([]byte{codeRegister, 1, 2, 3, 4, 5, 6, 7, 8, 9})
    73  	require.Contains(t, err.Error(), "bytes")
    74  
    75  	// missing slash
    76  	_, _, err = lookupKeyToRegisterID([]byte{codeRegister, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
    77  	require.Contains(t, err.Error(), "slash")
    78  
    79  	// missing second slash
    80  	_, _, err = lookupKeyToRegisterID([]byte{codeRegister, 1, 2, 3, '/', 5, 6, 7, 8, 9, 10})
    81  	require.Contains(t, err.Error(), "separator")
    82  
    83  	// invalid height
    84  	_, _, err = lookupKeyToRegisterID([]byte{codeRegister, 1, 2, 3, '/', 5, 6, 7, 8, '/', 10})
    85  	require.Contains(t, err.Error(), "height")
    86  
    87  	// invalid height
    88  	_, _, err = lookupKeyToRegisterID([]byte{codeRegister, 1, 2, 3, '/', 5, '/', 7, 8, 9, 10})
    89  	require.Contains(t, err.Error(), "height")
    90  
    91  	// invalid height
    92  	_, _, err = lookupKeyToRegisterID([]byte{codeRegister, 1, 2, 3, '/', 5, '/', 7, 8, 9, 10, 11, 12, 13})
    93  	require.Contains(t, err.Error(), "height")
    94  
    95  	// valid height
    96  	_, _, err = lookupKeyToRegisterID([]byte{codeRegister, 1, 2, 3, '/', 5, '/', 7, 8, 9, 10, 11, 12, 13, 14})
    97  	require.NoError(t, err)
    98  }
    99  
   100  func Test_prefix_error(t *testing.T) {
   101  	correctKey := newLookupKey(uint64(0), flow.RegisterID{Owner: "owner", Key: "key"})
   102  	incorrectKey := firstHeightKey
   103  	_, _, err := lookupKeyToRegisterID(correctKey.Bytes())
   104  	require.NoError(t, err)
   105  
   106  	_, _, err = lookupKeyToRegisterID(incorrectKey)
   107  	require.ErrorContains(t, err, "incorrect prefix")
   108  }