54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
"""测试 PyArmor 加密功能"""
|
|||
|
|
|
||
|
|
import tempfile
|
||
|
|
import os
|
||
|
|
import sys
|
||
|
|
import shutil
|
||
|
|
|
||
|
|
# 确保能导入项目模块
|
||
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
||
|
|
|
||
|
|
|
||
|
|
def test_pyarmor_encrypt():
|
||
|
|
"""测试 PyArmor encrypt_script 功能"""
|
||
|
|
import pyarmor.pyarmor as pa
|
||
|
|
|
||
|
|
tmpdir = tempfile.mkdtemp()
|
||
|
|
try:
|
||
|
|
src = os.path.join(tmpdir, "test_mod.py")
|
||
|
|
with open(src, "w", encoding="utf-8") as f:
|
||
|
|
f.write('def hello():\n print("Hello from PyArmor!")\n')
|
||
|
|
|
||
|
|
dest = os.path.join(tmpdir, "dist", "test_mod.py")
|
||
|
|
os.makedirs(os.path.dirname(dest), exist_ok=True)
|
||
|
|
|
||
|
|
# Check capsule
|
||
|
|
capsule = pa.DEFAULT_CAPSULE
|
||
|
|
print(f"Capsule: {capsule}")
|
||
|
|
print(f"Capsule exists: {os.path.exists(capsule)}")
|
||
|
|
|
||
|
|
# Try encrypt
|
||
|
|
result = pa.encrypt_script(
|
||
|
|
pubkey=capsule,
|
||
|
|
filename=src,
|
||
|
|
destname=dest,
|
||
|
|
)
|
||
|
|
print(f"Result: {result}")
|
||
|
|
|
||
|
|
# Check output
|
||
|
|
if os.path.exists(dest):
|
||
|
|
with open(dest, "r", encoding="utf-8") as f:
|
||
|
|
content = f.read()
|
||
|
|
print(f"Output length: {len(content)} bytes")
|
||
|
|
print(f"First 300 chars:\n{content[:300]}")
|
||
|
|
print(f"\n--- Full content ({len(content)} bytes) ---")
|
||
|
|
print(content)
|
||
|
|
else:
|
||
|
|
print(f"ERROR: Output file not created at {dest}")
|
||
|
|
finally:
|
||
|
|
shutil.rmtree(tmpdir, ignore_errors=True)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
test_pyarmor_encrypt()
|