2021-02-03 12:19:10 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2021-08-23 11:53:37 +00:00
|
|
|
"path/filepath"
|
2021-02-03 12:19:10 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-07-19 18:04:08 +00:00
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/internal/testutils"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model"
|
2021-09-03 13:48:39 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity"
|
2021-02-03 12:19:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindInputPaths(t *testing.T) {
|
|
|
|
options := testutils.DefaultOptions
|
|
|
|
|
|
|
|
testutils.Init(options)
|
|
|
|
templateID := "testing-file"
|
|
|
|
request := &Request{
|
2021-02-05 09:13:11 +00:00
|
|
|
ID: templateID,
|
|
|
|
MaxSize: 1024,
|
|
|
|
NoRecursive: false,
|
2021-03-08 13:50:40 +00:00
|
|
|
Extensions: []string{"all", ".lock"},
|
2021-02-05 09:13:11 +00:00
|
|
|
ExtensionDenylist: []string{".go"},
|
2021-02-03 12:19:10 +00:00
|
|
|
}
|
|
|
|
executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{
|
|
|
|
ID: templateID,
|
2021-09-03 13:48:39 +00:00
|
|
|
Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},
|
2021-02-03 12:19:10 +00:00
|
|
|
})
|
|
|
|
err := request.Compile(executerOpts)
|
|
|
|
require.Nil(t, err, "could not compile file request")
|
|
|
|
|
|
|
|
tempDir, err := ioutil.TempDir("", "test-*")
|
|
|
|
require.Nil(t, err, "could not create temporary directory")
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
|
|
|
|
files := map[string]string{
|
|
|
|
"test.go": "TEST",
|
|
|
|
"config.yaml": "TEST",
|
|
|
|
"final.yaml": "TEST",
|
|
|
|
"image_ignored.png": "TEST",
|
|
|
|
"test.js": "TEST",
|
|
|
|
}
|
|
|
|
for k, v := range files {
|
2021-08-23 11:53:37 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(tempDir, k), []byte(v), 0777)
|
2021-02-03 12:19:10 +00:00
|
|
|
require.Nil(t, err, "could not write temporary file")
|
|
|
|
}
|
|
|
|
expected := []string{"config.yaml", "final.yaml", "test.js"}
|
|
|
|
got := []string{}
|
|
|
|
err = request.getInputPaths(tempDir+"/*", func(item string) {
|
2021-08-23 11:53:37 +00:00
|
|
|
base := filepath.Base(item)
|
2021-02-03 12:19:10 +00:00
|
|
|
got = append(got, base)
|
|
|
|
})
|
|
|
|
require.Nil(t, err, "could not get input paths for glob")
|
|
|
|
require.ElementsMatch(t, expected, got, "could not get correct file matches for glob")
|
|
|
|
|
|
|
|
got = []string{}
|
|
|
|
err = request.getInputPaths(tempDir, func(item string) {
|
2021-08-23 11:53:37 +00:00
|
|
|
base := filepath.Base(item)
|
2021-02-03 12:19:10 +00:00
|
|
|
got = append(got, base)
|
|
|
|
})
|
|
|
|
require.Nil(t, err, "could not get input paths for directory")
|
|
|
|
require.ElementsMatch(t, expected, got, "could not get correct file matches for directory")
|
|
|
|
}
|