-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_basic.py
More file actions
111 lines (93 loc) · 3.58 KB
/
test_basic.py
File metadata and controls
111 lines (93 loc) · 3.58 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
WAF API 基本功能测试脚本
"""
import requests
import json
import time
from config import API_CONFIG
def test_api_connection():
"""测试API连接"""
print("=" * 50)
print("WAF API 基本功能测试")
print("=" * 50)
base_url = f"http://localhost:{API_CONFIG['port']}" # 使用localhost而不是0.0.0.0
token = API_CONFIG['tokens'][0]
headers = {"token": token}
print(f"测试服务器: {base_url}")
print(f"使用Token: {token[:20]}...")
print()
# 测试1: 检查服务器状态
print("1. 测试服务器状态...")
try:
response = requests.get(f"{base_url}/", timeout=5)
if response.status_code == 200:
print("✓ 服务器运行正常")
else:
print(f"✗ 服务器响应异常: {response.status_code}")
except requests.exceptions.ConnectionError:
print("✗ 无法连接到服务器 - 请确保服务器正在运行")
print(" 启动命令: python start_server.py")
return False
except Exception as e:
print(f"✗ 连接错误: {e}")
return False
# 测试2: 检查配置状态
print("\n2. 测试配置状态...")
try:
response = requests.get(f"{base_url}/api/status/waf1_instance", headers=headers, timeout=5)
if response.status_code == 200:
print("✓ 配置状态查询正常")
data = response.json()
print(f" 响应数据: {json.dumps(data, indent=2, ensure_ascii=False)}")
else:
print(f"✗ 配置状态查询失败: {response.status_code}")
except Exception as e:
print(f"✗ 配置状态查询异常: {e}")
# 测试3: 测试管理界面
print("\n3. 测试管理界面...")
try:
response = requests.get(f"{base_url}/admin", timeout=5)
if response.status_code == 200:
print("✓ 管理界面访问正常")
else:
print(f"✗ 管理界面访问失败: {response.status_code}")
except Exception as e:
print(f"✗ 管理界面访问异常: {e}")
print("\n" + "=" * 50)
print("基本功能测试完成")
print("=" * 50)
return True
def test_config_files():
"""测试配置文件"""
print("\n" + "=" * 50)
print("配置文件测试")
print("=" * 50)
try:
from config import POLICY_CONFIGS, CHAOS_CONFIGS, CHALLENGE_CONFIGS, API_CONFIG
print("✓ 主配置文件加载成功")
print(f" API Token数量: {len(API_CONFIG['tokens'])}")
print(f" 策略配置组: {len(POLICY_CONFIGS)}")
print(f" 混沌配置组: {len(CHAOS_CONFIGS)}")
print(f" 挑战配置组: {len(CHALLENGE_CONFIGS)}")
# 显示配置详情
print("\n配置详情:")
for group_name, instances in POLICY_CONFIGS.items():
print(f" 策略组 '{group_name}': {len(instances)} 个实例")
for group_name, instances in CHAOS_CONFIGS.items():
print(f" 混沌组 '{group_name}': {len(instances)} 个实例")
for group_name, instances in CHALLENGE_CONFIGS.items():
print(f" 挑战组 '{group_name}': {len(instances)} 个实例")
except Exception as e:
print(f"✗ 配置文件加载失败: {e}")
return False
print("\n" + "=" * 50)
print("配置文件测试完成")
print("=" * 50)
return True
if __name__ == "__main__":
# 先测试配置文件
test_config_files()
# 再测试API连接
test_api_connection()