Skip to content

Commit c1071b7

Browse files
committed
Enable no_expect_ids with current id an allow multiple request arguments
Signed-off-by: Sebitosh <[email protected]>
1 parent 468481e commit c1071b7

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,16 @@ To combine the default check on the current rule id with additional checks, the
620620

621621
This way, the status check will be used in addition to the default rule id check.
622622

623+
For writing negative tests, you can also use the `no_expect_ids` test in the same way:
624+
625+
```yaml
626+
output:
627+
log:
628+
no_expect_ids: []
629+
```
630+
631+
This way, the current rule id will be appended and the check verifies it does not show up in logs.
632+
623633
Exact properties, syntax, available checks and parameters are dependent on the used version of `go-ftw`. The generator will simply replace what is defined under the `output` field in the corresponding field of the generated test case.
624634

625635
As described for `go-ftw`, [if any of the checks fail the test will fail](https://github.com/coreruleset/go-ftw?tab=readme-ov-file#how-log-parsing-works).

mrts/generate-rules.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import re
1010
import copy
1111
from ast import literal_eval
12+
import urllib.parse
1213

1314
NAME = "MRTS"
1415
VERSION = "0.1"
@@ -331,17 +332,19 @@ def genrulefromtemplate(self, tpl, current_confdata):
331332
item['desc'] = "Test case for rule %d, #%d" % (self.currid, testcnt)
332333
item['stages'][0]['description'] = "Send request"
333334
item['stages'][0]['input']['method'] = self.current_confdata['phase_methods'][phase].upper()
334-
if self.current_testdata['phase_methods'][phase].lower() == "post":
335-
if isinstance(test['test']['data'], dict):
336-
ik, iv = list(test['test']['data'].items())[0]
337-
item['stages'][0]['input']['data'] = "%s=%s" % (ik, iv)
338-
elif isinstance(test['test']['data'], str):
339-
item['stages'][0]['input']['data'] = "%s" % (test['test']['data'])
335+
method = self.current_testdata['phase_methods'][phase].lower()
336+
data = test['test']['data']
337+
if method == "post":
338+
if isinstance(data, dict):
339+
encoded_data = urllib.parse.urlencode(data)
340+
item['stages'][0]['input']['data'] = encoded_data
341+
elif isinstance(data, str):
342+
item['stages'][0]['input']['data'] = data
340343
item['stages'][0]['input']['uri'] = "/post"
341-
if self.current_testdata['phase_methods'][phase].lower() == "get":
342-
if isinstance(test['test']['data'], dict):
343-
ik, iv = list(test['test']['data'].items())[0]
344-
item['stages'][0]['input']['uri'] = "/?%s=%s" % (ik, iv)
344+
elif method == "get":
345+
if isinstance(data, dict):
346+
query = urllib.parse.urlencode(data)
347+
item['stages'][0]['input']['uri'] = "/?" + query
345348
# add headers if there are
346349
if 'input' in test['test']:
347350
if 'headers' in test['test']['input']:
@@ -354,10 +357,12 @@ def genrulefromtemplate(self, tpl, current_confdata):
354357
# overwrite default output field
355358
if 'output' in test['test']:
356359
item['stages'][0]['output'] = test['test']['output']
357-
# if expect_ids is in rewrite, append the current rule id
360+
# if [no_]expect_ids is in rewrite, append the current rule id
358361
if 'log' in item['stages'][0]['output']:
359362
if 'expect_ids' in item['stages'][0]['output']['log']:
360-
item['stages'][0]['output']['log']['expect_ids'].append(self.currid)
363+
item['stages'][0]['output']['log']['expect_ids'] = [self.currid]
364+
if 'no_expect_ids' in item['stages'][0]['output']['log']:
365+
item['stages'][0]['output']['log']['no_expect_ids'] = [self.currid]
361366
else:
362367
item['stages'][0]['output']['log']['expect_ids'].append(self.currid)
363368

0 commit comments

Comments
 (0)