Skip to content

Commit

Permalink
Merge branch '1.2.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell authored Dec 29, 2022
2 parents 6f6d288 + dccbd24 commit bff1d3a
Show file tree
Hide file tree
Showing 23 changed files with 499 additions and 463 deletions.
141 changes: 141 additions & 0 deletions x/filetree/keeper/marshalling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package keeper_test

import (
"encoding/json"
"fmt"
"strings"

sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/google/uuid"
"github.com/jackalLabs/canine-chain/x/filetree/keeper"
"github.com/jackalLabs/canine-chain/x/filetree/types"
)

func (suite *KeeperTestSuite) TestJSONMarshalling() {
suite.SetupSuite()
msgSrvr, _, context := setupMsgServer(suite)

alice, err := sdkTypes.AccAddressFromBech32("cosmos1ytwr7x4av05ek0tf8z9s4zmvr6w569zsm27dpg")
suite.Require().NoError(err)

bob, err := sdkTypes.AccAddressFromBech32("cosmos17j2hkm7n9fz9dpntyj2kxgxy5pthzd289nvlfl")
suite.Require().NoError(err)

// set root folder for alice
aliceRootFolder, err := types.CreateRootFolder(alice.String())
suite.Require().NoError(err)
suite.filetreeKeeper.SetFiles(suite.ctx, *aliceRootFolder)

aliceViewerID := strings.Split(alice.String(), ",")
aliceEditorID := aliceViewerID

// set home folder for alice
aliceHomeFolder, err := types.CreateFolderOrFile(alice.String(), aliceEditorID, aliceViewerID, "s/home/")
suite.Require().NoError(err)
suite.filetreeKeeper.SetFiles(suite.ctx, *aliceHomeFolder)

// put pepe in home
// pepe's viewing access is a marshalled slice which means the keeper will fail to unmarshall
pepejpg, err := CreateBadFile(alice.String(), aliceEditorID, aliceViewerID, "s/home/pepe.jpg")
suite.Require().NoError(err)
suite.filetreeKeeper.SetFiles(suite.ctx, *pepejpg)
pepeMerklePath := types.MerklePath("s/home/pepe.jpg")
aliceAccountHash := types.HashThenHex(alice.String())
pepeOwnerAddress := types.MakeOwnerAddress(pepeMerklePath, aliceAccountHash)
bobPepeViewerAddress := keeper.MakeViewerAddress(pepejpg.TrackingNumber, bob.String())

// Create a good file
bunnyjpg, err := types.CreateFolderOrFile(alice.String(), aliceEditorID, aliceViewerID, "s/home/bunny.jpg")
suite.Require().NoError(err)
suite.filetreeKeeper.SetFiles(suite.ctx, *bunnyjpg)
bunnyMerklePath := types.MerklePath("s/home/bunny.jpg")
bunnyOwnerAddress := types.MakeOwnerAddress(bunnyMerklePath, aliceAccountHash)
bobBunnyViewerAddress := keeper.MakeViewerAddress(bunnyjpg.TrackingNumber, bob.String())

cases := []struct {
preRun func() *types.MsgAddViewers
expErr bool
expErrMsg string
name string
}{
{ // alice fails to add a viewer
preRun: func() *types.MsgAddViewers {
return types.NewMsgAddViewers(
alice.String(),
bobPepeViewerAddress,
fmt.Sprintf("%x", "encryptedPepeAESKeyAndIV"),
pepeMerklePath,
pepeOwnerAddress,
)
},
expErr: true,
name: "alice fails to add a viewer",
expErrMsg: "cannot unmarshall data from json",
},
{ // alice successfully adds a viewer
preRun: func() *types.MsgAddViewers {
return types.NewMsgAddViewers(
alice.String(),
bobBunnyViewerAddress,
fmt.Sprintf("%x", "encryptedBunnyAESKeyAndIV"),
bunnyMerklePath,
bunnyOwnerAddress,
)
},
expErr: false,
name: "alice adds a viewer",
},
}

for _, tc := range cases {
suite.Run(tc.name, func() {
msg := tc.preRun()
suite.Require().NoError(err)
res, err := msgSrvr.AddViewers(context, msg)
if tc.expErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.expErrMsg)
} else {

suite.Require().NoError(err)
suite.Require().EqualValues(types.MsgAddViewersResponse{}, *res)

}
})
}
}

func CreateBadFile(creator string, editorIds []string, viewerIds []string, readablePath string) (*types.Files, error) {
merklePath := types.MerklePath(readablePath)
trackingNumber := uuid.NewString()

jsonEditors, err := types.MakeEditorAccessMap(trackingNumber, editorIds, "place holder key")
if err != nil {
return nil, err
}

viewers := make([]string, 10)

for i := range viewerIds {
viewers[i] = fmt.Sprintf("%x", "aesKey")
}

jsonViewers, err := json.Marshal(viewers)
if err != nil {
return nil, types.ErrCantMarshall
}

accountHash := types.HashThenHex(creator)
ownerAddress := types.MakeOwnerAddress(merklePath, accountHash)

File := types.Files{
Contents: "Contents: FID goes here",
Owner: ownerAddress,
ViewingAccess: string(jsonViewers),
EditAccess: string(jsonEditors),
Address: merklePath,
TrackingNumber: trackingNumber,
}

return &File, nil
}
2 changes: 1 addition & 1 deletion x/filetree/keeper/msg_server_add_editors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (k msgServer) AddEditors(goCtx context.Context, msg *types.MsgAddEditors) (

jeacc := make(map[string]string)
if err := json.Unmarshal([]byte(peacc), &jeacc); err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrCantUnmarshall
}

ids := strings.Split(msg.EditorIds, ",")
Expand Down
2 changes: 1 addition & 1 deletion x/filetree/keeper/msg_server_add_viewers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (k msgServer) AddViewers(goCtx context.Context, msg *types.MsgAddViewers) (

jvacc := make(map[string]string)
if err := json.Unmarshal([]byte(pvacc), &jvacc); err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrCantUnmarshall
}

ids := strings.Split(msg.ViewerIds, ",")
Expand Down
4 changes: 2 additions & 2 deletions x/filetree/keeper/msg_server_post_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (k msgServer) PostFile(goCtx context.Context, msg *types.MsgPostFile) (*typ

hasEdit, err := HasEditAccess(parentFile, msg.Creator)
if err != nil {
// Error raised when json unmarshalling is failed
ctx.Logger().Error(err.Error())
// Error raised when json unmarshalling has failed
return nil, err
}

if !hasEdit {
Expand Down
2 changes: 1 addition & 1 deletion x/filetree/keeper/msg_server_remove_editors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (k msgServer) RemoveEditors(goCtx context.Context, msg *types.MsgRemoveEdit

jeacc := make(map[string]string)
if err := json.Unmarshal([]byte(peacc), &jeacc); err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrCantUnmarshall
}

ids := strings.Split(msg.EditorIds, ",")
Expand Down
2 changes: 1 addition & 1 deletion x/filetree/keeper/msg_server_remove_viewers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (k msgServer) RemoveViewers(goCtx context.Context, msg *types.MsgRemoveView

jvacc := make(map[string]string)
if err := json.Unmarshal([]byte(pvacc), &jvacc); err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrCantUnmarshall
}

ids := strings.Split(msg.ViewerIds, ",")
Expand Down
2 changes: 1 addition & 1 deletion x/filetree/keeper/msg_server_reset_editors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (k msgServer) ResetEditors(goCtx context.Context, msg *types.MsgResetEditor
// Unmarshall current edit access to this blank map
jeacc := make(map[string]string)
if err := json.Unmarshal([]byte(peacc), &jeacc); err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrCantUnmarshall
}

ownerKey := jeacc[ownerEditorAddress]
Expand Down
2 changes: 1 addition & 1 deletion x/filetree/keeper/msg_server_reset_viewers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (k msgServer) ResetViewers(goCtx context.Context, msg *types.MsgResetViewer
// Unmarshall current edit access to this blank map
jvacc := make(map[string]string)
if err := json.Unmarshal([]byte(pvacc), &jvacc); err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrCantUnmarshall
}

ownerKey := jvacc[ownerViewerAddress]
Expand Down
33 changes: 0 additions & 33 deletions x/filetree/keeper/tracker.go

This file was deleted.

1 change: 0 additions & 1 deletion x/filetree/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var (
ErrParentFileNotFound = sdkerrors.Register(ModuleName, 1106, "Parent folder does not exist")
ErrCannotWrite = sdkerrors.Register(ModuleName, 1107, "You are not permitted to write to this folder")
ErrNoViewingAccess = sdkerrors.Register(ModuleName, 1108, "You do not have viewing access. Failed to decrypt.")
ErrTrackerNotFound = sdkerrors.Register(ModuleName, 1109, "Tracking number not found")
ErrCannotDelete = sdkerrors.Register(ModuleName, 1110, "You are not authorized to delete this file")
ErrNotOwner = sdkerrors.Register(ModuleName, 1111, "Not permitted to remove or reset edit/view access. You are not the owner of this file")
ErrCantGiveAway = sdkerrors.Register(ModuleName, 1112, "You do not own this file and cannot give it away")
Expand Down
4 changes: 2 additions & 2 deletions x/filetree/types/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ func MakeViewerAccessMap(trackingNumber string, viewerIds []string, aesKey strin

}

jsonEditors, err := json.Marshal(viewers)
jsonViewers, err := json.Marshal(viewers)
if err != nil {
return nil, ErrCantMarshall
}

return jsonEditors, nil
return jsonViewers, nil
}

func CreateMsgPostFile(creator string, readablePath string, jsonEditAccess []byte, trackingNumber string) (*MsgPostFile, error) {
Expand Down
Loading

0 comments on commit bff1d3a

Please sign in to comment.