github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/common/fuzz/parts_test.go (about)

     1  package fuzz
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
     7  	"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/contextargs"
     8  	"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/interactsh"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestExecuteQueryPartRule(t *testing.T) {
    13  	URL := "http://localhost:8080/?url=localhost&mode=multiple&file=passwdfile"
    14  	options := &protocols.ExecutorOptions{
    15  		Interactsh: &interactsh.Client{},
    16  	}
    17  	t.Run("single", func(t *testing.T) {
    18  		rule := &Rule{
    19  			ruleType: postfixRuleType,
    20  			partType: queryPartType,
    21  			modeType: singleModeType,
    22  			options:  options,
    23  		}
    24  		var generatedURL []string
    25  		input := contextargs.NewWithInput(URL)
    26  		err := rule.executeQueryPartRule(&ExecuteRuleInput{
    27  			Input: input,
    28  			Callback: func(gr GeneratedRequest) bool {
    29  				generatedURL = append(generatedURL, gr.Request.URL.String())
    30  				return true
    31  			},
    32  		}, "1337'")
    33  		require.NoError(t, err, "could not execute part rule")
    34  		require.ElementsMatch(t, []string{
    35  			"http://localhost:8080/?url=localhost1337'&mode=multiple&file=passwdfile",
    36  			"http://localhost:8080/?url=localhost&mode=multiple1337'&file=passwdfile",
    37  			"http://localhost:8080/?url=localhost&mode=multiple&file=passwdfile1337'",
    38  		}, generatedURL, "could not get generated url")
    39  	})
    40  	t.Run("multiple", func(t *testing.T) {
    41  		rule := &Rule{
    42  			ruleType: postfixRuleType,
    43  			partType: queryPartType,
    44  			modeType: multipleModeType,
    45  			options:  options,
    46  		}
    47  		var generatedURL string
    48  		input := contextargs.NewWithInput(URL)
    49  		err := rule.executeQueryPartRule(&ExecuteRuleInput{
    50  			Input: input,
    51  			Callback: func(gr GeneratedRequest) bool {
    52  				generatedURL = gr.Request.URL.String()
    53  				return true
    54  			},
    55  		}, "1337'")
    56  		require.NoError(t, err, "could not execute part rule")
    57  		require.Equal(t, "http://localhost:8080/?url=localhost1337'&mode=multiple1337'&file=passwdfile1337'", generatedURL, "could not get generated url")
    58  	})
    59  }
    60  
    61  func TestExecuteReplaceRule(t *testing.T) {
    62  	tests := []struct {
    63  		ruleType    ruleType
    64  		value       string
    65  		replacement string
    66  		expected    string
    67  	}{
    68  		{replaceRuleType, "test", "replacement", "replacement"},
    69  		{prefixRuleType, "test", "prefix", "prefixtest"},
    70  		{postfixRuleType, "test", "postfix", "testpostfix"},
    71  		{infixRuleType, "", "infix", "infix"},
    72  		{infixRuleType, "0", "infix", "0infix"},
    73  		{infixRuleType, "test", "infix", "teinfixst"},
    74  	}
    75  	for _, test := range tests {
    76  		rule := &Rule{ruleType: test.ruleType}
    77  		returned := rule.executeReplaceRule(nil, test.value, test.replacement)
    78  		require.Equal(t, test.expected, returned, "could not get correct value")
    79  	}
    80  }