Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/file/find_test.go
2070 views
1
package file
2
3
import (
4
"os"
5
"path/filepath"
6
"testing"
7
8
"github.com/stretchr/testify/require"
9
10
"github.com/projectdiscovery/nuclei/v3/pkg/model"
11
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
12
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
13
permissionutil "github.com/projectdiscovery/utils/permission"
14
)
15
16
func TestFindInputPaths(t *testing.T) {
17
options := testutils.DefaultOptions
18
19
testutils.Init(options)
20
templateID := "testing-file"
21
request := &Request{
22
ID: templateID,
23
MaxSize: "1Gb",
24
NoRecursive: false,
25
Extensions: []string{"all", ".lock"},
26
DenyList: []string{".go"},
27
Operators: newMockOperator(),
28
}
29
executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{
30
ID: templateID,
31
Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},
32
})
33
err := request.Compile(executerOpts)
34
require.Nil(t, err, "could not compile file request")
35
36
tempDir, err := os.MkdirTemp("", "test-*")
37
require.Nil(t, err, "could not create temporary directory")
38
defer func() {
39
_ = os.RemoveAll(tempDir)
40
}()
41
42
files := map[string]string{
43
"test.go": "TEST",
44
"config.yaml": "TEST",
45
"final.yaml": "TEST",
46
"image_ignored.png": "TEST",
47
"test.js": "TEST",
48
}
49
for k, v := range files {
50
err = os.WriteFile(filepath.Join(tempDir, k), []byte(v), permissionutil.TempFilePermission)
51
require.Nil(t, err, "could not write temporary file")
52
}
53
expected := []string{"config.yaml", "final.yaml", "test.js"}
54
got := []string{}
55
err = request.getInputPaths(tempDir+"/*", func(item string) {
56
base := filepath.Base(item)
57
got = append(got, base)
58
})
59
require.Nil(t, err, "could not get input paths for glob")
60
require.ElementsMatch(t, expected, got, "could not get correct file matches for glob")
61
62
got = []string{}
63
err = request.getInputPaths(tempDir, func(item string) {
64
base := filepath.Base(item)
65
got = append(got, base)
66
})
67
require.Nil(t, err, "could not get input paths for directory")
68
require.ElementsMatch(t, expected, got, "could not get correct file matches for directory")
69
}
70
71