forked from ait-testbed/attackmate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_regexexecutor.py
More file actions
194 lines (175 loc) · 7.85 KB
/
Copy pathtest_regexexecutor.py
File metadata and controls
194 lines (175 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import pytest
from unittest.mock import MagicMock
from attackmate.variablestore import VariableStore
from attackmate.processmanager import ProcessManager
from attackmate.schemas.regex import RegExCommand
from attackmate.executors.common.regexexecutor import RegExExecutor
class TestRegExExecutor:
def setup_method(self, method):
self.varstore = VariableStore()
self.process_manager = ProcessManager()
self.executor = RegExExecutor(self.process_manager, self.varstore)
self.executor.logger = MagicMock()
def test_log_command(self):
command = RegExCommand(type='regex', cmd='test', mode='findall', output={})
self.executor.log_command(command)
self.executor.logger.warning.assert_called_once_with("RegEx: 'test', Mode: 'findall'")
def test_forge_variables(self):
# Test with None
result = self.executor.forge_variables(None)
assert result is None
# Test with string
result = self.executor.forge_variables('test')
assert result == {'MATCH_0': 'test'}
# Test with list of strings
result = self.executor.forge_variables(['test1', 'test2'])
assert result == {'MATCH_0': 'test1', 'MATCH_1': 'test2'}
# Test with nested list
result = self.executor.forge_variables([['test1', 'test2'], ['test3', 'test4']])
assert result == {
'MATCH_0_0': 'test1',
'MATCH_0_1': 'test2',
'MATCH_1_0': 'test3',
'MATCH_1_1': 'test4',
}
def test_register_outputvars(self):
matches = {'MATCH_0': 'test'}
outputvars = {'output_var': 'Matched: $MATCH_0'}
self.executor.register_outputvars(outputvars, matches)
assert self.varstore.get_variable('output_var') == 'Matched: test'
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == ['test']
def test_forge_and_register_variables(self):
output = {'output_var': 'Matched: $MATCH_0'}
data = 'test'
self.executor.forge_and_register_variables(output, data)
assert self.varstore.get_variable('output_var') == 'Matched: test'
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == ['test']
@pytest.mark.asyncio
async def test_exec_cmd_findall(self):
# Test mode "findall"
self.varstore.set_variable('input_var', 'test1 test2 test3')
command = RegExCommand(
type='regex',
cmd='test\\d',
mode='findall',
input='input_var',
output={'output_var': 'Found: $MATCH_0, $MATCH_1, $MATCH_2'},
)
await self.executor._exec_cmd(command)
assert self.varstore.get_variable('output_var') == 'Found: test1, test2, test3'
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == ['test1', 'test2', 'test3']
@pytest.mark.asyncio
async def test_exec_cmd_split(self):
# Test mode "split"
self.varstore.set_variable('input_var', 'test1 test2 test3')
command = RegExCommand(
type='regex',
cmd='\\s',
mode='split',
input='input_var',
output={'output_var': 'First: $MATCH_0, Second: $MATCH_1, Third: $MATCH_2'},
)
await self.executor._exec_cmd(command)
assert self.varstore.get_variable('output_var') == 'First: test1, Second: test2, Third: test3'
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == ['test1', 'test2', 'test3']
@pytest.mark.asyncio
async def test_exec_cmd_search(self):
# Test mode "search"
self.varstore.set_variable('input_var', 'test1 test2 test3')
command = RegExCommand(
type='regex',
cmd='test\\d',
mode='search',
input='input_var',
output={'output_var': 'Found: $MATCH_0'},
)
await self.executor._exec_cmd(command)
assert self.varstore.get_variable('output_var') == 'Found: test1'
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == ['test1']
@pytest.mark.asyncio
async def test_exec_cmd_sub(self):
# Test mode "sub"
# emulates behaviour of re.sub, if no match is found input string is returned
self.varstore.set_variable('input_var', 'test1 test2 test3')
command = RegExCommand(
type='regex',
cmd='test',
mode='sub',
replace='TEST',
input='input_var',
output={'output_var': 'Replaced: $MATCH_0'},
)
await self.executor._exec_cmd(command)
assert self.varstore.get_variable('output_var') == 'Replaced: TEST1 TEST2 TEST3'
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == ['TEST1 TEST2 TEST3']
@pytest.mark.asyncio
async def test_exec_cmd_findall_no_match(self):
# Test mode "findall" without matches
self.varstore.set_variable('input_var', 'no matches here')
command = RegExCommand(
type='regex',
cmd='test\\d',
mode='findall',
input='input_var',
output={'output_var': 'Found: $MATCH_0'},
)
await self.executor._exec_cmd(command)
assert self.varstore.get_variable('output_var') == ''
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == []
@pytest.mark.asyncio
async def test_exec_cmd_split_no_match(self):
# Test mode "split" without matches
# emulates behaviour of re.split, if no match is found input string is returned
self.varstore.set_variable('input_var', 'no matches here')
command = RegExCommand(
type='regex', cmd='\\d', mode='split', input='input_var', output={'output_var': 'First: $MATCH_0'}
)
await self.executor._exec_cmd(command)
assert self.varstore.get_variable('output_var') == 'First: no matches here'
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == ['no matches here']
@pytest.mark.asyncio
async def test_exec_cmd_search_no_match(self):
# Test mode "search" without matches
self.varstore.set_variable('input_var', 'no matches here')
command = RegExCommand(
type='regex',
cmd='test\\d',
mode='search',
input='input_var',
output={'output_var': 'Found: $MATCH_0'},
)
await self.executor._exec_cmd(command)
assert self.varstore.get_variable('output_var') == ''
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == []
@pytest.mark.asyncio
async def test_exec_cmd_sub_no_match(self):
# Test mode "sub" without matches
# emulates behaviour of re.sub, if no match is found input string is returned
self.varstore.set_variable('input_var', 'no matches here')
command = RegExCommand(
type='regex',
cmd='test',
mode='sub',
replace='TEST',
input='input_var',
output={'output_var': 'Replaced: $MATCH_0'},
)
await self.executor._exec_cmd(command)
assert self.varstore.get_variable('output_var') == 'Replaced: no matches here'
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == ['no matches here']
@pytest.mark.asyncio
async def test_exec_cmd_search_no_match_clears_stale_value(self):
# Regression: in a loop a previous iteration might set output
# variable. A following no-match must clear it, not leave the old value.
self.varstore.set_variable('PORT_STATUS', 'open')
self.varstore.set_variable('input_var', 'closed')
command = RegExCommand(
type='regex',
cmd='open',
mode='search',
input='input_var',
output={'PORT_STATUS': '$MATCH_0'},
)
await self.executor._exec_cmd(command)
assert self.varstore.get_variable('PORT_STATUS') == ''
assert self.varstore.get_variable('REGEX_MATCHES_LIST') == []