github.com/Finschia/finschia-sdk@v0.48.1/x/capability/types/keys_test.go (about) 1 package types_test 2 3 import ( 4 "fmt" 5 "runtime" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/Finschia/finschia-sdk/x/capability/types" 11 ) 12 13 func TestRevCapabilityKey(t *testing.T) { 14 expected := []byte("bank/rev/send") 15 require.Equal(t, expected, types.RevCapabilityKey("bank", "send")) 16 } 17 18 func TestFwdCapabilityKey(t *testing.T) { 19 cap := types.NewCapability(23) 20 key := fmt.Sprintf("%#010p", cap) 21 if len(key) > 10 { 22 key = key[len(key)-10:] 23 } 24 require.Equal(t, 10, len(key)) 25 expected := []byte(fmt.Sprintf("bank/fwd/0x%s", key)) 26 require.Equal(t, expected, types.FwdCapabilityKey("bank", cap)) 27 } 28 29 func TestIndexToKey(t *testing.T) { 30 require.Equal(t, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5a}, types.IndexToKey(3162)) 31 } 32 33 func TestIndexFromKey(t *testing.T) { 34 require.Equal(t, uint64(3162), types.IndexFromKey([]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5a})) 35 } 36 37 // to test the backward compatibiltiy of the new function 38 func legacyFwdCapabilityKey(module string, cap *types.Capability) []byte { 39 return []byte(fmt.Sprintf("%s/fwd/%p", module, cap)) 40 } 41 42 func TestFwdCapabilityKeyCompatibility(t *testing.T) { 43 cap := types.NewCapability(24) 44 new := types.FwdCapabilityKey("bank", cap) 45 old := legacyFwdCapabilityKey("bank", cap) 46 if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { 47 // the legacy version has 1 more byte on mac m1 48 require.Equal(t, len(old), len(new)+1) 49 } else { 50 // otherwise, the new version is identical 51 require.Equal(t, new, old) 52 } 53 }