Additional helpers (#2359)

* add starts_with, ends_with helper functions

* add test cases, update dsl signatures

* change split_starts_with to line_starts_with
dev
Sajad 2022-08-06 23:16:03 +05:30 committed by GitHub
parent d9f9324e12
commit c4ba2b4edb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 1 deletions

View File

@ -81,6 +81,10 @@ requests:
69: {{zlib("Hello")}} 69: {{zlib("Hello")}}
70: {{zlib_decode(hex_decode("789cf248cdc9c907040000ffff058c01f5"))}} 70: {{zlib_decode(hex_decode("789cf248cdc9c907040000ffff058c01f5"))}}
71: {{hex_encode(aes_gcm("AES256Key-32Characters1234567890", "exampleplaintext"))}} 71: {{hex_encode(aes_gcm("AES256Key-32Characters1234567890", "exampleplaintext"))}}
72: {{starts_with("Hello", "He")}}
73: {{ends_with("Hello", "lo")}}
74: {{line_starts_with("Hi\nHello", "He")}}
75: {{line_ends_with("Hello\nHi", "lo")}}
extractors: extractors:
- type: regex - type: regex

View File

@ -256,7 +256,7 @@ func (h *httpDSLFunctions) Execute(filePath string) error {
} }
totalExtracted := strings.Split(submatch[1], ",") totalExtracted := strings.Split(submatch[1], ",")
numberOfDslFunctions := 71 numberOfDslFunctions := 75
if len(totalExtracted) != numberOfDslFunctions { if len(totalExtracted) != numberOfDslFunctions {
return errors.New("incorrect number of results") return errors.New("incorrect number of results")
} }

View File

@ -246,6 +246,64 @@ func init() {
"contains": makeDslFunction(2, func(args ...interface{}) (interface{}, error) { "contains": makeDslFunction(2, func(args ...interface{}) (interface{}, error) {
return strings.Contains(types.ToString(args[0]), types.ToString(args[1])), nil return strings.Contains(types.ToString(args[0]), types.ToString(args[1])), nil
}), }),
"starts_with": makeDslWithOptionalArgsFunction(
"(str string, prefix ...string) bool",
func(args ...interface{}) (interface{}, error) {
if len(args) < 2 {
return nil, invalidDslFunctionError
}
for _, prefix := range args[1:] {
if strings.HasPrefix(types.ToString(args[0]), types.ToString(prefix)) {
return true, nil
}
}
return false, nil
},
),
"line_starts_with": makeDslWithOptionalArgsFunction(
"(str string, prefix ...string) bool", func(args ...interface{}) (interface{}, error) {
if len(args) < 2 {
return nil, invalidDslFunctionError
}
for _, line := range strings.Split(types.ToString(args[0]), "\n") {
for _, prefix := range args[1:] {
if strings.HasPrefix(line, types.ToString(prefix)) {
return true, nil
}
}
}
return false, nil
},
),
"ends_with": makeDslWithOptionalArgsFunction(
"(str string, suffix ...string) bool",
func(args ...interface{}) (interface{}, error) {
if len(args) < 2 {
return nil, invalidDslFunctionError
}
for _, suffix := range args[1:] {
if strings.HasSuffix(types.ToString(args[0]), types.ToString(suffix)) {
return true, nil
}
}
return false, nil
},
),
"line_ends_with": makeDslWithOptionalArgsFunction(
"(str string, suffix ...string) bool", func(args ...interface{}) (interface{}, error) {
if len(args) < 2 {
return nil, invalidDslFunctionError
}
for _, line := range strings.Split(types.ToString(args[0]), "\n") {
for _, suffix := range args[1:] {
if strings.HasSuffix(line, types.ToString(suffix)) {
return true, nil
}
}
}
return false, nil
},
),
"concat": makeDslWithOptionalArgsFunction( "concat": makeDslWithOptionalArgsFunction(
"(args ...interface{}) string", "(args ...interface{}) string",
func(arguments ...interface{}) (interface{}, error) { func(arguments ...interface{}) (interface{}, error) {

View File

@ -142,6 +142,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
contains(arg1, arg2 interface{}) interface{} contains(arg1, arg2 interface{}) interface{}
date_time(dateTimeFormat string, optionalUnixTime interface{}) string date_time(dateTimeFormat string, optionalUnixTime interface{}) string
dec_to_hex(arg1 interface{}) interface{} dec_to_hex(arg1 interface{}) interface{}
ends_with(str string, suffix ...string) bool
generate_java_gadget(arg1, arg2, arg3 interface{}) interface{} generate_java_gadget(arg1, arg2, arg3 interface{}) interface{}
gzip(arg1 interface{}) interface{} gzip(arg1 interface{}) interface{}
gzip_decode(arg1 interface{}) interface{} gzip_decode(arg1 interface{}) interface{}
@ -152,6 +153,8 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
html_unescape(arg1 interface{}) interface{} html_unescape(arg1 interface{}) interface{}
join(separator string, elements ...interface{}) string join(separator string, elements ...interface{}) string
len(arg1 interface{}) interface{} len(arg1 interface{}) interface{}
line_ends_with(str string, suffix ...string) bool
line_starts_with(str string, prefix ...string) bool
md5(arg1 interface{}) interface{} md5(arg1 interface{}) interface{}
mmh3(arg1 interface{}) interface{} mmh3(arg1 interface{}) interface{}
print_debug(args ...interface{}) print_debug(args ...interface{})
@ -170,6 +173,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
reverse(arg1 interface{}) interface{} reverse(arg1 interface{}) interface{}
sha1(arg1 interface{}) interface{} sha1(arg1 interface{}) interface{}
sha256(arg1 interface{}) interface{} sha256(arg1 interface{}) interface{}
starts_with(str string, prefix ...string) bool
to_lower(arg1 interface{}) interface{} to_lower(arg1 interface{}) interface{}
to_number(arg1 interface{}) interface{} to_number(arg1 interface{}) interface{}
to_string(arg1 interface{}) interface{} to_string(arg1 interface{}) interface{}
@ -248,6 +252,10 @@ func TestDslExpressions(t *testing.T) {
`len("Hello")`: float64(5), `len("Hello")`: float64(5),
`len(1234)`: float64(4), `len(1234)`: float64(4),
`contains("Hello", "lo")`: true, `contains("Hello", "lo")`: true,
`starts_with("Hello", "He")`: true,
`ends_with("Hello", "lo")`: true,
"line_starts_with('Hi\nHello', 'He')": true, // back quotes do not support escape sequences
"line_ends_with('Hii\nHello', 'ii')": true, // back quotes do not support escape sequences
`regex("H([a-z]+)o", "Hello")`: true, `regex("H([a-z]+)o", "Hello")`: true,
`wait_for(1)`: nil, `wait_for(1)`: nil,
`print_debug(1+2, "Hello")`: nil, `print_debug(1+2, "Hello")`: nil,