-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Fuzzers for Environment (#4483)
* Added Fuzzers for Environment Signed-off-by: Saranya-jena <[email protected]> * separated out fuzzers and UTs Signed-off-by: Saranya-jena <[email protected]> * removed old testdata Signed-off-by: Saranya-jena <[email protected]> * fixed imports Signed-off-by: Saranya-jena <[email protected]> --------- Signed-off-by: Saranya-jena <[email protected]> Co-authored-by: Namkyu Park <[email protected]>
- Loading branch information
1 parent
c7f896d
commit 8ee3076
Showing
5 changed files
with
135 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
chaoscenter/graphql/server/pkg/environment/handler/handler_fuzz_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
fuzz "github.com/AdaLogics/go-fuzz-headers" | ||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" | ||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" | ||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb" | ||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/mock" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func FuzzCreateEnvironment(f *testing.F) { | ||
utils.Config.JwtSecret = JwtSecret | ||
f.Fuzz(func(t *testing.T, data []byte) { | ||
fuzzConsumer := fuzz.NewConsumer(data) | ||
targetStruct := &struct { | ||
input model.CreateEnvironmentRequest | ||
projectID string | ||
}{} | ||
err := fuzzConsumer.GenerateStruct(targetStruct) | ||
if err != nil { | ||
return | ||
} | ||
mongodbMockOperator.On("Create", mock.Anything, mongodb.EnvironmentCollection, mock.Anything).Return(nil).Once() | ||
token, err := GetSignedJWT("testUser") | ||
if err != nil { | ||
logrus.Errorf("Error genrating token %v", err) | ||
} | ||
|
||
ctx := context.WithValue(context.Background(), authorization.AuthKey, token) | ||
service := NewEnvironmentService(environmentOperator) | ||
|
||
env, err := service.CreateEnvironment(ctx, targetStruct.projectID, &targetStruct.input) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if env == nil { | ||
t.Errorf("Returned environment is nil") | ||
} | ||
}) | ||
} | ||
|
||
func FuzzTestDeleteEnvironment(f *testing.F) { | ||
utils.Config.JwtSecret = JwtSecret | ||
testCases := []struct { | ||
projectID string | ||
environmentID string | ||
}{ | ||
{ | ||
projectID: "testProject", | ||
environmentID: "testEnvID", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
f.Add(tc.projectID, tc.environmentID) | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, projectID string, environmentID string) { | ||
|
||
findResult := []interface{}{bson.D{ | ||
{Key: "environment_id", Value: environmentID}, | ||
{Key: "project_id", Value: projectID}, | ||
}} | ||
singleResult := mongo.NewSingleResultFromDocument(findResult[0], nil, nil) | ||
mongodbMockOperator.On("Get", mock.Anything, mongodb.EnvironmentCollection, mock.Anything).Return(singleResult, nil).Once() | ||
mongodbMockOperator.On("UpdateMany", mock.Anything, mongodb.EnvironmentCollection, mock.Anything, mock.Anything, mock.Anything).Return(&mongo.UpdateResult{}, nil).Once() | ||
token, err := GetSignedJWT("testUser") | ||
if err != nil { | ||
logrus.Errorf("Error genrating token %v", err) | ||
} | ||
|
||
ctx := context.WithValue(context.Background(), authorization.AuthKey, token) | ||
service := NewEnvironmentService(environmentOperator) | ||
|
||
env, err := service.DeleteEnvironment(ctx, projectID, environmentID) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
if env == "" { | ||
t.Errorf("Returned environment is nil") | ||
} | ||
}) | ||
} | ||
|
||
func FuzzTestGetEnvironment(f *testing.F) { | ||
utils.Config.JwtSecret = JwtSecret | ||
testCases := []struct { | ||
projectID string | ||
environmentID string | ||
}{ | ||
{ | ||
projectID: "testProject", | ||
environmentID: "testEnvID", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
f.Add(tc.projectID, tc.environmentID) | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, projectID string, environmentID string) { | ||
|
||
findResult := []interface{}{bson.D{ | ||
{Key: "environment_id", Value: environmentID}, | ||
{Key: "project_id", Value: projectID}, | ||
}} | ||
singleResult := mongo.NewSingleResultFromDocument(findResult[0], nil, nil) | ||
mongodbMockOperator.On("Get", mock.Anything, mongodb.EnvironmentCollection, mock.Anything).Return(singleResult, nil).Once() | ||
service := NewEnvironmentService(environmentOperator) | ||
|
||
env, err := service.GetEnvironment(projectID, environmentID) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
if env == nil { | ||
t.Errorf("Returned environment is nil") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...aphql/server/pkg/environment/handler/testdata/fuzz/FuzzCreateEnvironment/582528ddfad69eb5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("0") |