Skip to content

Commit

Permalink
Added Fuzz test suite for Get Environment API (#4437)
Browse files Browse the repository at this point in the history
* Added fuzz test-case for env

Signed-off-by: Saranya-jena <[email protected]>

* Added fuzz test for get environment

Signed-off-by: Saranya-jena <[email protected]>

* Added build file for OSS-Fuzz integration

Signed-off-by: Saranya-jena <[email protected]>

* Added license details

Signed-off-by: Saranya-jena <[email protected]>

---------

Signed-off-by: Saranya-jena <[email protected]>
Co-authored-by: Vedant Shrotria <[email protected]>
  • Loading branch information
Saranya-jena and Jonsy13 authored Feb 15, 2024
1 parent 78d6dec commit d65c8af
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
21 changes: 21 additions & 0 deletions chaoscenter/graphql/server/fuzz_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash -eu
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
cd chaoscenter/graphql/server && go mod download
go install github.com/AdamKorcz/go-118-fuzz-build@latest
go get github.com/AdamKorcz/go-118-fuzz-build/testing

compile_native_go_fuzzer $(pwd)/pkg/environment/handler FuzzTestGetEnvironment test-fuzz
46 changes: 42 additions & 4 deletions chaoscenter/graphql/server/pkg/environment/handler/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package handler_test
package handler

import (
"context"
"errors"
"testing"
"time"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb"
"go.mongodb.org/mongo-driver/mongo"

"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"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/environments"
dbOperationsEnvironment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
dbMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/mocks"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/environment/handler"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
"github.com/stretchr/testify/mock"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -97,7 +99,7 @@ func TestCreateEnvironment(t *testing.T) {
token := tc.given()
ctx := context.WithValue(context.Background(), authorization.AuthKey, token)
mockOperator := environmentOperator
service := handler.NewEnvironmentService(mockOperator)
service := NewEnvironmentService(mockOperator)

env, err := service.CreateEnvironment(ctx, tc.projectID, tc.input)
if (err != nil && tc.expectedErr == nil) ||
Expand Down Expand Up @@ -174,7 +176,7 @@ func TestDeleteEnvironment(t *testing.T) {
ctx := context.WithValue(context.Background(), authorization.AuthKey, token)

mockOperator := environmentOperator
service := handler.NewEnvironmentService(mockOperator)
service := NewEnvironmentService(mockOperator)

_, err := service.DeleteEnvironment(ctx, tc.projectID, tc.environmentID)
if (err != nil && tc.expectedErr == nil) ||
Expand All @@ -185,3 +187,39 @@ func TestDeleteEnvironment(t *testing.T) {
})
}
}

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")
}
})
}

0 comments on commit d65c8af

Please sign in to comment.