§
    ö­»j'  ã                   ó„   — d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZ  G d„ d	¦  «        ZdS )
aÊ  
Unit Test Suite for FreeExile Anti-Fog Memory Tampering & Anti-Exploit Verifier.
Validates:
1. Fog Hash Challenge-Response (HMAC-SHA256 integrity verification).
2. Detection of Client RAM tampering (hacker forcing fog_matrix to VISIBLE).
3. Server-Authoritative Action Line-of-Sight (LoS) Sanity Validation.
4. Detection of out-of-vision targeting and wall-penetrating clicks.
5. Integration with Independent Security Division (Audit telemetry & flagging).
é    N)ÚTuple)ÚFogStateÚMapGridDataÚPlayerVisibilityState)ÚWarFogEngine)ÚProceduralMapEngine)ÚFogIntegrityVerifierÚFogTamperViolationExceptionc                   óL   — e Zd Zej        d„ ¦   «         Zd„ Zd„ Zd„ Zd„ Z	d„ Z
dS )Ú(TestFogIntegrityAndMemoryTamperDetectionc                 ó  — t          dd¬¦  «        }|                     dd¬¦  «        }t          ¦   «         }|                     ddd¬¦  «        }|j        \  }}|                     ||||d¬	¦  «         t          ¦   «         }||||fS )
Né    )ÚwidthÚheighti90  ÚBLEACHED_BONE_CANYON)ÚseedÚbiome_idÚplayer_honest)Ú	player_idr   r   é   )Úplayer_xÚplayer_yÚview_radius)r   Úgenerate_mapr   Úcreate_player_visibilityÚspawn_pointÚupdate_visibilityr	   )ÚselfÚengineÚmap_dataÚ
fog_engineÚvisÚsxÚsyÚverifiers           úGC:\Projects\FreeExile\tests\unit\test_fog_integrity_and_anti_exploit.pyÚsetup_systemz5TestFogIntegrityAndMemoryTamperDetection.setup_system   s˜   € å$¨2°bÐ9Ñ9Ô9ˆØ×&Ò&¨EÐ<RÐ&ÑSÔSˆÝ!‘^”^ˆ
Ø×1Ò1¸OÐSUÐ^`Ð1ÑaÔaˆØÔ%‰ˆˆBØ×$Ò$ S¨(¸RÈ"ÐZ[Ð$Ñ\Ô\Ð\Ý'Ñ)Ô)ˆØ˜ S¨(Ð2Ð2ó    c                 óæ   — |\  }}}}|                      |j        ¦  «        }|                     ||¦  «        }|                     |||¦  «        }|du sJ ‚|                     ||¦  «        }	|	du sJ ‚dS )zXHonest client computing hash matching server fog_matrix succeeds and consumes challenge.TFN)Úgenerate_fog_challenger   Úcompute_expected_fog_hashÚverify_client_fog_hash)
r   r'   r    r!   r"   r%   ÚnonceÚexpected_hashÚis_validÚis_replay_valids
             r&   Ú"test_honest_client_fog_hash_passeszKTestFogIntegrityAndMemoryTamperDetection.test_honest_client_fog_hash_passes)   s’   € à.:Ñ+ˆ�*˜c 8Ø×/Ò/°´Ñ>Ô>ˆð !×:Ò:¸3ÀÑFÔFˆØ×2Ò2°3¸ÀuÑMÔMˆØ˜4ÐÐÐÐð #×9Ò9¸#¸}ÑMÔMˆØ %Ð'Ð'Ð'Ð'Ð'Ð'r(   c                 óp  — |\  }}}}|                      |j        ¦  «        }d„ t          d¦  «        D ¦   «         }t          d„ |D ¦   «         ¦  «        }t	          j        |                     d¦  «        |t          j        ¦  «         	                    ¦   «         }	| 
                    ||	|¦  «        }
|
du sJ ‚dS )zPHacker modifying RAM to remove fog of war produces hash mismatch and is flagged.c                 ó@   — g | ]}d „ t          d¦  «        D ¦   «         ‘ŒS )c                 ó0   — g | ]}t           j        j        ‘ŒS © )r   ÚVISIBLEÚvalue©Ú.0Ú_s     r&   ú
<listcomp>zoTestFogIntegrityAndMemoryTamperDetection.test_ram_tampered_client_fog_hash_fails.<locals>.<listcomp>.<listcomp>=   s   € ÐFÐFÐF°q�HÔ,Ô2ÐFÐFÐFr(   r   )Úranger8   s     r&   r;   zdTestFogIntegrityAndMemoryTamperDetection.test_ram_tampered_client_fog_hash_fails.<locals>.<listcomp>=   s-   € ÐZÐZÐZÈ1ÐFÐF½EÀ"¹I¼IÐFÑFÔFÐZÐZÐZr(   r   c                 ó   — g | ]	}|D ]}|‘ŒŒ
S r5   r5   )r9   ÚrowÚcells      r&   r;   zdTestFogIntegrityAndMemoryTamperDetection.test_ram_tampered_client_fog_hash_fails.<locals>.<listcomp>>   s%   € ÐPÐPÐP¨ÈCÐPÐPÀD ÐPÐPÐPÐPr(   zutf-8FN)r*   r   r<   ÚbytesÚhmacÚnewÚencodeÚhashlibÚsha256Ú	hexdigestr,   )r   r'   r    r!   r"   r%   r-   Útampered_matrixÚtampered_bytesÚhacker_hashr/   s              r&   Ú'test_ram_tampered_client_fog_hash_failszPTestFogIntegrityAndMemoryTamperDetection.test_ram_tampered_client_fog_hash_fails7   s¾   € à.:Ñ+ˆ�*˜c 8Ø×/Ò/°´Ñ>Ô>ˆð [ÐZÕPUÐVXÑPYÔPYÐZÑZÔZˆÝÐPÐP°ÐPÑPÔPÑQÔQˆÝ”h˜uŸ|š|¨GÑ4Ô4°nÅgÄnÑUÔU×_Ò_ÑaÔaˆð ×2Ò2°3¸ÀUÑKÔKˆØ˜5Ð Ð Ð Ð Ð Ð r(   c                 óÒ   — |\  }}}}|j         \  }}|dz   |f}t          j        j        |j        |         |dz   <   |                     ||||f|¬¦  «        \  }	}
|	du sJ ‚|
dk    sJ ‚dS )zGActions targeting entities inside VISIBLE Line-of-Sight are legitimate.é   ©Ú
player_posÚ
target_posTÚIN_LINE_OF_SIGHTN)r   r   r6   r7   Ú
fog_matrixÚvalidate_player_action_los)r   r'   r    r!   r"   r%   r#   r$   rO   Ú
is_allowedÚreasons              r&   Ú%test_action_target_inside_los_allowedzNTestFogIntegrityAndMemoryTamperDetection.test_action_target_inside_los_allowedE   sž   € à.:Ñ+ˆ�*˜c 8ØÔ%‰ˆˆBð ˜1‘f˜b�\ˆ
Ý%-Ô%5Ô%;ˆŒ�rÔ˜2 ™6Ñ"à%×@Ò@Ø� r¨2 h¸:ð Añ 
ô 
Ñˆ
�Fð ˜TÐ!Ð!Ð!Ð!ØÐ+Ò+Ð+Ð+Ð+Ð+Ð+r(   c                 óä   — |\  }}}}|j         \  }}|j        \  }}	|j        |	         |         t          j        j        k    sJ ‚|                     ||||f||	f¬¦  «        \  }
}|
du sJ ‚d|v sJ ‚dS )zRActions targeting monsters hidden in fog/unexplored are blocked with exploit flag.rM   FÚTARGET_NOT_VISIBLE_IN_FOGN)r   Ú
boss_pointrQ   r   r6   r7   rR   )r   r'   r    r!   r"   r%   r#   r$   ÚbxÚbyrS   rT   s               r&   Ú/test_action_target_in_fog_or_unexplored_blockedzXTestFogIntegrityAndMemoryTamperDetection.test_action_target_in_fog_or_unexplored_blockedT   s¨   € à.:Ñ+ˆ�*˜c 8ØÔ%‰ˆˆBØÔ$‰ˆˆBð Œ~˜bÔ! "Ô%­Ô)9Ô)?Ò?Ð?Ð?Ð?à%×@Ò@Ø� r¨2 h¸BÀ¸8ð Añ 
ô 
Ñˆ
�Fð ˜UÐ"Ð"Ð"Ð"Ø*¨fÐ4Ð4Ð4Ð4Ð4Ð4r(   c                 ó  — |\  }}}}|j         \  }}d}t          d¦  «        D ]4}	t          d¦  «        D ]}
|                     |
|	¦  «        r|
|	f} nŒ|r nŒ5|€J ‚|                     ||||f|¬¦  «        \  }}|du sJ ‚dS )zGActions targeting through solid walls are blocked as wall-hack exploit.Nr   rM   F)r   r<   Úblocks_visionrR   )r   r'   r    r!   r"   r%   r#   r$   Úwall_posÚyÚxrS   rT   s                r&   Ú,test_action_target_behind_solid_wall_blockedzUTestFogIntegrityAndMemoryTamperDetection.test_action_target_behind_solid_wall_blockedc   sà   € à.:Ñ+ˆ�*˜c 8ØÔ%‰ˆˆBð ˆÝ�r‘”ð 	ð 	ˆAÝ˜2‘Y”Yð ð �Ø×)Ò)¨!¨QÑ/Ô/ð Ø ! 1˜v�HØ�Eðð ð Ø�ðð Ð#Ð#Ð#à%×@Ò@Ø� r¨2 h¸8ð Añ 
ô 
Ñˆ
�Fð ˜UÐ"Ð"Ð"Ð"Ð"Ð"r(   N)Ú__name__Ú
__module__Ú__qualname__ÚpytestÚfixturer'   r1   rJ   rU   r[   ra   r5   r(   r&   r   r      sz   € € € € € Ø„^ð3ð 3ñ „^ð3ð(ð (ð (ð!ð !ð !ð,ð ,ð ,ð5ð 5ð 5ð#ð #ð #ð #ð #r(   r   )Ú__doc__re   rA   rD   Útypingr   Úserver.world.map_data_typesr   r   r   Úserver.world.war_fog_enginer   Ú"server.world.procedural_map_enginer   Ú&server.security.fog_integrity_verifierr	   r
   r   r5   r(   r&   ú<module>rm      sú   ððð ð €€€Ø €€€Ø €€€Ø Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð
 5Ð 4Ð 4Ð 4Ð 4Ð 4Ø BÐ BÐ BÐ BÐ BÐ Bðð ð ð ð ð ð ð ðZ#ð Z#ð Z#ð Z#ð Z#ñ Z#ô Z#ð Z#ð Z#ð Z#r(   