github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/common/expressions/expressions_test.go (about) 1 package expressions 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestEvaluate(t *testing.T) { 10 items := []struct { 11 input string 12 expected string 13 extra map[string]interface{} 14 }{ 15 {input: "{{url_encode('test}aaa')}}", expected: "test%7Daaa", extra: map[string]interface{}{}}, 16 {input: "{{hex_encode('PING')}}", expected: "50494e47", extra: map[string]interface{}{}}, 17 {input: "{{hex_encode('{{')}}", expected: "7b7b", extra: map[string]interface{}{}}, 18 {input: `{{concat("{{", 123, "*", 123, "}}")}}`, expected: "{{123*123}}", extra: map[string]interface{}{}}, 19 {input: `{{concat("{{", "123*123", "}}")}}`, expected: "{{123*123}}", extra: map[string]interface{}{}}, 20 {input: `{{"{{" + '123*123' + "}}"}}`, expected: `{{"{{" + '123*123' + "}}"}}`, extra: map[string]interface{}{}}, 21 {input: `{{a + '123*123' + b}}`, expected: `aa123*123bb`, extra: map[string]interface{}{"a": "aa", "b": "bb"}}, 22 {input: `{{concat(123,'*',123)}}`, expected: "123*123", extra: map[string]interface{}{}}, 23 {input: `{{1+1}}`, expected: "{{1+1}}", extra: map[string]interface{}{}}, 24 {input: `{{"1"+"1"}}`, expected: `{{"1"+"1"}}`, extra: map[string]interface{}{}}, 25 {input: `{{"1" + '*' + "1"}}`, expected: `{{"1" + '*' + "1"}}`, extra: map[string]interface{}{}}, 26 {input: `{{"a" + 'b' + "c"}}`, expected: `{{"a" + 'b' + "c"}}`, extra: map[string]interface{}{}}, 27 {input: `{{10*2}}`, expected: `{{10*2}}`, extra: map[string]interface{}{}}, 28 {input: `{{10/2}}`, expected: `{{10/2}}`, extra: map[string]interface{}{}}, 29 {input: `{{10-2}}`, expected: `{{10-2}}`, extra: map[string]interface{}{}}, 30 {input: "test", expected: "test", extra: map[string]interface{}{}}, 31 {input: "{{hex_encode(Item)}}", expected: "50494e47", extra: map[string]interface{}{"Item": "PING"}}, 32 {input: "{{hex_encode(Item)}}\r\n", expected: "50494e47\r\n", extra: map[string]interface{}{"Item": "PING"}}, 33 {input: "{{someTestData}}{{hex_encode('PING')}}", expected: "{{someTestData}}50494e47", extra: map[string]interface{}{}}, 34 {input: `_IWP_JSON_PREFIX_{{base64("{\"iwp_action\":\"add_site\",\"params\":{\"username\":\"\"}}")}}`, expected: "_IWP_JSON_PREFIX_eyJpd3BfYWN0aW9uIjoiYWRkX3NpdGUiLCJwYXJhbXMiOnsidXNlcm5hbWUiOiIifX0=", extra: map[string]interface{}{}}, 35 {input: "{{}}", expected: "{{}}", extra: map[string]interface{}{}}, 36 {input: `"{{hex_encode('PING')}}"`, expected: `"50494e47"`, extra: map[string]interface{}{}}, 37 } 38 for _, item := range items { 39 value, err := Evaluate(item.input, item.extra) 40 require.Nil(t, err, "could not evaluate helper") 41 42 require.Equal(t, item.expected, value, "could not get correct expression") 43 } 44 } 45 46 func TestEval(t *testing.T) { 47 items := []struct { 48 input string 49 values map[string]interface{} 50 expected interface{} 51 }{ 52 {input: "'a' + 'a'", values: nil, expected: "aa"}, 53 {input: "10 + to_number(b)", values: map[string]interface{}{"b": "4"}, expected: float64(14)}, 54 } 55 for _, item := range items { 56 value, err := Eval(item.input, item.values) 57 require.Nil(t, err, "could not evaluate helper") 58 require.Equal(t, item.expected, value, "could not get correct expression") 59 } 60 }