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

     1  package offlinehttp
     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/operators"
    13  	"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
    14  	permissionutil "github.com/projectdiscovery/utils/permission"
    15  )
    16  
    17  func TestFindResponses(t *testing.T) {
    18  	options := testutils.DefaultOptions
    19  
    20  	testutils.Init(options)
    21  	templateID := "testing-offline"
    22  	request := &Request{}
    23  	executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{
    24  		ID:   templateID,
    25  		Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},
    26  	})
    27  	executerOpts.Operators = []*operators.Operators{{}}
    28  	err := request.Compile(executerOpts)
    29  	require.Nil(t, err, "could not compile file request")
    30  
    31  	tempDir, err := os.MkdirTemp("", "test-*")
    32  	require.Nil(t, err, "could not create temporary directory")
    33  	defer os.RemoveAll(tempDir)
    34  
    35  	files := map[string]string{
    36  		"test.go":           "TEST",
    37  		"config.txt":        "TEST",
    38  		"final.txt":         "TEST",
    39  		"image_ignored.png": "TEST",
    40  		"test.txt":          "TEST",
    41  	}
    42  	for k, v := range files {
    43  		err = os.WriteFile(filepath.Join(tempDir, k), []byte(v), permissionutil.TempFilePermission)
    44  		require.Nil(t, err, "could not write temporary file")
    45  	}
    46  	expected := []string{"config.txt", "final.txt", "test.txt"}
    47  	got := []string{}
    48  	err = request.getInputPaths(tempDir+"/*", func(item string) {
    49  		base := filepath.Base(item)
    50  		got = append(got, base)
    51  	})
    52  	require.Nil(t, err, "could not get input paths for glob")
    53  	require.ElementsMatch(t, expected, got, "could not get correct file matches for glob")
    54  
    55  	got = []string{}
    56  	err = request.getInputPaths(tempDir, func(item string) {
    57  		base := filepath.Base(item)
    58  		got = append(got, base)
    59  	})
    60  	require.Nil(t, err, "could not get input paths for directory")
    61  	require.ElementsMatch(t, expected, got, "could not get correct file matches for directory")
    62  }