U
    g'                     @   s   d dl Z d dlmZmZ d dlmZ d dlmZmZ G dd de j	Z
G dd de j	Zi fd	d
Zedkrdd Ze jdd dS )    N)	unhexlifyhexlify)list_test_cases)strxorstrxor_cc                   @   st   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )StrxorTestsc                 C   s@   t d}t d}t d}| t||| | t||| d S N   ff339a83e5cd4cdf5649   383d4ba020573314395b   c70ed123c59a7fcb6f12)r   assertEqualr   )selfterm1term2result r   H/tmp/pip-unpacked-wheel-_q8s9isk/Cryptodome/SelfTest/Util/test_strxor.pytest1+   s
    zStrxorTests.test1c                 C   s   d}|  t||| d S )N    )r   r   )r   esr   r   r   test22   s    zStrxorTests.test2c                 C   s*   t d}dt| }| t||| d S )Nr	       )r   lenr   r   )r   r   Z	all_zerosr   r   r   test36   s    zStrxorTests.test3c                 C   s$   t d}t d}| tt|| d S )Nr	   s   ff339a83e5cd4cdf564990)r   assertRaises
ValueErrorr   r   r   r   r   r   r   test_wrong_length;   s    zStrxorTests.test_wrong_lengthc                 C   s6   t d}t|}t d}t d}| t||| d S r   )r   	bytearrayr   r   )r   r   term1_bar   r   r   r   r   test_bytearray@   s
    zStrxorTests.test_bytearrayc                 C   s6   t d}t|}t d}t d}| t||| d S r   )r   
memoryviewr   r   )r   r   term1_mvr   r   r   r   r   test_memoryviewH   s
    zStrxorTests.test_memoryviewc                 C   s~   t d}t d}|dd }|dd }t d}tt|}t|||d}| |d | || | || | || dS z3Verify result can be stored in pre-allocated memoryr	   r
   Nr   output)r   r   r   r   r   r   r   r   original_term1original_term2expected_xorr&   r   r   r   r   test_output_bytearrayP   s    z!StrxorTests.test_output_bytearrayc                 C   s   t d}t d}|dd }|dd }t d}ttt|}t|||d}| |d | || | || | || dS r$   )r   r!   r   r   r   r   r'   r   r   r   test_output_memoryviewa   s    z"StrxorTests.test_output_memoryviewc                 C   s^   t td}td}|dd }td}t|||d}| |d | || | || dS 1Verify result can be stored in overlapping memoryr	   r
   Nr   r%   )r   r   r   r   r   r   r   r)   r*   r   r   r   r   !test_output_overlapping_bytearrayr   s    z-StrxorTests.test_output_overlapping_bytearrayc                 C   sb   t ttd}td}|dd }td}t|||d}| |d | || | || dS r-   )r!   r   r   r   r   r/   r   r   r   "test_output_overlapping_memoryview   s    z.StrxorTests.test_output_overlapping_memoryviewc                 C   s(   t d}t d}| jtt|||d dS 2Verify result cannot be stored in read-only memoryr	   r
   r%   N)r   r   	TypeErrorr   r   r   r   r   test_output_ro_bytes   s    z StrxorTests.test_output_ro_bytesc                 C   s,   t td}td}| jtt|||d dS r2   )r!   r   r   r4   r   r   r   r   r   test_output_ro_memoryview   s    z%StrxorTests.test_output_ro_memoryviewc                 C   s8   t d}t d}tt|d }| jtt|||d dS )<Verify result cannot be stored in memory of incorrect lengthr	   r
      r%   N)r   r   r   r   r   r   )r   r   r   r&   r   r   r   test_output_incorrect_length   s    z(StrxorTests.test_output_incorrect_lengthN)__name__
__module____qualname__r   r   r   r   r    r#   r+   r,   r0   r1   r5   r6   r9   r   r   r   r   r   )   s   r   c                   @   st   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )Strxor_cTestsc                 C   s&   t d}t d}| t|d| d S Nr	      be72dbc2a48c0d9e1708A   r   r   r   )r   r   r   r   r   r   r      s    zStrxor_cTests.test1c                 C   s   t d}| t|d| d S )Nr	   r   rA   r   r   r   r   r   r      s    zStrxor_cTests.test2c                 C   s   |  tddd d S )Nr   Z   )r   r   )r   r   r   r   r      s    zStrxor_cTests.test3c                 C   s,   t d}| tt|d | tt|d d S )Nr	      )r   r   r   r   rB   r   r   r   test_wrong_range   s    zStrxor_cTests.test_wrong_rangec                 C   s.   t d}t|}t d}| t|d| d S r>   )r   r   r   r   )r   r   r   r   r   r   r   r       s    zStrxor_cTests.test_bytearrayc                 C   s.   t d}t|}t d}| t|d| d S r>   )r   r!   r   r   )r   r   r"   r   r   r   r   r#      s    zStrxor_cTests.test_memoryviewc                 C   s^   t d}|d d  }t d}tt|}t|d|d}| |d  | || | || d S Nr	   r?   r@   r%   )r   r   r   r   r   r   r   r(   Zexpected_resultr&   r   r   r   r   r+      s    z#Strxor_cTests.test_output_bytearrayc                 C   sb   t d}|d d  }t d}ttt|}t|d|d}| |d  | || | || d S rG   )r   r!   r   r   r   r   rH   r   r   r   r,      s    z$Strxor_cTests.test_output_memoryviewc                 C   s>   t td}td}t|d|d}| |d | || dS r.   r	   r?   r@   r%   N)r   r   r   r   r   r   r*   r   r   r   r   r0      s
    z/Strxor_cTests.test_output_overlapping_bytearrayc                 C   sB   t ttd}td}t|d|d}| |d | || dS rI   )r!   r   r   r   r   rJ   r   r   r   r1      s
    z0Strxor_cTests.test_output_overlapping_memoryviewc                 C   s    t d}| jtt|d|d dS )r3   r	   r@   r%   N)r   r   r4   r   rB   r   r   r   r5      s    z"Strxor_cTests.test_output_ro_bytesc                 C   s,   t td}td}| jtt|d|d dS )r3   r	   r
   r@   r%   N)r!   r   r   r4   r   r   r   r   r   r6      s    z'Strxor_cTests.test_output_ro_memoryviewc                 C   s0   t d}tt|d }| jtt|d|d dS )r7   r	   r8   r@   r%   N)r   r   r   r   r   r   )r   r   r&   r   r   r   r9     s    z*Strxor_cTests.test_output_incorrect_lengthN)r:   r;   r<   r   r   r   rF   r    r#   r+   r,   r0   r1   r5   r6   r9   r   r   r   r   r=      s   r=   c                 C   s    g }|t t7 }|t t7 }|S N)r   r   r=   )configtestsr   r   r   	get_tests  s    rN   __main__c                   C   s   t t S rK   )unittestZ	TestSuiterN   r   r   r   r   <lambda>  r   rQ   suite)ZdefaultTest)rP   binasciir   r   ZCryptodome.SelfTest.st_commonr   ZCryptodome.Util.strxorr   r   ZTestCaser   r=   rN   r:   rR   mainr   r   r   r   <module>"   s   g