compliance/test_ssl_ignore.py
2025-08-07 15:23:25 +08:00

182 lines
5.4 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
测试SSL证书忽略功能
"""
import sys
import subprocess
import logging
# 设置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def test_ssl_ignore():
"""测试SSL忽略功能"""
print("🔧 测试SSL证书忽略功能")
print("=" * 60)
# 测试命令
test_command = [
"python", "run_api_tests.py",
"--dms", "./assets/doc/dms/domain.json",
"--base-url", "https://www.dev.ideas.cnpc",
"--ignore-ssl",
"--strictness-level", "CRITICAL",
"--output", "./test_reports/ssl_test",
"--format", "json"
]
print("🚀 执行测试命令:")
print(" ".join(test_command))
print()
try:
# 执行测试
result = subprocess.run(
test_command,
capture_output=True,
text=True,
timeout=300 # 5分钟超时
)
print("📊 测试结果:")
print(f"返回码: {result.returncode}")
print()
if result.stdout:
print("📝 标准输出:")
print(result.stdout)
print()
if result.stderr:
print("⚠️ 错误输出:")
print(result.stderr)
print()
# 分析结果
if result.returncode == 0:
print("✅ SSL忽略功能测试成功")
return True
else:
print("❌ SSL忽略功能测试失败")
# 检查是否还有SSL错误
if "SSL" in result.stderr or "certificate" in result.stderr.lower():
print("🔍 仍然存在SSL相关错误可能需要进一步调试")
else:
print("🔍 SSL错误已解决但可能存在其他问题")
return False
except subprocess.TimeoutExpired:
print("⏰ 测试超时5分钟")
return False
except Exception as e:
print(f"❌ 测试执行出错: {e}")
return False
def test_without_ssl_ignore():
"""测试不使用SSL忽略的情况应该失败"""
print("\n🔧 测试不忽略SSL证书预期失败")
print("=" * 60)
# 测试命令(不包含--ignore-ssl
test_command = [
"python", "run_api_tests.py",
"--dms", "./assets/doc/dms/domain.json",
"--base-url", "https://www.dev.ideas.cnpc",
"--strictness-level", "CRITICAL",
"--output", "./test_reports/ssl_test_no_ignore",
"--format", "json"
]
print("🚀 执行测试命令不忽略SSL:")
print(" ".join(test_command))
print()
try:
# 执行测试
result = subprocess.run(
test_command,
capture_output=True,
text=True,
timeout=60 # 1分钟超时应该很快失败
)
print("📊 测试结果:")
print(f"返回码: {result.returncode}")
print()
if result.stderr:
print("⚠️ 错误输出:")
print(result.stderr[:500] + "..." if len(result.stderr) > 500 else result.stderr)
print()
# 分析结果
if result.returncode != 0 and ("SSL" in result.stderr or "certificate" in result.stderr.lower()):
print("✅ 预期的SSL错误出现证明SSL验证正常工作")
return True
else:
print("⚠️ 未出现预期的SSL错误可能配置有问题")
return False
except subprocess.TimeoutExpired:
print("⏰ 测试超时可能SSL验证导致连接挂起")
return True # 这也算是预期行为
except Exception as e:
print(f"❌ 测试执行出错: {e}")
return False
def main():
"""主函数"""
print("🧪 DMS合规性测试工具 - SSL证书忽略功能测试")
print("=" * 80)
# 检查必要文件
import os
if not os.path.exists("./assets/doc/dms/domain.json"):
print("❌ 找不到DMS域映射文件: ./assets/doc/dms/domain.json")
print("请确保文件存在后重试")
sys.exit(1)
if not os.path.exists("run_api_tests.py"):
print("❌ 找不到主测试脚本: run_api_tests.py")
sys.exit(1)
# 创建测试报告目录
os.makedirs("./test_reports", exist_ok=True)
success_count = 0
total_tests = 2
# 测试1: 使用SSL忽略
if test_ssl_ignore():
success_count += 1
# 测试2: 不使用SSL忽略预期失败
if test_without_ssl_ignore():
success_count += 1
# 总结
print("\n" + "=" * 80)
print("📋 测试总结")
print("=" * 80)
print(f"通过测试: {success_count}/{total_tests}")
if success_count == total_tests:
print("🎉 所有测试通过SSL忽略功能工作正常")
print("\n💡 使用建议:")
print("- 在开发和测试环境中使用 --ignore-ssl 参数")
print("- 在生产环境中不要使用此参数确保SSL证书验证")
print("- 如果需要在生产环境中使用请配置正确的SSL证书")
sys.exit(0)
else:
print("❌ 部分测试失败,请检查配置")
sys.exit(1)
if __name__ == "__main__":
main()