github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/file/find_test.go (about)

     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/v2/pkg/model"
    11  	"github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity"
    12  	"github.com/projectdiscovery/nuclei/v2/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 os.RemoveAll(tempDir)
    39  
    40  	files := map[string]string{
    41  		"test.go":           "TEST",
    42  		"config.yaml":       "TEST",
    43  		"final.yaml":        "TEST",
    44  		"image_ignored.png": "TEST",
    45  		"test.js":           "TEST",
    46  	}
    47  	for k, v := range files {
    48  		err = os.WriteFile(filepath.Join(tempDir, k), []byte(v), permissionutil.TempFilePermission)
    49  		require.Nil(t, err, "could not write temporary file")
    50  	}
    51  	expected := []string{"config.yaml", "final.yaml", "test.js"}
    52  	got := []string{}
    53  	err = request.getInputPaths(tempDir+"/*", func(item string) {
    54  		base := filepath.Base(item)
    55  		got = append(got, base)
    56  	})
    57  	require.Nil(t, err, "could not get input paths for glob")
    58  	require.ElementsMatch(t, expected, got, "could not get correct file matches for glob")
    59  
    60  	got = []string{}
    61  	err = request.getInputPaths(tempDir, func(item string) {
    62  		base := filepath.Base(item)
    63  		got = append(got, base)
    64  	})
    65  	require.Nil(t, err, "could not get input paths for directory")
    66  	require.ElementsMatch(t, expected, got, "could not get correct file matches for directory")
    67  }