§
    ”U»j´  ã            
       ó€  — d Z ddlZddlmc mZ ddlZddlZddl	Z	ddl
Z
e	j                             de
j                             e
j                             e
j                             e¦  «        d¦  «        ¦  «        ¦  «         ddlmZmZ ddlmZmZ  G d„ dej        ¦  «        Zedk    r ej        ¦   «          dS dS )	zÄ
Unit tests for FreeExile Security Fortress:
- Apple App Attest & DeviceCheck Verification
- ChaCha20-Poly1305 / AEAD Packet Encryption & Integrity Verification
- Anti-Replay Sliding Window Guard
é    Nz../../server)ÚAppleAppAttestValidatorÚAttestationToken)ÚPacketCipherEngineÚAntiReplayWindowc                   ó,   — e Zd Zd„ Zd„ Zd„ Zd„ Zd„ ZdS )ÚTestSecurityFortressc                 ó„   — t          d¬¦  «        | _        t          d¬¦  «        | _        t	          d¬¦  «        | _        d S )Núcom.freeexile.game.ios)Úexpected_bundle_ids   FREEEXILE_32BYTE_MASTER_KEY_001)Ú
shared_keyé@   )Úwindow_size)r   Úattest_validatorr   Úcipher_enginer   Úreplay_guard©Úselfs    ú:C:\Projects\FreeExile\tests\unit\test_security_fortress.pyÚsetUpzTestSecurityFortress.setUp   sC   € Ý 7ÐKcÐ dÑ dÔ dˆÔÝ/Ð;]Ð^Ñ^Ô^ˆÔÝ,¸Ð<Ñ<Ô<ˆÔÐÐó    c                 óò   — | j                              dd¬¦  «        }| j                              |¦  «        \  }}|                      |¦  «         |                      d|                     ¦   «         ¦  «         dS )zTGenuine iPhone with valid Secure Enclave key and valid challenge passes attestation.Úiphone_15_pro_secure_enclave_01r
   ©Ú	device_idÚ	bundle_idÚverifiedN)r   Úgenerate_mock_attestationÚverify_attestationÚ
assertTrueÚassertInÚlower)r   ÚtokenÚis_validÚmsgs       r   Ú$test_app_attest_valid_genuine_devicez9TestSecurityFortress.test_app_attest_valid_genuine_device   sv   € àÔ%×?Ò?Ø7Ø.ð @ñ 
ô 
ˆð Ô-×@Ò@ÀÑGÔG‰ˆ�#Ø�Š˜Ñ!Ô!Ð!Ø�Š�j #§)¢)¡+¤+Ñ.Ô.Ð.Ð.Ð.r   c                 óò   — | j                              dd¬¦  «        }| j                              |¦  «        \  }}|                      |¦  «         |                      d|                     ¦   «         ¦  «         dS )zHAttestation with modified/cracked IPA bundle ID is immediately rejected.Újailbroken_device_02zcom.cracked.freeexiler   ÚbundleN)r   r   r   ÚassertFalser    r!   )r   Útampered_tokenr#   r$   s       r   Ú'test_app_attest_reject_bundle_tamperingz<TestSecurityFortress.test_app_attest_reject_bundle_tampering#   sx   € àÔ.×HÒHØ,Ø-ð Iñ 
ô 
ˆð Ô-×@Ò@ÀÑPÔP‰ˆ�#Ø×Ò˜Ñ"Ô"Ð"Ø�Š�h §	¢	¡¤Ñ,Ô,Ð,Ð,Ð,r   c                 óÖ  — d}d}| j                              ||¦  «        \  }}}|                      ||¦  «         | j                              ||||¦  «        }|                      ||¦  «         t          |¦  «        }|dxx         dz  cc<   |                      t          ¦  «        5  | j                              t          |¦  «        |||¦  «         ddd¦  «         dS # 1 swxY w Y   dS )zPPayload is encrypted; tampering with any byte causes MAC authentication failure.s   PLAYER_MOVE_DIR_X_1.0_DIR_Y_0.0ié  r   éÿ   N)	r   ÚencryptÚassertNotEqualÚdecryptÚassertEqualÚ	bytearrayÚassertRaisesÚ
ValueErrorÚbytes)r   Ú	plaintextÚseq_numÚ
ciphertextÚtagÚnonceÚ	decryptedÚtampered_ciphertexts           r   Ú+test_packet_encryption_and_tamper_detectionz@TestSecurityFortress.test_packet_encryption_and_tamper_detection-   sS  € à6ˆ	Øˆà!%Ô!3×!;Ò!;¸IÀwÑ!OÔ!OÑˆ
�C˜Ø×Ò˜J¨	Ñ2Ô2Ð2ð Ô&×.Ò.¨z¸3ÀÀwÑOÔOˆ	Ø×Ò˜ IÑ.Ô.Ð.õ (¨
Ñ3Ô3ÐØ˜AÐÐÔ $Ñ&ÐÐÑØ×Ò�zÑ*Ô*ð 	Xð 	XØÔ×&Ò&¥uÐ-@Ñ'AÔ'AÀ3ÈÈwÑWÔWÐWð	Xð 	Xð 	Xñ 	Xô 	Xð 	Xð 	Xð 	Xð 	Xð 	Xð 	Xð 	Xøøøð 	Xð 	Xð 	Xð 	Xð 	Xð 	Xs   Â&+CÃC"Ã%C"c                 ó|  — |                       | j                             d¦  «        ¦  «         |                       | j                             d¦  «        ¦  «         |                      | j                             d¦  «        ¦  «         |                      | j                             d¦  «        ¦  «         |                       | j                             d¦  «        ¦  «         |                       | j                             d¦  «        ¦  «         |                      | j                             d¦  «        ¦  «         dS )z<Replayed packets or duplicate sequence numbers are rejected.é   é   é
   é   N)r   r   Úcheck_and_updater)   r   s    r   Útest_anti_replay_sliding_windowz4TestSecurityFortress.test_anti_replay_sliding_window?   s  € ð 	�Š˜Ô)×:Ò:¸1Ñ=Ô=Ñ>Ô>Ð>à�Š˜Ô)×:Ò:¸1Ñ=Ô=Ñ>Ô>Ð>à×Ò˜Ô*×;Ò;¸AÑ>Ô>Ñ?Ô?Ð?à×Ò˜Ô*×;Ò;¸AÑ>Ô>Ñ?Ô?Ð?à�Š˜Ô)×:Ò:¸2Ñ>Ô>Ñ?Ô?Ð?à�Š˜Ô)×:Ò:¸1Ñ=Ô=Ñ>Ô>Ð>à×Ò˜Ô*×;Ò;¸AÑ>Ô>Ñ?Ô?Ð?Ð?Ð?r   N)Ú__name__Ú
__module__Ú__qualname__r   r%   r+   r=   rD   © r   r   r   r      sg   € € € € € ð=ð =ð =ð
/ð /ð /ð-ð -ð -ðXð Xð Xð$@ð @ð @ð @ð @r   r   Ú__main__)Ú__doc__ÚbuiltinsÚ@py_builtinsÚ_pytest.assertion.rewriteÚ	assertionÚrewriteÚ
@pytest_arÚunittestÚtimeÚsysÚosÚpathÚinsertÚabspathÚjoinÚdirnameÚ__file__Úsecurity.app_attestr   r   Úsecurity.packet_cipherr   r   ÚTestCaser   rE   ÚmainrH   r   r   ú<module>r_      s  ððð ð € € € € € € € € € € € € €€€€Ø €€€Ø 
€
€
€
Ø 	€	€	€	à „‡‚��2”7—?’? 2¤7§<¢<°´·²ÀÑ0IÔ0IÈ>Ñ#ZÔ#ZÑ[Ô[Ñ \Ô \Ð \à IÐ IÐ IÐ IÐ IÐ IÐ IÐ IØ GÐ GÐ GÐ GÐ GÐ GÐ GÐ Gð;@ð ;@ð ;@ð ;@ð ;@˜8Ô,ñ ;@ô ;@ð ;@ð| ˆzÒÐØ€H„M�O„O€O€O€Oð Ðr   