github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/execution/contexts/permissions_context_test.go (about) 1 package contexts 2 3 import ( 4 "testing" 5 6 "github.com/hyperledger/burrow/acm/acmstate" 7 "github.com/hyperledger/burrow/logging" 8 "github.com/hyperledger/burrow/permission" 9 "github.com/hyperledger/burrow/txs/payload" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestPermissionsContext(t *testing.T) { 14 accountState := acmstate.NewMemoryState() 15 16 originPrivKey := newPrivKey(t) 17 originAccount := newAccountFromPrivKey(originPrivKey) 18 originAccount.Permissions.Base = permission.AllAccountPermissions.GetBase() 19 20 targetPrivKey := newPrivKey(t) 21 targetAccount := newAccountFromPrivKey(targetPrivKey) 22 23 ctx := &PermissionsContext{ 24 State: accountState, 25 Logger: logging.NewNoopLogger(), 26 } 27 28 callTx := &payload.CallTx{} 29 err := ctx.Execute(execFromTx(callTx), callTx) 30 require.Error(t, err, "should not continue with incorrect payload") 31 32 permsTx := &payload.PermsTx{ 33 Input: &payload.TxInput{ 34 Address: originAccount.Address, 35 }, 36 } 37 38 err = ctx.Execute(execFromTx(permsTx), permsTx) 39 require.Error(t, err, "account should not exist") 40 41 accountState.Accounts[originAccount.Address] = originAccount 42 accountState.Accounts[targetAccount.Address] = targetAccount 43 44 value := true 45 tests := []struct { 46 args permission.PermArgs 47 exp func(t *testing.T, err error) 48 }{ 49 { 50 args: permission.PermArgs{ 51 Action: 1337, 52 Target: &targetAccount.Address, 53 Permission: ptrPermFlag(permission.SetBase), 54 Value: &value, 55 }, 56 exp: errCallback(func(t *testing.T, err error) { 57 require.Error(t, err, "should error with unknown action") 58 }), 59 }, 60 { 61 args: permission.PermArgs{ 62 Action: permission.SetBase, 63 Target: &targetAccount.Address, 64 Permission: ptrPermFlag(permission.SetBase), 65 Value: &value, 66 }, 67 exp: errCallback(func(t *testing.T, err error) { 68 require.NoError(t, err) 69 }), 70 }, 71 { 72 args: permission.PermArgs{ 73 Action: permission.UnsetBase, 74 Target: &targetAccount.Address, 75 Permission: ptrPermFlag(permission.UnsetBase), 76 Value: &value, 77 }, 78 exp: errCallback(func(t *testing.T, err error) { 79 require.NoError(t, err) 80 }), 81 }, 82 { 83 args: permission.PermArgs{ 84 Action: permission.AddRole, 85 Target: &targetAccount.Address, 86 Permission: ptrPermFlag(permission.AddRole), 87 Value: &value, 88 Role: ptrRoleString(permission.BondString), 89 }, 90 exp: errCallback(func(t *testing.T, err error) { 91 require.NoError(t, err) 92 }), 93 }, 94 { 95 args: permission.PermArgs{ 96 Action: permission.RemoveRole, 97 Target: &targetAccount.Address, 98 Permission: ptrPermFlag(permission.RemoveRole), 99 Value: &value, 100 Role: ptrRoleString(permission.BondString), 101 }, 102 exp: errCallback(func(t *testing.T, err error) { 103 require.NoError(t, err) 104 }), 105 }, 106 { 107 args: permission.PermArgs{ 108 Action: permission.RemoveRole, 109 Target: &targetAccount.Address, 110 Permission: ptrPermFlag(permission.RemoveRole), 111 Value: &value, 112 Role: ptrRoleString(permission.BondString), 113 }, 114 exp: errCallback(func(t *testing.T, err error) { 115 require.Error(t, err, "can't remove role that isn't set") 116 }), 117 }, 118 } 119 120 for _, tt := range tests { 121 permsTx.PermArgs = tt.args 122 err = ctx.Execute(execFromTx(permsTx), permsTx) 123 tt.exp(t, err) 124 } 125 } 126 127 func ptrPermFlag(flag permission.PermFlag) *permission.PermFlag { 128 return &flag 129 } 130 131 func ptrRoleString(role string) *string { 132 return &role 133 } 134 135 func errCallback(condition func(t *testing.T, err error)) func(t *testing.T, err error) { 136 return func(t *testing.T, err error) { 137 condition(t, err) 138 } 139 }