Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add more test cases for cross chain apps #389

Merged
merged 13 commits into from
Aug 1, 2023
1 change: 1 addition & 0 deletions .testcoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ exclude:
- simulation/.*
- testutil/.*
- e2e/.*
- .*/cli/.*
- sdk/.*
- version/.*
- cmd/gnfd/.*
Expand Down
103 changes: 103 additions & 0 deletions e2e/tests/permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,109 @@ func (s *StorageTestSuite) TestStalePermissionForAccountGC() {
s.Require().ErrorContains(err, "No such Policy")
}

func (s *StorageTestSuite) TestDeleteObjectPolicy() {
var err error
ctx := context.Background()
user1 := s.GenAndChargeAccounts(1, 1000000)[0]

_, owner, bucketName, _, objectName, objectId := s.createObjectWithVisibility(storagetypes.VISIBILITY_TYPE_PUBLIC_READ)

principal := types.NewPrincipalWithAccount(user1.GetAddr())

// Put bucket policy
bucketStatement := &types.Statement{
Actions: []types.ActionType{types.ACTION_DELETE_BUCKET},
Effect: types.EFFECT_ALLOW,
}
msgPutBucketPolicy := storagetypes.NewMsgPutPolicy(owner.GetAddr(), types2.NewBucketGRN(bucketName).String(),
principal, []*types.Statement{bucketStatement}, nil)
s.SendTxBlock(owner, msgPutBucketPolicy)

// Put Object policy
objectStatement := &types.Statement{
Actions: []types.ActionType{types.ACTION_DELETE_OBJECT},
Effect: types.EFFECT_ALLOW,
}
msgPutObjectPolicy := storagetypes.NewMsgPutPolicy(owner.GetAddr(), types2.NewObjectGRN(bucketName, objectName).String(),
principal, []*types.Statement{objectStatement}, nil)
s.SendTxBlock(owner, msgPutObjectPolicy)

// Query the policy which is enforced on bucket and object
grn1 := types2.NewObjectGRN(bucketName, objectName)
queryPolicyForAccountResp, err := s.Client.QueryPolicyForAccount(ctx, &storagetypes.QueryPolicyForAccountRequest{Resource: grn1.String(),
PrincipalAddress: user1.GetAddr().String()})
s.Require().NoError(err)
s.Require().Equal(objectId, queryPolicyForAccountResp.Policy.ResourceId)

// Delete object policy
msgDeletePolicy := storagetypes.NewMsgDeletePolicy(owner.GetAddr(), grn1.String(), types.NewPrincipalWithAccount(user1.GetAddr()))
s.SendTxBlock(owner, msgDeletePolicy)

// verify permission
verifyPermReq := storagetypes.QueryVerifyPermissionRequest{
Operator: user1.GetAddr().String(),
BucketName: bucketName,
ObjectName: objectName,
ActionType: types.ACTION_DELETE_OBJECT,
}
verifyPermResp, err := s.Client.VerifyPermission(ctx, &verifyPermReq)
s.T().Logf("resp: %s, rep %s", verifyPermReq.String(), verifyPermResp.String())
s.Require().NoError(err)
s.Require().Equal(verifyPermResp.Effect, types.EFFECT_DENY)
}

func (s *StorageTestSuite) TestDeleteGroupPolicy() {
var err error
ctx := context.Background()

user := s.GenAndChargeAccounts(4, 1000000)
owner := user[0]
_ = s.BaseSuite.PickStorageProvider()

// Create Group
testGroupName := "testGroup"
msgCreateGroup := storagetypes.NewMsgCreateGroup(owner.GetAddr(), testGroupName,
[]sdk.AccAddress{user[1].GetAddr(), user[2].GetAddr(), user[3].GetAddr()},
"")
s.SendTxBlock(owner, msgCreateGroup)

// Head Group
headGroupRequest := storagetypes.QueryHeadGroupRequest{GroupOwner: owner.GetAddr().String(), GroupName: testGroupName}
headGroupResponse, err := s.Client.HeadGroup(ctx, &headGroupRequest)
s.Require().NoError(err)
s.Require().Equal(headGroupResponse.GroupInfo.GroupName, testGroupName)
s.Require().True(owner.GetAddr().Equals(sdk.MustAccAddressFromHex(headGroupResponse.GroupInfo.Owner)))
s.T().Logf("GroupInfo: %s", headGroupResponse.GetGroupInfo().String())

// Put policy
groupStatement := &types.Statement{
Actions: []types.ActionType{types.ACTION_UPDATE_GROUP_MEMBER},
Effect: types.EFFECT_ALLOW,
}
msgPutGroupPolicy := storagetypes.NewMsgPutPolicy(owner.GetAddr(), types2.NewGroupGRN(owner.GetAddr(), testGroupName).String(),
types.NewPrincipalWithAccount(user[1].GetAddr()), []*types.Statement{groupStatement}, nil)
s.SendTxBlock(owner, msgPutGroupPolicy)

// Query for policy
grn := types2.NewGroupGRN(owner.GetAddr(), testGroupName)
queryPolicyForAccountReq := storagetypes.QueryPolicyForAccountRequest{Resource: grn.String(),
PrincipalAddress: user[1].GetAddr().String()}

queryPolicyForAccountResp, err := s.Client.QueryPolicyForAccount(ctx, &queryPolicyForAccountReq)
s.Require().NoError(err)
s.Require().Equal(queryPolicyForAccountResp.Policy.ResourceType, resource.RESOURCE_TYPE_GROUP)
s.T().Logf("policy is %s", queryPolicyForAccountResp.Policy.String())

// Delete policy
msgDeletePolicy := storagetypes.NewMsgDeletePolicy(owner.GetAddr(), grn.String(), types.NewPrincipalWithAccount(user[1].GetAddr()))
s.SendTxBlock(owner, msgDeletePolicy)

// verify permission
_, err = s.Client.QueryPolicyForAccount(ctx, &queryPolicyForAccountReq)
s.Require().Error(err)
s.Require().ErrorContains(err, "No such Policy")
}

// When resources are deleted, policies which associated with group and resources(Bucket and Object)
// will also be garbage collected.
func (s *StorageTestSuite) TestStalePermissionForGroupGC() {
Expand Down
111 changes: 111 additions & 0 deletions e2e/tests/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ func (s *StorageTestSuite) TestCreateBucket() {
s.Require().NoError(err)
s.Require().Equal(queryHeadBucketResponseAfterUpdateBucket.BucketInfo.Visibility, storagetypes.VISIBILITY_TYPE_PRIVATE)

// verify HeadBucketById
queryHeadBucketResponseAfterUpdateBucket, err = s.Client.HeadBucketById(ctx, &storagetypes.QueryHeadBucketByIdRequest{BucketId: queryHeadBucketResponseAfterUpdateBucket.BucketInfo.Id.String()})
s.Require().NoError(err)
s.Require().Equal(queryHeadBucketResponseAfterUpdateBucket.BucketInfo.Visibility, storagetypes.VISIBILITY_TYPE_PRIVATE)
s.Require().Equal(queryHeadBucketResponseAfterUpdateBucket.BucketInfo.BucketName, bucketName)

// verify HeadBucketNFT
headBucketNftResponse, err := s.Client.HeadBucketNFT(ctx, &storagetypes.QueryNFTRequest{
TokenId: queryHeadBucketResponseAfterUpdateBucket.BucketInfo.Id.String(),
})
s.Require().NoError(err)
s.Require().Equal(headBucketNftResponse.MetaData.BucketName, bucketName)

// verify QueryIsPriceChanged
isPriceChanged, err := s.Client.QueryIsPriceChanged(ctx, &storagetypes.QueryIsPriceChangedRequest{
BucketName: bucketName,
})
s.Require().NoError(err)
s.Require().Equal(isPriceChanged.Changed, false)

// DeleteBucket
msgDeleteBucket := storagetypes.NewMsgDeleteBucket(user.GetAddr(), bucketName)
s.SendTxBlock(user, msgDeleteBucket)
Expand Down Expand Up @@ -205,6 +225,21 @@ func (s *StorageTestSuite) TestCreateObject() {
s.Require().Equal(len(queryListObjectsResponse.ObjectInfos), 1)
s.Require().Equal(queryListObjectsResponse.ObjectInfos[0].ObjectName, objectName)

// verify ListObjectsByBucketId
queryListObjectsResponse, err = s.Client.ListObjectsByBucketId(ctx, &storagetypes.QueryListObjectsByBucketIdRequest{
BucketId: queryHeadBucketResponse.BucketInfo.Id.String(),
})
s.Require().NoError(err)
s.Require().Equal(len(queryListObjectsResponse.ObjectInfos), 1)
s.Require().Equal(queryListObjectsResponse.ObjectInfos[0].ObjectName, objectName)

// verify HeadObjectNFT
headObjectNftResponse, err := s.Client.HeadObjectNFT(ctx, &storagetypes.QueryNFTRequest{
TokenId: queryListObjectsResponse.ObjectInfos[0].Id.String(),
})
s.Require().NoError(err)
s.Require().Equal(headObjectNftResponse.MetaData.ObjectName, objectName)

// UpdateObjectInfo
updateObjectInfo := storagetypes.NewMsgUpdateObjectInfo(
user.GetAddr(), bucketName, objectName, storagetypes.VISIBILITY_TYPE_INHERIT)
Expand All @@ -218,6 +253,12 @@ func (s *StorageTestSuite) TestCreateObject() {
s.Require().NoError(err)
s.Require().Equal(queryHeadObjectAfterUpdateObjectResponse.ObjectInfo.Visibility, storagetypes.VISIBILITY_TYPE_INHERIT)

// verify HeadObjectById
queryHeadObjectAfterUpdateObjectResponse, err = s.Client.HeadObjectById(context.Background(), &storagetypes.QueryHeadObjectByIdRequest{ObjectId: queryHeadObjectAfterUpdateObjectResponse.ObjectInfo.Id.String()})
s.Require().NoError(err)
s.Require().Equal(queryHeadObjectAfterUpdateObjectResponse.ObjectInfo.Visibility, storagetypes.VISIBILITY_TYPE_INHERIT)
s.Require().Equal(queryHeadObjectAfterUpdateObjectResponse.ObjectInfo.ObjectName, objectName)

// DeleteObject
msgDeleteObject := storagetypes.NewMsgDeleteObject(user.GetAddr(), bucketName, objectName)
s.SendTxBlock(user, msgDeleteObject)
Expand Down Expand Up @@ -246,6 +287,13 @@ func (s *StorageTestSuite) TestCreateGroup() {
s.Require().Equal(queryHeadGroupResp.GroupInfo.GroupName, groupName)
s.Require().Equal(queryHeadGroupResp.GroupInfo.Owner, owner.GetAddr().String())

// 2.1. HeadGroupNFT
headGroupNftResponse, err := s.Client.HeadGroupNFT(ctx, &storagetypes.QueryNFTRequest{
TokenId: queryHeadGroupResp.GroupInfo.Id.String(),
})
s.Require().NoError(err)
s.Require().Equal(headGroupNftResponse.MetaData.GroupName, groupName)

// 3. ListGroup
queryListGroupReq := storagetypes.QueryListGroupRequest{GroupOwner: owner.GetAddr().String()}
queryListGroupResp, err := s.Client.ListGroup(ctx, &queryListGroupReq)
Expand Down Expand Up @@ -292,6 +340,69 @@ func (s *StorageTestSuite) TestCreateGroup() {
s.SendTxBlockWithExpectErrorString(msgCreateGroup, owner, "exists")
}

func (s *StorageTestSuite) TestLeaveGroup() {
ctx := context.Background()

owner := s.GenAndChargeAccounts(1, 1000000)[0]
member := s.GenAndChargeAccounts(1, 1000000)[0]
groupName := storageutils.GenRandomGroupName()

// 1. CreateGroup
msgCreateGroup := storagetypes.NewMsgCreateGroup(owner.GetAddr(), groupName, []sdk.AccAddress{member.GetAddr()}, "")
s.SendTxBlock(owner, msgCreateGroup)
s.T().Logf("CerateGroup success, owner: %s, group name: %s", owner.GetAddr().String(), groupName)

// 2. HeadGroup
queryHeadGroupReq := storagetypes.QueryHeadGroupRequest{GroupOwner: owner.GetAddr().String(), GroupName: groupName}
queryHeadGroupResp, err := s.Client.HeadGroup(ctx, &queryHeadGroupReq)
s.Require().NoError(err)
s.Require().Equal(queryHeadGroupResp.GroupInfo.GroupName, groupName)
s.Require().Equal(queryHeadGroupResp.GroupInfo.Owner, owner.GetAddr().String())

// 2.1. HeadGroupNFT
headGroupNftResponse, err := s.Client.HeadGroupNFT(ctx, &storagetypes.QueryNFTRequest{
TokenId: queryHeadGroupResp.GroupInfo.Id.String(),
})
s.Require().NoError(err)
s.Require().Equal(headGroupNftResponse.MetaData.GroupName, groupName)

// 3. ListGroup
queryListGroupReq := storagetypes.QueryListGroupRequest{GroupOwner: owner.GetAddr().String()}
queryListGroupResp, err := s.Client.ListGroup(ctx, &queryListGroupReq)
s.Require().NoError(err)
s.Require().GreaterOrEqual(len(queryListGroupResp.GroupInfos), 1)

// 3. HeadGroupMember
queryHeadGroupMemberReq := storagetypes.QueryHeadGroupMemberRequest{
Member: member.GetAddr().String(),
GroupName: groupName,
GroupOwner: owner.GetAddr().String(),
}
queryHeadGroupMemberResp, err := s.Client.HeadGroupMember(ctx, &queryHeadGroupMemberReq)
s.Require().NoError(err)
s.Require().Equal(queryHeadGroupMemberResp.GroupMember.GroupId, queryHeadGroupResp.GroupInfo.Id)

// 4. UpdateGroupMember
member2 := s.GenAndChargeAccounts(1, 1000000)[0]
membersToAdd := []sdk.AccAddress{member2.GetAddr()}
membersToDelete := []sdk.AccAddress{member.GetAddr()}
msgUpdateGroupMember := storagetypes.NewMsgUpdateGroupMember(owner.GetAddr(), owner.GetAddr(), groupName, membersToAdd, membersToDelete)
s.SendTxBlock(owner, msgUpdateGroupMember)

// 5. leave group
msgLeaveGroup := storagetypes.NewMsgLeaveGroup(member2.GetAddr(), owner.GetAddr(), groupName)
s.SendTxBlock(member2, msgLeaveGroup)

// 6. HeadGroupMember (leave)
queryHeadGroupMemberReqDelete := storagetypes.QueryHeadGroupMemberRequest{
Member: member2.GetAddr().String(),
GroupName: groupName,
GroupOwner: owner.GetAddr().String(),
}
_, err = s.Client.HeadGroupMember(ctx, &queryHeadGroupMemberReqDelete)
s.Require().True(strings.Contains(err.Error(), storagetypes.ErrNoSuchGroupMember.Error()))
}

func (s *StorageTestSuite) TestDeleteBucket() {
var err error
user := s.GenAndChargeAccounts(1, 1000000)[0]
Expand Down
7 changes: 3 additions & 4 deletions x/storage/abci.go → x/storage/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package storage
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

paymenttypes "github.com/bnb-chain/greenfield/x/payment/types"
k "github.com/bnb-chain/greenfield/x/storage/keeper"
)

func BeginBlocker(ctx sdk.Context, keeper k.Keeper) {
func BeginBlocker(ctx sdk.Context, keeper Keeper) {
blockHeight := uint64(ctx.BlockHeight())
countingWindow := keeper.DiscontinueCountingWindow(ctx)
if blockHeight > 0 && countingWindow > 0 && blockHeight%countingWindow == 0 {
Expand All @@ -16,7 +15,7 @@ func BeginBlocker(ctx sdk.Context, keeper k.Keeper) {
}
}

func EndBlocker(ctx sdk.Context, keeper k.Keeper) {
func EndBlocker(ctx sdk.Context, keeper Keeper) {
deletionMax := keeper.DiscontinueDeletionMax(ctx)
if deletionMax == 0 {
return
Expand Down
Loading
Loading