Skip to content

Commit

Permalink
Fix bug - Identifier content is not validated (#113)
Browse files Browse the repository at this point in the history
* Fix bug - Identifier content is not validated

* Added negative tests
  • Loading branch information
emilev-ms authored and Evgeniy-L committed Sep 6, 2018
1 parent 65fb7a6 commit a5d09a3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
6 changes: 3 additions & 3 deletions gorm/resource/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (ExampleProtoMessage) String() string { return "ExampleProtoMessag
func (ExampleProtoMessage) ProtoMessage() {}

func Example() {
RegisterApplication("app")
RegisterApplication("simpleapp")

// you want to convert PB type to your application type
toGoTypeFunc := func(msg *ExampleProtoMessage) (*ExampleGoType, error) {
Expand Down Expand Up @@ -53,7 +53,7 @@ func Example() {
pb := &ExampleProtoMessage{
Id: &resourcepb.Identifier{
ApplicationName: "simpleapp",
ResourceType: "examples",
ResourceType: "example_proto_message",
ResourceId: "12",
},
// ExternalId stores data about "external_resource" that belongs to
Expand Down Expand Up @@ -114,7 +114,7 @@ func Example() {
// Output:
//application name of integer id: 12
//application name of fqstring id: externalapp/external_resource/id
//application name of internal id: app
//application name of internal id: simpleapp
//resource type of internal id: example_proto_message
//resource id of internal id: 12
//application name of fqstring id: externalapp
Expand Down
11 changes: 11 additions & 0 deletions gorm/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ func Decode(pb proto.Message, id *resourcepb.Identifier) (driver.Value, error) {
return resourcepb.BuildString(id.GetApplicationName(), id.GetResourceType(), id.GetResourceId()), nil
}

appName := ApplicationName()
resourceName := Name(pb)

if id.ApplicationName != appName {
return 0, fmt.Errorf("resource: invalid application name, expected %s", appName)
}

if id.ResourceType != resourceName {
return 0, fmt.Errorf("resource: invalid resource name, expected %s", resourceName)
}

// resource id
return id.GetResourceId(), nil
}
Expand Down
26 changes: 24 additions & 2 deletions gorm/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestDecode(t *testing.T) {
{
Identifier: &resourcepb.Identifier{
ApplicationName: "app",
ResourceType: "res",
ResourceType: "identifier",
ResourceId: "1",
},
Message: &resourcepb.Identifier{}, // any proto not registered
Expand All @@ -225,6 +225,26 @@ func TestDecode(t *testing.T) {
Message: &resourcepb.Identifier{},
Value: nil,
},
{
Identifier: &resourcepb.Identifier{
ApplicationName: "noValidApp",
ResourceType: "identifier",
ResourceId: "1",
},
Message: &resourcepb.Identifier{},
Value: 0,
ExpectedError: "resource: invalid application name, expected app",
},
{
Identifier: &resourcepb.Identifier{
ApplicationName: "app",
ResourceType: "noValidResource",
ResourceId: "1",
},
Message: &resourcepb.Identifier{},
Value: 0,
ExpectedError: "resource: invalid resource name, expected identifier",
},
}

for n, tc := range tcases {
Expand Down Expand Up @@ -290,7 +310,9 @@ func TestDecodeInt64(t *testing.T) {
},
{
Identifier: &resourcepb.Identifier{
ResourceId: "s",
ApplicationName: "app",
ResourceType: "identifier",
ResourceId: "s",
},
Message: &resourcepb.Identifier{}, // any proto not registered
ExpectedError: "resource: invalid value type, expected int64",
Expand Down

0 comments on commit a5d09a3

Please sign in to comment.