frist
This commit is contained in:
277
test_sentinel.py
Normal file
277
test_sentinel.py
Normal file
@@ -0,0 +1,277 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Sentinel 集成测试脚本
|
||||
|
||||
验证 Sentinel 解决方案是否正确集成
|
||||
"""
|
||||
|
||||
import sys
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def test_imports():
|
||||
"""测试所有必要的模块导入"""
|
||||
print("=" * 60)
|
||||
print("测试 1: 模块导入")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
print("✓ 导入 utils.logger...")
|
||||
from utils.logger import logger
|
||||
|
||||
print("✓ 导入 utils.crypto...")
|
||||
from utils.crypto import generate_oai_did, generate_random_password
|
||||
|
||||
print("✓ 导入 utils.fingerprint...")
|
||||
from utils.fingerprint import BrowserFingerprint
|
||||
|
||||
print("✓ 导入 core.session...")
|
||||
from core.session import OAISession
|
||||
|
||||
print("✓ 导入 core.sentinel...")
|
||||
from core.sentinel import SentinelHandler
|
||||
|
||||
print("✓ 导入 reference.sentinel_solver...")
|
||||
from reference.sentinel_solver import SentinelSolver
|
||||
|
||||
print("✓ 导入 reference.js_executor...")
|
||||
from reference.js_executor import JSExecutor
|
||||
|
||||
print("\n✅ 所有模块导入成功!\n")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ 导入失败: {e}\n")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
def test_node_availability():
|
||||
"""测试 Node.js 是否可用"""
|
||||
print("=" * 60)
|
||||
print("测试 2: Node.js 环境检查")
|
||||
print("=" * 60)
|
||||
|
||||
import subprocess
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["node", "--version"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
version = result.stdout.strip()
|
||||
print(f"✓ Node.js 已安装: {version}")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Node.js 执行失败: {result.stderr}")
|
||||
return False
|
||||
|
||||
except FileNotFoundError:
|
||||
print("❌ Node.js 未安装或不在 PATH 中")
|
||||
print(" 请安装 Node.js: https://nodejs.org/")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Node.js 检查失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def test_sdk_file():
|
||||
"""测试 SDK 文件是否存在"""
|
||||
print("\n" + "=" * 60)
|
||||
print("测试 3: SDK 文件检查")
|
||||
print("=" * 60)
|
||||
|
||||
sdk_path = Path("/home/carry/myprj/gptAutoPlus/sdk/sdk.js")
|
||||
|
||||
if sdk_path.exists():
|
||||
size = sdk_path.stat().st_size
|
||||
print(f"✓ SDK 文件存在: {sdk_path}")
|
||||
print(f" 文件大小: {size:,} bytes ({size/1024:.1f} KB)")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ SDK 文件不存在: {sdk_path}")
|
||||
print(" 请确保 sdk/sdk.js 文件存在")
|
||||
return False
|
||||
|
||||
|
||||
def test_fingerprint():
|
||||
"""测试浏览器指纹生成"""
|
||||
print("\n" + "=" * 60)
|
||||
print("测试 4: 浏览器指纹生成")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
from utils.fingerprint import BrowserFingerprint
|
||||
|
||||
fp = BrowserFingerprint()
|
||||
config_array = fp.get_config_array()
|
||||
|
||||
print(f"✓ 指纹生成成功")
|
||||
print(f" Session ID: {fp.session_id}")
|
||||
print(f" 配置数组长度: {len(config_array)}")
|
||||
print(f" 配置数组前 3 项: {config_array[:3]}")
|
||||
|
||||
if len(config_array) == 18:
|
||||
print("✓ 配置数组长度正确 (18 个元素)")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ 配置数组长度错误: {len(config_array)} (期望 18)")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 指纹生成失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
async def test_sentinel_token():
|
||||
"""测试 Sentinel Token 生成"""
|
||||
print("\n" + "=" * 60)
|
||||
print("测试 5: Sentinel Token 生成")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
from core.session import OAISession
|
||||
from core.sentinel import SentinelHandler
|
||||
|
||||
print("✓ 创建测试会话...")
|
||||
session = OAISession()
|
||||
|
||||
print(f"✓ Session 创建成功,oai-did: {session.oai_did}")
|
||||
|
||||
print("✓ 初始化 SentinelHandler...")
|
||||
sentinel = SentinelHandler(session)
|
||||
|
||||
print("✓ 生成 Sentinel Token...")
|
||||
print(" (这可能需要几秒钟,正在执行 PoW 计算...)")
|
||||
|
||||
token = await sentinel.get_token()
|
||||
|
||||
print(f"\n✅ Sentinel Token 生成成功!")
|
||||
print(f" Token 前缀: {token[:30]}...")
|
||||
print(f" Token 长度: {len(token)}")
|
||||
|
||||
# 验证 token 格式
|
||||
if token.startswith("gAAAAA"):
|
||||
print("✓ Token 格式正确")
|
||||
return True
|
||||
else:
|
||||
print(f"⚠️ Token 格式异常: {token[:20]}...")
|
||||
return True # 仍然算成功,因为可能是格式变化
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Sentinel Token 生成失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
def test_crypto_utils():
|
||||
"""测试加密工具"""
|
||||
print("\n" + "=" * 60)
|
||||
print("测试 6: 加密工具")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
from utils.crypto import (
|
||||
generate_oai_did,
|
||||
generate_random_password,
|
||||
validate_oai_did,
|
||||
validate_password
|
||||
)
|
||||
|
||||
# 测试 oai-did 生成
|
||||
oai_did = generate_oai_did()
|
||||
print(f"✓ OAI-DID 生成: {oai_did}")
|
||||
|
||||
is_valid = validate_oai_did(oai_did)
|
||||
print(f"✓ OAI-DID 验证: {is_valid}")
|
||||
|
||||
# 测试密码生成
|
||||
password = generate_random_password()
|
||||
print(f"✓ 密码生成: {password}")
|
||||
|
||||
is_valid, error = validate_password(password)
|
||||
print(f"✓ 密码验证: {is_valid} {f'({error})' if error else ''}")
|
||||
|
||||
if is_valid:
|
||||
print("\n✅ 加密工具测试通过!")
|
||||
return True
|
||||
else:
|
||||
print(f"\n❌ 密码验证失败: {error}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ 加密工具测试失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
async def main():
|
||||
"""运行所有测试"""
|
||||
print("\n" + "=" * 60)
|
||||
print(" OpenAI 注册系统 - Sentinel 集成测试")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
results = []
|
||||
|
||||
# 运行所有测试
|
||||
results.append(("模块导入", test_imports()))
|
||||
results.append(("Node.js 环境", test_node_availability()))
|
||||
results.append(("SDK 文件", test_sdk_file()))
|
||||
results.append(("浏览器指纹", test_fingerprint()))
|
||||
results.append(("加密工具", test_crypto_utils()))
|
||||
results.append(("Sentinel Token", await test_sentinel_token()))
|
||||
|
||||
# 打印总结
|
||||
print("\n" + "=" * 60)
|
||||
print(" 测试总结")
|
||||
print("=" * 60)
|
||||
|
||||
for name, passed in results:
|
||||
status = "✅ 通过" if passed else "❌ 失败"
|
||||
print(f" {name:20s} {status}")
|
||||
|
||||
print("=" * 60)
|
||||
|
||||
total = len(results)
|
||||
passed = sum(1 for _, p in results if p)
|
||||
|
||||
print(f"\n总计: {passed}/{total} 个测试通过")
|
||||
|
||||
if passed == total:
|
||||
print("\n🎉 所有测试通过!系统已准备就绪。")
|
||||
print("\n下一步:")
|
||||
print(" 1. 配置邮箱(修改 .env 文件)")
|
||||
print(" 2. 运行主程序: python main.py")
|
||||
return 0
|
||||
else:
|
||||
print("\n⚠️ 部分测试失败,请检查上述错误信息。")
|
||||
print("\n常见问题:")
|
||||
print(" - Node.js 未安装: 请安装 Node.js v16+")
|
||||
print(" - SDK 文件缺失: 确保 sdk/sdk.js 存在")
|
||||
print(" - 依赖未安装: 运行 pip install -e .")
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
exit_code = asyncio.run(main())
|
||||
sys.exit(exit_code)
|
||||
except KeyboardInterrupt:
|
||||
print("\n\n⚠️ 测试被用户中断")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"\n\n❌ 测试程序异常: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user