nuclei/pkg/protocols/file/request_test.go

83 lines
3.0 KiB
Go
Raw Normal View History

2021-02-03 12:19:10 +00:00
package file
import (
"os"
"path/filepath"
2021-02-03 12:19:10 +00:00
"testing"
"github.com/stretchr/testify/require"
"github.com/projectdiscovery/nuclei/v3/pkg/model"
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
"github.com/projectdiscovery/nuclei/v3/pkg/operators/extractors"
"github.com/projectdiscovery/nuclei/v3/pkg/operators/matchers"
"github.com/projectdiscovery/nuclei/v3/pkg/output"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
permissionutil "github.com/projectdiscovery/utils/permission"
2021-02-03 12:19:10 +00:00
)
func TestFileExecuteWithResults(t *testing.T) {
options := testutils.DefaultOptions
testutils.Init(options)
templateID := "testing-file"
executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{
ID: templateID,
Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},
})
2021-02-03 12:19:10 +00:00
request := &Request{
ID: templateID,
MaxSize: "1Gb",
NoRecursive: false,
Extensions: []string{"all"},
DenyList: []string{".go"},
2021-02-03 12:19:10 +00:00
Operators: operators.Operators{
Matchers: []*matchers.Matcher{{
Name: "test",
Part: "raw",
Type: matchers.MatcherTypeHolder{MatcherType: matchers.WordsMatcher},
2021-02-03 12:19:10 +00:00
Words: []string{"1.1.1.1"},
}},
Extractors: []*extractors.Extractor{{
Part: "raw",
Type: extractors.ExtractorTypeHolder{ExtractorType: extractors.RegexExtractor},
2021-02-03 12:19:10 +00:00
Regex: []string{"[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"},
}},
},
options: executerOpts,
2021-02-03 12:19:10 +00:00
}
err := request.Compile(executerOpts)
require.Nil(t, err, "could not compile file request")
tempDir, err := os.MkdirTemp("", "test-*")
2021-02-03 12:19:10 +00:00
require.Nil(t, err, "could not create temporary directory")
defer os.RemoveAll(tempDir)
files := map[string]string{
"config.yaml": "TEST\r\n1.1.1.1\r\n",
}
for k, v := range files {
err = os.WriteFile(filepath.Join(tempDir, k), []byte(v), permissionutil.TempFilePermission)
2021-02-03 12:19:10 +00:00
require.Nil(t, err, "could not write temporary file")
}
var finalEvent *output.InternalWrappedEvent
t.Run("valid", func(t *testing.T) {
metadata := make(output.InternalEvent)
previous := make(output.InternalEvent)
ctxArgs := contextargs.NewWithInput(tempDir)
err := request.ExecuteWithResults(ctxArgs, metadata, previous, func(event *output.InternalWrappedEvent) {
2021-02-03 12:19:10 +00:00
finalEvent = event
})
require.Nil(t, err, "could not execute file request")
})
require.NotNil(t, finalEvent, "could not get event output from request")
require.Equal(t, 1, len(finalEvent.Results), "could not get correct number of results")
require.Equal(t, "test", finalEvent.Results[0].MatcherName, "could not get correct matcher name of results")
require.Equal(t, 1, len(finalEvent.Results[0].ExtractedResults), "could not get correct number of extracted results")
require.Equal(t, "1.1.1.1", finalEvent.Results[0].ExtractedResults[0], "could not get correct extracted results")
finalEvent = nil
}