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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
import os
import sys
from support.organize_config import OrganizeConfig
import time
from datetime import datetime, timedelta
import pytz
from support.api_utils.log_in import LogIn
from support.packet_generator.ui_verification_deprecated import *
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from support.common_utils.data_translation import DataTranslation
import support.api_utils.get_log_deprecated
import support.api_utils.policy_verify
import support.ui_utils.env
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from support.api_utils.log_in import LogIn
from support.ui_utils.objects.create_objects import CreateObject
from support.ui_utils.objects.search_objects import SearchObject
from support.ui_utils.objects.delete_objects import DeleteObject
from support.api_utils.delete_policy import DeletePolicy
from support.api_utils.create_policy import CreatePolicy
from support.packet_generator.verify_deprecated import Verify
from support.api_utils.analyse_result import Analyse
from support.common_utils.data_translation import DataTranslation
import support.ui_utils.env
from datetime import datetime
from support.ui_utils.element_position.map_element_position_library import *
def main():
# 已知参数
test_rule_element_position = {
"page_jump": {
"mainPage_navigationBar_logo_posXpath": mainPage_navigationBar_logo_posXpath,
"mainPage_currentPage_vsysName_posXpath": mainPage_currentPage_vsysName_posXpath,
"mainPage_button_menu_posXCss": mainPage_button_menu_posXCss,
"mainPage_policyFirstLevelMenu_posId": mainPage_policyFirstLevelMenu_posXpath,
"mainPage_policySecondLevelMenu_posXpath": mainPage_policySecondLevelMenu_dos_posXpath
},
"create": {
"policyRuleListPage_createButton_posId": dosRuleListPage_createButton_posXpath,
"policyRulePage_inputName_posXpath": dosRulePage_inputName_posXpath,
"policyRulePage_selectAction_posId": dosRulePage_selectProtect_posXpath,
"policyRulePage_addSrcIP_posXpath": dosRulePage_addSrcIP_posXpath,
"policyRulePage_searchSrcIP_posXpath": dosRulePage_searchSrcIP_posXpath,
"policyRulePage_selectSrcIP_posXpath": dosRulePage_selectSrcIP_posXpath,
"policyRulePage_closeSrcIP_posXpath": dosRulePage_closeSrcIP_posXpath,
"policyRulePage_addApplication_posXpath": dosRulePage_addApplication_posXpath,
"policyRulePage_selectApplicationLabel_posXpath": dosRulePage_selectApplicationLabel_posXpath,
"policyRulePage_searchApplication_posXpath": dosRulePage_searchApplication_posXpath,
"policyRulePage_selectApplication_posXpath": dosRulePage_selectApplication_posXpath,
"policyRulePage_closeApplication_posXpath": dosRulePage_closeApplication_posXpath,
"policyRulePage_thresholdType_posId": "",
"policyRulePage_groupBy_dropDownBox_posXpath": dosRulePage_groupBy_dropDownBox_posXpath,
"policyRulePage_groupBy_select_posXpath": dosRulePage_groupBy_select_posXpath,
"policyRulePage_requestPerPeriod_posXpath": dosRulePage_requestPerPeriod_posXpath,
"policyRulePage_countingPeriod_dropDownBox_posXpath": dosRulePage_countingPeriod_dropDownBox_posXpath,
"policyRulePage_countingPeriod_select_posXpath": dosRulePage_countingPeriod_select_posXpath,
"policyRulePage_concurrentSsessions_posXpath": dosRulePage_concurrentSsessions_posXpath,
"policyRulePage_mitigationBehavior_posId": "",
"policyRulePage_mitigationTimeouts_dropDownBox_posXpath": dosRulePage_mitigationTimeouts_dropDownBox_posXpath,
"policyRulePage_mitigationTimeouts_select_posXpath": "",
"policyRulePage_enable_posXpath": dosRulePage_enable_posXpath,
"policyRulePage_okButton_yes_posId": dosRulePage_ok_posXpath,
"policyRulePage_okButton_warningYes_posXpath": dosRulePage_okButton_warningYes_posXpath
},
"search": {
"policyRuleListPage_searchLable_posId": dosRuleListPage_searchLable_posXpath,
"policyRuleListPage_searchLable_dropDown_item_posXpath": dosRuleListPage_searchLable_dropDown_item_posXpath,
"policyRuleListPage_searchLable_input_itemContent_posXpath": dosRuleListPage_searchLable_input_itemContent_posXpath,
"policyRuleListPage_searchLable_searchButton_posId": dosRuleListPage_searchLable_searchButton_posXpath,
"policyRuleListPage_tableTbody_posXpath": dosRuleListPage_tableTbody_posXpath
},
"delete": {
"policyRuleListPage_allSelect_posXpath": dosRuleListPage_allSelect_posXpath,
"policyRuleListPage_deleteButton_posId": dosRuleListPage_deleteButton_posXpath,
"policyRuleListPage_okButton_warningYes_posXpath": dosRuleListpage_okButton_warningYes_posXpath,
"policyRuleListPage_okButton_warningNo_posXpath": dosRuleListpage_okButton_warningNo_posXpath
}
}
parameter = {
"username": "lina2024",
"password": "lina2024",
"test_pc_ip": "192.168.64.96",
"test_subcriber_id": "test6776",
"api_server": "http://192.168.44.72",
"script_type": "api",
"env": "tsgx",
"vsys_id": 5,
"is_log": 1,
"debug_flag": "local",
"root_path": "E:/tsg_test",
"path": "E:/tsg_test/tests/ui",
"module_name": "dos_protection",
"test_case_name": "dos_protect_srcip_http_rate_srcip_d3"
}
# 其他参数
rule_num = 1
# 获取开始时间
utc_tz = pytz.timezone('UTC')
current_utc_time = datetime.now(utc_tz) - timedelta(seconds=80000)
start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ')
#获取结束时间
utc_tz = pytz.timezone('UTC')
current_utc_time = datetime.now(utc_tz)
end_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ')
test_data = {
"is_multi_priority": False,
"rule_num": 1,
"policy_type": "dos_protection",
"rule_name": "dos_protect_srcip_http_rate_serverfqdn_cperiod_1min_d3_1h",
"rule_action": "protect",
"rule_type": "create",
"do_log": 2,
"condition": {
"source_ip": [
{
"name": "dos_srcip",
"object_type": "ip",
"select_type": False,
"negate": False,
"item": [
{
"item_operation": "add",
"item_type": "ipv4",
"item_value": "10.64.230.96"
}
]
}
],
"application": [
{
"name": "http",
"object_type": "application",
"negate": False
}
]
},
"threshold": {
"type": "rate",
"rate_threshold": {
"group_by": "server_fqdn",
"request_per_period": 1,
"counting_period": "1min"
}
},
"mitigation": {
"behavior": "deny",
"timeout": "1h"
},
"expected_return": "",
"counters": {
"hits": "many"
},
"log_query_param": [
{"query_field_key": "source_country", "query_value": "Private Network"},
{"query_field_key": "destination_country", "query_value": "US"},
#
{"query_field_key": "sessions", "query_value": ""},
{"query_field_key": "packets", "query_value": ""},
{"query_field_key": "bytes", "query_value": ""},
{"query_field_key": "bit_rate", "query_value": ""},
{"query_field_key": "packet_rate", "query_value": ""},
{"query_field_key": "session_rate", "query_value": ""},
{"query_field_key": "rule_uuid", "query_value": ""},
{"query_field_key": "source_ip", "query_value": ""},
{"query_field_key": "destination_ip", "query_value": ""},
],
"traffic": {
"protocol": "",
"type": "trex",
"clients_start_ip": "10.64.230.96",
"clients_end_ip": "10.64.230.96",
"servers_start_ip": "6.116.89.203",
"servers_end_ip": "6.116.89.203",
"m": 2,
"d": 5,
"yaml_name": "test",
"pcap_name": "http"
},
"token": "cdd9d680-6f67-4360-8be7-3ff518970a92&11&",
"rule_uuid":'0193190f-2add-775a-a34f-8a1b7b0c2ba4'
}
output = {'total_packets': 873, 'total_packets_sent': 198, 'total_packets_received': 675, 'total_bytes': 978822, 'total_bytes_sent': 76869, 'total_bytes_received': 901953, 'total_syn_pkt': 9}
log_in = LogIn()
pwd = log_in.encryptPwd(parameter["password"], parameter["api_server"])
token = log_in.login(parameter["username"], pwd, parameter["api_server"])
test_data["token"] = token
# support.ui_utils.env.initiate_selenium_request()
# driver = support.ui_utils.env.my_driver()
# support.ui_utils.env.setup()
# 数据转换,适配API的test_data
translation = DataTranslation()
temp_test_data = translation.data_translation(test_data["policy_type"], test_data)
test_data["obj_condition"] = temp_test_data["obj_condition"]
test_data["application"] = temp_test_data["application"]
print(test_data)
# 添加测试过程中涉及的template
template_path = {}
template_path["rule_template"] = parameter["root_path"] + "/support/api_utils/template/rule_template.json"
template_path["object_template"] = parameter["root_path"] + "/support/api_utils/template/object_template.json"
# 创建Object、Profile、Rule
create = CreatePolicy()
cfg_list = create.create_policy(parameter, test_data, template_path)
#1.api校验
api_server = parameter["api_server"]
vsys_id = parameter["vsys_id"]
script_type = parameter["script_type"]
create_policies_ids = [{'dos_protection': test_data['rule_uuid']}]
#如进行Log验证,需在api中添加test_data打印语句,;在get_log中打印temp_log_result
# 调用 GetLog 的 get_log_by_condition 函数
log = support.api_utils.get_log_deprecated.GetLog()
log_result = log.get_log_by_condition(
token,
rule_num,
start_time,
end_time,
parameter["test_pc_ip"],
parameter["test_subcriber_id"],
create_policies_ids,
test_data,
api_server,
vsys_id,
script_type,
output
)
#打印 log_result 结果
print("log_result:", log_result)
print(test_data)
# 删除配置
delete = DeletePolicy()
delete.delele_policy_by_uuid(token, parameter, cfg_list)
# #2.ui校验
# api_verification_result = [True,True,True]
#
# ui_verification = UiVerification()
#
# ui_verification_result = ui_verification.ui_verification(driver,parameter, test_data, api_verification_result,test_rule_element_position, output,111111)
# print("ui_verification_result:", ui_verification_result)
if __name__ == "__main__":
main()
# # 打印所有参数
# print(f"token: {token}")
# print(f"rule_num: {rule_num}")
# print(f"start_time: {start_time}")
# print(f"end_time: {end_time}")
# print(f"test_pc_ip: {test_pc_ip}")
# print(f"test_sub_id: {test_sub_id}")
# print(f"create_policies_ids: {create_policies_ids}")
# print(f"test_data: {test_data}")
# print(f"api_server: {api_host}")
# print(f"vsys_id: {vsys_id}")
# print(f"script_type: {script_type}")
# print(f"output: {output}")
#concurrency_test_data
# test_data = {
# 'is_multi_priority': False,
# 'rule_num': 1,
# 'policy_type': 'dos_protection',
# 'rule_name': 'dos_protect_srcip_http_concurrency_srcip_d3',
# 'rule_action': 'protect',
# 'rule_type': 'create',
# 'do_log': 2,
# 'condition': {
# 'source_ip': [{'name': 'dos_srcip', 'object_type': 'ip', 'select_type': False, 'negate': False,
# 'item': [{'item_operation': 'add', 'item_type': 'ipv4', 'item_value': '10.64.20.210'}]}],
# 'application': [{'name': 'ssl', 'object_type': 'application', 'negate': False}]
# },
# 'threshold': {
# 'type': 'concurrency',
# 'concurrency_threshold': {'group_by': 'source_ip', 'concurrent_sessions': 2}
# },
# 'mitigation': {'behavior': 'deny', 'timeout': '5min'},
# 'expected_return': '',
# 'counters': {'hits': 'many'},
# 'log_query_param': [
# {'query_field_key': 'attack_type', 'query_value': 'custom_network_attack'},
# {'query_field_key': 'sessions', 'query_value': ''},
# {'query_field_key': 'packets', 'query_value': ''},
# {'query_field_key': 'bytes', 'query_value': ''},
# {'query_field_key': 'bit_rate', 'query_value': ''},
# {'query_field_key': 'packet_rate', 'query_value': ''},
# {'query_field_key': 'session_rate', 'query_value': ''}
# ],
# 'traffic': {
# 'protocol': '',
# 'type': 'trex',
# 'clients_start_ip': '10.64.20.210',
# 'clients_end_ip': '10.64.20.210',
# 'servers_start_ip': '2.1.1.18',
# 'servers_end_ip': '2.1.1.18',
# 'active_flows': 3,
# 'd': 5,
# 'm': 1,
# 'yaml_name': 'test',
# 'pcap_name': 'tls_long'
# },
# 'token': 'd21627b9-cb69-4c83-b111-8c8a2149cf26&27&',
# 'rule_uuid': '157553'
# }
# output = {'total_bytes': 19027648, 'total_bytes_received': 18671260, 'total_bytes_sent': 356388, 'total_packets': 18056, 'total_packets_received': 12368, 'total_packets_sent': 5688, 'total_syn_pkt': 4} # 示例的输出
|