1+ import shutil
2+ import pytest
3+ from pyencrypt .encrypt import *
4+ from pyencrypt .decrypt import *
5+ import os
6+
7+ from constants import AES_KEY
8+ from pyencrypt .generate import generate_aes_key
9+
10+
11+
12+ @pytest .mark .parametrize ('key' , [
13+ AES_KEY ,
14+ generate_aes_key (),
15+ ])
16+ def test_encrypt_key (key ):
17+ cipher , d , n = encrypt_key (key )
18+ assert isinstance (cipher , str )
19+ assert isinstance (d , int )
20+ assert isinstance (n , int )
21+
22+
23+ @pytest .mark .parametrize (
24+ 'path,expected' , [
25+ (Path ('__init__.py' ), False ),
26+ (Path ('pyencrypt/__init__.py' ), False ),
27+ (Path ('management/commands/user.py' ), False ),
28+ (Path ('tests/test.pye' ), False ),
29+ (Path ('tests/test_encrypt.py' ), True ),
30+ ]
31+ )
32+ def test_can_encrypt (path , expected ):
33+ assert can_encrypt (path ) == expected
34+
35+
36+ class TestGenarateSoFile :
37+
38+ def setup_method (self , method ):
39+ if method .__name__ == 'test_generate_so_file_default_path' :
40+ shutil .rmtree ((Path (os .getcwd ()) / 'encrypted' ).as_posix (), ignore_errors = True )
41+
42+ @pytest .mark .parametrize ('key' , [
43+ AES_KEY ,
44+ generate_aes_key (),
45+ ])
46+ def test_generate_so_file (self , key , tmp_path ):
47+ cipher_key , d , n = encrypt_key (key )
48+ assert generate_so_file (cipher_key , d , n , tmp_path )
49+ assert (tmp_path / 'encrypted' / 'loader.py' ).exists () == True
50+ assert (tmp_path / 'encrypted' / 'loader_origin.py' ).exists () == True
51+ assert list ((tmp_path / 'encrypted' ).glob ('loader.cpython-*-*.so' )) != []
52+
53+ @pytest .mark .parametrize ('key' , [
54+ AES_KEY ,
55+ generate_aes_key (),
56+ ])
57+ def test_generate_so_file_default_path (self , key ):
58+ cipher_key , d , n = encrypt_key (key )
59+ assert generate_so_file (cipher_key , d , n )
60+ assert (Path (os .getcwd ()) / 'encrypted' / 'loader.py' ).exists () == True
61+ assert (Path (os .getcwd ()) / 'encrypted' / 'loader_origin.py' ).exists () == True
62+ assert list ((Path (os .getcwd ()) / 'encrypted' ).glob ('loader.cpython-*-*.so' )) != []
63+
64+
65+ @pytest .mark .parametrize (
66+ 'path,key,exception' ,
67+ [
68+ (Path ('tests/test.py' ), AES_KEY , FileNotFoundError ),
69+ (Path ('tests/test.pye' ), AES_KEY , Exception ), # TODO: 封装Exception
70+ (Path ('tests/__init__.py' ), AES_KEY , Exception ),
71+ ]
72+ )
73+ def test_encrypt_file_path_exception (path , key , exception ):
74+ with pytest .raises (exception ) as excinfo :
75+ encrypt_file (path , key )
76+ assert excinfo .value .__class__ == exception
77+
78+
79+ @pytest .fixture
80+ def python_file_path (tmp_path ):
81+ fn = tmp_path / 'test.py'
82+ fn .touch ()
83+ fn .write_text ('print("hello world")' )
84+ return fn
85+
86+
87+ def test_encrypt_file_default (python_file_path ):
88+ assert isinstance (encrypt_file (python_file_path , AES_KEY ), bytes ) == True
89+ assert python_file_path .exists () == True
90+
91+
92+ def test_encrypt_file_delete_origin (python_file_path ):
93+ encrypt_file (python_file_path , AES_KEY , delete_origin = True )
94+ assert python_file_path .exists () == False
95+
96+
97+ def test_encrypt_file_new_path (python_file_path ):
98+ new_path = python_file_path .parent / 'test.pye'
99+ encrypt_file (python_file_path , AES_KEY , new_path = new_path )
100+ assert new_path .exists () == True
101+ assert python_file_path .exists () == True
102+
103+ def test_encrypt_file_new_path_exception (python_file_path ):
104+ new_path = python_file_path .parent / 'test.py'
105+ with pytest .raises (Exception ) as excinfo :
106+ encrypt_file (python_file_path , AES_KEY , new_path = new_path )
107+ assert str (excinfo .value ) == 'Encrypted file path must be pye suffix.'
0 commit comments